【问题标题】:how to check if a input is a valid english word using pydictionary [duplicate]如何使用pydictionary检查输入是否是有效的英文单词[重复]
【发布时间】:2021-08-31 05:58:45
【问题描述】:

我考虑过使用

from PyDictionary import PyDictionary

word = input("enter a word: ")
dictionary = PyDictionary(word)

check = dictionary.getMeanings()

print(check)
input("press enter to exit")

【问题讨论】:

  • 您对解决方案有疑问吗?

标签: python pydictionary


【解决方案1】:

如果您使用PyDictionary,您可以检查单词的含义并将返回值包装在bool 函数中:

from PyDictionary import PyDictonary
dictionary = PyDictionary()
valid_word = bool(dictionary.meaning("hdaoij")) # False
valid_word = bool(dictionary.meaning("hello")) # True

valid_word = bool(dictionary.meaning(input("enter a word: ")))

否则,我会在@RoadJDK 的回答中使用enchant 中的check 函数

【讨论】:

    【解决方案2】:

    不久前我看到了这个帖子: How to check if a word is an English word with Python?

    >>> import enchant
    >>> d = enchant.Dict("en_US")
    >>> d.check("Hello")
    True
    >>> d.check("Helo")
    False
    >>> d.suggest("Helo")
    ['He lo', 'He-lo', 'Hello', 'Helot', 'Help', 'Halo', 'Hell', 'Held', 'Helm', 
    'Hero', 
    "He'll"]
    >>>
    

    【讨论】:

    • 我有一个问题要做什么 >>> 做什么
    猜你喜欢
    • 1970-01-01
    • 2022-06-17
    • 2021-02-13
    • 1970-01-01
    • 2013-12-10
    • 1970-01-01
    • 2016-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多