forked from mrlan/EnglishPal
				
			Fix bug 563
							parent
							
								
									00ae770195
								
							
						
					
					
						commit
						56c8dc24c6
					
				| 
						 | 
				
			
			@ -9,8 +9,53 @@
 | 
			
		|||
# Note: unlike pick_idea.py, now the second item is not frequency, but a list of dates.
 | 
			
		||||
 | 
			
		||||
import pickle
 | 
			
		||||
import requests
 | 
			
		||||
import hashlib
 | 
			
		||||
import random
 | 
			
		||||
from urllib.parse import urlencode
 | 
			
		||||
 | 
			
		||||
from datetime import datetime
 | 
			
		||||
 | 
			
		||||
class BaiduContent:
 | 
			
		||||
    APPID = '20200314000398337'  # 将'您的APPID'替换为实际的APPID
 | 
			
		||||
    KEY = 'uZ6Sdwz_V1zu9q1peowk'     # 将'您的密钥'替换为实际的API密钥
 | 
			
		||||
 | 
			
		||||
def generate_sign(appid, q, salt, key):
 | 
			
		||||
    sign_str = appid + q + str(salt) + key
 | 
			
		||||
    sign = hashlib.md5(sign_str.encode('utf-8')).hexdigest()
 | 
			
		||||
    return sign
 | 
			
		||||
def is_valid_word(word):
 | 
			
		||||
    url = "https://fanyi-api.baidu.com/api/trans/vip/translate"
 | 
			
		||||
    salt = random.randint(32768, 65536)
 | 
			
		||||
    sign = generate_sign(BaiduContent.APPID, word, salt, BaiduContent.KEY)
 | 
			
		||||
 | 
			
		||||
    params = {
 | 
			
		||||
        'q': word,
 | 
			
		||||
        'from': 'en',
 | 
			
		||||
        'to': 'zh',
 | 
			
		||||
        'appid': BaiduContent.APPID,
 | 
			
		||||
        'salt': salt,
 | 
			
		||||
        'sign': sign
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    headers = {'Content-Type': "application/x-www-form-urlencoded"}
 | 
			
		||||
    data = urlencode(params).encode('utf-8')
 | 
			
		||||
 | 
			
		||||
    try:
 | 
			
		||||
        response = requests.post(url, data=data, headers=headers)
 | 
			
		||||
        if response.status_code == 200:
 | 
			
		||||
            data = response.json()
 | 
			
		||||
            # print(data['trans_result'][0]['dst'])
 | 
			
		||||
            # 检查是否含有翻译结果,并判断翻译后的文本是否与原文不同
 | 
			
		||||
            if 'trans_result' in data and data['trans_result'][0]['dst'] != word:
 | 
			
		||||
                return True
 | 
			
		||||
            else:
 | 
			
		||||
                return False
 | 
			
		||||
        else:
 | 
			
		||||
            return False
 | 
			
		||||
    except requests.RequestException:
 | 
			
		||||
        return False
 | 
			
		||||
 | 
			
		||||
def lst2dict(lst, d):
 | 
			
		||||
    ''' 
 | 
			
		||||
    Store the information in list lst to dictionary d. 
 | 
			
		||||
| 
						 | 
				
			
			@ -61,17 +106,30 @@ def load_record(pickle_fname):
 | 
			
		|||
    f.close()
 | 
			
		||||
    return d
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    # exclusion_lst = ['one', 'no', 'has', 'had', 'do', 'that', 'have', 'by', 'not', 'but', 'we', 'this', 'my', 'him', 'so', 'or', 'as', 'are', 'it', 'from', 'with', 'be', 'can', 'for', 'an', 'if', 'who', 'whom', 'whose', 'which', 'the', 'to', 'a', 'of', 'and', 'you', 'i', 'he', 'she', 'they', 'me', 'was', 'were', 'is', 'in', 'at', 'on', 'their', 'his', 'her', 's', 'said', 'all', 'did', 'been', 'w']
 | 
			
		||||
def save_frequency_to_pickle(d, pickle_fname):
 | 
			
		||||
    f = open(pickle_fname, 'wb')
 | 
			
		||||
    exclusion_lst = ['one', 'no', 'has', 'had', 'do', 'that', 'have', 'by', 'not', 'but', 'we', 'this', 'my', 'him', 'so', 'or', 'as', 'are', 'it', 'from', 'with', 'be', 'can', 'for', 'an', 'if', 'who', 'whom', 'whose', 'which', 'the', 'to', 'a', 'of', 'and', 'you', 'i', 'he', 'she', 'they', 'me', 'was', 'were', 'is', 'in', 'at', 'on', 'their', 'his', 'her', 's', 'said', 'all', 'did', 'been', 'w']
 | 
			
		||||
    with open(pickle_fname, 'wb') as f:  # 使用 with 语句自动处理文件关闭
 | 
			
		||||
        exclusion_lst = []
 | 
			
		||||
        d2 = {}
 | 
			
		||||
        illegal = False  # 标记是否合法
 | 
			
		||||
        added = False
 | 
			
		||||
        for k in d:
 | 
			
		||||
        if not k in exclusion_lst and not k.isnumeric() and not len(k) < 2:
 | 
			
		||||
            d2[k] = list(sorted(d[k])) # 原先这里是d2[k] = list(sorted(set(d[k])))
 | 
			
		||||
    pickle.dump(d2, f)
 | 
			
		||||
    f.close()
 | 
			
		||||
            if k not in exclusion_lst and not k.isnumeric():
 | 
			
		||||
                if is_valid_word(k):  # 只有当单词不合法时进行标记
 | 
			
		||||
                    d2[k] = list(sorted(d[k]))
 | 
			
		||||
                    added =True
 | 
			
		||||
                else:
 | 
			
		||||
                    illegal = True  # 标记至少处理了一个有效单词
 | 
			
		||||
 | 
			
		||||
        if illegal:
 | 
			
		||||
            if not added:
 | 
			
		||||
                pickle.dump({}, f)
 | 
			
		||||
            else:
 | 
			
		||||
                pickle.dump(d2, f)
 | 
			
		||||
            return 0  # 返回0表示成功处理存在非法单词
 | 
			
		||||
        else:
 | 
			
		||||
            pickle.dump(d2, f)
 | 
			
		||||
            return 1  # 返回1表示成功处理并保存至少一个单词
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
if __name__ == '__main__':
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -184,8 +184,11 @@ def user_mark_word(username):
 | 
			
		|||
        if len(lst_history) > 999:
 | 
			
		||||
            flash('You have way too many words in your difficult-words book. Delete some first.')
 | 
			
		||||
        else:
 | 
			
		||||
            pickle_idea2.save_frequency_to_pickle(d, user_freq_record)
 | 
			
		||||
 | 
			
		||||
            if pickle_idea2.save_frequency_to_pickle(d, user_freq_record):
 | 
			
		||||
                flash('Added %s.' % (', '.join(request.form.getlist('marked'))))
 | 
			
		||||
            else:
 | 
			
		||||
                flash('%s.存在非法单词' % (', '.join(request.form.getlist('marked'))))
 | 
			
		||||
        return redirect(url_for('user_bp.userpage', username=username))
 | 
			
		||||
    else:
 | 
			
		||||
        return 'Under construction'
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue