簡體   English   中英

在Gensim中添加停用詞

[英]Add stop words in Gensim

感謝您的光臨! 我有一個關於添加停用詞的快速問題。 我的數據集中顯示了一些單詞,但我希望可以將它們添加到gensims停止單詞列表中。 我已經看到了很多使用nltk的示例,我希望有一種方法可以在gensim中進行相同的操作。 我將在下面發布我的代碼:

 def preprocess(text): result = [] for token in gensim.utils.simple_preprocess(text): if token not in gensim.parsing.preprocessing.STOPWORDS and len(token) > 3: nltk.bigrams(token) result.append(lemmatize_stemming(token)) return result 

為方便起見, gensim.parsing.preprocessing.STOPWORDS已預先定義,並且碰巧是frozenset因此無法直接添加到其中,但您可以輕松地創建一個更大的集,包括這些單詞和您的添加內容。 例如:

from gensim.parsing.preprocessing import STOPWORDS
my_stop_words = STOPWORDS.union(set(['mystopword1', 'mystopword2']))

然后在后續的停用詞刪除代碼中使用新的較大的my_stop_words gensimsimple_preprocess()函數不會自動刪除停用詞。)

 def preprocess(text): result = [] for token in gensim.utils.simple_preprocess(text): newStopWords = ['stopword1','stopword2'] if token not in gensim.parsing.preprocessing.STOPWORDS and token not in newStopWords and len(token) > 3: nltk.bigrams(token) result.append(lemmatize_stemming(token)) return result 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM