【问题标题】:Add and remove words from the NLTK stopwords list在 NLTK 停用词列表中添加和删除单词
【发布时间】:2019-01-03 04:46:36
【问题描述】:

我正在尝试在 NLTK 停用词列表中添加和删除单词:

from nltk.corpus import stopwords

stop_words = set(stopwords.words('french'))

#add words that aren't in the NLTK stopwords list
new_stopwords = ['cette', 'les', 'cet']
new_stopwords_list = set(stop_words.extend(new_stopwords))

#remove words that are in NLTK stopwords list
not_stopwords = {'n', 'pas', 'ne'} 
final_stop_words = set([word for word in new_stopwords_list if word not in not_stopwords])

print(final_stop_words)

输出:

Traceback (most recent call last):
  File "test_stop.py", line 10, in <module>
new_stopwords_list = set(stop_words.extend(new_stopwords))
AttributeError: 'set' object has no attribute 'extend'

【问题讨论】:

    标签: python python-3.x list set nltk


    【解决方案1】:

    试试这个:

    from nltk.corpus import stopwords
    
    stop_words = set(stopwords.words('french'))
    
    #add words that aren't in the NLTK stopwords list
    new_stopwords = ['cette', 'les', 'cet']
    new_stopwords_list = stop_words.union(new_stopwords)
    
    #remove words that are in NLTK stopwords list
    not_stopwords = {'n', 'pas', 'ne'} 
    final_stop_words = set([word for word in new_stopwords_list if word not in not_stopwords])
    
    print(final_stop_words)
    

    【讨论】:

      【解决方案2】:

      您可以使用update 代替extend 并以这种方式替换此行new_stopwords_list = set(stop_words.extend(new_stopwords)):

      stop_words.update(new_stopwords)
      new_stopwords_list = set(stop_words)
      

      顺便说一句,如果您使用包含单词 list 的名称调用 set,这可能会造成混淆

      【讨论】:

        【解决方案3】:

        使用list(set(...)) insted of set(...),因为只有列表有一个名为extend的方法:

        ...
        stop_words = list(set(stopwords.words('french')))
        ...
        

        【讨论】:

          猜你喜欢
          • 2021-02-02
          • 1970-01-01
          • 2011-07-27
          • 1970-01-01
          • 2013-05-12
          • 2018-09-28
          • 2016-01-19
          • 2013-10-08
          • 2018-04-05
          相关资源
          最近更新 更多