python - 不可哈希类型 : 'list' error for stopwords

标签 python pandas nltk data-analysis stop-words

这是我的代码

CSV 文件的 URL:https://github.com/eugeneketeni/web-mining-final-project/blob/master/Test_file.csv

import pandas as pd

data = pd.read_csv("https://raw.githubusercontent.com/eugeneketeni/web- 
mining-final-project/master/Test_file.csv")

import nltk
from nltk import word_tokenize, sent_tokenize


data['text'] = data.loc[:, 'text'].astype(str)

text = data.loc[:, "text"].astype(str)
tokenizer = [word_tokenize(text[i]) for i in range(len(text))]
print(tokenizer)

filtered_sentence = []


from nltk.corpus import stopwords
stopwords = set(stopwords.words('english'))

filtered_sentence = [w for w in tokenizer if not w in stopwords]
print(filtered_sentence) 

我的分词器可以工作,但是当我尝试删除默认的停用词时,我不断收到“unhashable type: 'list'”错误。我不确定到底发生了什么。我将不胜感激任何帮助。谢谢。

最佳答案

TL;DR

from nltk import word_tokenize
from nltk.corpus import stopwords

import pandas as pd

stoplist = set(stopwords.words('english'))

data = pd.read_csv("Test_file.csv")

data['filtered_text'] = data['text'].astype(str).apply(lambda line: [token for token in word_tokenize(line) if token not in stoplist])

请参阅Why is my NLTK function slow when processing the DataFrame?更详细的解释:

  • 对数据框中的文本进行标记
  • 删除停用词
  • 其他相关清洁流程
<小时/>

为了更好,twitter 文本处理

pip3 install -U nltk[twitter]

然后使用这个:

从 nltk.corpus 导入停用词

from nltk.tokenize import TweetTokenizer

import pandas as pd

word_tokenize = TweetTokenizer().tokenize

stoplist = set(stopwords.words('english'))

data = pd.read_csv("Test_file.csv")

data['filtered_text'] = data['text'].astype(str).apply(lambda line: [token for token in word_tokenize(line) if token not in stoplist])

关于python - 不可哈希类型 : 'list' error for stopwords,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53698679/

相关文章:

python - 从 virtualenv 环境中隐藏 Mac OS X 开发工具

python - django tastypie 和 xml 错误消息 : Please check your `` formats `` and ` `content_types`` on your Serializer

python - Pandas Groupby 绘制按顶级分组的多重索引

python - dataframe iloc 在 pandas 中出乎意料地工作

python - 按用法对单词排序

python - 使用 python 从其后代中提取主词

python - python中的继承问题

python - 对象可 pickle (或可 pickle )意味着什么?

python - 修改数据框行 - Panda Python

python - 通过删除 Python 中的子集来折叠短语列表