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实现字典依据value排序
Feb 24 Python
使用Python编写一个最基础的代码解释器的要点解析
Jul 12 Python
使用Python的Django和layim实现即时通讯的方法
May 25 Python
Python中property函数用法实例分析
Jun 04 Python
对python遍历文件夹中的所有jpg文件的实例详解
Dec 08 Python
Python在图片中插入大量文字并且自动换行
Jan 02 Python
Python实现的拉格朗日插值法示例
Jan 08 Python
Python实现插入排序和选择排序的方法
May 12 Python
pytorch 输出中间层特征的实例
Aug 17 Python
pandas将多个dataframe以多个sheet的形式保存到一个excel文件中
Oct 10 Python
服务器端jupyter notebook映射到本地浏览器的操作
Apr 14 Python
获取python运行输出的数据并解析存为dataFrame实例
Jul 07 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 return语句另类用法不止是在函数中
2014/09/17 PHP
php中文字符串截取多种方法汇总
2016/10/06 PHP
php7基于递归实现删除空文件夹的方法示例
2017/06/15 PHP
Windows平台实现PHP连接SQL Server2008的方法
2017/07/26 PHP
ThinkPHP3.2框架操作Redis的方法分析
2019/05/05 PHP
Yii2框架视图(View)操作及Layout的使用方法分析
2019/05/27 PHP
下载站控制介绍字数显示的脚本 显示全部 隐藏介绍等功能
2009/09/19 Javascript
jQuery实现ajax调用WCF服务的方法(附带demo下载)
2015/12/04 Javascript
jQuery unbind 删除绑定事件详解
2016/05/24 Javascript
JavaScript ES6的新特性使用新方法定义Class
2016/06/28 Javascript
js关于getImageData跨域问题的解决方法
2016/10/14 Javascript
详解React-Todos入门例子
2016/11/08 Javascript
正则表达式替换html元素属性的方法
2016/11/26 Javascript
根据Bootstrap Paginator改写的js分页插件
2016/12/25 Javascript
JavaScript对象封装的简单实现方法(3种方法)
2017/01/03 Javascript
微信小程序 自定义消息提示框
2017/08/06 Javascript
js异步上传多张图片插件的使用方法
2018/10/22 Javascript
微信小程序 swiper 组件遇到的问题及解决方法
2019/05/26 Javascript
json_decode 索引为数字时自动排序问题解决方法
2020/03/28 Javascript
Python中random模块生成随机数详解
2016/03/10 Python
python字典操作实例详解
2017/11/16 Python
Python获取指定字符前面的所有字符方法
2018/05/02 Python
python定时检测无响应进程并重启的实例代码
2019/04/22 Python
Python使用QQ邮箱发送邮件报错smtplib.SMTPAuthenticationError
2019/12/20 Python
python读取ini配置的类封装代码实例
2020/01/08 Python
Holiday Inn中国官网:IHG旗下假日酒店预订
2018/04/08 全球购物
斯图尔特·韦茨曼鞋加拿大官网:Stuart Weitzman加拿大
2019/10/13 全球购物
俄罗斯最大的在线珠宝大卖场:Nebo
2019/12/08 全球购物
地球鞋加拿大官网:Earth Shoes Canada
2020/11/17 全球购物
个人委托书怎么写
2014/04/04 职场文书
答谢会策划方案
2014/05/12 职场文书
原料仓仓管员岗位职责
2014/07/08 职场文书
装修活动策划方案
2014/08/27 职场文书
奔腾年代观后感
2015/06/09 职场文书
开业庆典嘉宾致辞
2015/08/01 职场文书
Python中glob库实现文件名的匹配
2021/06/18 Python