判断一串字符是不是单词

对于判断一串字符是不是英语单词,比如cake是英语单词,ccck不是,这要怎么做呢?
其实方法很多,可以去下个电子版英语词典,将其用python读入,做成一个键值对的大字典。当然,更方便地,nltk为我们提供了这样的接口,使得可以快速判断一串字符是不是单词。

from nltk.corpus import wordnet
word = 'case'
if wordnet.synsets(word)!=[]:
    print("{}是一个单词".format(word))
else:
    print("{}不是一个单词".format(word))

如果字符串word不是一个单词,那么wordnet.synsets(word)会返回一个空列表。

猜你喜欢

转载自blog.csdn.net/qq_40438165/article/details/84572888
今日推荐