python - 在 python 中对整个句子进行词形还原不起作用

标签 python pandas scikit-learn nltk text-mining

我使用Python中NLTK包中的WordNetLemmatizer()函数来对电影评论数据集的整个句子进行词形还原。

这是我的代码:

from nltk.stem import LancasterStemmer, WordNetLemmatizer
lemmer = WordNetLemmatizer()

def preprocess(x):

    #Lemmatization
    x = ' '.join([lemmer.lemmatize(w) for w in x.rstrip().split()])

    # Lower case
    x = x.lower()

    # Remove punctuation
    x = re.sub(r'[^\w\s]', '', x)

    # Remove stop words
    x = ' '.join([w for w in x.split() if w not in stop_words])    
    ## EDIT CODE HERE ## 

    return x

df['review_clean'] = df['review'].apply(preprocess)

df中的review是我要处理的文本评论栏

在 df 上使用预处理函数后,新列 review_clean 包含清理后的文本数据,但仍然没有词形还原的文本。例如。我可以看到很多单词以 -ed、-ing 结尾。

提前致谢。

最佳答案

您必须传递“v”(动词)才能进行词形还原:

x = ' '.join([lemmer.lemmatize(w, 'w') for w in x.rstrip().split()])
<小时/>

示例:

In [11]: words = ["answered", "answering"]

In [12]: [lemmer.lemmatize(w) for w in words]
Out[12]: ['answered', 'answering']

In [13]: [lemmer.lemmatize(w, 'v') for w in words]
Out[13]: ['answer', 'answer']

关于python - 在 python 中对整个句子进行词形还原不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54845821/

相关文章:

python - 根据特定列的值计数添加/删除行

python - 使用 sklearn 找出错误率

machine-learning - 归一化或标准化后,线性回归给出更差的结果

python - Tensorflow 2.1.0 - DLL 加载失败 - Windows CPU

Python SQLite3插入没有返回错误但表中没有数据

python - 更有效地使用 itertools.groupby()

python - 计算多索引 pandas 数据框中值出现的最快方法

python - 如何按顺序合并多个数据帧?

python - sklearn OPTICS 和预先计算的余弦矩阵不产生簇

c++ - 如何桥接 Python 和 C++