""" This module provides functions about article """ from WordFreq import WordFreq from wordfreqCMD import youdao_link, sort_in_descending_order from UseSqlite import InsertQuery, RecordQuery import pickle_idea, pickle_idea2 import os import random, glob import hashlib from datetime import datetime from flask import Flask, request, redirect, render_template, url_for, session, abort, flash, get_flashed_messages from difficulty import get_difficulty_level, text_difficulty_level, user_difficulty_level path_prefix = '/var/www/wordfreq/wordfreq/' path_prefix = './' # comment this line in deployment def total_number_of_essays(): rq = RecordQuery(path_prefix + 'static/wordfreqapp.db') rq.instructions("SELECT * FROM article") rq.do() result = rq.get_results() return len(result) def get_article_title(s): return s.split('\n')[0] def get_article_body(s): lst = s.split('\n') lst.pop(0) # remove the first line return '\n'.join(lst) def get_today_article(user_word_list, articleID): rq = RecordQuery(path_prefix + 'static/wordfreqapp.db') if articleID == None: rq.instructions("SELECT * FROM article") else: rq.instructions('SELECT * FROM article WHERE article_id=%d' % (articleID)) rq.do() result = rq.get_results() random.shuffle(result) # Choose article according to reader's level d1 = load_freq_history(path_prefix + 'static/frequency/frequency.p') d2 = load_freq_history(path_prefix + 'static/words_and_tests.p') d3 = get_difficulty_level(d1, d2) d = {} d_user = load_freq_history(user_word_list) user_level = user_difficulty_level(d_user, d3) # more consideration as user's behaviour is dynamic. Time factor should be considered. random.shuffle(result) # shuffle list d = random.choice(result) text_level = text_difficulty_level(d['text'], d3) if articleID == None: for reading in result: text_level = text_difficulty_level(reading['text'], d3) factor = random.gauss(0.8, 0.1) # a number drawn from Gaussian distribution with a mean of 0.8 and a stand deviation of 1 if within_range(text_level, user_level, (8.0 - user_level) * factor): d = reading break s = '
Article added on: %s
' % (d['date']) s += '%s
' % (article_title) s += '%s
' % (article_body) s += '%s
' % (d['source']) s += '%s
' % (get_question_part(d['question'])) s = s.replace('\n', '