|
|
@ -1,22 +1,75 @@
|
|
|
|
from flask import Flask, request
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
from flask import Flask, request, jsonify, abort
|
|
|
|
|
|
|
|
from functools import lru_cache
|
|
|
|
from cppy.cp_util import *
|
|
|
|
from cppy.cp_util import *
|
|
|
|
|
|
|
|
from functools import cache
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
使用Flask构建一个简单的服务端
|
|
|
|
|
|
|
|
该服务端能够接收POST请求,并返回词频统计结果
|
|
|
|
|
|
|
|
先运行该代码搭建起服务端,然后运行request文件进行词频统计
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/word_frequency', methods=['POST'])
|
|
|
|
# 模拟数据库
|
|
|
|
def word_frequency():
|
|
|
|
books_db = []
|
|
|
|
# 获取POST请求中的JSON数据
|
|
|
|
|
|
|
|
data = request.get_json()
|
|
|
|
# 用于缓存用户数据库的装饰器
|
|
|
|
data = data['file']
|
|
|
|
@lru_cache(maxsize=None)
|
|
|
|
# 统计词频
|
|
|
|
def get_books_db():
|
|
|
|
word_freq = get_frequencies(data)
|
|
|
|
return books_db
|
|
|
|
word_freq = sort_dict(word_freq)
|
|
|
|
|
|
|
|
return word_freq
|
|
|
|
#查询所有资源
|
|
|
|
|
|
|
|
@app.route('/books', methods=['GET'])
|
|
|
|
|
|
|
|
def get_books():
|
|
|
|
|
|
|
|
return jsonify(get_books_db())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#查询某个资源
|
|
|
|
|
|
|
|
@app.route('/books/<int:book_id>', methods=['GET'])
|
|
|
|
|
|
|
|
def get_book(book_id):
|
|
|
|
|
|
|
|
book = next((book for book in get_books_db() if book['id'] == book_id), None)
|
|
|
|
|
|
|
|
if book is None:
|
|
|
|
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
return jsonify(book['content'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 创建或更新新资源
|
|
|
|
|
|
|
|
@app.route('/books/<int:book_id>', methods=['PUT'])
|
|
|
|
|
|
|
|
def update_book(book_id):
|
|
|
|
|
|
|
|
global books_db
|
|
|
|
|
|
|
|
book_to_update = request.json
|
|
|
|
|
|
|
|
print(book_to_update)
|
|
|
|
|
|
|
|
books_db = get_books_db()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
book = next((book for book in books_db if book['id'] == book_id), None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if book is None:
|
|
|
|
|
|
|
|
# 如果资源不存在,创建新资源
|
|
|
|
|
|
|
|
books_db.append(book_to_update)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
# 如果资源存在,更新资源
|
|
|
|
|
|
|
|
book.update(book_to_update)
|
|
|
|
|
|
|
|
# 清除缓存的数据库
|
|
|
|
|
|
|
|
cache.delete(get_books_db)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return jsonify(books_db), 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#操作一个资源
|
|
|
|
|
|
|
|
@app.route('/books/<int:book_id>/word_frequency', methods=['GET'])
|
|
|
|
|
|
|
|
def word_frequency(book_id):
|
|
|
|
|
|
|
|
global books_db
|
|
|
|
|
|
|
|
book = next((book for book in get_books_db() if book['id'] == book_id), None)
|
|
|
|
|
|
|
|
filepath = book['content']
|
|
|
|
|
|
|
|
word_list = extract_file_words(filepath)
|
|
|
|
|
|
|
|
word_frequency = get_frequencies(word_list)
|
|
|
|
|
|
|
|
word_frequency = sort_dict(word_frequency)
|
|
|
|
|
|
|
|
print_word_freqs(word_frequency)
|
|
|
|
|
|
|
|
return jsonify(word_frequency), 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/books/<int:book_id>', methods=['DELETE'])
|
|
|
|
|
|
|
|
def delete_book(book_id):
|
|
|
|
|
|
|
|
global books_db
|
|
|
|
|
|
|
|
books_db = [book for book in books_db if book['id'] != book_id]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if len(books_db) == len([l for l in books_db if l['id'] == book_id]):
|
|
|
|
|
|
|
|
abort(404) # 用户不存在
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return jsonify({'message': f'book {book_id} deleted'}), 200
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
if __name__ == '__main__':
|
|
|
|
app.run(debug=True)
|
|
|
|
app.run(debug=True)
|
|
|
|