Ale*_*lex 98 python nltk stop-words
所以我有一个数据集,我想删除使用的停止词
stopwords.words('english')
Run Code Online (Sandbox Code Playgroud)
我正在努力如何在我的代码中使用它只是简单地取出这些单词.我已经有了这个数据集中的单词列表,我正在努力的部分是与此列表进行比较并删除停用词.任何帮助表示赞赏.
Dar*_*mas 184
from nltk.corpus import stopwords
# ...
filtered_words = [word for word in word_list if word not in stopwords.words('english')]
Run Code Online (Sandbox Code Playgroud)
小智 19
你也可以做一个设置差异,例如:
list(set(nltk.regexp_tokenize(sentence, pattern, gaps=True)) - set(nltk.corpus.stopwords.words('english')))
Run Code Online (Sandbox Code Playgroud)
das*_*zul 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
Run Code Online (Sandbox Code Playgroud)
sum*_*njr 10
要排除所有类型的停用词,包括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]
Run Code Online (Sandbox Code Playgroud)
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')]
Run Code Online (Sandbox Code Playgroud)
这个包下载非常轻量级(与 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
Run Code Online (Sandbox Code Playgroud)
如果您想立即将答案放入字符串(而不是过滤后的单词列表)中,这是我对此的看法:
STOPWORDS = set(stopwords.words('english'))
text = ' '.join([word for word in text.split() if word not in STOPWORDS]) # delete stopwords from text
Run Code Online (Sandbox Code Playgroud)
使用textcleaner库从您的数据中删除停用词。
按照此链接:https : //yugantm.github.io/textcleaner/documentation.html#remove_stpwrds
请按照以下步骤使用此库执行此操作。
pip install textcleaner
Run Code Online (Sandbox Code Playgroud)
安装后:
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
Run Code Online (Sandbox Code Playgroud)
使用上面的代码删除停用词。