Python 获取文档中发现的单词频率的累积计数

Python 获取文档中发现的单词频率的累积计数,python,nltk,word-frequency,trend,Python,Nltk,Word Frequency,Trend,我一直在尝试检测文字片段上的单词/双字符趋势。到目前为止,我所做的是删除停止词、小写和获取词频,并在列表中添加每个文本最常见的30个单词 e、 g 然后我将上面的列表转换为一个巨大的列表,其中包含所有单词及其每文档频率,现在我需要做的是返回一个排序列表,即: [(u'snow', 32), (u'said.', 12), (u'GoT', 10), (u'death', 8), (u'entertainment', 4)..] 有什么想法吗 代码: 如果您只想对列表进行排序,则可以使用 imp

我一直在尝试检测文字片段上的单词/双字符趋势。到目前为止,我所做的是删除停止词、小写和获取词频,并在列表中添加每个文本最常见的30个单词

e、 g

然后我将上面的列表转换为一个巨大的列表,其中包含所有单词及其每文档频率,现在我需要做的是返回一个排序列表,即:

[(u'snow', 32), (u'said.', 12), (u'GoT', 10), (u'death', 8), (u'entertainment', 4)..]
有什么想法吗

代码:


如果您只想对列表进行排序,则可以使用

import operator

fdists = [(u'seeing', 2), (u'said.', 2), (u'one', 2), (u'death', 2), (u'entertainment',   2), (u'it\u2019s', 2), (u'weiss', 2), (u'read', 2), (u'\u201cit', 1), (u'shot', 1), (u'show\u2019s', 1), (u'people', 1), (u'dead,\u201d', 1), (u'bloody', 1)]
fdists2 = [(u'seeing', 3), (u'said.', 4), (u'one', 2), (u'death', 2), (u'entertainment',   2), (u'it\u2019s', 2), (u'weiss', 2), (u'read', 2)]
fdists += fdists2

fdict = {}
for i in fdists:
    if i[0] in fdict:
        fdict[i[0]] += i[1]
    else:
        fdict[i[0]] = i[1]

sorted_f = sorted(fdict.items(), key=operator.itemgetter(1), reverse=True)
print sorted_f[:30]

[(u'said.', 6), (u'seeing', 5), (u'death', 4), (u'entertainment', 4), (u'read', 4), (u'it\u2019s', 4), (u'weiss', 4), (u'one', 4), (u'\u201cit', 1), (u'shot', 1), (u'show\u2019s', 1), (u'people', 1), (u'dead,\u201d', 1), (u'bloody', 1)]
处理重复项的另一种方法是使用pandas
groupby()
函数,然后使用
sort()
函数按
count
word
进行排序

from pandas import *
import pandas as pd

fdists = [(u'seeing', 2), (u'said.', 2), (u'one', 2), (u'death', 2), (u'entertainment',   2), (u'it\u2019s', 2), (u'weiss', 2), (u'read', 2), (u'\u201cit', 1), (u'shot', 1), (u'show\u2019s', 1), (u'people', 1), (u'dead,\u201d', 1), (u'bloody', 1)]
fdists2 = [(u'seeing', 3), (u'said.', 4), (u'one', 2), (u'death', 2), (u'entertainment',   2), (u'it\u2019s', 2), (u'weiss', 2), (u'read', 2)]
fdists += fdists2

df = DataFrame(data = fdists, columns = ['word','count'])
df= DataFrame([{'word': k, 'count': (v['count'].sum())} for k,v in df.groupby(['word'])], columns = ['word','count'])

Sorted = df.sort(['count','word'], ascending = [0,1])
print Sorted[:30]

             word  count
8           said.      6
9          seeing      5
2           death      4
3   entertainment      4
4            it’s      4
5             one      4
7            read      4
12          weiss      4
0          bloody      1
1          dead,”      1
6          people      1
10           shot      1
11         show’s      1
13            “it      1

为什么不使用字典?从一开始就为了捕获每个唯一单词的出现或for循环之后?我尝试从一开始就使用collection.Counter,但它需要永远执行。与其使用列表存储单词,不如使用字典,然后根据值进行排序。这种方法可以找到单词的累积计数吗?我的意思是,如果在两份文件中分别提到5次和2次“死亡”一词,那么“死亡”一词的字数将是7,或者将有两个单独的条目?感谢you@Swan87我更新了熊猫的答案来解释duplicates@Swan87两个答案都更新了,但我个人更喜欢熊猫。我认为它看起来更干净,如果你想用它做其他事情,你可以比列表更容易地操作你的数据帧。
import operator

fdists = [(u'seeing', 2), (u'said.', 2), (u'one', 2), (u'death', 2), (u'entertainment',   2), (u'it\u2019s', 2), (u'weiss', 2), (u'read', 2), (u'\u201cit', 1), (u'shot', 1), (u'show\u2019s', 1), (u'people', 1), (u'dead,\u201d', 1), (u'bloody', 1)]
fdists2 = [(u'seeing', 3), (u'said.', 4), (u'one', 2), (u'death', 2), (u'entertainment',   2), (u'it\u2019s', 2), (u'weiss', 2), (u'read', 2)]
fdists += fdists2

fdict = {}
for i in fdists:
    if i[0] in fdict:
        fdict[i[0]] += i[1]
    else:
        fdict[i[0]] = i[1]

sorted_f = sorted(fdict.items(), key=operator.itemgetter(1), reverse=True)
print sorted_f[:30]

[(u'said.', 6), (u'seeing', 5), (u'death', 4), (u'entertainment', 4), (u'read', 4), (u'it\u2019s', 4), (u'weiss', 4), (u'one', 4), (u'\u201cit', 1), (u'shot', 1), (u'show\u2019s', 1), (u'people', 1), (u'dead,\u201d', 1), (u'bloody', 1)]
from pandas import *
import pandas as pd

fdists = [(u'seeing', 2), (u'said.', 2), (u'one', 2), (u'death', 2), (u'entertainment',   2), (u'it\u2019s', 2), (u'weiss', 2), (u'read', 2), (u'\u201cit', 1), (u'shot', 1), (u'show\u2019s', 1), (u'people', 1), (u'dead,\u201d', 1), (u'bloody', 1)]
fdists2 = [(u'seeing', 3), (u'said.', 4), (u'one', 2), (u'death', 2), (u'entertainment',   2), (u'it\u2019s', 2), (u'weiss', 2), (u'read', 2)]
fdists += fdists2

df = DataFrame(data = fdists, columns = ['word','count'])
df= DataFrame([{'word': k, 'count': (v['count'].sum())} for k,v in df.groupby(['word'])], columns = ['word','count'])

Sorted = df.sort(['count','word'], ascending = [0,1])
print Sorted[:30]

             word  count
8           said.      6
9          seeing      5
2           death      4
3   entertainment      4
4            it’s      4
5             one      4
7            read      4
12          weiss      4
0          bloody      1
1          dead,”      1
6          people      1
10           shot      1
11         show’s      1
13            “it      1