专注于自动化测试,性能测试.......
使用python提取英文文章中的单词及出现的次数(原创)
上一篇 /
下一篇 2009-09-15 19:06:09
/ 个人分类:Python
51Testing软件测试网D}wMidZ6H 经常要读一些英文的文章,但文章中的生僻单词经常会影响阅读的速度和节奏,所以就像先把文章中所有的单词提取出来,然后找出自己不认识的单词进行预读和背诵。下边是使用python进行单词的提取。51Testing软件测试网
Ax'J,v&Y
u
51Testing软件测试网7Q;B
N%vNz #-*-coding:gb2312-*-51Testing软件测试网"Q'h&LL%I3v"S}
import re51Testing软件测试网:rj,|;_]H0O
import string
M0tlCbq.A0#输出结果
B1IY_O UOL[9r)M(pX0f = open("D:\\result.txt","w")51Testing软件测试网$}tJ:}$v8Q\b
#输入文本51Testing软件测试网$F8N&w$x|4a2f
r = open("D:\\input.txt","r")
i \5u2Ay
W,K0strs =r.read()51Testing软件测试网 a0E^7l1xj;Gu
#使用正则表达式,把单词提出出来,并都修改为小写格式51Testing软件测试网]!LIR?,R(eM"Z7G+{
s = re.findall("\w+",str.lower(strs))51Testing软件测试网
A#x`6Sdz\
#去除列表中的重复项,并排序51Testing软件测试网u5Yr4F;t.\p(VZ%e
l = sorted(list(set(s)))
XE^~G5Xfzh.S0#去除含有数字和符号,以及长度小于5的字符串
@'`rWi4w1~Z0for i in l:51Testing软件测试网V4NL O'}E
m = re.search("\d+",i)
J(R/r6\W ?j&ZH0 n = re.search("\W+",i)51Testing软件测试网QQ!AafA
if not m and not n and len(i)>4:51Testing软件测试网 q3}|;Pa-y)G x
f.write(i +" : "+str(s.count(i))+"\n")
/h/[,m?wLmH6NR0r.close()51Testing软件测试网D.s!O7}F
U
f.close()51Testing软件测试网"DD1s2]2^9_W i