Python实现统计英文文章词频的方法分析


Posted in Python onJanuary 28, 2019

本文实例讲述了Python实现统计英文文章词频的方法。分享给大家供大家参考,具体如下:

应用介绍:

统计英文文章词频是很常见的需求,本文利用python实现。

思路分析:

1、把英文文章的每个单词放到列表里,并统计列表长度;
2、遍历列表,对每个单词出现的次数进行统计,并将结果存储在字典中;
3、利用步骤1中获得的列表长度,求出每个单词出现的频率,并将结果存储在频率字典中;
4、以字典键值对的“值”为标准,对字典进行排序,输出结果(也可利用切片输出频率最大或最小的特定几个,因为经过排序sorted()函数处理后,单词及其频率信息已经存储在元组中,所有元组再组成列表。)

代码实现:

fin = open('The_Magic_Skin _Honore_de_Balzac.txt') #the txt is up
#to you
lines=fin.readlines()
fin.close()
'''transform the article into word list
'''
def words_list():
  chardigit='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 '
  all_lines = ''
  for line in lines:
    one_line=''
    for ch in line:
      if ch in chardigit:
        one_line = one_line + ch
    all_lines = all_lines + one_line
  return all_lines.split()
'''calculate the total number of article list
s is the article list
'''
def total_num(s):
  return len(s)
'''calculate the occurrence times of every word
t is the article list
'''
def word_dic(t):
  fre_dic = dict()
  for i in range(len(t)):
    fre_dic[t[i]] = fre_dic.get(t[i],0) + 1
  return fre_dic
'''calculate the occurrence times of every word
w is dictionary of the occurrence times of every word
'''
def word_fre(w):
  for key in w:
    w[key] = w[key] / total
  return w
'''sort the dictionary
v is the frequency of words
'''
def word_sort(v):
  sort_dic = sorted(v.items(), key = lambda e:e[1])
  return sort_dic
'''This is entrance of functions
output is the ten words with the largest frequency
'''
total = total_num(words_list())
print(word_sort(word_fre(word_dic(words_list())))[-10:])
Python 相关文章推荐
python读文件逐行处理的示例代码分享
Dec 27 Python
python字典序问题实例
Sep 26 Python
在Python的Flask框架中实现全文搜索功能
Apr 20 Python
Python爬取国外天气预报网站的方法
Jul 10 Python
python中的不可变数据类型与可变数据类型详解
Sep 16 Python
Python实现的爬取小说爬虫功能示例
Mar 30 Python
python实现多线程端口扫描
Aug 31 Python
pytorch 实现打印模型的参数值
Dec 30 Python
PyCharm永久激活方式(推荐)
Sep 22 Python
ubuntu16.04升级Python3.5到Python3.7的方法步骤
Aug 20 Python
Python代码风格与编程习惯重要吗?
Jun 03 Python
Python 处理表格进行成绩排序的操作代码
Jul 26 Python
Python3实现统计单词表中每个字母出现频率的方法示例
Jan 28 #Python
Python判断变量名是否合法的方法示例
Jan 28 #Python
Python使用while循环花式打印乘法表
Jan 28 #Python
Python实现程序判断季节的代码示例
Jan 28 #Python
Python后台管理员管理前台会员信息的讲解
Jan 28 #Python
Python之列表实现栈的工作功能
Jan 28 #Python
Python中常用的内置方法
Jan 28 #Python
You might like
PHP开发规范手册之PHP代码规范详解
2011/01/13 PHP
浅谈php自定义错误日志
2015/02/13 PHP
Laravel6.0.4中将添加计划任务事件的方法步骤
2019/10/15 PHP
PHP实现简单用户登录界面
2019/10/23 PHP
用Javascript 和 CSS 实现脚注(Footnote)效果
2009/09/09 Javascript
精通JavaScript 纠正 cleanWhitespace函数
2010/03/11 Javascript
jquery.validate使用详解
2016/06/02 Javascript
Highcharts学习之数据列
2016/08/03 Javascript
Node.js检测端口(port)是否被占用的简单示例
2016/09/29 Javascript
利用VUE框架,实现列表分页功能示例代码
2017/01/12 Javascript
原生JS实现层叠轮播图
2017/05/17 Javascript
JavaScript字符串_动力节点Java学院整理
2017/06/27 Javascript
bootstrap 通过加减按钮实现输入框组功能
2017/11/15 Javascript
使用vue脚手架(vue-cli)搭建一个项目详解
2019/05/09 Javascript
Openlayers绘制地图标注
2020/09/28 Javascript
原生JavaScript实现留言板
2021/01/10 Javascript
[02:03]DOTA2亚洲邀请赛 HGT战队出场宣传片
2015/02/07 DOTA
如何搜索查找并解决Django相关的问题
2014/06/30 Python
基于scrapy实现的简单蜘蛛采集程序
2015/04/17 Python
python中matplotlib实现最小二乘法拟合的过程详解
2017/07/11 Python
django实现前后台交互实例
2017/08/07 Python
python2.7实现爬虫网页数据
2018/05/25 Python
Python利用scapy实现ARP欺骗的方法
2019/07/23 Python
Python虚拟环境库virtualenvwrapper安装及使用
2020/06/17 Python
用opencv给图片换背景色的示例代码
2020/07/08 Python
Python基于locals返回作用域字典
2020/10/17 Python
html标签之Object和EMBED标签详解
2013/07/04 HTML / CSS
北美最大的手工艺品零售商之一:Michaels Stores
2019/02/27 全球购物
Zooplus罗马尼亚:宠物食品和配件
2019/11/02 全球购物
师范应届生教师求职信
2013/11/05 职场文书
2014党员四风对照检查材料思想汇报
2014/09/17 职场文书
2014学习十八届四中全会精神思想汇报范文
2014/10/23 职场文书
招商银行收入证明
2015/06/17 职场文书
vue3如何优雅的实现移动端登录注册模块
2021/03/29 Vue.js
利用Python判断整数是否是回文数的3种方法总结
2021/07/07 Python
Python中的协程(Coroutine)操作模块(greenlet、gevent)
2022/05/30 Python