文件方式实现完整的英文词频统计实例

本文介绍了一个中文文本处理的实例,包括读取字符串、提取单词、计数并排序等步骤,最终输出了出现频率最高的20个词。

1.读入待分析的字符串

2.分解提取单词 

3.计数字典

4.排除语法型词汇

5.排序

6.输出TOP(20)

str='''For Chinese people, eating mooncakes with families is one of the important ways to celebrate the Mid-Autumn Festival, which falls on Oct 4 this year, or the 15th day of the 8th Chinese lunar month.

As mass-produced mooncakes sweep market with different flavors mooncakes and various stuffing, customers are inclined to turn to traditional taste, which makes hand-made mooncakes a hot commodity in the market.

Wei Quanfa's hand-made mooncakes is one such case. The 72-year-old baker inherited the traditional craft from his family and started his new business in Zhengzhou, Central China's Henan province.

He carries out every procedure by himself, mooncakes including making stuffing and the mooncake mould, boiling oil, and baking. Because of that, Wei is able to make no more than 40 kg a day in the last month before the festival, and a 30-centimeter-diameter mooncake is priced at over 200 yuan ($30).

With the festival just around the corner, reserving Wei's mooncakes even two days in advance is next to impossible.'''
fo=open('C:\\Users\\Administrator\\Desktop\\news.txt','w')
fo.write(str)
fo.close


fo=open('C:\\Users\\Administrator\\Desktop\\news.txt','r')
news=fo.read()
fo.close()

news=str.lower()  #小写

for i in ",.()-$":
    news=news.replace(i," ")  #空格替换


news=news.split(" ")  #分割字段

exc={'the','one','which','his',' ','a','with','for','to','is','of','on','in','out','no','at','over''such','that','and','or'}



words=set(news)
words=words-exc

dt={}

for i in words:
    dt[i]=news.count(i)  #统计


item=list(dt.items())

item.sort(key=lambda x:x[1],reverse=True)

for i in range(20):
    print(item[i])

 

转载于:https://www.cnblogs.com/silu666-666/p/7596039.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值