[英]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
。 ( gensim
的simple_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.