python - 最常见单词或短语的 FreqDist

标签 python machine-learning nlp nltk tokenize

我正在尝试分析应用评论中的一些数据。

我想使用 nltk 的 FreqDist 来查看文件中最常出现的短语。它可以是单个标记或关键短语。 我不想对数据进行标记,因为这只会给我最常见的标记。但现在,FreqDist 函数将每条评论作为一个字符串进行处理,而不是提取每条评论中的单词。

df = pd.read_csv('Positive.csv')

def pre_process(text):
    translator = str.maketrans("", "", string.punctuation)
    text = text.lower().strip().replace("\n", " ").replace("’", "").translate(translator)
    return text

df['Description'] = df['Description'].map(pre_process)
df = df[df['Description'] != '']

word_dist = nltk.FreqDist(df['Description'])

(“描述”是评论的正文/消息。)

例如,我想要得到类似的东西 最常见的术语: “我喜欢”、“有用”、“非常好的应用程序” 但相反我得到 最常见的术语: “我真的很喜欢这个应用程序,因为 babblabla”(完整评论)

这就是为什么当我绘制 FreqDist 时我得到这个:

enter image description here

最佳答案

TL;DR

使用ngramseverygrams:

>>> from itertools import chain
>>> import pandas as pd
>>> from nltk import word_tokenize
>>> from nltk import FreqDist

>>> df = pd.read_csv('x')
>>> df['Description']
0            Here is a sentence.
1    This is a foo bar sentence.
Name: Description, dtype: object

>>> df['Description'].map(word_tokenize)
0              [Here, is, a, sentence, .]
1    [This, is, a, foo, bar, sentence, .]
Name: Description, dtype: object

>>> sents = df['Description'].map(word_tokenize).tolist()

>>> FreqDist(list(chain(*[everygrams(sent, 1, 3) for sent in sents])))
FreqDist({('sentence',): 2, ('is', 'a'): 2, ('sentence', '.'): 2, ('is',): 2, ('.',): 2, ('a',): 2, ('Here', 'is', 'a'): 1, ('a', 'foo'): 1, ('a', 'sentence'): 1, ('bar', 'sentence', '.'): 1, ...})

关于python - 最常见单词或短语的 FreqDist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56280140/

相关文章:

python - Django 对过滤后的查询集的所有相关对象求和

python - 图里创建错误: 'module' object not callable

python - 如果 Rasa 无法识别意图怎么办?

machine-learning - 如何查看每个单词的 tf-idf 分数

machine-learning - NLP 预训练模型的数据预处理(例如 ELMo、Bert)

python - NamedTuple 的子类化给出了不兼容的返回类型/参数类型

Python(图像库): Resample string as argument

python - 如何枚举文件的特定列

machine-learning - SVM——评分函数

sql - 数据准备: Alternatives to constructing a table using sql resulting in many columns