【发布时间】:2017-11-06 19:27:02
【问题描述】:
目前我正在使用以下代码使用 spaCy 对某些文本数据进行词形还原和计算 TF-IDF 值:
lemma = []
for doc in nlp.pipe(df['col'].astype('unicode').values, batch_size=9844,
n_threads=3):
if doc.is_parsed:
lemma.append([n.lemma_ for n in doc if not n.lemma_.is_punct | n.lemma_ != "-PRON-"])
else:
lemma.append(None)
df['lemma_col'] = lemma
vect = sklearn.feature_extraction.text.TfidfVectorizer()
lemmas = df['lemma_col'].apply(lambda x: ' '.join(x))
vect = sklearn.feature_extraction.text.TfidfVectorizer()
features = vect.fit_transform(lemmas)
feature_names = vect.get_feature_names()
dense = features.todense()
denselist = dense.tolist()
df = pd.DataFrame(denselist, columns=feature_names)
df = pd.DataFrame(denselist, columns=feature_names)
lemmas = pd.concat([lemmas, df])
df= pd.concat([df, lemmas])
我需要去除专有名词、标点符号和停用词,但在我当前的代码中执行此操作时遇到了一些问题。我读过一些documentation 和other resources,但现在遇到了错误:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-21-e924639f7822> in <module>()
7 if doc.is_parsed:
8 tokens.append([n.text for n in doc])
----> 9 lemma.append([n.lemma_ for n in doc if not n.lemma_.is_punct or n.lemma_ != "-PRON-"])
10 pos.append([n.pos_ for n in doc])
11 else:
<ipython-input-21-e924639f7822> in <listcomp>(.0)
7 if doc.is_parsed:
8 tokens.append([n.text for n in doc])
----> 9 lemma.append([n.lemma_ for n in doc if not n.lemma_.is_punct or n.lemma_ != "-PRON-"])
10 pos.append([n.pos_ for n in doc])
11 else:
AttributeError: 'str' object has no attribute 'is_punct'
有没有更简单的方法可以从文本中删除这些内容,而不必彻底改变我的方法?
提供完整代码here。
【问题讨论】:
标签: python python-3.x nlp spacy