-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathChatMerge.py
104 lines (92 loc) · 2.96 KB
/
ChatMerge.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import json
import os
import pickle
import time
import ChatClass
from ChatClass import json_dump, json_load, pickle_dump, pickle_load
def getconfig():
config = json_load('config.clc')
#print(config)
try:
return config['merge'], config['mergetime'], config[
'unmergegrouplist'], config['tag']
except:
return config['merge'], config['mergetime'], config['unmergegrouplist']
def Merge(Mergedict, filename):
version_error = 0
repeatquestion_num = 0
cldict = pickle_load('WordStock/' + filename)
#print(Mergedict)
try:
repeatquestion = Mergedict.keys() & cldict.keys()
for i in repeatquestion:
tempquestiondict = cldict[i]
newquestiondict = Mergedict[i]
try:
newquestiondict['freq'] += tempquestiondict['freq']
except:
version_error = 1
#print(newanswer)
newanswer = newquestiondict['answer']
tempanswer = tempquestiondict['answer']
newanswer.extend(tempanswer)
del cldict[i]
repeatquestion_num += 1
#print('相同问题已合并')
except:
pass
Mergedict.update(cldict)
if repeatquestion_num != 0:
print('已合并', filename, repeatquestion_num, '个相同问题')
if version_error == 1:
print('问题次数无法合并,请确认是否存在2.8.0版本以下的词库? 错误词库:{}'.format(filename))
return Mergedict
def getfile():
tagdict = getconfig()[3]
Taglist = []
for i in tagdict.values():
Taglist.extend(i)
Taglist = list(set(Taglist))
filelist = os.listdir('WordStock')
cllist = []
for i in filelist:
if i[-3:] == '.cl':
#print(i)
cllist.append(i)
#print(cllist)
Mergedict = {}
for i in cllist:
if i == 'Merge.cl':
continue
elif i[:-3] in Taglist:
continue
try:
if i[:-3] in str(getconfig()[2]):
continue
except:
pass
if not (i[:-3] in tagdict.keys()):
Mergedict = Merge(Mergedict, i)
pickle_dump(Mergedict, 'WordStock/' + 'Merge.cl')
for i in Taglist:
print('Tag:{} 合并'.format(i))
Mergedict_Tag = {}
for k in cllist:
if k[:-3] in str(tagdict.keys()):
if i in tagdict[k[:-3]]:
Mergedict_Tag = Merge(Mergedict_Tag, k)
pickle_dump(Mergedict_Tag, 'WordStock/' + '{}.cl'.format(i))
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), '词库合并完成')
#print('->',end='')
#os.system('pause')
def main():
getfile()
now_time = int(time.time())
while 1:
config = getconfig()
if config[0] == 0 or ChatClass.stop_run():
return None
if int(time.time()) - now_time >= config[1]:
getfile()
now_time = int(time.time())
time.sleep(0.5)