NLTK includes some corpora that are nothing more than wordlists. The Words Corpus is the /usr/share/dict/words file from Unix, used by some spell checkers. We can use it to find unusual or mis-spelt words in a text corpus, as shown in :
def unusual_words(text):
text_vocab = set(w.lower() for w in text.split() if w.isalpha())
english_vocab = set(w.lower() for w in nltk.corpus.words.words())
unusual = text_vocab - english_vocab
return sorted(unusual)
在这种情况下,您可以使用english_vocab检查单词的成员关系。
>>> import nltk
>>> english_vocab = set(w.lower() for w in nltk.corpus.words.words())
>>> 'a' in english_vocab
True
>>> 'this' in english_vocab
True
>>> 'nothing' in english_vocab
True
>>> 'nothingg' in english_vocab
False
>>> 'corpus' in english_vocab
True
>>> 'Terminology'.lower() in english_vocab
True
>>> 'sorted' in english_vocab
True
我尝试了上面的方法,但是对于很多应该存在的单词,我尝试了wordnet。我认为这有更全面的疫苗
from nltk.corpus import wordnet if wordnet.synsets(word): #Do something else: #Do some otherthing
在这种情况下,您可以使用
english_vocab
检查单词的成员关系。相关问题 更多 >
编程相关推荐