【发布时间】:2018-02-07 18:53:27
【问题描述】:
我有一个 JSON 格式的亚马逊用户评论数据,我将其导入到 pandas 数据框并使用它来训练文本分类模型。我正在尝试在使用该数据训练模型之前预处理用户评论文本。我有两个问题:
1) 我已经编写了一个代码来使用 Python 中的 Textblob 库来检测它的语言,该库运行良好,但耗费了大量时间。请告诉我是否有最佳方法。我在 python 中使用 Textblob 库,代码是:
from textblob import TextBlob
def detect_language(text):
if len(text)>3:
r=TextBlob(text)
lang = r.detect_language()
return lang
dataset['language']=dataset.reviewText.apply(lambda x: detect_language(x))
2) 我想在训练模型之前对我的话进行词形还原。但是如果我们用词标记词性,NLTK 中的词形还原将正常工作,我正在尝试如下但得到一些错误:
from nltk import pos_tag
from nltk.stem import WordNetLemmatizer
text='my name is shubham'
text=pos_tag(text.split())
wl=WordNetLemmatizer()
for i in text:
print(wl.lemmatize(i))
这里我的帖子被标记为:
[('my', 'PRP$'), ('name', 'NN'), ('is', 'VBZ'), ('shubham', 'JJ')]
在进行词形还原时出现以下错误:
AttributeError: 'tuple' object has no attribute 'endswith'
您能否建议一种执行词形还原的有效方法。 这是我正在执行语言检测和词形还原的示例数据:
overall reviewText
5 Not much to write about here, but it does exac...
5 The product does exactly as it should and is q...
5 The primary job of this device is to block the...
5 Nice windscreen protects my MXL mic and preven...
5 This pop filter is great. It looks and perform...
【问题讨论】:
-
需要有关错误消息的更多信息。哪一行导致它,如果是 wl.lemmeatize 则在执行之前打印 i。
-
@gout 我在
print(wl.lemmatize(i))中遇到错误。我尝试打印 i 并知道它对第一个单词本身给出错误,即 ('my', 'PRP$')。 -
@alvas 我现在可以做到。谢谢。
标签: python pandas nltk lemmatization textblob