Python 词频统计

Python 词频统计

大家好,又见面了,我是你们的朋友全栈君。

利用Python做一个词频统计

GitHub地址:FightingBob 【Give me a star , thanks.】

  • 词频统计

  对纯英语的文本文件【Eg: 瓦尔登湖(英文版).txt】的英文单词出现的次数进行统计,并记录起来

  • 代码实现

  •  1 import string
     2 from os import path
     3 with open('瓦尔登湖(英文版).txt','rb') as text1:
     4     words = [word.strip(string.punctuation).lower() for word in str(text1.read()).split()]
     5     words_index = set(words)
     6     count_dict = {index:words.count(index) for index in words_index}
     7     with open(path.dirname(__file__) + '/file1.txt','a+') as text2:
     8         text2.writelines('以下是词频统计的结果:' + '\n')
     9         for word in sorted(count_dict,key=lambda x:count_dict[x],reverse=True):
    10             text2.writelines('{}--{} times'.format(word,count_dict[word]) + '\n')
    11         text1.close()
    12         text2.close()

     

  • 代码解析  

    • 获取文件,以二进制格式打开文件,用于读取内容

      •   1 with open(‘瓦尔登湖(英文版).txt’,’rb’) as text1:

    • 获取单词列表

      • 先读取内容

        •   content = text1.read()
      • 再获取单词列表(使用split() 通过指定分隔符对字符串进行切片)

        •   words = content.split()
      • 单词大写改小写,去掉单词前后符号

        •   word,strip(string.punctuation).lower()
      • 去除重复的单词

        •   words_index = set(words)
    • 设置单词:单词次数的字典      

      •   count_dict = {index:words.count(index) for index in words_index}
    • 写入词频统计

      • 先创建文件,获取当前目录,并以追加写入的方式写入

        •   with open(path.dirname(__file__) + ‘/file1.txt’,’a+’) as text2:
      • 换行写入

        •   text2.writelines(‘以下是词频统计的结果:’ + ‘\n’)
      • 对单词进行排序,根据次数从大到小【key=lambda x:count_dict[x]以值排序】

        •   sorted(count_dict,key=lambda x:count_dict[x],reverse=True)
      • 换行写入词频

        •   text2.writelines(‘{}–{} times’.format(word,count_dict[word]) + ‘\n’)
      • 关闭资源

        •   text1.close()
        •   text2.close()

GitHub地址:FightingBob 【Give me a star , thanks.】          

 

转载于:https://www.cnblogs.com/littlebob/p/9189794.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/107469.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • B2C电子商务不只是开个网上商店那么简单

    B2C电子商务不只是开个网上商店那么简单

    2021年7月24日
  • android 通过类名跳转activity,Activity跳转方式总结

    android 通过类名跳转activity,Activity跳转方式总结一、显式调用方法方法一:Intentintent=newIntent(本类,将要跳转的类);//Intentintent=newIntent(MainActivity.this,JumpToActivity.class);startActivity(intent);方法二:Intentintent2=newIntent();intent2.setClass(本类,将要跳转的类)…

    2022年5月22日
  • 网站程序下载_可以免费下载程序的网站

    网站程序下载_可以免费下载程序的网站1、博客网站:Wordpress、zblog2、门户网站:Dedecms、phpcms、帝国cms3、论坛网站:Discuz4、企业网站:Dedecms、phpcms、帝国cms5、商城网站:

    2022年8月6日
  • spring整合log4j_log4j2异步日志配置

    spring整合log4j_log4j2异步日志配置常用日志框架log4j、log4j2(log4j的升级版,最常用的)、logback(spring boot默认)、Jboss-logging…等slf4 是日志接口规范,代码对接slf4,实现和具体日志框架解耦,无需修改编码即可切换日志框架。修改pom依赖<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-st

    2022年8月9日
  • 最新版黑苹果MacOS 10.14 Mojave安装教程

    最新版黑苹果MacOS 10.14 Mojave安装教程安装分为三部分:1.制作安装U盘2.安装MacOS系统3.安装clover(四叶草)用到的安装工具(按用到的先后顺序):1.Clover_v2.4k_r4679.pkg2.CloverConfigurator.zip3.一般台式机都能使用的通用EFI下载链接:https://pan.baidu.com/s/1sk6tYbCZ0riy0I6…

    2022年6月11日
  • ArrayList 扩容规则[通俗易懂]

    ArrayList 扩容规则[通俗易懂]ArrayList()会使用长度为零的数组ArrayList(intinitialCapacity)会使用指定容量的数组publicArrayList(Collection<?extendsE>c)会使用c的大小作为数组容量add(Objecto)首次扩容为10,再次扩容为上次容量的1.5倍addAll(Collectionc)没有元素时,扩容为Math.max(10,实际元素个数),有元素时为Math.max(原容量1.5倍,实际元素个数.

    2022年5月4日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号