1
0
Fork 0
EnglishPal/app/pickle_idea2.py

93 lines
2.8 KiB
Python
Raw Permalink Normal View History

2021-04-06 16:22:03 +08:00
###########################################################################
# Copyright 2019 (C) Hui Lan <hui.lan@cantab.net>
# Written permission must be obtained from the author for commercial uses.
###########################################################################
# Purpose: dictionary & pickle as a simple means of database.
# Task: incorporate the functions into wordfreqCMD.py such that it will also show cumulative frequency.
# Note: unlike pick_idea.py, now the second item is not frequency, but a list of dates.
import pickle
from datetime import datetime
def lst_to_dict(lst, d):
"""
2021-04-06 16:22:03 +08:00
Store the information in list lst to dictionary d.
Note: nothing is returned.
"""
2021-04-06 16:22:03 +08:00
for x in lst:
word = x[0]
dates = x[1]
if word not in d:
2021-04-06 16:22:03 +08:00
d[word] = dates
else:
d[word] += dates
def delete_record(path, word):
with open(path, 'rb') as f:
db = pickle.load(f)
try:
db.pop(word)
except KeyError:
print("sorry")
with open(path, 'wb') as ff:
pickle.dump(db, ff)
2021-04-06 16:22:03 +08:00
def dict_to_lst(d):
2021-04-06 16:22:03 +08:00
if len(d) > 0:
keys = list(d.keys())
if isinstance(d[keys[0]], int):
lst = []
for k in d:
lst.append((k, [datetime.now().strftime('%Y%m%d%H%M')]))
return lst
elif isinstance(d[keys[0]], list):
return list(d.items()) # a list of (key, value) pairs
2021-04-06 16:22:03 +08:00
return []
2021-04-06 16:22:03 +08:00
def merge_frequency(lst1, lst2):
d = {}
lst_to_dict(lst1, d)
lst_to_dict(lst2, d)
2021-04-06 16:22:03 +08:00
return d
def load_record(pickle_fname):
f = open(pickle_fname, 'rb')
d = pickle.load(f)
f.close()
return d
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']
d2 = {}
for k in d:
if k not 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])))
2021-04-06 16:22:03 +08:00
pickle.dump(d2, f)
f.close()
if __name__ == '__main__':
lst1 = [('apple',['201910251437', '201910251438']), ('banana', ['201910251439'])]
2021-04-06 16:22:03 +08:00
d = {}
lst_to_dict(lst1, d) # d will change
2021-04-06 16:22:03 +08:00
save_frequency_to_pickle(d, 'frequency.p') # frequency.p is our database
lst2 = [('banana',['201910251439']), ('orange', ['201910251440', '201910251439'])]
d = load_record('frequency.p')
lst1 = dict_to_lst(d)
2021-04-06 16:22:03 +08:00
d = merge_frequency(lst2, lst1)
print(d)