【词频统计】--用python的jieba进行英文文本词频统计

目录

1、基本思路:统计哈利波特小说中词频最高的前20个,去掉一些停用词(如is)

2、停用词(截取部分)

3、代码如下

4、小知识:元组可以用来这样赋值  


1、基本思路:统计哈利波特小说中词频最高的前20个,去掉一些停用词(如is)

2、停用词(截取部分)

 


3、代码如下

# -*- coding: utf-8 -*-
"""
@File  : 04.py
@author: FxDr
@Time  : 2022/10/19 14:33
"""
import jieba

'''
    英文文本词频统计.
'''
# 打开文件,读取内容
txt = open("Harry Potter and The Half Blood Prince.txt", "r").read()

# 转小写
txt = txt.lower()
# 去掉一些特殊符号
for each in '"’—!|“#$%&()*+,-./:;<=>?@[\\]^{|}~”':
    txt = txt.replace(each, " ")  # 用空格代替特殊符号

# 文本分词
words = jieba.cut(txt)  # 默认用空格分离并以列表形式返回
stopword = []
with open('stopwords_EN.txt', 'r') as f:
    stopwords = f.read() # 一些无意义不需要统计的词

counts = {}

for word in words:
    if word not in stopwords:
        counts[word] = counts.get(word, 0) + 1

items = list(counts.items())  # 将字典转为列表
items.sort(key=lambda x: x[1], reverse=True)  # 按第二列排序,从高到低
print(type(items))  # 列表类型
print(type(items[0]))  # 元组类型
for i in range(20):
    word, count = items[i]
    print("{0:<10}{1:>5}".format(word, count))

其中

输出如下:harry 2815
dumbledore 1034
hermione  694
slughorn  397
snape  379
malfoy  371
professor  280
voldemort  243
ginny  233
hagrid  231
weasley  211
eyes  206
dark  200
voice  195
wand  191
door  179
moment  167
people  165
head  162
told  160

 

4、小知识:元组可以用来这样赋值 

# -*- coding: utf-8 -*-
"""
@File  : 00.py
@author: FxDr
@Time  : 2022/10/30 2:07
"""
t = (1, 2)
print(type(t))
a, b = t
print(a, b)

 

 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值