【问题标题】:How to check for English words in a list如何检查列表中的英语单词
【发布时间】:2020-01-25 21:40:48
【问题描述】:

我正在尝试检查一个单词是否是英文单词。不确定这是否是最好的方法,有什么建议吗?它不想工作。

from nltk import wordnet

word_to_test = input("Please enter a word: ")
if not wordnet.synsets(word_to_test):
    print("FALSE")
    #not english word
else:
    print("TRUE")

【问题讨论】:

  • 欢迎来到 StackOverflow。请按照您创建此帐户时的建议阅读并遵循帮助文档中的发布指南。 Minimal, complete, verifiable example 适用于此。在您发布 MCVE 代码并准确说明问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中并重现您指定的问题。 StackOverflow 不是设计、编码、研究或教程资源。
  • “它不想工作”不是问题规范。您发布的代码没有按照给定的方式运行:至少有一个语法错误,并且等待输入。
  • 我尝试运行代码并得到AttributeError: module 'nltk.stem.wordnet' has no attribute 'synsets'
  • 可能重复中提到的enchant 包不再维护,但也有提到其他方法。

标签: python shared-libraries nltk


【解决方案1】:

使用以下代码:

  • from nltk.corpus import wordnetwordnet.synsets 未能成功识别英文单词。
    • word_list 中的所有单词都标识为True
  • 能否成功识别英文单词,取决于使用的词典。
from nltk.corpus import words

def check_words(word_list: list):
    for word in word_list:
        print(word in words.words())

word_list = ['poisson', 'stark', 'nihongo', 'abstract', 'pedo']

输出:

check_words(word_list)

False
True
False
True
False

【讨论】:

    猜你喜欢
    • 2017-12-11
    • 1970-01-01
    • 1970-01-01
    • 2011-02-20
    • 2011-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-10
    相关资源
    最近更新 更多