Skip to content

Commit ef6e7d1

Browse files
committed
19100205 10day
1 parent 267987e commit ef6e7d1

File tree

6 files changed

+49
-10
lines changed

6 files changed

+49
-10
lines changed

19100205/11661246/d5_exercise_string.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
# 删除包含ea的单词
2727
list_str = str.split () # 将字符串转换为列表
28-
list_str1 = []
28+
list_str1 = []
2929
for i in list_str:
3030
if 'ea' not in i:
3131
list_str1.append(i) # 此循环用if条件删除包含ea的单词

19100205/11661246/main.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,4 @@
2626
except ValueError:
2727
print('输入的不是文本格式,请重新输入:')
2828

29-
print(mymodule.stats_word.stats_text_cn(text))
30-
29+
print(mymodule.stats_word.stats_text_cn(text))
Binary file not shown.

19100205/11661246/mymodule/stats_word.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# encoding=utf
2+
import jieba #day10
3+
import collections
4+
15
# 函数1:统计输入文本中英文单词的词频:
26
def stats_text_en(text):
37
if not isinstance(text,str):
@@ -17,14 +21,19 @@ def stats_text_en(text):
1721
def stats_text_cn(text):
1822
if not isinstance(text,str):
1923
raise ValueError('输入的不是文本格式,请重新输入:') # 第8天作业要求,添加参数类型检查
20-
dic = {}
24+
25+
text1 = []
2126
for i in text: # 这个循环有效,说明一串汉字也是一个字符串,每个汉字就是其中的一个元素,可以用for in 来遍历,其中i代表了每个汉字的unicode编码
22-
if u'\u4e00' <= i <= u'\u9fff': # 挑选出中文字
23-
dic[i] = text.count(i) # 用.count()函数/方法来对每个元素(这里是汉字)进行计数,形成一个字典
24-
import collections
27+
if u'\u4e00' > i > u'\u9fff': # 挑选出非中文字
28+
text=text.split(i,"") # 将非中文字符替换为空格
29+
seg_list = jieba.cut(text,cut_all =False)
30+
31+
for j in seg_list:
32+
if len(j) >= 2 : #只统计长度大于等于2的词
33+
text1.append(j)
2534
count = int(input('请输入要限制输出的元素个数:'))
26-
dic = collections.Counter(dic).most_common(count) #按出现次数从大到小排列
27-
return dic
35+
text1 = collections.Counter(text1).most_common(count) #按出现次数从大到小排列
36+
return text1
2837

2938

3039
# 函数3:统计中英文混合词频:

19100205/11661246/mymodule/test.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import jieba
2+
import collections
3+
# 函数2:统计输入文本中中文字的词频:
4+
def stats_text_cn(text):
5+
if not isinstance(text,str):
6+
raise ValueError('输入的不是文本格式,请重新输入:') # 第8天作业要求,添加参数类型检查
7+
8+
text1 = []
9+
for i in text: # 这个循环有效,说明一串汉字也是一个字符串,每个汉字就是其中的一个元素,可以用for in 来遍历,其中i代表了每个汉字的unicode编码
10+
if i <u'\u4e00' or > u'\u9fff': # 挑选出非中文字
11+
text=text.split(i,"") # 将非中文字符删除
12+
seg_list = jieba.cut(text,cut_all =False)
13+
14+
for j in seg_list:
15+
if len(j) >= 2 : #只统计长度大于等于2的词
16+
text1.append(j)
17+
count = int(input('请输入要限制输出的元素个数:'))
18+
text1 = collections.Counter(text1).most_common(count) #按出现次数从大到小排列
19+
return text1
20+
21+
with open('tang300.json','r',encoding='UTF-8') as f:
22+
a = f.read()
23+
try:
24+
if not isinstance(a,str):
25+
raise ValueError()
26+
27+
except ValueError:
28+
print('输入的不是文本格式,请重新输入:')
29+
30+
print(stats_text_cn(a))

19100205/11661246/mymodule/text11.PY

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import collections
22
import re
3+
import jieba
34
def stats_text_en(en,count):
45
'''英文词频统计'''
56
'''参数类型检查;如果输入参数不为字符串则抛出ValueError'''
@@ -37,4 +38,4 @@ def stats_text(text_e_c,count_e_c):
3738
else:
3839
raise ValueError("输入的不是字符串")
3940

40-
41+

0 commit comments

Comments
 (0)