Fix bug 533
							parent
							
								
									e8fbccdcf7
								
							
						
					
					
						commit
						c499eaeff9
					
				|  | @ -1,4 +1,5 @@ | |||
| from WordFreq import WordFreq | ||||
| from app.UseSqlite import RecordQuery | ||||
| from wordfreqCMD import youdao_link, sort_in_descending_order | ||||
| import pickle_idea, pickle_idea2 | ||||
| import os | ||||
|  | @ -14,6 +15,14 @@ path_prefix = './' | |||
| db_path_prefix = './db/'  # comment this line in deployment | ||||
| 
 | ||||
| 
 | ||||
| def load_text_list_from_db(db_file): | ||||
|     rq = RecordQuery(db_file) | ||||
|     rq.instructions("SELECT text FROM article") | ||||
|     rq.do() | ||||
|     result = rq.get_results() | ||||
|     text_list = [row['text'] for row in result if 'text' in row] | ||||
|     return text_list | ||||
| 
 | ||||
| def total_number_of_essays(): | ||||
|     return get_number_of_articles() | ||||
| 
 | ||||
|  |  | |||
|  | @ -0,0 +1,86 @@ | |||
| ########################################################################### | ||||
| # Copyright 2019 (C) Hui Lan <hui.lan@cantab.net> | ||||
| # Written permission must be obtained from the author for commercial uses. | ||||
| ########################################################################### | ||||
| 
 | ||||
| 
 | ||||
| # Reference: Dusty Phillips.  Python 3 Objected-oriented Programming Second Edition. Pages 326-328. | ||||
| # Copyright (C) 2019 Hui Lan | ||||
| 
 | ||||
| import sqlite3 | ||||
| 
 | ||||
| class Sqlite3Template: | ||||
|     def __init__(self, db_fname): | ||||
|         self.db_fname = db_fname | ||||
| 
 | ||||
|     def connect(self, db_fname): | ||||
|         self.conn = sqlite3.connect(self.db_fname) | ||||
| 
 | ||||
|     def instructions(self, query_statement): | ||||
|         raise NotImplementedError() | ||||
| 
 | ||||
|     def operate(self): | ||||
|         self.conn.row_factory = sqlite3.Row | ||||
|         self.results = self.conn.execute(self.query) # self.query is to be given in the child classes | ||||
|         self.conn.commit() | ||||
| 
 | ||||
|     def format_results(self): | ||||
|         raise NotImplementedError() | ||||
| 
 | ||||
|     def do(self): | ||||
|         self.connect(self.db_fname) | ||||
|         self.instructions(self.query) | ||||
|         self.operate() | ||||
| 
 | ||||
|     def instructions_with_parameters(self, query_statement, parameters): | ||||
|         self.query = query_statement | ||||
|         self.parameters = parameters | ||||
| 
 | ||||
|     def do_with_parameters(self): | ||||
|         self.connect(self.db_fname) | ||||
|         self.instructions_with_parameters(self.query, self.parameters) | ||||
|         self.operate_with_parameters() | ||||
| 
 | ||||
|     def operate_with_parameters(self): | ||||
|         self.conn.row_factory = sqlite3.Row | ||||
|         self.results = self.conn.execute(self.query, self.parameters) # self.query is to be given in the child classes | ||||
|         self.conn.commit() | ||||
| 
 | ||||
| 
 | ||||
| class InsertQuery(Sqlite3Template): | ||||
|     def instructions(self, query): | ||||
|         self.query = query | ||||
| 
 | ||||
| 
 | ||||
| class RecordQuery(Sqlite3Template): | ||||
|     def instructions(self, query): | ||||
|         self.query = query | ||||
| 
 | ||||
|     def format_results(self): | ||||
|         output = [] | ||||
|         for row_dict in self.results.fetchall(): | ||||
|             lst = [] | ||||
|             for k in dict(row_dict): | ||||
|                 lst.append( row_dict[k] ) | ||||
|             output.append(', '.join(lst)) | ||||
|         return '\n\n'.join(output) | ||||
| 
 | ||||
|     def get_results(self): | ||||
|         result = [] | ||||
|         for row_dict in self.results.fetchall(): | ||||
|             result.append( dict(row_dict) ) | ||||
|         return result | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| if __name__ == '__main__': | ||||
| 
 | ||||
|     #iq = InsertQuery('RiskDB.db') | ||||
|     #iq.instructions("INSERT INTO inspection Values ('FoodSupplies', 'RI2019051301', '2019-05-13', '{}')") | ||||
|     #iq.do() | ||||
|     #iq.instructions("INSERT INTO inspection Values ('CarSupplies', 'RI2019051302', '2019-05-13', '{[{\"risk_name\":\"elevator\"}]}')") | ||||
|     #iq.do() | ||||
|     rq = RecordQuery('wordfreqapp.db') | ||||
|     rq.instructions("SELECT * FROM article WHERE level=3") | ||||
|     rq.do() | ||||
|     #print(rq.format_results()) | ||||
							
								
								
									
										38
									
								
								app/main.py
								
								
								
								
							
							
						
						
									
										38
									
								
								app/main.py
								
								
								
								
							|  | @ -1,19 +1,23 @@ | |||
| #! /usr/bin/python3 | ||||
| # -*- coding: utf-8 -*- | ||||
| import csv | ||||
| 
 | ||||
| ########################################################################### | ||||
| # Copyright 2019 (C) Hui Lan <hui.lan@cantab.net> | ||||
| # Written permission must be obtained from the author for commercial uses. | ||||
| ########################################################################### | ||||
| from flask import abort | ||||
| from markupsafe import escape | ||||
| from flask import escape | ||||
| from Login import * | ||||
| from Article import * | ||||
| import Yaml | ||||
| from app.wordfreqCMD import sort_in_descending_order | ||||
| from user_service import userService | ||||
| from account_service import accountService | ||||
| from admin_service import adminService, ADMIN_NAME | ||||
| import os | ||||
| 
 | ||||
| 
 | ||||
| app = Flask(__name__) | ||||
| app.secret_key = os.urandom(32) | ||||
| app.secret_key = 'lunch.time!' | ||||
| 
 | ||||
| # 将蓝图注册到Lab app | ||||
| app.register_blueprint(userService) | ||||
|  | @ -23,6 +27,17 @@ app.register_blueprint(adminService) | |||
| path_prefix = '/var/www/wordfreq/wordfreq/' | ||||
| path_prefix = './'  # comment this line in deployment | ||||
| 
 | ||||
| 
 | ||||
| def load_word_list(csv_file): | ||||
|     with open(csv_file, 'r') as f: | ||||
|         reader = csv.reader(f) | ||||
|         return set(word.strip().lower() for row in reader for word in row[0].split(',')) | ||||
| 
 | ||||
| def calculate_coverage(text_list, word_set): | ||||
|     total_words = sum(len(article.split()) for article in text_list) | ||||
|     covered_words = sum(len(set(article.split()).intersection(word_set)) for article in text_list) | ||||
|     return (covered_words / total_words) * 10000 if total_words else 0 | ||||
| 
 | ||||
| def get_random_image(path): | ||||
|     ''' | ||||
|     返回随机图 | ||||
|  | @ -54,6 +69,7 @@ def appears_in_test(word, d): | |||
|     else: | ||||
|         return ','.join(d[word]) | ||||
| 
 | ||||
| 
 | ||||
| @app.route("/mark", methods=['GET', 'POST']) | ||||
| def mark_word(): | ||||
|     ''' | ||||
|  | @ -79,6 +95,14 @@ def mainpage(): | |||
|     根据GET或POST方法来返回不同的主界面 | ||||
|     :return: 主界面 | ||||
|     ''' | ||||
| 
 | ||||
|     db_file = 'static/wordfreqapp.db' | ||||
|     csv_file = 'static/The_Oxford.csv' | ||||
|     text_list = load_text_list_from_db(db_file) | ||||
|     word_set = load_word_list(csv_file) | ||||
|     coverage_percentage = "{:.2f}".format(calculate_coverage(text_list, word_set)) | ||||
| 
 | ||||
| 
 | ||||
|     if request.method == 'POST':  # when we submit a form | ||||
|         content = escape(request.form['content']) | ||||
|         f = WordFreq(content) | ||||
|  | @ -88,7 +112,7 @@ def mainpage(): | |||
|         lst_history = pickle_idea.dict2lst(d) | ||||
|         d = pickle_idea.merge_frequency(lst, lst_history) | ||||
|         pickle_idea.save_frequency_to_pickle(d, path_prefix + 'static/frequency/frequency.p') | ||||
|         return render_template('mainpage_post.html', lst=lst, yml=Yaml.yml) | ||||
|         return render_template('mainpage_post.html', lst=lst, yml=Yaml.yml,coverage_percentage=coverage_percentage) | ||||
| 
 | ||||
|     elif request.method == 'GET':  # when we load a html page | ||||
|         random_ads = get_random_ads() | ||||
|  | @ -102,8 +126,8 @@ def mainpage(): | |||
|                                d_len=d_len, | ||||
|                                lst=lst, | ||||
|                                yml=Yaml.yml, | ||||
|                                number_of_essays=number_of_essays) | ||||
| 
 | ||||
|                                number_of_essays=number_of_essays, | ||||
|                                coverage_percentage=coverage_percentage) | ||||
| 
 | ||||
| if __name__ == '__main__': | ||||
|     ''' | ||||
|  |  | |||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
										
											Binary file not shown.
										
									
								
							|  | @ -3,7 +3,6 @@ | |||
| <head> | ||||
|     <meta charset="utf-8"> | ||||
|     <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=0.5, maximum-scale=3.0, user-scalable=yes" /> | ||||
| 
 | ||||
|     {{ yml['header'] | safe }} | ||||
|     {% if yml['css']['item'] %} | ||||
|         {% for css in yml['css']['item'] %} | ||||
|  | @ -31,8 +30,9 @@ | |||
|             <p><a href="/login">登录</a>  <a href="/signup">注册</a> <a href="/static/usr/instructions.html">使用说明</a></p > | ||||
|             <p><b> {{ random_ads }}。 <a href="/signup">试试</a>吧!</b></p> | ||||
|         {% endif %} | ||||
|         <div class="alert alert-success" role="alert">共有文章 <span class="badge bg-success"> {{ number_of_essays }} </span> 篇</div> | ||||
|         <p>粘贴1篇文章 (English only)</p> | ||||
| 
 | ||||
|         <div class="alert alert-success" role="alert">共有文章 <span class="badge bg-success"> {{ number_of_essays }} </span> 篇     其中Oxford覆盖率为: <span class="badge bg-success"> {{ coverage_percentage }}% </span></div> | ||||
|         <p>粘贴1篇文章 (<E></E>nglish only)</p> | ||||
|         <form method="post" action="/"> | ||||
|             <textarea name="content" rows="10" cols="120"></textarea><br/> | ||||
|             <input type="submit" value="get文章中的词频"/> | ||||
|  | @ -44,7 +44,6 @@ | |||
|                 <a href="http://youdao.com/w/eng/{{x[0]}}/#keyfrom=dict2.index">{{x[0]}}</a> {{x[1]}} | ||||
|             {% endfor %} | ||||
|         {% endif %} | ||||
|         <p class="text-muted">Version: 20230810</p> | ||||
|         <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script> | ||||
|     </div> | ||||
|     {{ yml['footer'] | safe }} | ||||
|  |  | |||
|  | @ -0,0 +1,23 @@ | |||
| 
 | ||||
| from app.Article import load_text_list_from_db | ||||
| from app.main import load_word_list, calculate_coverage | ||||
| 
 | ||||
| 
 | ||||
| def test_coverage_percentage(): | ||||
|     try: | ||||
|         db_file = 'static/wordfreqapp.db' | ||||
|         csv_file = 'static/The_Oxford.csv' | ||||
|         text_list = load_text_list_from_db(db_file) | ||||
|         word_set = load_word_list(csv_file) | ||||
|         coverage_percentage = "{:.2f}".format(calculate_coverage(text_list, word_set)) | ||||
|         print("coverage_percentage:", coverage_percentage); | ||||
|     except Exception as e: | ||||
|         print(e) | ||||
| 
 | ||||
| if __name__ == '__main__': | ||||
|     test_coverage_percentage(); | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
|  | @ -47,6 +47,8 @@ def get_next_article(username): | |||
|         return 'Under construction' | ||||
|     return json.dumps(data) | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| @userService.route("/get_pre_article/<username>",methods=['GET']) | ||||
| def get_pre_article(username): | ||||
|     user_freq_record = path_prefix + 'static/frequency/' + 'frequency_%s.pickle' % (username) | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue