美文网首页Python专题干货
工具使用——统计文章中的英文单词

工具使用——统计文章中的英文单词

作者: 无与童比 | 来源:发表于2014-06-05 14:44 被阅读219次

统计英语单词工具V1.0


本来我是准备好好研究一下爬虫的,结果早晨起来读英语的时候我发现如果能写一个简单的程序将我读书过程中的生词记录下来集合成一张纸,然后去背掉它的话,那岂不是妙极?这个也顺便练习一下做爬虫的正则表达式的能力。

当然,正则表达式是我早就想学习的东西了,不过一直没有找到机会。
好吧,我们开始今天先写一个比较简单的程序来统计英语单词,然后我们根据这个再改动一下,看看能不能进行更加细腻的操作,看看能不能把输入变得更加完善。

首先,我们需要一个txt文档,这里面都是英语文章,然后我们运行程序。
老规矩,小程序先贴代码,后讲解

# -*- coding: utf-8 -*-
#使用方法:把文本用ANSI编码存下来
# 把文章存到成input.txt中并且放到C盘根目录下面,这样比较方便操作
import re
import string
#输出文件
output_file = open("C:\\result.txt","w")
#输入文本文件
input_file = open("C:\\input.txt","r")
strs =input_file.read()
#使用正则表达式,把单词提出出来,并都修改为小写格式
s = re.findall("\w+",str.lower(strs))
# 返回一个列表

#去除列表中的重复项,并排序
l = sorted(list(set(s)))

for i in l:
m = re.search("\d+",i)
n = re.search("\W+",i)
if not m and  not n and len(i)>4:
output_file.write(i +" : "+str(s.count(i))+"\n")
        # 不属于数字也不属于非(英文+数字)并且字母长度大于4的集合
input_file.close()
output_file.close()

好,我们先复制以下的内容,存储到C盘根目录下,文件名 input.txt

 first metacharacters we’ll look at are [ and ]. They’re used for specifying a character class, which is a set of characters that you wish to match. Characters can be listed individually, or a range of characters can be indicated by giving two characters and separating them by a '-'. For example, [abc] will match any of the characters a, b, or c; this is the same as [a-c], which uses a range to express the same set of characters. If you wanted to match only lowercase letters, your RE would be [a-z].

Metacharacters are not active inside classes. For example, [akm$] will match any of the characters 'a', 'k', 'm', or '$'; '$' is usually a metacharacter, but inside a character class it’s stripped of its special nature.

You can match the characters not listed within the class by complementing the set. This is indicated by including a '^' as the first character of the class; '^' outside a character class will simply match the '^' character. For example, [^5] will match any character except '5'.

Perhaps the most important metacharacter is the backslash, \. As in Python string literals, the backslash can be followed by various characters to signal various special sequences. It’s also used to escape all the metacharacters so you can still match them in patterns; for example, if you need to match a [ or \, you can precede them with a backslash to remove their special meaning: \[ or \\.

Some of the special sequences beginning with '\' represent predefined sets of characters that are often useful, such as the set of digits, the set of letters, or the set of anything that isn’t whitespace.

运行程序
成果如下


相关文章

  • 工具使用——统计文章中的英文单词

    统计英语单词工具V1.0 本来我是准备好好研究一下爬虫的,结果早晨起来读英语的时候我发现如果能写一个简单的程序将我...

  • iOS - CMP控件曝光统计

    1 常用统计工具 众所周知,市面上已经有很多统计工具,下面是自己使用过的(免费的)并且使用率较高的一些品牌统计工具...

  • LeakCanary源码解读

    版权声明:本文为原创文章,未经允许不得转载。 LeakCanary是一个安卓的性能统计工具,想必大家都有使用,工具...

  • 利用Hadoop运行第一个程序,计算文章中不同单词数量

    今天通过Hadoop来运行第一个程序,来统计一篇文章中不同英文单词的个数。 1.创建一个 t1.txt 文件夹,并...

  • 产品的数据统计分析及精细化运营

    在对产品的数据分析中,需要对统计分析哪些数据?使用什么工具?以及如何通过应用统计进行分析,从而做到精细化运营? 以...

  • 文章微信复制统计工具{免费试用}

    加粉统计,来粉统计,微信号复制统计,推广链接微信号管理!免费注册使用 加粉统计,微信号复制统计,关键词复制统计工具...

  • 2018-12-15

    加粉统计,来粉统计,微信号复制统计,推广链接微信号管理!免费注册使用 加粉统计,微信号复制统计,关键词复制统计工具...

  • 百度统计工具

    记录百度统计工具使用方法 在首页加入统计代码,其中,baiduId为公司百度统计账号 封装统计方法 调用统计方法 ...

  • JDK14性能管理工具:jstack使用介绍

    简介 在之前的文章中,我们介绍了JDK14中jstat工具的使用,本文我们再深入探讨一下jstack工具的使用。 ...

  • JDK 工具系列之监控工具

    监控工具 你可以用下面的这些工具来监控 JVM 性能统计信息。本文所描述的工具正在试验阶段,不推荐大家使用,使用前...

网友评论

    本文标题:工具使用——统计文章中的英文单词

    本文链接:https://www.haomeiwen.com/subject/whodtttx.html