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清除字符串里非数字字符的方法
Jul 02 Python
Python处理XML格式数据的方法详解
Mar 21 Python
Python开发的十个小贴士和技巧及长常犯错误
Sep 27 Python
Django objects的查询结果转化为json的三种方式的方法
Nov 07 Python
Python 调用 zabbix api的方法示例
Jan 06 Python
python 基于TCP协议的套接字编程详解
Jun 29 Python
python可视化爬虫界面之天气查询
Jul 03 Python
解决Jupyter notebook中.py与.ipynb文件的import问题
Apr 21 Python
使用Python将图片转正方形的两种方法实例代码详解
Apr 29 Python
Virtualenv 搭建 Py项目运行环境的教程详解
Jun 22 Python
Python脚本打包成可执行文件过程解析
Oct 20 Python
详解运行Python的神器Jupyter Notebook
Jun 03 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加密算法之实现可逆加密算法和解密分享
2014/01/21 PHP
php循环创建目录示例分享(php创建多级目录)
2014/03/04 PHP
PHP抓取、分析国内视频网站的视频信息工具类
2014/04/02 PHP
php中使用array_filter()函数过滤空数组的实现代码
2014/08/19 PHP
PHP中变量引用与变量销毁机制分析
2014/11/15 PHP
js 学习笔记(三)
2009/12/29 Javascript
JS 控制小数位数的实现代码
2011/08/02 Javascript
JS特殊函数(Function()构造函数、函数直接量)区别介绍
2013/05/19 Javascript
比较常见的javascript中定义函数的区别
2015/11/09 Javascript
vue2.0中click点击当前li实现动态切换class
2017/06/21 Javascript
vue this.reload 方法 配置
2018/09/12 Javascript
详解koa2学习中使用 async 、await、promise解决异步的问题
2018/11/13 Javascript
jQuery实现的别踩白块小游戏完整示例
2019/01/07 jQuery
JavaScript实现选项卡效果的分析及步骤
2019/04/16 Javascript
什么时候不能在 Node.js 中使用 Lock Files
2019/06/24 Javascript
vue项目引入ts步骤(小结)
2019/10/31 Javascript
解决Nuxt使用axios跨域问题
2020/07/06 Javascript
JS如何监听div的resize事件详解
2020/12/03 Javascript
[00:47]DOTA2荣耀之路6:天火,天火!
2018/05/30 DOTA
[01:45:05]VGJ.T vs Newbee Supermajor 败者组 BO3 第二场 6.6
2018/06/07 DOTA
python使用点操作符访问字典(dict)数据的方法
2015/03/16 Python
Python使用MYSQLDB实现从数据库中导出XML文件的方法
2015/05/11 Python
Python 探针的实现原理
2016/04/23 Python
Python实现TCP/IP协议下的端口转发及重定向示例
2016/06/14 Python
Python数据结构与算法之二叉树结构定义与遍历方法详解
2017/12/12 Python
python递归实现快速排序
2018/08/18 Python
pandas pivot_table() 按日期分多列数据的方法
2018/11/16 Python
python 使用poster模块进行http方式的文件传输到服务器的方法
2019/01/15 Python
Python操作Word批量生成合同的实现示例
2020/08/28 Python
欧洲当代手工玻璃和瓷器的领先品牌:LSA International
2018/06/03 全球购物
英国高街奥特莱斯:Highstreet Outlet
2019/11/21 全球购物
中考冲刺决心书
2014/03/11 职场文书
党的群众路线剖析材料
2014/10/09 职场文书
2014乡党委副书记党建工作汇报材料
2014/11/02 职场文书
2014年幼儿园德育工作总结
2014/12/17 职场文书
组织委员竞选稿
2015/11/21 职场文书