forked from mrlan/EnglishPal
				
			Compare commits
	
		
			9 Commits 
		
	
	
		
			master
			...
			Bug499-Wan
		
	
	| Author | SHA1 | Date | 
|---|---|---|
| 
							
							
								
									
								
								 | 
						348c37032b | |
| 
							
							
								
									
								
								 | 
						ca87c1b935 | |
| 
							
							
								 | 
						3125af6846 | |
| 
							
							
								 | 
						18f54e3505 | |
| 
							
							
								
									
								
								 | 
						cff0ce32b2 | |
| 
							
							
								
									
								
								 | 
						c14de06c86 | |
| 
							
							
								
									
								
								 | 
						ee6eb45d4b | |
| 
							
							
								
									
								
								 | 
						b8560a34e6 | |
| 
							
							
								
									
								
								 | 
						415ac25a31 | 
							
								
								
									
										15
									
								
								app/Yaml.py
								
								
								
								
							
							
						
						
									
										15
									
								
								app/Yaml.py
								
								
								
								
							| 
						 | 
				
			
			@ -1,13 +1,15 @@
 | 
			
		|||
'''
 | 
			
		||||
"""
 | 
			
		||||
Yaml.py
 | 
			
		||||
配置文件包括:
 | 
			
		||||
    ./static/config.yml
 | 
			
		||||
    ./layout/partial/header.html
 | 
			
		||||
    ./layout/partial/footer.html
 | 
			
		||||
'''
 | 
			
		||||
"""
 | 
			
		||||
import yaml as YAML
 | 
			
		||||
import os
 | 
			
		||||
 | 
			
		||||
from app.file_open import FileOpen
 | 
			
		||||
 | 
			
		||||
path_prefix = './'  # comment this line in deployment
 | 
			
		||||
 | 
			
		||||
# YAML文件路径
 | 
			
		||||
| 
						 | 
				
			
			@ -15,13 +17,16 @@ ymlPath = path_prefix + 'static/config.yml'
 | 
			
		|||
 | 
			
		||||
# partial文件夹路径
 | 
			
		||||
partialPath = path_prefix + 'layout/partial/'
 | 
			
		||||
f = open(ymlPath, 'r', encoding='utf-8') # 以'UTF-8'格式打开YAML文件
 | 
			
		||||
# f = open(ymlPath, 'r', encoding='utf-8') # 以'UTF-8'格式打开YAML文件
 | 
			
		||||
f = FileOpen.read_only(ymlPath, 'r', encoding='utf-8') # 以'UTF-8'格式打开YAML文件
 | 
			
		||||
cont = f.read()  # 以文本形式读取YAML
 | 
			
		||||
 | 
			
		||||
yml = YAML.load(cont, Loader=YAML.FullLoader)  # 加载YAML
 | 
			
		||||
 | 
			
		||||
with open(partialPath + 'header.html', 'r', encoding='utf-8') as f:
 | 
			
		||||
# with open(partialPath + 'header.html', 'r', encoding='utf-8') as f:
 | 
			
		||||
with FileOpen.read_only(partialPath + 'header.html', 'r', encoding='utf-8') as f:
 | 
			
		||||
    yml['header'] = f.read() # header内的文本会被直接添加到所有页面的head标签内
 | 
			
		||||
 | 
			
		||||
with open(partialPath + 'footer.html', 'r', encoding='utf-8') as f:
 | 
			
		||||
# with open(partialPath + 'footer.html', 'r', encoding='utf-8') as f:
 | 
			
		||||
with FileOpen.read_only(partialPath + 'footer.html', 'r', encoding='utf-8') as f:
 | 
			
		||||
    yml['footer'] = f.read() # footer内的文本会被直接添加到所有页面的最底部
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,4 +1,6 @@
 | 
			
		|||
from flask import *
 | 
			
		||||
from markupsafe import escape
 | 
			
		||||
 | 
			
		||||
from Login import check_username_availability, verify_user, add_user, get_expiry_date, change_password, WarningMessage
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -7,11 +7,14 @@
 | 
			
		|||
 | 
			
		||||
import pickle
 | 
			
		||||
import math
 | 
			
		||||
 | 
			
		||||
from app.file_open import FileOpen
 | 
			
		||||
from wordfreqCMD import remove_punctuation, freq, sort_in_descending_order, sort_in_ascending_order
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def load_record(pickle_fname):
 | 
			
		||||
    f = open(pickle_fname, 'rb')
 | 
			
		||||
    # f = open(pickle_fname, 'rb')
 | 
			
		||||
    f = FileOpen.read_only(pickle_fname, 'rb')
 | 
			
		||||
    d = pickle.load(f)
 | 
			
		||||
    f.close()
 | 
			
		||||
    return d
 | 
			
		||||
| 
						 | 
				
			
			@ -237,7 +240,8 @@ We need — for our farmers, our manufacturers, for, frankly, unions and non-uni
 | 
			
		|||
 | 
			
		||||
 | 
			
		||||
    #f = open('bbc-fulltext/bbc/entertainment/001.txt')
 | 
			
		||||
    f = open('wordlist.txt')
 | 
			
		||||
    # f = open('wordlist.txt')
 | 
			
		||||
    f = FileOpen.read_only('wordlist.txt')
 | 
			
		||||
    s = f.read()
 | 
			
		||||
    f.close()
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,47 @@
 | 
			
		|||
import os
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class ModeErrorException(Exception):
 | 
			
		||||
    def __init__(self, error_info):
 | 
			
		||||
        super().__init__(self)
 | 
			
		||||
        self.error_info = error_info
 | 
			
		||||
 | 
			
		||||
    def __str__(self):
 | 
			
		||||
        return self.error_info
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class FileOpen:
 | 
			
		||||
    def __init__(self):
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def read_only(path, mode='r', encoding=None):
 | 
			
		||||
        if mode not in ['r', 'r+', 'rb', 'rt', 'rb+', 'rt+']:
 | 
			
		||||
            raise ModeErrorException('the input mode is wrong')
 | 
			
		||||
        my_dir = os.path.dirname(path)  # 获得路径的目录
 | 
			
		||||
        if not os.path.exists(my_dir):  # 判断文件的所有父文件夹是否存在
 | 
			
		||||
            os.makedirs(my_dir)  # 创建所有父文件夹
 | 
			
		||||
        # 读文件需要判断文件是否存在,不存在则创建
 | 
			
		||||
        if not os.path.exists(path):
 | 
			
		||||
            f = open(path, 'w', encoding)
 | 
			
		||||
            f.close()
 | 
			
		||||
        return open(path, mode=mode, encoding=encoding)
 | 
			
		||||
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def write_able(path, mode='w', encoding=None):
 | 
			
		||||
        if mode not in ['w', 'w+', 'wb', 'wt', 'wb+', 'wt+']:
 | 
			
		||||
            raise ModeErrorException('the input mode is wrong')
 | 
			
		||||
        my_dir = os.path.dirname(path)  # 获得路径的目录
 | 
			
		||||
        if not os.path.exists(my_dir):  # 判断文件的所有父文件夹是否存在
 | 
			
		||||
            os.makedirs(my_dir)  # 创建所有父文件夹
 | 
			
		||||
        return open(path, mode=mode, encoding=encoding)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
if __name__ == "__main__":
 | 
			
		||||
    wf = FileOpen.write_able(r"D:\Workspace\gitcode\EnglishPal\app\static\test.txt")
 | 
			
		||||
    wf.write("asdasdasd")
 | 
			
		||||
    wf.close()
 | 
			
		||||
 | 
			
		||||
    rf = FileOpen.read_only(r"D:\Workspace\gitcode\EnglishPal\app\static\test.txt")
 | 
			
		||||
    for line in rf.readlines():
 | 
			
		||||
        print(line)
 | 
			
		||||
| 
						 | 
				
			
			@ -9,6 +9,8 @@
 | 
			
		|||
import pickle
 | 
			
		||||
from datetime import datetime
 | 
			
		||||
 | 
			
		||||
from app.file_open import FileOpen
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def lst2dict(lst, d):
 | 
			
		||||
    ''' 
 | 
			
		||||
| 
						 | 
				
			
			@ -37,14 +39,16 @@ def merge_frequency(lst1, lst2):
 | 
			
		|||
 | 
			
		||||
 | 
			
		||||
def load_record(pickle_fname):
 | 
			
		||||
    f = open(pickle_fname, 'rb')
 | 
			
		||||
    # f = open(pickle_fname, 'rb')
 | 
			
		||||
    f = FileOpen.read_only(pickle_fname, 'rb')
 | 
			
		||||
    d = pickle.load(f)
 | 
			
		||||
    f.close()
 | 
			
		||||
    return d
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def save_frequency_to_pickle(d, pickle_fname):
 | 
			
		||||
    f = open(pickle_fname, 'wb')
 | 
			
		||||
    # f = open(pickle_fname, 'wb')
 | 
			
		||||
    f = FileOpen.write_able(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']
 | 
			
		||||
    exclusion_lst = []
 | 
			
		||||
    d2 = {}
 | 
			
		||||
| 
						 | 
				
			
			@ -55,20 +59,24 @@ def save_frequency_to_pickle(d, pickle_fname):
 | 
			
		|||
    f.close()
 | 
			
		||||
 | 
			
		||||
def unfamiliar(path,word):
 | 
			
		||||
    f = open(path,"rb")
 | 
			
		||||
    # f = open(path,"rb")
 | 
			
		||||
    f = FileOpen.read_only(path, 'rb')
 | 
			
		||||
    dic = pickle.load(f)
 | 
			
		||||
    dic[word] += [datetime.now().strftime('%Y%m%d%H%M')]
 | 
			
		||||
    fp = open(path,"wb")
 | 
			
		||||
    # fp = open(path,"wb")
 | 
			
		||||
    fp = FileOpen.write_able(path, 'wb')
 | 
			
		||||
    pickle.dump(dic,fp)
 | 
			
		||||
 | 
			
		||||
def familiar(path,word):
 | 
			
		||||
    f = open(path,"rb")
 | 
			
		||||
    # f = open(path,"rb")
 | 
			
		||||
    f = FileOpen.read_only(path, 'rb')
 | 
			
		||||
    dic = pickle.load(f)
 | 
			
		||||
    if len(dic[word])>1:
 | 
			
		||||
        del dic[word][0]
 | 
			
		||||
    else:
 | 
			
		||||
        dic.pop(word)
 | 
			
		||||
    fp = open(path,"wb")
 | 
			
		||||
    # fp = open(path,"wb")
 | 
			
		||||
    fp = FileOpen.write_able(path, 'wb')
 | 
			
		||||
    pickle.dump(dic,fp)
 | 
			
		||||
 | 
			
		||||
if __name__ == '__main__':
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -11,6 +11,9 @@
 | 
			
		|||
import pickle
 | 
			
		||||
from datetime import datetime
 | 
			
		||||
 | 
			
		||||
from app.file_open import FileOpen
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def lst2dict(lst, d):
 | 
			
		||||
    ''' 
 | 
			
		||||
    Store the information in list lst to dictionary d. 
 | 
			
		||||
| 
						 | 
				
			
			@ -26,13 +29,15 @@ def lst2dict(lst, d):
 | 
			
		|||
            d[word] += dates
 | 
			
		||||
 | 
			
		||||
def deleteRecord(path,word):
 | 
			
		||||
    with open(path, 'rb') as f:
 | 
			
		||||
    # with open(path, 'rb') as f:
 | 
			
		||||
    with FileOpen.read_only(path, 'rb') as f:
 | 
			
		||||
        db = pickle.load(f)
 | 
			
		||||
    try:
 | 
			
		||||
        db.pop(word)
 | 
			
		||||
    except KeyError:
 | 
			
		||||
        print("sorry")
 | 
			
		||||
    with open(path, 'wb') as ff:
 | 
			
		||||
    # with open(path, 'wb') as ff:
 | 
			
		||||
    with FileOpen.write_able(path, 'wb') as ff:
 | 
			
		||||
        pickle.dump(db, ff)
 | 
			
		||||
 | 
			
		||||
def dict2lst(d):
 | 
			
		||||
| 
						 | 
				
			
			@ -56,14 +61,16 @@ def merge_frequency(lst1, lst2):
 | 
			
		|||
 | 
			
		||||
 | 
			
		||||
def load_record(pickle_fname):
 | 
			
		||||
    f = open(pickle_fname, 'rb')
 | 
			
		||||
    # f = open(pickle_fname, 'rb')
 | 
			
		||||
    f = FileOpen.read_only(pickle_fname, 'rb')
 | 
			
		||||
    d = pickle.load(f)
 | 
			
		||||
    f.close()
 | 
			
		||||
    return d
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def save_frequency_to_pickle(d, pickle_fname):
 | 
			
		||||
    f = open(pickle_fname, 'wb')
 | 
			
		||||
    # f = open(pickle_fname, 'wb')
 | 
			
		||||
    f = FileOpen.write_able(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']
 | 
			
		||||
    d2 = {}
 | 
			
		||||
    for k in d:
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,6 +1,7 @@
 | 
			
		|||
from datetime import datetime
 | 
			
		||||
 | 
			
		||||
from flask import *
 | 
			
		||||
from markupsafe import escape
 | 
			
		||||
 | 
			
		||||
# from app import Yaml
 | 
			
		||||
# from app.Article import get_today_article, load_freq_history
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -8,6 +8,8 @@ import string
 | 
			
		|||
import operator
 | 
			
		||||
import os, sys # 引入模块sys,因为我要用里面的sys.argv列表中的信息来读取命令行参数。
 | 
			
		||||
import pickle_idea
 | 
			
		||||
from app.file_open import FileOpen
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def freq(fruit):
 | 
			
		||||
    '''
 | 
			
		||||
| 
						 | 
				
			
			@ -32,7 +34,8 @@ def youdao_link(s): # 有道链接
 | 
			
		|||
 | 
			
		||||
 | 
			
		||||
def file2str(fname):#文件转字符
 | 
			
		||||
    f = open(fname) #打开
 | 
			
		||||
    # f = open(fname) #打开
 | 
			
		||||
    f = FileOpen.read_only(fname)
 | 
			
		||||
    s = f.read()    #读取
 | 
			
		||||
    f.close()       #关闭
 | 
			
		||||
    return s
 | 
			
		||||
| 
						 | 
				
			
			@ -80,7 +83,8 @@ def make_html_page(lst, fname):
 | 
			
		|||
        # <a href="">word</a>
 | 
			
		||||
        s += '<p>%d <a href="%s">%s</a> (%d)</p>' % (count, youdao_link(x[0]), x[0], x[1])
 | 
			
		||||
        count += 1
 | 
			
		||||
    f = open(fname, 'w')
 | 
			
		||||
    # f = open(fname, 'w')
 | 
			
		||||
    f = FileOpen.write_able(fname)
 | 
			
		||||
    f.write(s)
 | 
			
		||||
    f.close()
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue