Beispiel #1
0
def mainpage():
    if request.method == 'POST':  # when we submit a form
        content = request.form['content']
        f = WordFreq(content)
        lst = f.get_freq()
        page = '<form method="post" action="/mark">\n'
        count = 1
        for x in lst:
            page += '<p><font color="grey">%d</font>: <a href="%s">%s</a> (%d)  <input type="checkbox" name="marked" value="%s"></p>\n' % (
                count, youdao_link(x[0]), x[0], x[1], x[0])
            count += 1
        page += ' <input type="submit" value="确定并返回"/>\n'
        page += '</form>\n'
        # save history
        d = load_freq_history(path_prefix + 'static/frequency/frequency.p')
        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 page
    elif request.method == 'GET':  # when we load a html page
        page = '''
             <html lang="zh">
               <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" />
                 <title>EnglishPal 英文单词高效记</title>

               </head>
               <body>
        '''
        page += '<p><b><font size="+3" color="red">English Pal - Learn English in a smart way!</font></b></p>'
        if session.get('logged_in'):
            page += ' <a href="%s">%s</a></p>\n' % (session['username'],
                                                    session['username'])
        else:
            page += '<p><a href="/login">登录</a>  <a href="/signup">成为会员</a> <a href="/static/usr/instructions.html">使用说明</a></p>\n'
        #page += '<p><img src="%s" width="400px" alt="advertisement"/></p>' % (get_random_image(path_prefix + 'static/img/'))
        page += '<p><b>%s</b></p>' % (get_random_ads())
        page += '<p>粘帖1篇文章 (English only)</p>'
        page += '<form method="post" action="/">'
        page += ' <textarea name="content" rows="10" cols="120"></textarea><br/>'
        page += ' <input type="submit" value="get文章中的词频"/>'
        page += ' <input type="reset" value="清除"/>'
        page += '</form>'
        d = load_freq_history(path_prefix + 'static/frequency/frequency.p')
        if len(d) > 0:
            page += '<p><b>最常见的词</b></p>'
            for x in sort_in_descending_order(pickle_idea.dict2lst(d)):
                if x[1] <= 99:
                    break
                page += '<a href="%s">%s</a> %d\n' % (youdao_link(
                    x[0]), x[0], x[1])

        page += '</body></html>'
        return page
Beispiel #2
0
def mark_word():
    if request.method == 'POST':
        d = load_freq_history(path_prefix + 'static/frequency.p')
        lst_history = pickle_idea.dict2lst(d)
        lst = []
        for word in request.form.getlist('marked'):
            lst.append((word, 1))
        d = pickle_idea.merge_frequency(lst, lst_history)
        pickle_idea.save_frequency_to_pickle(
            d, path_prefix + 'static/frequency.p')
        return redirect(url_for('mainpage'))
    else:
        return 'Under construction'
Beispiel #3
0
def user_mark_word(username):
    username = session[username]
    user_freq_record = path_prefix + 'static/' + 'frequency_%s.pickle' % (
        username)
    if request.method == 'POST':
        d = load_freq_history(user_freq_record)
        lst_history = pickle_idea.dict2lst(d)
        lst = []
        for word in request.form.getlist('marked'):
            lst.append((word, 1))
        d = pickle_idea.merge_frequency(lst, lst_history)
        pickle_idea.save_frequency_to_pickle(d, user_freq_record)
        return redirect(url_for('userpage', username=username))
    else:
        return 'Under construction'
Beispiel #4
0
def mainpage():
    if request.method == 'POST':  # when we submit a form
        content = request.form['content']
        f = WordFreq(content)
        lst = f.get_freq()
        page = '<form method="post" action="/mark">\n'
        count = 1
        for x in lst:
            page += '<p><font color="grey">%d</font>: <a href="%s">%s</a> (%d)  <input type="checkbox" name="marked" value="%s"></p>\n' % (
                count, youdao_link(x[0]), x[0], x[1], x[0])
            count += 1
        page += ' <input type="submit" value="确定并返回"/>\n'
        page += '</form>\n'
        # save history
        d = load_freq_history(path_prefix + 'static/frequency.p')
        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.p')

        return page
    elif request.method == 'GET':  # when we load a html page
        page = '<p><b><font color="red">English Pal - Make you better</font></b>'
        if session.get('logged_in'):
            page += ' <a href="%s">%s</a></p>\n' % (session['username'],
                                                    session['username'])
        else:
            page += ' <a href="/login">我有账号</a>  <a href="/signup">我没有账号</a></p>\n'
        page += '<p><img src="%s" width="400px" alt="advertisement"/></p>' % (
            get_random_image(path_prefix + 'static/img/'))
        page += '<form method="post" action="/">'
        page += ' <textarea name="content" rows="20" cols="80"></textarea><br/>'
        page += ' <input type="submit" value="统计"/>'
        page += ' <input type="reset" value="清除"/>'
        page += '</form>'
        d = load_freq_history(path_prefix + 'static/frequency.p')
        if len(d) > 0:
            page += '<p><b>常见词</b></p>'
            for x in sort_in_descending_order(pickle_idea.dict2lst(d)):
                if x[1] <= 99:
                    break
                page += '<a href="%s">%s</a> %d\n' % (youdao_link(
                    x[0]), x[0], x[1])

        return page
Beispiel #5
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')