From 9b5c02a9d9d9a151658e02d585dca609be12e168 Mon Sep 17 00:00:00 2001 From: PassionPit <53104220+PassionPit@users.noreply.github.com> Date: Fri, 30 Aug 2019 14:56:26 +0800 Subject: [PATCH 1/2] Create stats_word.py --- .../1901100139/d12/mymodule/stats_word.py | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 exercises/1901100139/d12/mymodule/stats_word.py diff --git a/exercises/1901100139/d12/mymodule/stats_word.py b/exercises/1901100139/d12/mymodule/stats_word.py new file mode 100644 index 000000000..a1b269578 --- /dev/null +++ b/exercises/1901100139/d12/mymodule/stats_word.py @@ -0,0 +1,22 @@ +from collections import Counter +def stats_text_en(text,count): + elements = text.split() + words = [] + symbols = ',.*-!' + for element in elements: + for symbol in symbols: + element = element.replace(symbol,'') + if len(element) and element.isascii(): + words.append(element) + return Counter(words).most_common(count) +def stats_text_cn(text,count): + cn_characters = [] + for character in text: + if '\u4e00' <= character <= '\u9fff': + cn_characters.append(character) + return Counter(cn_characters).most_common(count) +def stats_text(text,count): + ''' + 合并中英词频的结果 + ''' + return stats_text_en(text,count) + stats_text_cn(text,count) \ No newline at end of file From e5fd7c798e691f6589975c40820850316b9787ab Mon Sep 17 00:00:00 2001 From: PassionPit <53104220+PassionPit@users.noreply.github.com> Date: Fri, 30 Aug 2019 14:56:29 +0800 Subject: [PATCH 2/2] Create main.py --- exercises/1901100139/d12/main.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 exercises/1901100139/d12/main.py diff --git a/exercises/1901100139/d12/main.py b/exercises/1901100139/d12/main.py new file mode 100644 index 000000000..9e58a1533 --- /dev/null +++ b/exercises/1901100139/d12/main.py @@ -0,0 +1,32 @@ + +import logging +import requests +import pyquery +from mymodule import stats_word +from wxpy import * + +logging.basicConfig(format='file:%(filename)s|line:%(lineno)d|message:%(message)s',level=logging.DEBUG) + +def get_article(url): + r=requests.get(url) + document= pyquery.PyQuery(r.text) + return document('#js_content').text() + +def main(): + bot=Bot() + friends=bot.friends() + + @bot.register(friends,SHARING) + def handler(msg): + try: + logging.info('sharing url=%s',msg.url) + article=get_article(msg.url) + result=stats_word.stats_text_cn(article,100) + msg.reply(str(result)) + except Exception as e: + logging.exception(e) + embed + + +if __name__=='__main__': + main() \ No newline at end of file