Skip to content

Commit bd0ae29

Browse files
committed
no message
1 parent fe2ee8e commit bd0ae29

File tree

3 files changed

+105
-72
lines changed

3 files changed

+105
-72
lines changed
653 Bytes
Binary file not shown.

19100102/daweijian/mymodule/main.py

100644100755
Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
1-
#通过stats_word导入stats_text模块
2-
from stats_word import stats_text
3-
4-
text = '''
5-
愚公移山
6-
太行,王屋二山的北面,住了一個九十歲的老翁,名叫愚公。二山佔地廣闊,擋住去路,使他和家人往來極為不便。
7-
一天,愚公召集家人說:「讓我們各盡其力,剷平二山,開條道路,直通豫州,你們認為怎樣?」
8-
大家都異口同聲贊成,只有他的妻子表示懷疑,並說:「你連開鑿一個小丘的力量都沒有,怎可能剷平太行、王屋二山呢?況且,鑿出的土石又丟到哪裏去呢?」
9-
大家都熱烈地說:「把土石丟進渤海裏。」
10-
於是愚公就和兒孫,一起開挖土,把土石搬運到渤海去。
11-
愚公的鄰居是個寡婦,有個兒子八歲也興致勃勃地走來幫忙。
12-
寒來暑往,他們要一年才能往返渤海一次。
13-
住在黃河河畔的智叟,看見他們這樣辛苦,取笑愚公說:「你不是很愚蠢嗎?你已一把年紀了,就是用盡你的氣力,也不能挖去山的一角呢?」
14-
愚公歎息道:「你有這樣的成見,是不會明白的。你比那寡婦的小兒子還不如呢!就算我死了,還有我的兒子,我的孫子,我的曾孫子,他們一直傳下去。而這二山是不會加大的,總有一天,我們會把它們剷平。」
15-
智叟聽了,無話可說:
16-
二山的守護神被愚公的堅毅精神嚇倒,便把此事奏知天帝。天帝佩服愚公的精神,就命兩位大力神揹走二山。
17-
How The Foolish Old Man Moved Mountains
18-
Yugong was a ninety-year-old man who lived at the north of two high mountains, Mount Taixing and Mount Wangwu.
19-
Stretching over a wide expanse of land, the mountains blocked yugong’s way making it inconvenient for him and his family to get around.
20-
One day yugong gathered his family together and said,”Let’s do our best to level these two mountains. We shall open a road that leads to Yuzhou. What do you think?”
21-
All but his wife agreed with him.
22-
“You don’t have the strength to cut even a small mound,” muttered his wife. “How on earth do you suppose you can level Mount Taixin and Mount Wanwu? Moreover, where will all the earth and rubble go?”
23-
“Dump them into the Sea of Bohai!” said everyone.
24-
So Yugong, his sons, and his grandsons started to break up rocks and remove the earth. They transported the earth and rubble to the Sea of Bohai.
25-
Now Yugong’s neighbour was a widow who had an only child eight years old. Evening the young boy offered his help eagerly.
26-
Summer went by and winter came. It took Yugong and his crew a full year to travel back and forth once.
27-
On the bank of the Yellow River dwelled an old man much respected for his wisdom. When he saw their back-breaking labour, he ridiculed Yugong saying,”Aren’t you foolish, my friend? You are very old now, and with whatever remains of your waning strength, you won’t be able to remove even a corner of the mountain.”
28-
Yugong uttered a sigh and said,”A biased person like you will never understand. You can’t even compare with the widow’s little boy!”
29-
“Even if I were dead, there will still be my children, my grandchildren, my great grandchildren, my great great grandchildren. They descendants will go on forever. But these mountains will not grow any taler. We shall level them one day!” he declared with confidence.
30-
The wise old man was totally silenced.
31-
When the guardian gods of the mountains saw how determined Yugong and his crew were, they were struck with fear and reported the incident to the Emperor of Heavens.
32-
Filled with admiration for Yugong, the Emperor of Heavens ordered two mighty gods to carry the mountains away.
33-
'''
34-
35-
#运行导入的函数
36-
stats_text(text)
1+
# 通过stats_word导入stats_text模块
2+
from stats_word import stats_text
3+
4+
text = '''
5+
愚公移山
6+
太行,王屋二山的北面,住了一個九十歲的老翁,名叫愚公。二山佔地廣闊,擋住去路,使他和家人往來極為不便。
7+
一天,愚公召集家人說:「讓我們各盡其力,剷平二山,開條道路,直通豫州,你們認為怎樣?」
8+
大家都異口同聲贊成,只有他的妻子表示懷疑,並說:「你連開鑿一個小丘的力量都沒有,怎可能剷平太行、王屋二山呢?況且,鑿出的土石又丟到哪裏去呢?」
9+
大家都熱烈地說:「把土石丟進渤海裏。」
10+
於是愚公就和兒孫,一起開挖土,把土石搬運到渤海去。
11+
愚公的鄰居是個寡婦,有個兒子八歲也興致勃勃地走來幫忙。
12+
寒來暑往,他們要一年才能往返渤海一次。
13+
住在黃河河畔的智叟,看見他們這樣辛苦,取笑愚公說:「你不是很愚蠢嗎?你已一把年紀了,就是用盡你的氣力,也不能挖去山的一角呢?」
14+
愚公歎息道:「你有這樣的成見,是不會明白的。你比那寡婦的小兒子還不如呢!就算我死了,還有我的兒子,我的孫子,我的曾孫子,他們一直傳下去。而這二山是不會加大的,總有一天,我們會把它們剷平。」
15+
智叟聽了,無話可說:
16+
二山的守護神被愚公的堅毅精神嚇倒,便把此事奏知天帝。天帝佩服愚公的精神,就命兩位大力神揹走二山。
17+
How The Foolish Old Man Moved Mountains
18+
Yugong was a ninety-year-old man who lived at the north of two high mountains, Mount Taixing and Mount Wangwu.
19+
Stretching over a wide expanse of land, the mountains blocked yugong’s way making it inconvenient for him and his family to get around.
20+
One day yugong gathered his family together and said,”Let’s do our best to level these two mountains. We shall open a road that leads to Yuzhou. What do you think?”
21+
All but his wife agreed with him.
22+
“You don’t have the strength to cut even a small mound,” muttered his wife. “How on earth do you suppose you can level Mount Taixin and Mount Wanwu? Moreover, where will all the earth and rubble go?”
23+
“Dump them into the Sea of Bohai!” said everyone.
24+
So Yugong, his sons, and his grandsons started to break up rocks and remove the earth. They transported the earth and rubble to the Sea of Bohai.
25+
Now Yugong’s neighbour was a widow who had an only child eight years old. Evening the young boy offered his help eagerly.
26+
Summer went by and winter came. It took Yugong and his crew a full year to travel back and forth once.
27+
On the bank of the Yellow River dwelled an old man much respected for his wisdom. When he saw their back-breaking labour, he ridiculed Yugong saying,”Aren’t you foolish, my friend? You are very old now, and with whatever remains of your waning strength, you won’t be able to remove even a corner of the mountain.”
28+
Yugong uttered a sigh and said,”A biased person like you will never understand. You can’t even compare with the widow’s little boy!”
29+
“Even if I were dead, there will still be my children, my grandchildren, my great grandchildren, my great great grandchildren. They descendants will go on forever. But these mountains will not grow any taler. We shall level them one day!” he declared with confidence.
30+
The wise old man was totally silenced.
31+
When the guardian gods of the mountains saw how determined Yugong and his crew were, they were struck with fear and reported the incident to the Emperor of Heavens.
32+
Filled with admiration for Yugong, the Emperor of Heavens ordered two mighty gods to carry the mountains away.
33+
'''
34+
35+
# 运行导入的函数
36+
stats_text(text)
Lines changed: 69 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,78 @@
1+
# 封装d5的代码
2+
import re
13

2-
#封装d5的代码
3-
def cut_clean(s):
4-
#切分字符串
5-
s=s.split()
6-
#清洗标点符号
7-
i=0
8-
while i<len(s):
9-
s[i]=s[i].strip('*-.')
10-
if s[i]=='': #清洗完成之后若为空元素‘’,则删除元素
11-
s.remove('')
4+
5+
def clean_ip_list(words): # 清理掉空格 标点符号
6+
i = 0
7+
while i < len(words):
8+
words[i] = words[i].strip('*-.')
9+
if words[i] == '': # 清洗完成之后若为空元素‘’,则删除元素
10+
words.remove('')
1211
else:
13-
i=i+1
12+
i = i + 1
13+
return words
14+
15+
16+
def cut_clean(s):
17+
# 切分字符串
18+
s = s.split()
19+
# 清洗标点符号
20+
clean_ip_list(s)
1421
return s
1522

16-
#把list转化为dict并统计词频
17-
def list_dict(aList):
18-
aDict={}
19-
for i in range(len(aList)):
20-
aDict[aList[i]]=0
21-
for i in range(len(aList)):
22-
aDict[aList[i]]=aDict[aList[i]]+1
23-
return aDict
24-
25-
def stats_text_en(checkstr):
26-
checkstr = cut_clean(checkstr) #切分字符串并清洗标点符号
27-
s_dict = list_dict(checkstr) #将tempiate转化为字典并统计词频
28-
#对字典按照value值排序
29-
s_s_dict = sorted(s_dict.items(),key=lambda item:item[1],reverse=True)
23+
24+
# 把list转化为dict并统计词频
25+
def list_dict(l):
26+
cadiz = {}
27+
b = True # 是否是英文
28+
for word in l:
29+
for char in word:
30+
if (char >= '\u0041' and char <= '\u005a') or (char >= '\u0061' and char <= '\u007a'): # 字符是英文
31+
b = True
32+
break
33+
else: #存在一个字符非英文 所以整个词非英文单词
34+
b = False
35+
break
36+
if (b):
37+
if word in cadiz:
38+
cadiz[word] += 1
39+
else:
40+
cadiz[word] = 1
41+
return cadiz
42+
43+
44+
def stats_text_en(s):
45+
s = cut_clean(s) # 切分字符串并清洗标点符号
46+
s_dict = list_dict(s) # 将tempiate转化为字典并统计词频
47+
# 对字典按照value值排序
48+
s_s_dict = sorted(s_dict.items(), key=lambda item: item[1], reverse=True)
3049
print(s_s_dict)
3150
return s_s_dict
3251

33-
def stats_text_cn(checkstr): #定义检索中文函数
34-
cut_clean(checkstr)
35-
cndic = {}
36-
for i in checkstr:
37-
if '\u4e00' <= i <= '\u9fff':
38-
cndic[i] = checkstr.count(i)
39-
return cndic
4052

41-
#定义stats_text函数
42-
def stats_text(str):
43-
stats_text_cn(str) #导入stats_text_cn函数
44-
stats_text_en(str) #导入stats_text_en函数
53+
def cut_count_cn(c, regex): # 取出所有中文 是一个列表
54+
words = regex.findall(c) # 对整段文字进行分词
55+
words = clean_ip_list(words) # 分词后去掉空格标点符号
56+
cadiz = {}
57+
for word in words: # 取出每一个词语
58+
for char in word: # 取出每个词语中的每一个词
59+
if '\u4e00' <= char <= '\u9fff': # 如果是中文
60+
if char in cadiz:
61+
cadiz[char] += 1
62+
else:
63+
cadiz[char] = 1
64+
return cadiz
65+
66+
67+
def stats_text_cn(s): # 定义检索中文函数
68+
regex = re.compile("(?x)(?: [\w -]+ | [\x80 -\xff]{3} )")
69+
words = cut_count_cn(s, regex)
70+
s_s_dict = sorted(words.items(), key=lambda item: item[1], reverse=True)
71+
print(s_s_dict)
72+
return s_s_dict
73+
4574

75+
# 定义stats_text函数
76+
def stats_text(s):
77+
stats_text_cn(s) # 导入stats_text_cn函数
78+
stats_text_en(s) # 导入stats_text_en函数

0 commit comments

Comments
 (0)