如何使用nltk或python删除停用词


109

所以我有一个数据集,我想从中删除停用词

stopwords.words('english')

我在如何在我的代码中使用它以简单地取出这些单词的过程中苦苦挣扎。我已经有了这个数据集中的单词列表,我正在努力的部分是与此列表进行比较并删除停用词。任何帮助表示赞赏。


4
您从哪里获得停用词?这是NLTK的吗?
— tumultous_rooster 2014年

37
@ MattO'Brien from nltk.corpus import stopwords供未来的Google员工使用
— -danodonovan

13
nltk.download("stopwords")为了使停用词词典可用,还必须运行。
— sffc


1
请注意,诸如“ not”之类的单词在nltk中也被视为停用词。如果您进行情感分析,垃圾邮件过滤之类的操作,否定可能会改变句子的整个含义,并且如果从处理阶段中删除它,则可能无法获得准确的结果。
— 达科夫,

Answers:


205
from nltk.corpus import stopwords
# ...
filtered_words = [word for word in word_list if word not in stopwords.words('english')]

多亏了这两个答案,尽管我似乎在我的代码中有一个缺陷,阻止列表无法正常工作,但它们都可以工作。这应该是新问题吗?尚不确定这里的工作方式!
— 亚历克斯

51
要提高性能,请考虑考虑stops = set(stopwords.words("english"))。
— isakkarlsson

1
>>>导入nltk >>> nltk.download()来源

2
stopwords.words('english')是小写的。因此,请确保仅使用列表中的小写单词,例如[w.lower() for w in word_list]
— AlexG

19

您还可以设置差异,例如:

list(set(nltk.regexp_tokenize(sentence, pattern, gaps=True)) - set(nltk.corpus.stopwords.words('english')))

15
注意:这会将句子转换为SET,从而删除所有重复的单词,因此您将无法对结果使用频率计数
— David Dehghan

转换为集合可能会通过抓取多次出现的重要单词而从句子中删除可行的信息。
— Ujjwal

14

我想您有一个要删除停用词的单词列表(word_list)。您可以执行以下操作:

filtered_word_list = word_list[:] #make a copy of the word_list
for word in word_list: # iterate over word_list
  if word in stopwords.words('english'): 
    filtered_word_list.remove(word) # remove word from filtered_word_list if it is a stopword

5
这将
— 比达伦

12

要排除所有类型的停用词,包括nltk停用词,您可以执行以下操作:

from stop_words import get_stop_words
from nltk.corpus import stopwords

stop_words = list(get_stop_words('en'))         #About 900 stopwords
nltk_words = list(stopwords.words('english')) #About 150 stopwords
stop_words.extend(nltk_words)

output = [w for w in word_list if not w in stop_words]

我要len(get_stop_words('en')) == 174vslen(stopwords.words('english')) == 179
— Rubencart

6

stop-words为此,有一个非常简单的轻量级python软件包。

拳头使用以下方法安装软件包: pip install stop-words

然后,您可以使用列表理解功能将一行中的单词删除:

from stop_words import get_stop_words

filtered_words = [word for word in dataset if word not in get_stop_words('english')]

该软件包的下载量非常轻(不同于nltk),适用于Python 2和Python 3,并且具有许多其他语言的停用词,例如:

    Arabic
    Bulgarian
    Catalan
    Czech
    Danish
    Dutch
    English
    Finnish
    French
    German
    Hungarian
    Indonesian
    Italian
    Norwegian
    Polish
    Portuguese
    Romanian
    Russian
    Spanish
    Swedish
    Turkish
    Ukrainian

3

使用textcleaner库从数据中删除停用词。

单击此链接:https : //yugantm.github.io/textcleaner/documentation.html#remove_stpwrds

请按照以下步骤操作以使用此库。

pip install textcleaner

安装后:

import textcleaner as tc
data = tc.document(<file_name>) 
#you can also pass list of sentences to the document class constructor.
data.remove_stpwrds() #inplace is set to False by default

使用上面的代码删除停用词。


1

您可以使用此功能,请注意,您需要降低所有单词

from nltk.corpus import stopwords

def remove_stopwords(word_list):
        processed_word_list = []
        for word in word_list:
            word = word.lower() # in case they arenet all lower cased
            if word not in stopwords.words("english"):
                processed_word_list.append(word)
        return processed_word_list

1

使用过滤器:

from nltk.corpus import stopwords
# ...  
filtered_words = list(filter(lambda word: word not in stopwords.words('english'), word_list))

2
如果word_list太大,此代码将非常慢。最好在使用停用词列表之前将其转换为一个集合.. in set(stopwords.words('english'))。
— 罗伯特

0

这是我的看法,以防万一您想立即将答案放入字符串中(而不是过滤单词的列表):

STOPWORDS = set(stopwords.words('english'))
text =  ' '.join([word for word in text.split() if word not in STOPWORDS]) # delete stopwords from text

不要在法语中使用这种方法,否则将不会被捕获。
— David Beauchemin

0

如果您将数据存储为a Pandas DataFrame,则可以remove_stopwords从textero使用默认情况下使用NLTK停用词列表的数据。

import pandas as pd
import texthero as hero
df['text_without_stopwords'] = hero.remove_stopwords(df['text'])

0
from nltk.corpus import stopwords 

from nltk.tokenize import word_tokenize 

example_sent = "This is a sample sentence, showing off the stop words filtration."

  
stop_words = set(stopwords.words('english')) 
  
word_tokens = word_tokenize(example_sent) 
  
filtered_sentence = [w for w in word_tokens if not w in stop_words] 
  
filtered_sentence = [] 
  
for w in word_tokens: 
    if w not in stop_words: 
        filtered_sentence.append(w) 
  
print(word_tokens) 
print(filtered_sentence) 

-3
   import sys
print ("enter the string from which you want to remove list of stop words")
userstring = input().split(" ")
list =["a","an","the","in"]
another_list = []
for x in userstring:
    if x not in list:           # comparing from the list and removing it
        another_list.append(x)  # it is also possible to use .remove
for x in another_list:
     print(x,end=' ')

   # 2) if you want to use .remove more preferred code
    import sys
    print ("enter the string from which you want to remove list of stop words")
    userstring = input().split(" ")
    list =["a","an","the","in"]
    another_list = []
    for x in userstring:
        if x in list:           
            userstring.remove(x)  
    for x in userstring:           
        print(x,end = ' ') 
    #the code will be like this

最好添加stopwords.words(“ english”),而不是指定需要删除的每个单词。
— 带领
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.