【问题标题】:Fix SnowballStemmer Error "AttributeError: 'English' object has no attribute 'Default"修复 SnowballStemmer 错误“AttributeError: 'English' object has no attribute 'Default”
【发布时间】:2022-01-17 21:23:35
【问题描述】:

您好:) 我正在使用包 SnowballStemmer,但出现错误。我很高兴得到任何帮助:)

代码:

stem2 =[]

for word in stem:
    if word not in nlp.Default.stop_words: 
        stem2.append(word)

print(stem2)

此处出错:

line 127, in <module>
    if word not in nlp.Default.stop_words:  
AttributeError: 'English' object has no attribute 'Default'

【问题讨论】:

    标签: python package snowball-stemmer


    【解决方案1】:

    如果您能说明变量nlp 的来源,会更容易回答这个问题。

    但根据您的说法,我假设您指的是这个包:https://pypi.org/project/snowballstemmer,据我所知,它没有定义任何停用词。

    如果您使用的是nltk 包,那么您可以这样做:

    import nltk
    
    # needed once - nltk seems to cache it
    nltk.download('stopwords')
    # load cached stop words
    stopwords = frozenset(nltk.corpus.stopwords.words('english'))
    
    stem2 =[]
    for word in stem:
        if word not in stopwords:
            stem2.append(word)
    

    如果您使用的是 spacy 包,您可以这样做

    from spacy.lang.en.stop_words import STOP_WORDS
    
    for word in stem:
        if word not in STOP_WORDS:
            stem2.append(word)
    

    更快的应该是列表理解:

    stem2 = [word for word in stem if word not in STOP_WORDS]
    

    上面的代码当然假设定义了一个变量stem,它很可能是一个字符串列表。 根据您的要求,您可能需要检查实际的停用词,根据您选择的库,它们可能是略有不同的词组,因此上述解决方案通常不会返回相同的结果。

    【讨论】:

      猜你喜欢
      • 2019-07-19
      • 2022-12-01
      • 2022-12-03
      • 1970-01-01
      • 2014-12-20
      • 2021-11-08
      • 2012-02-29
      • 2021-04-10
      • 1970-01-01
      相关资源
      最近更新 更多