python - 下面的python代码有什么错误

标签 python nltk stop-words

我想删除停用词。这是我的代码

import nltk
from nltk.corpus import stopwords
import string

u="The apple is the pomaceous fruit of the apple tree, species Malus domestica in the rose family (Rosaceae). It is one of the most widely cultivated tree fruits, and the most widely known of the many members of genus Malus that are used by humans."

v="An orange is a fruit of the orangle tree. it is the most cultivated tree fruits"

u=u.lower()
v=v.lower()

u_list=nltk.word_tokenize(u)
v_list=nltk.word_tokenize(v)

for word in u_list:
    if word in stopwords.words('english'):
        u_list.remove(word)
for word in v_list:
    if word in stopwords.words('english'):
        v_list.remove(word)

print u_list
print "\n\n\n\n"
print v_list

但仅删除了一些停用词。请帮我解决这个问题

最佳答案

您所做的问题是 list.remove(x) 仅删除第一个出现的x,而不是每个 x。要删除每个实例,您可以使用过滤器,但我会选择这样的东西:

u_list = [word for word in u_list if word not in stopwords.words('english')] 

关于python - 下面的python代码有什么错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12702839/

相关文章:

python - 使用 PyGObject 自定义 Gtk.Container

python - 在 pythonbrew 中使用 virtualenv |安装依赖项

python - 如何在 Flask-Bootstrap 中使用 Bootstrap 4?

Python:tf-idf-cosine:查找文档相似度

python - 将 word_tokenize 转换为句子

Python - 如何将 Panda 中的元素从列表转换为字符串

string - 标记化文本中 ngram(字符串)的频率

language-agnostic - 为什么这些词被视为停用词?

java - 如何在 Lucene 4.4 中自定义停用词列表

SOLR 停用词 : words with 'of' give no results, 但是当 of 被排除时我们得到正确的结果