用 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', '.']

更新時間:20-6-2020

601 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告