我正在尝试分析应用评论中的一些数据。
我想使用 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 时我得到这个:
最佳答案
TL;DR
使用ngrams
或everygrams
:
>>> 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/