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.
|
|
|
|
|
|
|
|
import pickle
|
2021-05-31 08:48:03 +08:00
|
|
|
from datetime import datetime
|
2021-04-06 16:22:03 +08:00
|
|
|
|
|
|
|
|
2023-06-01 19:13:39 +08:00
|
|
|
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.
|
|
|
|
|
|
|
|
'''
|
|
|
|
for x in lst:
|
|
|
|
word = x[0]
|
|
|
|
freq = x[1]
|
|
|
|
if not word in d:
|
|
|
|
d[word] = freq
|
|
|
|
else:
|
|
|
|
d[word] += freq
|
|
|
|
|
|
|
|
|
2023-06-01 19:13:39 +08:00
|
|
|
def dict_to_lst(d):
|
|
|
|
return list(d.items()) # a list of (key, value) pairs
|
2021-04-06 16:22:03 +08:00
|
|
|
|
|
|
|
|
2023-06-01 19:13:39 +08:00
|
|
|
def merge_frequency(list1, list2):
|
2021-04-06 16:22:03 +08:00
|
|
|
d = {}
|
2023-06-01 19:13:39 +08:00
|
|
|
lst_to_dict(list1, d)
|
|
|
|
lst_to_dict(list2, 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']
|
|
|
|
exclusion_lst = []
|
|
|
|
d2 = {}
|
|
|
|
for k in d:
|
|
|
|
if not k in exclusion_lst and not k.isnumeric() and len(k) > 1:
|
|
|
|
d2[k] = d[k]
|
|
|
|
pickle.dump(d2, f)
|
|
|
|
f.close()
|
|
|
|
|
2023-06-01 19:13:39 +08:00
|
|
|
|
|
|
|
def unfamiliar(path, word):
|
|
|
|
f = open(path, "rb")
|
2021-05-31 08:48:03 +08:00
|
|
|
dic = pickle.load(f)
|
|
|
|
dic[word] += [datetime.now().strftime('%Y%m%d%H%M')]
|
2023-06-01 19:13:39 +08:00
|
|
|
fp = open(path, "wb")
|
|
|
|
pickle.dump(dic, fp)
|
|
|
|
|
2021-05-31 08:48:03 +08:00
|
|
|
|
2023-06-01 19:13:39 +08:00
|
|
|
def familiar(path, word):
|
|
|
|
f = open(path, "rb")
|
2021-05-31 08:48:03 +08:00
|
|
|
dic = pickle.load(f)
|
2023-06-01 19:13:39 +08:00
|
|
|
if len(dic[word]) > 1:
|
2021-05-31 08:48:03 +08:00
|
|
|
del dic[word][0]
|
|
|
|
else:
|
|
|
|
dic.pop(word)
|
2023-06-01 19:13:39 +08:00
|
|
|
fp = open(path, "wb")
|
|
|
|
pickle.dump(dic, fp)
|
2021-04-06 16:22:03 +08:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
|
|
lst1 = [('apple',2), ('banana',1)]
|
|
|
|
d = {}
|
2023-06-01 19:13:39 +08:00
|
|
|
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',2), ('orange', 4)]
|
|
|
|
d = load_record('frequency.p')
|
2023-06-01 19:13:39 +08:00
|
|
|
lst1 = dict_to_lst(d)
|
2021-04-06 16:22:03 +08:00
|
|
|
d = merge_frequency(lst2, lst1)
|
|
|
|
print(d)
|