查看英文文章里面的单词出现的次数,暂时还没有发现问题

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
# -*- coding: utf-8 -*-
"""
统计英文单词出现的个数
"""
import re
class FindWord:
def __init__(self,path="English.txt",word_dict={}):
self.path = path
self.word_dict = word_dict
def find(self):
print("View all or one(total\one)")
poi = input("View: ")
time = 1
with open(self.path,'r') as f:
read_word = f.read()
match = re.findall(r"[^a-zA-Z0-9']+",read_word)
for i in match:
read_word = read_word.replace(i," ")
lines = read_word.split()
for x in lines:
if x in self.word_dict:
self.word_dict[x]+=1
else:
self.word_dict[x]=1
if poi == "total":
for keys,values in sorted(self.word_dict.items()):
time+=1
print("%3d %-20s 出现了‘%s'次" % (time,keys,values))
elif poi == "one":
user_input = input("Word: ")
get_word = self.word_dict.get(user_input,"No Found.")
print("'%s' 出现了'%s'次" % (user_input,get_word))
else:
print("No '%s' parameter" % poi)

文章的内容是:

However mean your life is,meet it and live it ;do not shun it and call it hard names.It > is not so bad as you are.It looks poorest when you are richest.The fault-finder willfind faults in paradise.Love your life,poor as it is.You may perhaps have some pleasant,thrilling,glorious hourss,even in a poor-house.The setting sun is reflected from the windows of the alms-house as brightly as from the rich man’s abode;the snow melts before its door as early in the spring.I do not see but a quiet mind may live as contentedly there,and have as cheering thoughts,as in a palace.The town’s poor seem to me often to live the most independent lives of any.May be they are simply great enough to receive without misgiving.Most think that they are above being supported by the town;but it often happens that they are not above supporting themselves by dishonest means.which should be more disreputable.Cultivate poverty like a garden herb,like sage.Do not trouble yourself much to get new things,whether clothes or friends,Turn the old,return to them.Things do not change;we change.Sell your clothes and keep your thoughts.

下面是运行的结果:

Find_word.png