Exemplo n.º 1
0
def load_freq_history(path):
    d = {}
    if os.path.exists(path):
        d = pickle_idea.load_record(path)
    return d
Exemplo n.º 2
0
    if num == 1:  # 从键盘读入字符串
        s = input()
    elif num == 2:  # 从文件读入字符串
        fname = sys.argv[1]
        s = file2str(fname)
    else:
        print('I can accept at most 2 arguments.')
        sys.exit()  # 结束程序运行, 下面的代码不会被执行了。

    s = remove_punctuation(s)  # 这里是s是实参(argument),里面有值
    L = freq(s)
    for x in sort_in_descending_order(L):
        print('%s\t%d\t%s' % (x[0], x[1], youdao_link(x[0])))  #函数导出

    # 把频率的结果放result.html中
    make_html_page(sort_in_descending_order(L), 'result.html')

    print('\nHistory:\n')
    if os.path.exists('frequency.p'):
        d = pickle_idea.load_record('frequency.p')
    else:
        d = {}

    print(sort_in_descending_order(pickle_idea.dict2lst(d)))

    # 合并频率
    lst_history = pickle_idea.dict2lst(d)
    d = pickle_idea.merge_frequency(L, lst_history)
    pickle_idea.save_frequency_to_pickle(d, 'frequency.p')
Exemplo n.º 3
0
def userpage(username):

    if not session.get('logged_in'):
        return '<p>请先<a href="/login">登录</a>。</p>'

    user_expiry_date = session.get('expiry_date')
    if datetime.now().strftime('%Y%m%d') > user_expiry_date:
        return '<p>账号 %s 过期。</p><p>为了提高服务质量,English Pal 收取会员费用, 每天0元。</p> <p>请决定你要试用的时间长度,扫描下面支付宝二维码支付。 支付时请注明<i>English Pal Membership Fee</i>。 我们会于12小时内激活账号。</p><p><img src="static/donate-the-author-hidden.jpg" width="120px" alt="支付宝二维码" /></p><p>如果有问题,请加开发者微信 torontohui。</p> <p><a href="/logout">登出</a></p>' % (
            username)

    username = session.get('username')

    user_freq_record = path_prefix + 'static/frequency/' + 'frequency_%s.pickle' % (
        username)

    if request.method == 'POST':  # when we submit a form
        content = request.form['content']
        f = WordFreq(content)
        lst = f.get_freq()
        page = '<meta charset="UTF8">'
        page += '<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=0.5, maximum-scale=3.0, user-scalable=yes" />'
        page += '<p>勾选不认识的单词</p>'
        page += '<form method="post" action="/%s/mark">\n' % (username)
        page += ' <input type="submit" name="add-btn" value="加入我的生词簿"/>\n'
        count = 1
        words_tests_dict = pickle_idea.load_record(path_prefix +
                                                   'static/words_and_tests.p')
        for x in lst:
            page += '<p><font color="grey">%d</font>: <a href="%s" title="%s">%s</a> (%d)  <input type="checkbox" name="marked" value="%s"></p>\n' % (
                count, youdao_link(x[0]),
                appears_in_test(x[0], words_tests_dict), x[0], x[1], x[0])
            count += 1
        page += '</form>\n'
        return page

    elif request.method == 'GET':  # when we load a html page
        page = '<meta charset="UTF8">\n'
        page += '<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=0.5, maximum-scale=3.0, user-scalable=yes" />\n'
        page += '<meta name="format-detection" content="telephone=no" />\n'  # forbid treating numbers as cell numbers in smart phones
        page += '<title>EnglishPal Study Room for %s</title>' % (username)
        page += '<p><b>English Pal for <font color="red">%s</font></b> <a href="/logout">登出</a></p>' % (
            username)
        page += '<p><a href="/%s/reset">下一篇</a></p>' % (username)
        page += '<p><b>阅读文章并回答问题</b></p>\n'
        page += '<div id="text-content">%s</div>' % (get_today_article(
            user_freq_record, session['articleID']))
        page += '<p><b>收集生词吧</b> (可以在正文中划词,也可以复制黏贴)</p>'
        page += '<form method="post" action="/%s">' % (username)
        page += ' <textarea name="content" id="selected-words" rows="10" cols="120"></textarea><br/>'
        page += ' <input type="submit" value="get 所有词的频率"/>'
        page += ' <input type="reset" value="清除"/>'
        page += '</form>\n'
        page += ''' 
                 <script>
                   function getWord(){ 
                       var word = window.getSelection?window.getSelection():document.selection.createRange().text;
                       return word;
                   }
                   function fillinWord(){
                       var element = document.getElementById("selected-words");
                       element.value = element.value + " " + getWord();
                   }
                   document.getElementById("text-content").addEventListener("click", fillinWord, false);
                   document.getElementById("text-content").addEventListener("touchstart", fillinWord, false);
                 </script>
                 '''

        d = load_freq_history(user_freq_record)
        if len(d) > 0:
            page += '<p><b>我的生词簿</b></p>'
            lst = pickle_idea2.dict2lst(d)
            lst2 = []
            for t in lst:
                lst2.append((t[0], len(t[1])))
            for x in sort_in_descending_order(lst2):
                word = x[0]
                freq = x[1]
                if isinstance(d[word], list):  # d[word] is a list of dates
                    if freq > 1:
                        page += '<p class="new-word"> <a href="%s">%s</a>                     (<a title="%s">%d</a>) </p>\n' % (
                            youdao_link(word), word, '; '.join(d[word]), freq)
                    else:
                        page += '<p class="new-word"> <a href="%s">%s</a> <font color="white">(<a title="%s">%d</a>)</font> </p>\n' % (
                            youdao_link(word), word, '; '.join(d[word]), freq)
                elif isinstance(
                        d[word], int
                ):  # d[word] is a frequency. to migrate from old format.
                    page += '<a href="%s">%s</a>%d\n' % (youdao_link(word),
                                                         word, freq)

        return page