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