在Python中使用NLTK删除停用词
当计算机处理自然语言时,某些极端通用的单词似乎在帮助选择符合用户需求的文档方面几乎没有值,因此完全从词汇表中排除了。这些单词称为停用词。
例如,如果您输入的句子为-
John is a person who takes care of the people around him.
停止单词删除后,您将获得输出-
['John', 'person', 'takes', 'care', 'people', 'around', '.']
NLTK收集了这些停用词,我们可以将其从任何给定的句子中删除。这在NLTK.corpus模块内部。我们可以用它来过滤掉句子中的停用词。例如,
示例
from nltk.corpus import stopwords from nltk.tokenize import word_tokenize my_sent = "John is a person who takes care of people around him." tokens = word_tokenize(my_sent) filtered_sentence = [w for w in tokens if not w in stopwords.words()] print(filtered_sentence)
输出结果
这将给出输出-
['John', 'person', 'takes', 'care', 'people', 'around', '.']
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短