我已经导入了stopwords
from nltk.corpus
,但我得到STOPWORDS is not defined
错误。下面是我的代码:
import nltk
from nltk.corpus import stopwords
#Create stopword list:
stopwords = set(STOPWORDS)
上面给出了以下错误:
NameError: name 'STOPWORDS' is not defined
第一次使用stopwords
来自NLTK
包中,您需要执行以下代码,以便download列表到您的设备:
import nltk
nltk.download('stopwords')
然后,每次需要使用时stopwords
,你可以简单地load从包装中取出它们。例如,加载英文stopwords
列表,您可以使用以下内容:
from nltk.corpus import stopwords
stop_words = list(stopwords.words('english'))
你甚至可以extend如果您愿意,可以查看列表,如下所示(Note: if stopwords.words()
返回一个对象set
类型,然后转换为list
- 如上所示 - 是必需的,以便调用extend()
方法上的stop_words
目的):
stop_words.extend(["best", "item", "fast"])
To remove从文本中停止单词,您可以使用以下内容(看看各种可用的标记器here https://www.nltk.org/api/nltk.tokenize.html and here https://www.nltk.org/howto/tokenize.html):
from nltk.tokenize import word_tokenize
word_tokens = word_tokenize(text)
clean_word_data = [w for w in word_tokens if w.lower() not in stop_words]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)