【问题标题】:Unhashable type: 'list' error for stopwords不可散列的类型:停用词的“列表”错误
【发布时间】:2019-05-10 22:34:31
【问题描述】:

这是我的代码

CSV 文件的网址: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'”错误。我不确定到底发生了什么。我将不胜感激任何帮助。谢谢。

【问题讨论】:

  • 你能发布什么是分词器或类型吗?我相信它一定是一个字符串列表。
  • type(tokenizer) 是一个列表
  • 分词器的元素有哪些类型?

标签: python pandas nltk data-analysis stop-words


【解决方案1】:

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?了解更多详细说明:

  • 标记数据框中的文本
  • 删除停用词
  • 其他相关清洁工艺

为了更好,推特文本处理

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])

【讨论】:

  • 请您向我解释一下您是如何删除错误“unhashable type: 'list'”的,谢谢。
猜你喜欢
  • 2017-03-11
  • 1970-01-01
  • 2019-10-04
  • 1970-01-01
  • 1970-01-01
  • 2017-12-27
  • 2021-01-28
  • 2015-11-24
  • 2019-01-07
相关资源
最近更新 更多