diff --git a/exercises/1901010132/day10/main.py b/exercises/1901010132/day10/main.py new file mode 100644 index 000000000..beaccbf9d --- /dev/null +++ b/exercises/1901010132/day10/main.py @@ -0,0 +1,9 @@ +from mymodule import stats_word +import json + +with open(r'\Users\Administrator\Documents\GitHub\selfteaching-python-camp\exercises\1901010132\day10\tang300.json',encoding='UTF-8') as f: + read_date = f.read() +try: + print('统计前20的词频数: \n',stats_word.stats_text_cn(read_date,20)) +except ValueError as e: + print(e) \ No newline at end of file diff --git a/exercises/1901010132/day10/mymodule/stats_word.py b/exercises/1901010132/day10/mymodule/stats_word.py new file mode 100644 index 000000000..699c602ef --- /dev/null +++ b/exercises/1901010132/day10/mymodule/stats_word.py @@ -0,0 +1,50 @@ +text= ''' +"Her eyes beginning to water, she went on, +"So I would like you all to make me a promise: +from now on, on your way to school, +or on your way home, find something beautiful +to notice. It doesn' t have to be something you +see -it could be a scent perhaps of freshly +baked bread wafting out of someone 's house, +or it could be the sound of the breeze +slightly rustling the leaves in the trees, +or the way the morning light catches one +autumn leaf as it falls gently to the ground. +Please, look for these things, and remember them." +  她的眼睛开始湿润了,她接着说因此我想让你们每个人答应我: +从今以后,在你上学或者放学的路上,要发现一些美丽的事物。 +它不一定是你看到的某个东西——它可能是一种香味—— +也许是新鲜烤面包的味道从某一座房里飘出来,也许是微风轻拂树叶的声音, +或者是晨光照射在轻轻飘落的秋叶上的方式。请你们寻找这些东西并且记住它们吧。 +''' +import re +import collections +import jieba #结巴中文分词 +count=int() +def stats_text_en(text,count): + if type(text) == str: + b=re.sub(r'[^A-Za-z]',' ',text) + list1=b.split() + return(collections.Counter(list1).most_common(count)) + else: + raise ValueError('文本为非字符串') + +def stats_text_cn(text,count): + if not isinstance(text,str): + raise ValueError('文本为非字符串') + p=re.compile(u'[\u4e00-\u9fa5]') + a=re.findall(p,text) + str2=''.join(a) + seg_list = jieba.cut(str2,cut_all=False) #jieba.cut返回的结构是一个可迭代的生成器generator + newlist=[] + for i in seg_list: + if len(i) >= 2: + newlist.append(i) + return(collections.Counter(newlist).most_common(count)) + +def stats_text(text,count): + if not isinstance(text,str): + raise ValueError('文本为非字符串') + stats_text_cn(text,count) + stats_text_en(text,count) +stats_text(text,count) diff --git a/exercises/1901010132/day12/main.py b/exercises/1901010132/day12/main.py new file mode 100644 index 000000000..692dbac6d --- /dev/null +++ b/exercises/1901010132/day12/main.py @@ -0,0 +1,21 @@ +import requests +from pyquery import PyQuery +from mymodule import stats_word +from wxpy import Bot,Message,embed +#初始化机器人,扫码登陆 +bot = Bot() +#找到好友 +my_friend = bot.friends().search('PrincessAKing')[0] +#发送文本给好友 +my_friend.send('分享任意微信文章给我') +#监听消息 +#回复my_friend的消息 +@bot.register(my_friend) +def reply_my_friend(msg): + if msg.type == 'Sharing': + response = requests.get(msg.url) + document = PyQuery(response.text) + content = document('#js_content').text() + reply = stats_word.stats_text_cn(content,100) + return reply +embed() diff --git a/exercises/1901010132/day12/mymodule/stats_word.py b/exercises/1901010132/day12/mymodule/stats_word.py new file mode 100644 index 000000000..699c602ef --- /dev/null +++ b/exercises/1901010132/day12/mymodule/stats_word.py @@ -0,0 +1,50 @@ +text= ''' +"Her eyes beginning to water, she went on, +"So I would like you all to make me a promise: +from now on, on your way to school, +or on your way home, find something beautiful +to notice. It doesn' t have to be something you +see -it could be a scent perhaps of freshly +baked bread wafting out of someone 's house, +or it could be the sound of the breeze +slightly rustling the leaves in the trees, +or the way the morning light catches one +autumn leaf as it falls gently to the ground. +Please, look for these things, and remember them." +  她的眼睛开始湿润了,她接着说因此我想让你们每个人答应我: +从今以后,在你上学或者放学的路上,要发现一些美丽的事物。 +它不一定是你看到的某个东西——它可能是一种香味—— +也许是新鲜烤面包的味道从某一座房里飘出来,也许是微风轻拂树叶的声音, +或者是晨光照射在轻轻飘落的秋叶上的方式。请你们寻找这些东西并且记住它们吧。 +''' +import re +import collections +import jieba #结巴中文分词 +count=int() +def stats_text_en(text,count): + if type(text) == str: + b=re.sub(r'[^A-Za-z]',' ',text) + list1=b.split() + return(collections.Counter(list1).most_common(count)) + else: + raise ValueError('文本为非字符串') + +def stats_text_cn(text,count): + if not isinstance(text,str): + raise ValueError('文本为非字符串') + p=re.compile(u'[\u4e00-\u9fa5]') + a=re.findall(p,text) + str2=''.join(a) + seg_list = jieba.cut(str2,cut_all=False) #jieba.cut返回的结构是一个可迭代的生成器generator + newlist=[] + for i in seg_list: + if len(i) >= 2: + newlist.append(i) + return(collections.Counter(newlist).most_common(count)) + +def stats_text(text,count): + if not isinstance(text,str): + raise ValueError('文本为非字符串') + stats_text_cn(text,count) + stats_text_en(text,count) +stats_text(text,count) diff --git a/exercises/1901010132/day13/main.py b/exercises/1901010132/day13/main.py new file mode 100644 index 000000000..eae28f91c --- /dev/null +++ b/exercises/1901010132/day13/main.py @@ -0,0 +1,33 @@ +from wxpy import Bot,Message,embed + +bot = Bot() +#找到好友 +my_friend = bot.friends().search('PrincessAKing')[0] +#预先注册,利用Bot.register()完成 +#Bot.register(chats=None,msg_types=None,except_self=True,run_async=True,enable=True) +@bot.register(chats = my_friend,msg_types = 'Sharing',except_self = True) +def auto_reply(msg): + import requests + from pyquery import PyQuery +#r = requests.get('https://api.github.com/user', auth=('user', 'pass')) 官方事例 + response = requests.get(msg.url) + document = PyQuery(response.text) + content = document('#js_content').text() + from mymodule.stats_word import stats_text as a + msg1 = dict(a(content,20)) + + import numpy as np + import matplotlib.pyplot as plt + import matplotlib.font_manager as fm + import pandas as pd + from pandas import DataFrame,Series + plt.rcParams['font.sans-serif']=['SimHei'] + group_x = list(msg1.keys()) + group_y = list(msg1.values()) + df = pd.DataFrame(group_y,index = group_x) + df.plot(kind = 'barh') + + plt.savefig('day13.png') + + msg.reply_image('day13.png') +embed() \ No newline at end of file diff --git a/exercises/1901010132/day13/mymodule/stats_word.py b/exercises/1901010132/day13/mymodule/stats_word.py new file mode 100644 index 000000000..699c602ef --- /dev/null +++ b/exercises/1901010132/day13/mymodule/stats_word.py @@ -0,0 +1,50 @@ +text= ''' +"Her eyes beginning to water, she went on, +"So I would like you all to make me a promise: +from now on, on your way to school, +or on your way home, find something beautiful +to notice. It doesn' t have to be something you +see -it could be a scent perhaps of freshly +baked bread wafting out of someone 's house, +or it could be the sound of the breeze +slightly rustling the leaves in the trees, +or the way the morning light catches one +autumn leaf as it falls gently to the ground. +Please, look for these things, and remember them." +  她的眼睛开始湿润了,她接着说因此我想让你们每个人答应我: +从今以后,在你上学或者放学的路上,要发现一些美丽的事物。 +它不一定是你看到的某个东西——它可能是一种香味—— +也许是新鲜烤面包的味道从某一座房里飘出来,也许是微风轻拂树叶的声音, +或者是晨光照射在轻轻飘落的秋叶上的方式。请你们寻找这些东西并且记住它们吧。 +''' +import re +import collections +import jieba #结巴中文分词 +count=int() +def stats_text_en(text,count): + if type(text) == str: + b=re.sub(r'[^A-Za-z]',' ',text) + list1=b.split() + return(collections.Counter(list1).most_common(count)) + else: + raise ValueError('文本为非字符串') + +def stats_text_cn(text,count): + if not isinstance(text,str): + raise ValueError('文本为非字符串') + p=re.compile(u'[\u4e00-\u9fa5]') + a=re.findall(p,text) + str2=''.join(a) + seg_list = jieba.cut(str2,cut_all=False) #jieba.cut返回的结构是一个可迭代的生成器generator + newlist=[] + for i in seg_list: + if len(i) >= 2: + newlist.append(i) + return(collections.Counter(newlist).most_common(count)) + +def stats_text(text,count): + if not isinstance(text,str): + raise ValueError('文本为非字符串') + stats_text_cn(text,count) + stats_text_en(text,count) +stats_text(text,count) diff --git a/exercises/1901010132/day9/main.py b/exercises/1901010132/day9/main.py new file mode 100644 index 000000000..e2040b402 --- /dev/null +++ b/exercises/1901010132/day9/main.py @@ -0,0 +1,8 @@ +from mymodule import stats_word +import json + +with open(r'\Users\Administrator\Documents\GitHub\selfteaching-python-camp\exercises\1901010132\day9\tang300.json',encoding='UTF-8') as f: + text=f.read() + + +stats_word.stats_text_cn(text, 100) \ No newline at end of file diff --git a/exercises/1901010132/day9/mymodule/stats_word.py b/exercises/1901010132/day9/mymodule/stats_word.py new file mode 100644 index 000000000..7a3c0ab41 --- /dev/null +++ b/exercises/1901010132/day9/mymodule/stats_word.py @@ -0,0 +1,42 @@ +text= ''' +"Her eyes beginning to water, she went on, +"So I would like you all to make me a promise: +from now on, on your way to school, +or on your way home, find something beautiful +to notice. It doesn' t have to be something you +see -it could be a scent perhaps of freshly +baked bread wafting out of someone 's house, +or it could be the sound of the breeze +slightly rustling the leaves in the trees, +or the way the morning light catches one +autumn leaf as it falls gently to the ground. +Please, look for these things, and remember them." +  她的眼睛开始湿润了,她接着说因此我想让你们每个人答应我: +从今以后,在你上学或者放学的路上,要发现一些美丽的事物。 +它不一定是你看到的某个东西——它可能是一种香味—— +也许是新鲜烤面包的味道从某一座房里飘出来,也许是微风轻拂树叶的声音, +或者是晨光照射在轻轻飘落的秋叶上的方式。请你们寻找这些东西并且记住它们吧。 +''' +import re +import collections +count=int() +def stats_text_en(text,count): + if type(text)!=str: + raise ValueError("文本为非字符串") + text=re.sub('[^a-zA-Z]','',text.strip()) #表示所有非英文字母 + text=text.split() + print(collections.Counter(text).most_common(count)) + +def stats_text_cn(text,count): #定义检索中文函数 + if type(text)!=str: + raise ValueError("文本为非字符串") + text=re.sub('[^\u4e00-\u9fa5]','',text) #[^\u4e00-\u9fa5]表示所有非中文 + text=' '.join(text) + text=text.split() + print(collections.Counter(text).most_common(count)) + +def stats_word(text,count): #定义函数,实现统计汉字和英文单词出现次数 + if type(text)!=str: + raise ValueError("文本为非字符串") + stats_text_en(text,count) + stats_text_cn(text,count)