python之单词词频统计

本文介绍了一种使用Python统计英文文本中单词出现频率的方法,并选取了出现频率最高的10个单词进行展示。该方法适用于文本分析及信息提取等场景。

需求分析:指定任意英文文本,统计文本中个单词出现的频率,并选出出现频率最高的10个单词
环境:python 3.6.0
IDE:PyCharm 2017.2.3
module:string
数据类型:字符串,列表,元组,字典

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import string
import matplotlib.pyplot as plt
words_freq={ }

f=open("《教父》小说英文版.txt")

for line in f:
#间类似于twenty-one的单词分为两个单词
#分词
    words=line.replace("-"," ").split()
    for word in words:
    #去掉单词前后的标点符号
        word=word.strip(string.punctuation)
        #所有单词不区分大小写,全部转成小写
        word=word.lower()
        #统计,以字典存储
        if word in words_freq:
            words_freq[word]+=1
        else:
            words_freq[word]=1
freq_words=[]
for word,freq in words_freq.items():
    freq_words.append((freq,word))
freq_words.sort(reverse=True)
for freq,word in freq_words[:10]]
    print(word,freq)

运行结果

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值