forked from mrlan/EnglishPal
				
			Speed up loading next article
The key change is replacing "d1 = load_freq_history(path_prefix + 'static/frequency/ferquency.p)" with "d1 = load_freq_history(user_word_list)" in function get_today_article() from Article.py. Now, with a user_word_list of size about 500, the next article can be loaded within 100ms. The new d1 is much smaller than the old one, therefore the following computation "d3 = get_difficulty_level_for_user(d1, d2)" is much faster. The students did not feel that loading next article is slow; this is because their frequency.p is quite small. Also log information in app/log.txtBug-561
							parent
							
								
									10c291bed2
								
							
						
					
					
						commit
						1b211f107d
					
				|  | @ -13,3 +13,4 @@ app/db/wordfreqapp.db | ||||||
| app/static/donate-the-author.jpg | app/static/donate-the-author.jpg | ||||||
| app/static/donate-the-author-hidden.jpg | app/static/donate-the-author-hidden.jpg | ||||||
| app/model/__pycache__/ | app/model/__pycache__/ | ||||||
|  | app/log.txt | ||||||
|  |  | ||||||
|  | @ -8,7 +8,7 @@ import hashlib | ||||||
| from datetime import datetime | from datetime import datetime | ||||||
| from flask import Flask, request, redirect, render_template, url_for, session, abort, flash, get_flashed_messages | from flask import Flask, request, redirect, render_template, url_for, session, abort, flash, get_flashed_messages | ||||||
| from difficulty import get_difficulty_level_for_user, text_difficulty_level, user_difficulty_level | from difficulty import get_difficulty_level_for_user, text_difficulty_level, user_difficulty_level | ||||||
| 
 | import logging | ||||||
| 
 | 
 | ||||||
| path_prefix = './' | path_prefix = './' | ||||||
| db_path_prefix = './db/'  # comment this line in deployment | db_path_prefix = './db/'  # comment this line in deployment | ||||||
|  | @ -51,14 +51,20 @@ def get_today_article(user_word_list, visited_articles): | ||||||
|     random.shuffle(result) |     random.shuffle(result) | ||||||
| 
 | 
 | ||||||
|     # Choose article according to reader's level |     # Choose article according to reader's level | ||||||
|     d1 = load_freq_history(path_prefix + 'static/frequency/frequency.p') |     logging.debug('* get_today_article(): start d1 = ... ') | ||||||
|  |     d1 = load_freq_history(user_word_list) | ||||||
|     d2 = load_freq_history(path_prefix + 'static/words_and_tests.p') |     d2 = load_freq_history(path_prefix + 'static/words_and_tests.p') | ||||||
|  |     logging.debug(' ... get_today_article(): get_difficulty_level_for_user() start') | ||||||
|     d3 = get_difficulty_level_for_user(d1, d2) |     d3 = get_difficulty_level_for_user(d1, d2) | ||||||
|  |     logging.debug(' ... get_today_article(): done') | ||||||
| 
 | 
 | ||||||
|     d = None |     d = None | ||||||
|     result_of_generate_article = "not found" |     result_of_generate_article = "not found" | ||||||
|  | 
 | ||||||
|     d_user = load_freq_history(user_word_list) |     d_user = load_freq_history(user_word_list) | ||||||
|  |     logging.debug('* get_today_article(): user_difficulty_level() start') | ||||||
|     user_level = user_difficulty_level(d_user, d3)  # more consideration as user's behaviour is dynamic. Time factor should be considered. |     user_level = user_difficulty_level(d_user, d3)  # more consideration as user's behaviour is dynamic. Time factor should be considered. | ||||||
|  |     logging.debug('* get_today_article(): done') | ||||||
|     text_level = 0 |     text_level = 0 | ||||||
|     if visited_articles["index"] > len(visited_articles["article_ids"])-1:  # 生成新的文章 |     if visited_articles["index"] > len(visited_articles["article_ids"])-1:  # 生成新的文章 | ||||||
|         amount_of_visited_articles = len(visited_articles["article_ids"]) |         amount_of_visited_articles = len(visited_articles["article_ids"]) | ||||||
|  |  | ||||||
|  | @ -1,6 +1,3 @@ | ||||||
| #! /usr/bin/python3 |  | ||||||
| # -*- coding: utf-8 -*- |  | ||||||
| 
 |  | ||||||
| ########################################################################### | ########################################################################### | ||||||
| # Copyright 2019 (C) Hui Lan <hui.lan@cantab.net> | # Copyright 2019 (C) Hui Lan <hui.lan@cantab.net> | ||||||
| # Written permission must be obtained from the author for commercial uses. | # Written permission must be obtained from the author for commercial uses. | ||||||
|  |  | ||||||
|  | @ -15,6 +15,9 @@ from wordfreqCMD import sort_in_descending_order | ||||||
| import pickle_idea | import pickle_idea | ||||||
| import pickle_idea2 | import pickle_idea2 | ||||||
| 
 | 
 | ||||||
|  | import logging | ||||||
|  | logging.basicConfig(filename='log.txt', format='%(asctime)s %(message)s', level=logging.DEBUG) | ||||||
|  | 
 | ||||||
| # 初始化蓝图 | # 初始化蓝图 | ||||||
| userService = Blueprint("user_bp", __name__) | userService = Blueprint("user_bp", __name__) | ||||||
| 
 | 
 | ||||||
|  | @ -32,7 +35,9 @@ def get_next_article(username): | ||||||
|         else:  # 当前不为“null”,直接 index+=1 |         else:  # 当前不为“null”,直接 index+=1 | ||||||
|             visited_articles["index"] += 1 |             visited_articles["index"] += 1 | ||||||
|         session["visited_articles"] = visited_articles |         session["visited_articles"] = visited_articles | ||||||
|  |         logging.debug('/get_next_article: start calling get_today_arcile()') | ||||||
|         visited_articles, today_article, result_of_generate_article = get_today_article(user_freq_record, session.get('visited_articles')) |         visited_articles, today_article, result_of_generate_article = get_today_article(user_freq_record, session.get('visited_articles')) | ||||||
|  |         logging.debug('/get_next_arcile: done.') | ||||||
|         data = { |         data = { | ||||||
|             'visited_articles': visited_articles, |             'visited_articles': visited_articles, | ||||||
|             'today_article': today_article, |             'today_article': today_article, | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue