Как удалить стоп-слова с помощью nltk или python - PullRequest
94 голосов
/ 30 марта 2011

Итак, у меня есть набор данных, который я хотел бы удалить с помощью стоп-слов

stopwords.words('english')

Я изо всех сил пытаюсь использовать это в своем коде, чтобы просто вынуть эти слова. У меня уже есть список слов из этого набора данных, часть, с которой я борюсь, сравнивает этот список и удаляет стоп-слова. Любая помощь приветствуется.

Ответы [ 8 ]

180 голосов
/ 30 марта 2011
from nltk.corpus import stopwords
# ...
filtered_words = [word for word in word_list if word not in stopwords.words('english')]
19 голосов
/ 27 марта 2012

Вы также можете сделать набор diff, например:

list(set(nltk.regexp_tokenize(sentence, pattern, gaps=True)) - set(nltk.corpus.stopwords.words('english')))
14 голосов
/ 30 марта 2011

Полагаю, у вас есть список слов (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
10 голосов
/ 27 октября 2017

Чтобы исключить все типы стоп-слов, включая стоп-слова 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]
3 голосов
/ 12 февраля 2019

Используйте 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 голос
/ 02 октября 2017

с использованием фильтра :

from nltk.corpus import stopwords
# ...  
filtered_words = list(filter(lambda word: word not in stopwords.words('english'), word_list))
1 голос
/ 13 июня 2017

вы можете использовать эту функцию, вы должны заметить, что вам нужно опустить все слова

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
0 голосов
/ 19 марта 2017
   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
...