Skip to content

Commit e1b1e28

Browse files
committed
19100205 11day
1 parent ef6e7d1 commit e1b1e28

File tree

6 files changed

+190
-31
lines changed

6 files changed

+190
-31
lines changed

19100205/11661246/d11_training1.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import yagmail
2+
import getpass
3+
import requests
4+
from pyquery import PyQuery
5+
6+
# -*- coding: utf-8 -*-
7+
response = requests.get('https://mp.weixin.qq.com/s/pLmuGoc4bZrMNl7MSoWgiA', auth=('[email protected]', 'ZoroYa11661246'))
8+
9+
#提取公众号伪代码
10+
11+
document = PyQuery(response.text)
12+
content = document('#js_content').text() #把抓取的内容写成可视的文字
13+
14+
15+
16+
#链接邮箱服务器
17+
sender = input('输入发件人地址:')
18+
password = getpass.getpass('输入发件人密码:')
19+
recipients = input('输入收件人地址:')
20+
smtp = 'smtp.126.com' #服务器地址
21+
#Todo:在下面实现自己的分词统计和发送邮件的操作
22+
23+
#统计内容的前100词频
24+
print(mymodule.stats_word.stats_text(text))
25+
26+
27+
# 发送邮件
28+
yag.send('[email protected]', '19100205 11661246', text)

19100205/11661246/main.py

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

29-
print(mymodule.stats_word.stats_text_cn(text))
29+
print(mymodule.stats_word.stats_text_cn(text,10))
Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,47 @@
11
# encoding=utf
2+
# 调用jieba
23
import jieba #day10
4+
#调用counter
35
import collections
6+
import re
47

58
# 函数1:统计输入文本中英文单词的词频:
6-
def stats_text_en(text):
9+
def stats_text_en(text,count):
710
if not isinstance(text,str):
811
raise ValueError('输入的不是文本格式,请重新输入:') # 第8天作业要求,添加参数类型检查
912
text = text.replace('.', '').replace('!', '').replace('--', '').replace('*', '').replace(',', '').replace('(', '').replace(')', '').replace(';', '').replace(':', '').replace('\'', '').replace('?', '').replace('_', '').replace('-', '').replace('/', '') .replace('[', '') .replace(']', '') .replace('\\', '') .replace('\"', '').replace('{', '').replace('}', '').replace('\t', '').replace('\n', '').replace('\r\n', '')
1013

1114
# 以上替换去除各种标点符号
1215
list_text = text.split() # 将字符串转换为列表
1316
import collections
14-
count = int(input("请输入要限制输出的元素个数:"))
1517
dic = collections.Counter(list_text).most_common(count)
1618
return dic
1719

1820

1921

2022
# 函数2:统计输入文本中中文字的词频:
21-
def stats_text_cn(text):
23+
def stats_text_cn(text,count):
2224
if not isinstance(text,str):
2325
raise ValueError('输入的不是文本格式,请重新输入:') # 第8天作业要求,添加参数类型检查
2426

2527
text1 = []
2628
for i in text: # 这个循环有效,说明一串汉字也是一个字符串,每个汉字就是其中的一个元素,可以用for in 来遍历,其中i代表了每个汉字的unicode编码
2729
if u'\u4e00' > i > u'\u9fff': # 挑选出非中文字
28-
text=text.split(i,"") # 将非中文字符替换为空格
29-
seg_list = jieba.cut(text,cut_all =False)
30+
text=text.split(i,"") # 将非中文字符替换
31+
seg_list = jieba.cut(text,cut_all =False)
3032

3133
for j in seg_list:
3234
if len(j) >= 2 : #只统计长度大于等于2的词
3335
text1.append(j)
34-
count = int(input('请输入要限制输出的元素个数:'))
3536
text1 = collections.Counter(text1).most_common(count) #按出现次数从大到小排列
3637
return text1
3738

3839

3940
# 函数3:统计中英文混合词频:
40-
def stats_text(text):
41-
'''函数说明:
42-
本函数的功能是统计输入文本的汉字及英语单词词频,并以降序排列输出。'''
43-
dic_1 = stats_text_cn(text) # 调用函数1统计中文字词频
44-
if not isinstance(text,str):
45-
raise ValueError('输入的不是文本格式,请重新输入:') # 第8天作业要求,添加参数类型检查
46-
for i in text:
47-
if u'\u4e00' <= i <= u'\u9fff':
48-
text = text.replace(i,"") #删除所有中文字
49-
text = text.replace('「', '').replace('」', '').replace(',', '').replace('。', '').replace(':', '').replace('?', '').replace('!', '')
50-
# 以上一句删除所有中文标点
51-
dic_2 = stats_text_en(text) # 调用2函数统计英文单词词频
52-
dic_3 = {}
53-
dic_3.update(dic_2)
54-
dic_3.update(dic_1) # 将之前分别得到的两个中英文词频结果字典合并
55-
dic_3 = sorted(dic_3.items(),key = lambda x:x[1],reverse = True) # 对合并后的字典进行排序,得出混合排序结果
56-
57-
return(dic_3)
58-
59-
# print(stats_text(text))
60-
61-
print(stats_text.__doc__)
41+
def stats_text(text,count) :
42+
'''合并中英文词频'''
43+
'''参数类型检查,如果输入参数不为字符串抛出ValueError'''
44+
if not isinstance(text, str) :
45+
return(stats_text_en(text,count) + stats_text_cn(text,count))
46+
else:
47+
raise ValueError("输入的不是字符串")

19100205/11661246/mymodule/test.py

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import jieba
1+
'''import jieba
22
import collections
33
# 函数2:统计输入文本中中文字的词频:
44
def stats_text_cn(text):
@@ -27,4 +27,68 @@ def stats_text_cn(text):
2727
except ValueError:
2828
print('输入的不是文本格式,请重新输入:')
2929
30-
print(stats_text_cn(a))
30+
print(stats_text_cn(a))'''
31+
32+
'''
33+
r = requests.get('https://mp.weixin.qq.com/s/pLmuGoc4bZrMNl7MSoWgiA', auth=('[email protected]', 'yWt590109'))
34+
r.status_code
35+
200
36+
r.headers['content-type']
37+
'application/json; charset=utf8'
38+
r.encoding
39+
'utf-8'
40+
r.text
41+
u'{"type":"User"...'
42+
r.json()
43+
{u'disk_usage': 368627, u'private_gists': 484, ...}
44+
45+
print r.status_code
46+
print r.headers['content-type']'''
47+
48+
49+
'''# 函数3:统计中英文混合词频:
50+
def stats_text(text):
51+
52+
dic_1 = stats_text_cn(text) # 调用函数1统计中文字词频
53+
if not isinstance(text,str):
54+
raise ValueError('输入的不是文本格式,请重新输入:') # 第8天作业要求,添加参数类型检查
55+
for i in text:
56+
if u'\u4e00' <= i <= u'\u9fff':
57+
text = text.replace(i,"") #删除所有中文字
58+
text = text.replace('「', '').replace('」', '').replace(',', '').replace('。', '').replace(':', '').replace('?', '').replace('!', '')
59+
# 以上一句删除所有中文标点
60+
dic_2 = stats_text_en(text) # 调用2函数统计英文单词词频
61+
dic_3 = {}
62+
dic_3.update(dic_2)
63+
dic_3.update(dic_1) # 将之前分别得到的两个中英文词频结果字典合并
64+
dic_3 = sorted(dic_3.items(),key = lambda x:x[1],reverse = True) # 对合并后的字典进行排序,得出混合排序结果
65+
66+
return(dic_3) '''
67+
import jieba
68+
import collections
69+
# 函数2:统计输入文本中中文字的词频:
70+
def stats_text_cn(text,count):
71+
if not isinstance(text,str):
72+
raise ValueError('输入的不是文本格式,请重新输入:') # 第8天作业要求,添加参数类型检查
73+
74+
text1 = []
75+
for i in text: # 这个循环有效,说明一串汉字也是一个字符串,每个汉字就是其中的一个元素,可以用for in 来遍历,其中i代表了每个汉字的unicode编码
76+
if i < u'\u4e00' and i > u'\u9fff': # 挑选出非中文字
77+
text=text.split(i,"") # 将非中文字符替换
78+
seg_list = jieba.cut(text,cut_all =False)
79+
80+
for j in seg_list:
81+
if len(j) >= 2 : #只统计长度大于等于2的词
82+
text1.append(j)
83+
text1 = collections.Counter(text1).most_common(count) #按出现次数从大到小排列
84+
return text1
85+
86+
with open('tang300.json','r',encoding='UTF-8') as f:
87+
a = f.read()
88+
try:
89+
if not isinstance(a,str):
90+
raise ValueError()
91+
92+
except ValueError:
93+
print('输入的不是文本格式')
94+
print(stats_text-cn(a,10))
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import yagmail
2+
import getpass
3+
import requests
4+
from pyquery import PyQuery
5+
6+
response = requests.get('https://mp.weixin.qq.com/s/pLmuGoc4bZrMNl7MSoWgiA')
7+
8+
#提取公众号伪代码
9+
10+
document = PyQuery(response.text)
11+
content = document('#js_content').text() #把抓取的内容写成可视的文字
12+
13+
14+
15+
#链接邮箱服务器
16+
sender = input('输入发件人地址:')
17+
password = getpass.getpass('输入发件人密码:')
18+
recipients = input('输入收件人地址:')
19+
smtp = 'smtp.126.com' #服务器地址
20+
#Todo:在下面实现自己的分词统计和发送邮件的操作
21+
22+
import mymodule.stats_word
23+
#统计内容的前100词频
24+
statlist = mymodule.stats_word.stats_text_cn(content,100)
25+
statString = "".join(str(i) for i in statlist)
26+
27+
28+
# 发送邮件
29+
yagmail.SMTP(sender,password,smtp).send(recipients, '19100205 11661246', content)
30+
31+

19100205/11661246/mymodule/text11.PY

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,54 @@ def stats_text(text_e_c,count_e_c):
3838
else:
3939
raise ValueError("输入的不是字符串")
4040

41+
# encoding=utf
42+
import jieba #day10
43+
import collections
44+
45+
# 函数1:统计输入文本中英文单词的词频:
46+
def stats_text_en(text,count):
47+
if not isinstance(text,str):
48+
raise ValueError('输入的不是文本格式,请重新输入:') # 第8天作业要求,添加参数类型检查
49+
text = text.replace('.', '').replace('!', '').replace('--', '').replace('*', '').replace(',', '').replace('(', '').replace(')', '').replace(';', '').replace(':', '').replace('\'', '').replace('?', '').replace('_', '').replace('-', '').replace('/', '') .replace('[', '') .replace(']', '') .replace('\\', '') .replace('\"', '').replace('{', '').replace('}', '').replace('\t', '').replace('\n', '').replace('\r\n', '')
50+
51+
# 以上替换去除各种标点符号
52+
list_text = text.split() # 将字符串转换为列表
53+
import collections
54+
count = int(input("请输入要限制输出的元素个数:"))
55+
dic = collections.Counter(list_text).most_common(count)
56+
return dic
57+
58+
59+
60+
# 函数2:统计输入文本中中文字的词频:
61+
def stats_text_cn(text):
62+
if not isinstance(text,str):
63+
raise ValueError('输入的不是文本格式,请重新输入:') # 第8天作业要求,添加参数类型检查
64+
65+
text1 = []
66+
for i in text: # 这个循环有效,说明一串汉字也是一个字符串,每个汉字就是其中的一个元素,可以用for in 来遍历,其中i代表了每个汉字的unicode编码
67+
if u'\u4e00' > i > u'\u9fff': # 挑选出非中文字
68+
text=text.split(i,"") # 将非中文字符替换
69+
seg_list = jieba.cut(text,cut_all =False)
70+
71+
for j in seg_list:
72+
if len(j) >= 2 : #只统计长度大于等于2的词
73+
text1.append(j)
74+
count = int(input('请输入要限制输出的元素个数:'))
75+
text1 = collections.Counter(text1).most_common(count) #按出现次数从大到小排列
76+
return text1
77+
78+
79+
# 函数3:统计中英文混合词频:
80+
def stats_text(text) :
81+
'''合并中英文词频'''
82+
'''参数类型检查,如果输入参数不为字符串抛出ValueError'''
83+
if not isinstance(text, str) :
84+
return(stats_text_en(text) + stats_text_cn(text))
85+
else:
86+
raise ValueError("输入的不是字符串")
87+
88+
# print(stats_text(text))
89+
90+
print(stats_text.__doc__)
4191

0 commit comments

Comments
 (0)