From 726a8795c795ed53c86b6c63c29d998ea1c189aa Mon Sep 17 00:00:00 2001 From: pbr4nzfkh <18879212807@163.com> Date: Sat, 16 Mar 2024 22:11:44 +0800 Subject: [PATCH 1/6] =?UTF-8?q?Delete=20'=E5=9F=BA=E6=9C=AC=E7=BB=93?= =?UTF-8?q?=E6=9E=84/042=20restful/tf-34.py'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 基本结构/042 restful/tf-34.py | 107 ------------------------------ 1 file changed, 107 deletions(-) delete mode 100644 基本结构/042 restful/tf-34.py diff --git a/基本结构/042 restful/tf-34.py b/基本结构/042 restful/tf-34.py deleted file mode 100644 index ef904e3..0000000 --- a/基本结构/042 restful/tf-34.py +++ /dev/null @@ -1,107 +0,0 @@ -import re, sys -from cppy.cp_util import * - -stops = get_stopwords() -data = {} - -# 路径问题,测试输入 test.txt ,演示当前目录下这个文件的处理 -################################################################################# -# 服务端 -def error_state(): - return "Something wrong", ["get", "default", None] - -def quit_handler(args): - sys.exit(" ... ") - -def upload_get_handler(args): - return "Name of file to upload?", ["post", "file"] - -def default_get_handler(args): - rep = "What would you like to do?" - rep += "\n1 - Quit" + "\n2 - Upload file" - links = {"1" : ["post", "execution", None], - "2" : ["get", "file_form", None]} - return rep, links - -def upload_post_handler(args): - def create_data(fn): - if fn in data: return - word_freqs = {} - with open(fn) as f: - for w in [x.lower() for x in re.split("[^a-zA-Z]+", f.read()) if len(x) > 0 and x.lower() not in stops]: - word_freqs[w] = word_freqs.get(w, 0) + 1 - wf = list(word_freqs.items()) - data[fn] = sorted(wf,key=lambda x: x[1],reverse=True) - - if args == None: return error_state() - filename = args[0] - try: - script_dir = os.path.dirname(os.path.abspath(__file__)) - filename = os.path.join(script_dir, filename) - create_data(filename) - except: - print("Unexpected error: %s" % sys.exc_info()[0]) - return error_state() - return word_get_handler([filename, 0]) - -def word_get_handler(args): - def get_word(filename, word_index): - if word_index < len(data[filename]): - return data[filename][word_index] - else: - return ("no more words", 0) - - filename = args[0]; word_index = args[1] - word_info = get_word(filename, word_index) - rep = '\n#{0}: {1} - {2}'.format(word_index+1, word_info[0], word_info[1]) - rep += "\n\nWhat would you like to do next?" - rep += "\n1 - Quit" + "\n2 - Upload file" - rep += "\n3 - See next most-frequently occurring word" - links = {"1" : ["post", "execution", None], - "2" : ["get", "file_form", None], - "3" : ["get", "word", [filename, word_index+1]]} - return rep, links - -# Handler registration -handlers = {"post_execution" : quit_handler, - "get_default" : default_get_handler, - "get_file_form" : upload_get_handler, - "post_file" : upload_post_handler, - "get_word" : word_get_handler } - -# The "server" core -def handle_request(verb, uri, args): - def handler_key(verb, uri): - return verb + "_" + uri - if handler_key(verb, uri) in handlers: - return handlers[handler_key(verb, uri)](args) - else: - return handlers[handler_key("get", "default")](args) - -################################################################################# -# 仿真简单的浏览器客户端动作 -def render_and_get_input(state_representation, links): - print(state_representation) - sys.stdout.flush() - if type(links) is dict: - input = sys.stdin.readline().strip() - if input in links: - return links[input] - else: - return ["get", "default", None] - elif type(links) is list: - if links[0] == "post": # get "form" data - input = sys.stdin.readline().strip() - links.append([input]) # add the data at the end - return links - else: # get action, don't get user input - return links - else: - return ["get", "default", None] - - -if __name__ == "__main__": - request = ["get", "default", None] - while True: - state_representation, links = handle_request(*request) # "server" - request = render_and_get_input(state_representation, links) # "client" \ No newline at end of file From f8055c00447acb6a40b6055952ade0e3a174a448 Mon Sep 17 00:00:00 2001 From: pbr4nzfkh <18879212807@163.com> Date: Sat, 16 Mar 2024 22:15:43 +0800 Subject: [PATCH 2/6] restful-request --- 基本结构/042 restful/tf-35-request.py | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 基本结构/042 restful/tf-35-request.py diff --git a/基本结构/042 restful/tf-35-request.py b/基本结构/042 restful/tf-35-request.py new file mode 100644 index 0000000..235a88e --- /dev/null +++ b/基本结构/042 restful/tf-35-request.py @@ -0,0 +1,10 @@ +import requests +from cppy.cp_util import * + +url = 'http://127.0.0.1:5000/word_frequency' +data_list = extract_file_words(testfilepath) +data = { + 'file': data_list +} +response = requests.post(url, json=data) +print(response.json()[:10]) From 365c8bb76a5054b5aa3374dacf630c723e1acbfc Mon Sep 17 00:00:00 2001 From: pbr4nzfkh <18879212807@163.com> Date: Sat, 16 Mar 2024 22:15:59 +0800 Subject: [PATCH 3/6] =?UTF-8?q?Delete=20'=E5=9F=BA=E6=9C=AC=E7=BB=93?= =?UTF-8?q?=E6=9E=84/042=20restful/test.txt'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 基本结构/042 restful/test.txt | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 基本结构/042 restful/test.txt diff --git a/基本结构/042 restful/test.txt b/基本结构/042 restful/test.txt deleted file mode 100644 index 088a3d7..0000000 --- a/基本结构/042 restful/test.txt +++ /dev/null @@ -1,2 +0,0 @@ -" my Some sure acquaintance or other, my dear, sure,other I suppose; I am sure I do not -know. sure " From d54c43b45968105ba8ec5d1999ed86ea0898c965 Mon Sep 17 00:00:00 2001 From: pbr4nzfkh <18879212807@163.com> Date: Sat, 16 Mar 2024 22:17:03 +0800 Subject: [PATCH 4/6] restful-app --- 基本结构/042 restful/tf-35-app.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 基本结构/042 restful/tf-35-app.py diff --git a/基本结构/042 restful/tf-35-app.py b/基本结构/042 restful/tf-35-app.py new file mode 100644 index 0000000..b86da4f --- /dev/null +++ b/基本结构/042 restful/tf-35-app.py @@ -0,0 +1,18 @@ +from flask import Flask, request +from cppy.cp_util import * + +app = Flask(__name__) + +@app.route('/word_frequency', methods=['POST']) +def word_frequency(): + # 获取POST请求中的JSON数据 + data = request.get_json() + data = data['file'] + # 统计词频 + word_freq = get_frequencies(data) + word_freq = sort_dict(word_freq) + return word_freq + +if __name__ == '__main__': + app.run(debug=True) + From 4134d794ab503602b2924b8cdb3c6c8d781d118f Mon Sep 17 00:00:00 2001 From: pbr4nzfkh <18879212807@163.com> Date: Sat, 16 Mar 2024 22:45:00 +0800 Subject: [PATCH 5/6] =?UTF-8?q?Delete=20'=E5=9F=BA=E6=9C=AC=E7=BB=93?= =?UTF-8?q?=E6=9E=84/042=20restful/tf-35-app.py'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 基本结构/042 restful/tf-35-app.py | 18 ------------------ 1 file changed, 18 deletions(-) delete mode 100644 基本结构/042 restful/tf-35-app.py diff --git a/基本结构/042 restful/tf-35-app.py b/基本结构/042 restful/tf-35-app.py deleted file mode 100644 index b86da4f..0000000 --- a/基本结构/042 restful/tf-35-app.py +++ /dev/null @@ -1,18 +0,0 @@ -from flask import Flask, request -from cppy.cp_util import * - -app = Flask(__name__) - -@app.route('/word_frequency', methods=['POST']) -def word_frequency(): - # 获取POST请求中的JSON数据 - data = request.get_json() - data = data['file'] - # 统计词频 - word_freq = get_frequencies(data) - word_freq = sort_dict(word_freq) - return word_freq - -if __name__ == '__main__': - app.run(debug=True) - From 29dbff26ccd2cb29f818104a39db4f9c2c904be8 Mon Sep 17 00:00:00 2001 From: pbr4nzfkh <18879212807@163.com> Date: Sat, 16 Mar 2024 22:45:48 +0800 Subject: [PATCH 6/6] restful_app --- 基本结构/042 restful/tf-35-app.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 基本结构/042 restful/tf-35-app.py diff --git a/基本结构/042 restful/tf-35-app.py b/基本结构/042 restful/tf-35-app.py new file mode 100644 index 0000000..848cbd6 --- /dev/null +++ b/基本结构/042 restful/tf-35-app.py @@ -0,0 +1,23 @@ +from flask import Flask, request +from cppy.cp_util import * + +''' +使用Flask构建一个简单的服务端 +该服务端能够接收POST请求,并返回词频统计结果 +先运行该代码搭建起服务端,然后运行request文件进行词频统计 +''' +app = Flask(__name__) + +@app.route('/word_frequency', methods=['POST']) +def word_frequency(): + # 获取POST请求中的JSON数据 + data = request.get_json() + data = data['file'] + # 统计词频 + word_freq = get_frequencies(data) + word_freq = sort_dict(word_freq) + return word_freq + +if __name__ == '__main__': + app.run(debug=True) +