forked from apecodex/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFind_a_word.py
More file actions
72 lines (56 loc) · 1.58 KB
/
Find_a_word.py
File metadata and controls
72 lines (56 loc) · 1.58 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# -*- coding: utf-8 -*-
"""
统计英文单词出现的个数
"""
import re
def add_word(word, word_dict):
if word in word_dict:
word_dict[word]+=1
else:
word_dict[word]=1
def Find_word(path="English.txt"):
time = 1
with open(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()
word_dict = {}
for x in lines:
add_word(x,word_dict)
for keys,values in sorted(word_dict.items()):
time+=1
print("%3d %-20s %s" % (time,keys,values))
#Find_word()
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)
s = FindWord()
s.find()