【python实践】统计历年考研英语真题词频

原文跳转

86_17_1.txt ————1986年到2017年考研英语一真题txt文件
86_17_2.txt ————1986年到2017年考研英语二真题txt文件
86_17_1_2.txt ————1986年到2017年考研英语一、二真题txt文件

2.词频统计及保存结果

#!/usr/bin/python3
# -*- coding:utf-8 -*-
# @Time:2018/8/24 9:37
# @Author: wardseptember
# @File: CountHighFrequencyWords.py
import re

excludes = ['the', 'of', 'to', 'and', 'in', 'a', 'is', 'were', 'was', 'you',
            'I', 'he', 'his', 'there', 'those', 'she', 'her', 'their',
            'that', '[a]', '[b]', '[c]', '[d]', 'them', 'or','for','as',
            'are','on','it','be','with','by','have','from','not','they',
            'more','but','an','at','we','has','can','this','your','which','will',
            'one','should','points)','________','________.','all','than','what',
            'people','if','been','its','new','our','would','part','may','some','i',
            'who','answer','when','most','so','section','no','into','do','only',
            'each','other','following','had','such','much','out','--','up','these',
            'even','how','directions:','use','because','(10','time','(15','[d].',
            '-','it.','[b],','[a],','however,','1','c','1.','2.','b','d','a','(10',
            '2','12.','13.','29.','3.','4.','5.','6.','7.','8.','9.','10.','11.','14.',
            '15.']
#自行过滤简单词,太多了不写了
def getTxt():
    txt = open('86_17_1_2.txt').read()
    txt = txt.lower()
    for ch in '!"@#$%^&*()+,-./:;<=>?@[]_`~{|}': #替换特殊字符
        txt.replace(ch, ' ')
    return txt
#1.获取单词
EngTxt = getTxt()

#2.切割为列表格式
txtArr = EngTxt.split()

#3.遍历统计
counts = {}
for word in txtArr:
    flag=True
    for word1 in excludes:
        if word==word1:
            flag=False
        else:
            continue
    if flag is True:
        counts[word] = counts.get(word, 0) + 1
    else:
        continue

#4.转换格式,方便打印,将字典转换为列表
countsList = list(counts.items())
countsList.sort(key=lambda x:x[1], reverse=True)#按次数从大到小排序

#5.打印输出
for word,count in countsList:
    with open('output_3.txt','a+') as f:
        str1=word+' : '+str(count)+ '次'
        f.writelines(str1+'\n')
        f.close()
    #print('{0:<10}{1:>5}'.format(word,count))
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58

3.结果一览

这里写图片描述
这里写图片描述

4.所有文件下载

86_17_1.txt ————1986年到2017年考研英语一真题txt文件
86_17_2.txt ————1986年到2017年考研英语二真题txt文件
86_17_1_2.txt ————1986年到2017年考研英语一、二真题txt文件
output_1.txt ————英语一词频统计结果
output_2.txt ————英语二词频统计结果
output_3.txt ————英语一、二词频统计结果

链接: https://pan.baidu.com/s/1Q6uVy-wWBpQ0VHvNI_DQxA 密码: fw3r

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值