在 Gensim 中添加停用词

Sar*_*ara 1 python windows nlp stop-words 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
Run Code Online (Sandbox Code Playgroud)

goj*_*omo 9

虽然gensim.parsing.preprocessing.STOPWORDS是为了您的方便而预先定义的,并且恰好是一个frozenset因此不能直接添加到,但您可以轻松地制作一个更大的集合,其中包括这些单词和您的添加内容。例如:

from gensim.parsing.preprocessing import STOPWORDS
my_stop_words = STOPWORDS.union(set(['mystopword1', 'mystopword2']))
Run Code Online (Sandbox Code Playgroud)

然后my_stop_words在后续的停用词删除代码中使用新的、更大的。( 的simple_preprocess()功能gensim不会自动删除停用词。)