文本词频统计

1、统计纯英文文本

步骤:

1、打开要统计的文本,只读模式即可,'r',并获取所有内容,赋值给变量txt;

2、对获取的文本内容,去除文本中所有符号,并以空格代替,然后返回去除符号后的文本;

3、对文本内容进行切片,默认以空格切,切完得到的所有单词赋值给变量words;

4、创建一个空字典counts,遍历words,将出现在words中的单词添加到counts中,counts中第一次添加单词时默认赋值为0,之后没出现一次+1;

5、将字典的items转化为列表,并指定第二个元素排序,reverse=True为降序排列;

6、取列表中前十个元素,将元素赋值给word、counts并打印。

代码如下:


def getText():
    with open(r'D:\chromeDownload\The Hills of Refuge.txt', 'r',\
              encoding = 'utf -8') as f:
        txt = f.read()
        for i in '~!@#$%^&*()_+-={}|:"<>?[]\;,./—':
            txt = txt.replace(i, ' ')
        return txt

txt= getText()
words = txt.split()
counts = {}
for i in words:
    counts[i] = counts.get(i, 0) + 1

listhills = list(counts.items())
listhills.sort(key=lambda x:x[1], reverse=True)

for i in range(10):
    word, counts = listhills[i]
    print('{0:<10}{1:>5}'.format(word, counts))

打印结果:

the        4671
to         3251
and        3131
I          2783
a          2181
of         2090
he         2046
you        1860
was        1717
in         1603

2、统计纯中文文本

随机找了一本网文,要用到jieba库,命令行中安装:pip install jieba

版本一:

import jieba

def getWords():
    with open(r'D:\chromeDownload\all.txt', 'r',\
              encoding = 'utf -8') as f:
        txt = f.read()
        words = jieba.lcut(txt)
        return words
words = getWords()

counts = {}
for i in words:
    if len(i) >= 2:
        counts[i] = counts.get(i, 0) + 1

listhills = list(counts.items())
listhills.sort(key=lambda x:x[1], reverse=True)

for i in range(50):
    word, counts = listhills[i]
    print('{0:<10}{1:>5}'.format(word, counts))

截取一部分结果如下:

白夜        23003
说道         8487
没有         7284
一个         6412
可以         5061
什么         4938
这个         3968
交易         3939
不是         3840
已经         3326
自己         3220
他们         3151
就是         2841
世界         2566
事情         2552
现在         2526
起来         2341
知道         2189
不过         2075
一下         1981
还有         1948
尼禄         1939
问道         1933
时候         1866
不会         1845
只是         1841
一些         1828
蝙蝠侠        1825
你们         1759
但是         1753
出来         1752
的话         1749

看的出来,作者很喜欢蝙蝠侠~不过还是一样的套路,除了猪脚其他角色都是酱油,出现的次数简直太少

我们将名字长度修改为大于2,效果非常明显

import jieba

def getWords():
    with open(r'D:\chromeDownload\all.txt', 'r',\
              encoding = 'utf -8') as f:
        txt = f.read()
        words = jieba.lcut(txt)
        return words
words = getWords()

counts = {}
for i in words:
    if len(i) > 2:
        counts[i] = counts.get(i, 0) + 1

listhills = list(counts.items())
listhills.sort(key=lambda x:x[1], reverse=True)

for i in range(50):
    word, counts = listhills[i]
    print('{0:<10}{1:>5}'.format(word, counts))
蝙蝠侠        1825
亚历克斯       1032
拉尔斯         996
孙悟空         924
为什么         746
看上去         593
点点头         544
权财值         471
猪八戒         446
万磁王         420
布鲁斯         393
BOSS        358
甘道夫         347
接下来         333
怎么样         311
到时候         304
斯塔克         296
复仇者         285
哈利波         272
一会儿         267
其他人         263
一部分         257
是不是         252
可不是         249
有没有         246
没想到         242
红孩儿         241
普通人         240
不仅仅         239
实际上         237
有马贵         232
差不多         229
干什么         225
一段时间        219
是因为         216
房间内         210

三国演义:

# 三国演义出场人物统计

import jieba
txt = open(r'D:\chromeDownload\三国演义.txt', 'r', encoding = 'utf -8').read()

words = jieba.lcut(txt)
excludes = {'将军','却说','二人','不可','荆州','不能','如此','商议',\
            '如何','主公','军士','左右','军马','引兵','次日','大喜','天下',\
            '东吴','于是','今日','16','不敢','魏兵','陛下','人马','都督','一人',\
            '不知','汉中','众将','只见','后主','蜀兵','大叫','上马','此人','先主',\
            '太守','天子','后人','背后','一面','城中','何不','忽报','大军','先生',\
            '何故','然后','先锋','夫人','不如'}
counts = {}

for word in words:
    if len(word) == 1:
        continue
    elif word == '孔明' or word == '孔明曰':
        rword = '诸葛亮'
    elif word == '关公' or word == '云长':
        rword = '关羽'
    elif word == '玄德' or word == '玄德曰':
        rword = '张飞'
    elif word == '孟德' or word == '丞相' or word == '丞相曰' or word == '曹孟德':
        rword = '曹操'
    else:
        rword = word
    counts[rword] = counts.get(rword, 0) + 1


for i in excludes:
    del counts[i]

listhills = list(counts.items())
listhills.sort(key=lambda x:x[1], reverse=True)

for k in range(15):
    word, counts = listhills[k]
    print('{0:<10}{1:>5}'.format(word, counts))

出场人物:

曹操         1436
诸葛亮        1373
张飞         1301
关羽          779
吕布          299
刘备          271
孙权          264
赵云          255
司马懿         221
周瑜          217
袁绍          190
马超          185
魏延          177
黄忠          168
姜维          151

 

  • 2
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值