首页 > 解决方案 > 有没有办法用 python 检查列表中的字符串是否是常用英语中使用的真实单词?

问题描述

我正在尝试从关键字列表中提取仅是英语中常规正常单词的单词。

这是我的代码:

words = ['apple','a%32','j & quod','rectangle','house','fsdfdsoij','fdfd']
for word in words:
    if word ???: # How can I check if the words is a real word? Any module that I can use for that or a free API?
        print:
    else:
        pass

我只想得到结果:

apple
rectangle
house

标签: pythonpython-3.x

解决方案


第一步,安装nltk

然后:

import nltk
nltk.download('words')

from nltk.corpus import words

samplewords=['apple','a%32','j & quod','rectangle','house','fsdfdsoij','fdfd']

[i for i in samplewords if i in words.words()]

['apple', 'rectangle', 'house']

推荐阅读