Skip to content

Commit e7a7f5a

Browse files
committed
daily merge
1 parent 0405b18 commit e7a7f5a

File tree

2 files changed

+57
-39
lines changed

2 files changed

+57
-39
lines changed

19100303/lunbixiaozi/mymodule/main.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import stats_word
22
import re
33

4-
text = '''
4+
text = 5
5+
6+
'''
57
愚公移山
68
太行,王屋二山的北面,住了一個九十歲的老翁,名叫愚公。二山佔地廣闊,擋住去路,使他和家人往來極為不便。
79
一天,愚公召集家人說:「讓我們各盡其力,剷平二山,開條道路,直通豫州,你們認為怎樣?」
@@ -33,6 +35,10 @@
3335
Filled with admiration for Yugong, the Emperor of Heavens ordered two mighty gods to carry the mountains away.
3436
'''
3537

38+
try:
39+
stats_word.stats_text(text)
40+
except:
41+
print("Main program parameter type error: TypeError catched!")
3642

37-
stats_word.stats_text(text)
43+
3844

19100303/lunbixiaozi/mymodule/stats_word.py

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -37,65 +37,77 @@
3737
'''
3838

3939
def stats_text_en (text): #sort English words by the frequency.
40+
try:
41+
for i in range(len(text)):
42+
if (text[i] >= u'\u0041' and text[i]<=u'\u005a') or (text[i] >= u'\u0061' and text[i]<=u'\u007a'):
43+
break
4044

41-
for i in range(len(text)):
42-
if (text[i] >= u'\u0041' and text[i]<=u'\u005a') or (text[i] >= u'\u0061' and text[i]<=u'\u007a'):
43-
break
4445

46+
text_en = text[i:]
47+
text_en = text_en.replace('--', '')
48+
text_en = text_en.replace('!', '')
49+
text_en = text_en.replace('*', '')
50+
text_en = text_en.replace('.', ' ')
51+
text_en = text_en.replace(',', '')
4552

46-
text_en = text[i:]
47-
text_en = text_en.replace('--', '')
48-
text_en = text_en.replace('!', '')
49-
text_en = text_en.replace('*', '')
50-
text_en = text_en.replace('.', ' ')
51-
text_en = text_en.replace(',', '')
53+
# print("CN words frequency: ")
54+
# print(text_en)
5255

53-
# print("CN words frequency: ")
54-
# print(text_en)
56+
text_en = text_en.split()
5557

56-
text_en = text_en.split()
58+
counter_en = collections.Counter(text_en)
59+
print("\n\nEN words frequency: ")
60+
print(counter_en)
5761

58-
counter_en = collections.Counter(text_en)
59-
print("\n\nEN words frequency: ")
60-
print(counter_en)
62+
return counter_en
6163

62-
return counter_en
64+
except TypeError:
65+
print("English sorting: TypeError catched!")
66+
67+
68+
6369

6470

6571

6672

6773
def stats_text_cn (text): #sort Chinese words by the frequency.
68-
text_cn = ''
74+
try:
75+
text_cn = ''
6976

70-
for ch in text:
71-
if u'\u4e00' <= ch <= u'\u9fff': #only fetch the Chinese characthers
72-
text_cn = text_cn + ch
77+
for ch in text:
78+
if u'\u4e00' <= ch <= u'\u9fff': #only fetch the Chinese characthers
79+
text_cn = text_cn + ch
7380

7481

75-
# text = text.replace(':', '')
76-
# text = text.replace(',', '')
77-
# text = text.replace('\n', '')
78-
#text = text.replace('*', '')
79-
#print ('first char:')
80-
#print (text[0])
82+
# text = text.replace(':', '')
83+
# text = text.replace(',', '')
84+
# text = text.replace('\n', '')
85+
#text = text.replace('*', '')
86+
#print ('first char:')
87+
#print (text[0])
8188

82-
text_split = []
89+
text_split = []
8390

84-
for i in range(len(text_cn)):
85-
text_split.append(text_cn[i])
91+
for i in range(len(text_cn)):
92+
text_split.append(text_cn[i])
8693

87-
#text = text.split()
8894

89-
counter_cn = collections.Counter(text_split)
90-
print("CN wrods frequency: ")
91-
print(counter_cn)
92-
return counter_cn
95+
counter_cn = collections.Counter(text_split)
96+
print("CN wrods frequency: ")
97+
print(counter_cn)
98+
return counter_cn
99+
100+
except TypeError:
101+
print("Chinese sorting: TypeError catched!")
93102

94-
#print(stats_text_cn(text_cn))
95103

96104

97105
def stats_text (text): #call the functions above
106+
107+
try:
108+
stats_text_cn (text)
109+
stats_text_en (text)
110+
except TypeError:
111+
print("Text sorting: TypeError catched!")
98112

99-
stats_text_cn (text)
100-
stats_text_en (text)
101113

0 commit comments

Comments
 (0)