Skip to content

Commit c4f2a3a

Browse files
huyusuyingca1
huyusu
authored andcommitted
Create d9_exercise
1 parent 93a74bd commit c4f2a3a

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

19100302/huyusu/main.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#import stats_word
2+
import mymodule.stats_word
3+
4+
#text1 = 1
5+
6+
#while True:
7+
#try:
8+
#stats_word.stats_text(text1)
9+
#break
10+
#except ValueError:
11+
#print("导入字符非法")
12+
#raise
13+
14+
15+
#from mymodule import stats_word as sw
16+
with open('tang300.json',encoding='UTF-8') as poem:
17+
read_file = poem.read()
18+
poem.closed
19+
20+
print ('唐诗中最多的100个字:',sw.stats_text_cn(read_file,100))

19100302/huyusu/mymodule/main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import stats_word
22

3+
34
text1 = 1
45

56
while True:

19100302/huyusu/mymodule/stats_word.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@
6767
6868
'''
6969

70+
import collections
71+
7072
a = text.lower()
7173
a = a.split() # 指定分隔符对字符串进行切片
7274
a.pop() # 删除list中的中文元素
@@ -88,8 +90,8 @@ def stats_text_en(): # 定义检索中文函数
8890
found[i] += 1
8991

9092

91-
92-
print('英文单词词频:', sorted(found.items(), key=lambda x: x[1], reverse=True)) # 词频降序
93+
print('英文单词词频: ', collections.Counter(found).most_common(1))
94+
#print('英文单词词频:', sorted(found.items(), key=lambda x: x[1], reverse=True)) # 词频降序
9395

9496

9597
stats_text_en() # 调用函数
@@ -122,7 +124,8 @@ def stats_text_cn(): # 定义检索英文函数
122124
found[i] += 1
123125

124126

125-
print('中文汉字字频: ', sorted(found.items(), key=lambda x: x[1], reverse=True))
127+
print('中文单词词频: ', collections.Counter(found).most_common(100))
128+
#print('中文汉字字频: ', sorted(found.items(), key=lambda x: x[1], reverse=True))
126129

127130
stats_text_cn() # 调用函数
128131

19100302/huyusu/tang300.json

Whitespace-only changes.

0 commit comments

Comments
 (0)