【问题标题】:Adding words to nltk stoplist将单词添加到 nltk 停止列表
【发布时间】:2011-07-27 13:41:59
【问题描述】:

我有一些代码可以从我的数据集中删除停用词,因为停用词列表似乎也没有删除我想要的大部分单词,我希望将单词添加到这个停用词列表中,以便在这种情况下,它将删除它们。 我用来删除停用词的代码是:

word_list2 = [w.strip() for w in word_list if w.strip() not in nltk.corpus.stopwords.words('english')]

我不确定添加单词的正确语法,而且似乎在任何地方都找不到正确的语法。任何帮助表示赞赏。谢谢。

【问题讨论】:

    标签: python nltk stop-words


    【解决方案1】:

    您可以简单地使用 append 方法向其添加单词:

    stopwords = nltk.corpus.stopwords.words('english')
    stopwords.append('newWord')
    

    或扩展以附加单词列表,正如查理在 cmets 上所建议的那样。

    stopwords = nltk.corpus.stopwords.words('english')
    newStopWords = ['stopWord1','stopWord2']
    stopwords.extend(newStopWords)
    

    【讨论】:

    • CustomListofWordstoExclude = ['cat','dog'] stopwords.extend(CustomListofWordstoExclude) 我使用了您的代码,但后来使用extend() 将我自己的列表添加到其中
    【解决方案2】:
    import nltk
    stopwords = nltk.corpus.stopwords.words('english')
    new_words=('re','name', 'user', 'ct')
    for i in new_words:
        stopwords.append(i)
    print(stopwords)
    

    【讨论】:

      【解决方案3】:

      我在我的 Ubuntu 机器上的做法是,我 ctrl + F 在 root 中输入“停用词”。它给了我一个文件夹。我走进了里面,里面有不同的文件。我打开了只有 128 个单词的“english”。加上我的话。已保存并完成。

      【讨论】:

        【解决方案4】:

        英文停用词是 nltk/corpus/stopwords/english.txt 中的一个文件(我想它会在这里...我在这台机器上没有 nltk..最好的办法是在其中搜索 'english.txt nltk 回购)

        您可以在此文件中添加新的停用词。

        如果您的停用词列表增加到数百个,请尝试查看 bloom filters

        【讨论】:

        • 有什么好的英语停用词列表吗? nltk 似乎很差
        • @fabrizioM fs1.position2.com/bm/txt/stopwords.txt 这是我在上一家公司使用的列表..
        • @Rafi 这是一个比 NLTK 更好的列表!谢谢!
        【解决方案5】:

        我总是在任何需要它的模块的顶部做stopset = set(nltk.corpus.stopwords.words('english'))。然后很容易向集合中添加更多单词,而且成员资格检查速度更快。

        【讨论】:

          【解决方案6】:

          也在寻找解决方案。经过一些跟踪和错误后,我必须将单词添加到停止列表中。希望这会有所帮助。

          def removeStopWords(str):
          #select english stopwords
          cachedStopWords = set(stopwords.words("english"))
          #add custom words
          cachedStopWords.update(('and','I','A','And','So','arnt','This','When','It','many','Many','so','cant','Yes','yes','No','no','These','these'))
          #remove stop words
          new_str = ' '.join([word for word in str.split() if word not in cachedStopWords]) 
          return new_str
          

          【讨论】:

            【解决方案7】:

            我使用此代码在 python 中将新的停用词添加到 nltk 停用词列表

            from nltk.corpus import stopwords
            #...#
            stop_words = set(stopwords.words("english"))
            
            #add words that aren't in the NLTK stopwords list
            new_stopwords = ['apple','mango','banana']
            new_stopwords_list = stop_words.union(new_stopwords)
            
            print(new_stopwords_list)
            

            【讨论】:

              【解决方案8】:

              我找到了(Python 3.7、Windows 10 上的 jupyter notebook、企业防火墙) 创建一个列表并使用 'append' 命令会导致整个停用词列表作为原始列表的一个元素附加。

              这使得“停用词”成为列表列表。

              Snijesh 的回答和 Jayantha 的回答一样有效。

              【讨论】:

                【解决方案9】:
                 import nltk
                 nltk.download('stopwords')
                 from nltk.corpus import stopwords
                 #add new words to the list
                 new_stopwords = ["new", "custom", "words", "add","to","list"]
                 stopwrd = nltk.corpus.stopwords.words('english')
                 stopwrd.extend(new_stopwords)
                

                【讨论】:

                  【解决方案10】:

                  STOP_WORDS.add(“Lol”) #根据需要将新的停用词添加到语料库中

                  【讨论】:

                    猜你喜欢
                    • 2014-08-14
                    • 2019-01-03
                    • 2014-05-10
                    • 1970-01-01
                    • 2020-07-08
                    • 1970-01-01
                    • 1970-01-01
                    • 2018-04-05
                    • 1970-01-01
                    相关资源
                    最近更新 更多