【问题标题】:Custum stopwords in spaCy not workingspaCy中的自定义停用词不起作用
【发布时间】:2018-09-14 08:11:46
【问题描述】:

我在使用 spaCy 停用词时遇到了问题。任何帮助,将不胜感激。我正在将 TED 谈话记录加载到 pandas 数据框中

df['parsed_transcript'] = df['transcript'].apply(nlp)

#making a list of stop words to add
my_stop_words = ["thing", "people", "way", "year", " year " "time",  "lot", "day"]

#adding the list to the stop words
for stopword in my_stop_words:
    lexeme = nlp.vocab[stopword]
    lexeme.is_stop = True

#filtering out stop words and all non noun words
def preprocess_texts(texts_as_csv_column):
#Takes a column from a pandas datafram and converts it into a list of nouns.
    lemmas = []
    for doc in texts_as_csv_column: 
    # Append the lemmas of all nouns that are not stop words
        lemma = ([token.lemma_ for token in doc if token.pos_ == 'NOUN' and not token.is_stop])
        lemmas.append(lemma)

    return lemmas

现在,如果我计算一下“年”这个词,它会减少大约 4,000 次,但仍会显示超过 8,000 次。

count = 0
for row in df['list_of_words']:
    for word in row:
        if word == "year":
            count +=1

 print(count)

有些令牌被完全删除,有些被部分删除,有些根本没有。我尝试添加尾随和前导空格,但这无济于事。关于我可能做错了什么的任何想法?谢谢

【问题讨论】:

    标签: spacy stop-words


    【解决方案1】:

    代码看起来是正确的,只是您在my_stop_words 中有两次year,并且第二个实例和time 之间没有逗号,这将在文档中解释为year time

    【讨论】:

      猜你喜欢
      • 2019-03-13
      • 2019-01-11
      • 2021-06-02
      • 2017-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-12
      相关资源
      最近更新 更多