在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', '.']
热门推荐
8 十月简短祝福语
10 老爸生日暴富祝福语简短
11 年月日祝福语简短
12 怀念青春祝福语简短
13 公司开年仪式祝福语简短
14 新生祝福语简短8字
15 姐妹结婚快乐祝福语简短
16 商业mc祝福语大全简短
17 祝女性生日祝福语简短
18 老人过大寿祝福语简短