-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.py
403 lines (344 loc) · 14 KB
/
utils.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#!/usr/bin/python
# Author: GMFTBY
# Time: 2019.9.25
'''
1. utils functions for training the model or loading the dataset
2. the stat function for the graph
'''
import pickle
import argparse
from collections import Counter
import os
import numpy as np
import torch
import nltk
from tqdm import tqdm
import ipdb
import heapq
import random
def load_pickle(path):
with open(path, 'rb') as f:
obj = pickle.load(f)
return obj
def load_word_embedding(path, dimension=300):
# load chinese or english word embedding
unk = np.random.rand(dimension)
with open(path, 'r', encoding='utf-8') as f:
lines = f.readlines()
dic = {}
for line in tqdm(lines):
dic[line.split()[0]] = np.array([float(f) for f in line.strip().split()[1:]], dtype=np.float)
dic['<unk>'] = unk
return dic
def cos_similarity(gr, ge):
return np.dot(gr, ge) / (np.linalg.norm(gr) * np.linalg.norm(ge))
def num2seq(src, idx2w):
return [idx2w[int(i)] for i in src]
def transformer_list(obj):
# transform [batch, turn, lengths] to [turns, batch, lengths]
turns = []
batch_size, turn_size = len(obj), len(obj[0])
for i in range(turn_size):
turns.append([obj[j][i] for j in range(batch_size)])
return turns
def pad_sequence(pad, batch, bs):
maxlen = max([len(batch[i]) for i in range(bs)])
for i in range(bs):
batch[i].extend([pad] * (maxlen - len(batch[i])))
def load_best_model(dataset, model, net, min_threshold, max_threshold):
path = f'./ckpt/{dataset}/{model}/'
best_loss, best_file, best_epoch = np.inf, None, -1
for file in os.listdir(path):
try:
_, val_loss, _, epoch = file.split('_')
except:
continue
epoch = epoch.split('.')[0]
val_loss, epoch = float(val_loss), int(epoch)
if min_threshold <= epoch <= max_threshold and epoch > best_epoch:
best_file = file
best_epoch = epoch
if best_file:
file_path = path + best_file
print(f'[!] Load the model from {file_path}, threshold ({min_threshold}, {max_threshold})')
net.load_state_dict(torch.load(file_path)['net'])
else:
raise Exception('[!] No saved model')
def create_the_graph(turns, weights=[1, 1], threshold=4, bidir=True):
'''create the weighted directed graph of one conversation
sequenutial edge, user connected edge, [BERT/PMI] edge
param: turns: [turns(user, utterance)]
param: weights: [sequential_w, user_w]
param: bc bert encoder
output: [2, num_edges], [num_edges]'''
edges = {}
s_w, u_w = weights
# temporal information
turn_len = len(turns)
se, ue, pe = 0, 0, 0
# for i in range(turn_len - 1):
# edges[(i, i + 1)] = [s_w]
# se += 1
# role information
counter = {i:1 for i in range(1, turn_len)} # add the counter
for i in range(turn_len):
for j in range(turn_len):
if j > i:
useri, _ = turns[i]
userj, _ = turns[j]
# u_w = min(max(0.5 - 1 / (j - i), 0.1), 0.5)
# add the counter
if useri == userj:
if edges.get((i, j), None):
edges[(i, j)].append(u_w)
else:
edges[(i, j)] = [u_w]
# # add the counter
# counter[j] += 1
ue += 1
# absolute graph
# if edges.get((i, j), None):
# edges[(i, j)].append(u_w)
# else:
# edges[(i, j)] = [u_w]
# ue += 1
# sparse graph
pass
# clean the edges
e, w = [[], []], []
for src, tgt in edges.keys():
e[0].append(src)
e[1].append(tgt)
w.append(max(edges[(src, tgt)]))
if bidir and src != tgt:
# be careful of the self loop
e[0].append(tgt)
e[1].append(src)
w.append(max(edges[(src, tgt)]))
# if the in degree is bigger than threshold,
# delete (l - threshold) closest edges
return (e, w), se, ue, pe
def generate_graph(dialogs, path, threshold=4, bidir=True):
# dialogs: [datasize, turns]
# return: [datasize, (2, num_edges)/ (num_edges)]
# **make sure the bert-as-service is running**
# bc = BertClient()
edges = []
se, ue, pe = 0, 0, 0
for dialog in tqdm(dialogs):
edge, ses, ueu, pep = create_the_graph(dialog, threshold=threshold, bidir=bidir)
se += ses
ue += ueu
pe += pep
edges.append(edge)
with open(path, 'wb') as f:
pickle.dump(edges, f)
print(f'[!] graph information is converted in {path}')
print(f'[!] Avg se: {round(se / len(dialogs), 4)}; Avg ue: {round(ue / len(dialogs), 4)}; Avg pe: {round(pe / len(dialogs), 4)}')
def generate_vocab(files, vocab, cutoff=30000):
# training and validation files
words = []
for file in files:
obj = load_pickle(file)
for example in obj:
for turn in example:
user, utterance = turn
utterance = utterance.replace('<0>', '')
utterance = utterance.replace('<1>', '')
words.extend(nltk.word_tokenize(utterance))
words = Counter(words)
print(f'[!] whole vocab size: {len(words)}')
words = words.most_common(cutoff)
# special words
words.extend([('<sos>', 1), ('<eos>', 1), ('<unk>', 1),
('<pad>', 1), ('<silence>', 1), ('<0>', 1), ('<1>', 1)])
w2idx = {item[0]:idx for idx, item in enumerate(words)}
idx2w = [item[0] for item in words]
with open(vocab, 'wb') as f:
pickle.dump((w2idx, idx2w), f)
print(f'[!] save the vocab into {vocab}, vocab size: {len(w2idx)}')
def load_data_cf(src, tgt, src_vocab, tgt_vocab, maxlen):
# hierarchical and cf mode
# return src: (datasize, turns, lengths) & (datasize, turns)[user]
# return tgt: (datasize, lengths) & (datasize)[user]
# return label: [datasize,]
src_w2idx, src_idx2w = load_pickle(src_vocab)
tgt_w2idx, tgt_idx2w = load_pickle(tgt_vocab)
user_vocab = ['<0>', '<1>']
# load data
src, tgt = load_pickle(src), load_pickle(tgt)
src_dataset, tgt_dataset, src_user, tgt_user, label = [], [], [], [], []
# parse
for sexample, texample in tqdm(zip(src, tgt)):
# parse src
turns, srcu = [], []
for user, utterance in sexample:
if '<0>' in utterance: user_c = '<0>'
elif '<1>' in utterance: user_c = '<1>'
utterance = utterance.replace(user_c, '').strip()
line = [src_w2idx['<sos>'], src_w2idx[user_c]] + [src_w2idx.get(w, src_w2idx['<unk>']) for w in nltk.word_tokenize(utterance)] + [src_w2idx['<eos>']]
if len(line) > maxlen:
line = [src_w2idx['<sos>'], line[1]] + line[-maxlen:]
turns.append(line)
srcu.append(user_vocab.index(user))
# parse tgt
tgtu, utterance = texample[0]
if '<0>' in utterance: user_c = '<0>'
elif '<1>' in utterance: user_c = '<1>'
utterance = utterance.replace(user_c, '').strip()
tgtu = user_vocab.index(tgtu)
line = [tgt_w2idx['<sos>'], tgt_w2idx[user_c]] + [tgt_w2idx.get(w, tgt_w2idx['<unk>']) for w in nltk.word_tokenize(utterance)] + [tgt_w2idx['<eos>']]
if len(line) > maxlen:
line = [tgt_w2idx['<sos>'], line[1]] + line[-maxlen:]
label.append(1)
src_dataset.append(turns)
tgt_dataset.append(line)
src_user.append(srcu)
tgt_user.append(tgtu)
if tgtu != srcu[-1]:
# two parts
# 1. model is srcu, label 0, utterance add silence
label.append(0)
src_dataset.append(turns)
tgt_dataset.append([tgt_w2idx['<sos>'], tgt_w2idx['<silence>'], tgt_w2idx['<eos>']])
src_user.append(srcu)
tgt_user.append(srcu[-1])
# 2. model is tgtu, label 1
return src_dataset, src_user, tgt_dataset, tgt_user, label
def idx2sent(data, users, vocab):
# turn the index to the sentence
# data: [datasize, turn, length]
# user: [datasize, turn]
# return: [datasize, (user, turns)]
_, idx2w = load_pickle(vocab)
datasets = []
for example, user in tqdm(zip(data, users)):
# example: [turn, length], user: [turn]
turns = []
for turn, u in zip(example, user):
utterance = ' '.join([idx2w[w] for w in turn])
utterance = utterance.replace('<1>', '').replace('<0>', '').replace('<sos>', '').replace('<eos>', '').strip()
turns.append((u, utterance))
datasets.append(turns)
return datasets
# ========== stst of the graph ========== #
def analyse_graph(path, hops=3):
'''
This function analyzes the graph coverage stat of the graph in Dailydialog
and cornell dataset.
Stat the context node coverage of each node in the conversation.
:param: path, the path of the dataset graph file.
'''
def coverage(nodes, edges):
# return the coverage information of each node
# return list of tuple (context nodes, coverage nodes)
# edges to dict
e = {}
for i, j in zip(edges[0], edges[1]):
if i > j:
continue
if e.get(j, None):
e[j].append(i)
else:
e[j] = [i]
for key in e.keys(): # make the set
e[key] = list(set(e[key]))
collector = []
for node in nodes:
# context nodes
context_nodes = list(range(0, node))
if context_nodes:
# ipdb.set_trace()
# coverage nodes, BFS
coverage_nodes, tools, tidx = [], [(node, 0)], 0
while True:
try:
n, nidx = tools[tidx]
except:
break
if nidx < hops and e.get(n, None):
for src in e[n]:
if src not in tools:
tools.append((src, nidx + 1))
if src not in coverage_nodes:
coverage_nodes.append(src)
tidx += 1
collector.append((len(coverage_nodes), len(context_nodes)))
return collector
def avg_degree(ipt, opt):
nodes = {}
for ind, outd in zip(ipt, opt):
if ind not in nodes:
nodes[ind] = [0, 1]
else:
nodes[ind][1] += 1
if outd not in nodes:
nodes[outd] = [1, 0]
else:
nodes[outd][0] += 1
if nodes:
ind, outd = [i for i, j in nodes.items()], [j for i, j in nodes.items()]
return np.mean(ind), np.mean(outd)
else:
return None, None
graph = load_pickle(path) # [datasize, ([2, num_edge], [num_edge])]
avg_cover, avg_ind, avg_outd = [], [], []
# degree analyse
for idx, (edges, _) in enumerate(tqdm(graph)):
a, b = avg_degree(*edges)
if a:
avg_ind.append(a)
if b:
avg_outd.append(b)
print(f'[!] the avg in degree: {round(np.mean(avg_ind), 4)}')
print(f'[!] the avg out degree: {round(np.mean(avg_outd), 4)}')
# coverage
avg_nodes, avg_edges = [], []
for idx, (edges, _) in enumerate(tqdm(graph)):
# make sure the number of the nodes
max_n = max(edges[1]) + 1 if edges[1] else 1
nodes = list(range(max_n))
if max_n > 1:
avg_nodes.append(max_n)
if len(edges) > 1:
avg_edges.append(len(edges[0]))
avg_cover.extend(coverage(nodes, edges))
# ========== stat ========== #
ratio = [i / j for i, j in avg_cover]
print(f'[!] the avg turn length of the context is {round(np.mean(avg_nodes), 4)}')
print(f'[!] the avg edges of the context is {round(np.mean(avg_edges), 4)}')
print(f'[!] the avg graph coverage of the context is {round(np.mean(ratio), 4)}')
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='utils functions')
parser.add_argument('--mode', type=str, default='vocab')
parser.add_argument('--file', type=str, nargs='+', default=None,
help='file for generating the vocab')
parser.add_argument('--vocab', type=str, default='',
help='input or output vocabulary')
parser.add_argument('--graph', type=str, default=None)
parser.add_argument('--cutoff', type=int, default=50000,
help='cutoff of the vocabulary')
parser.add_argument('--threshold', type=float, default=4,
help='threshold for measuring the similarity between the utterances')
parser.add_argument('--maxlen', type=int, default=50)
parser.add_argument('--src_vocab', type=str, default=None)
parser.add_argument('--tgt_vocab', type=str, default=None)
parser.add_argument('--src', type=str, default=None)
parser.add_argument('--tgt', type=str, default=None)
parser.add_argument('--hops', type=int, default=3)
parser.add_argument('--bidir', dest='bidir', action='store_true')
parser.add_argument('--no-bidir', dest='bidir', action='store_false')
args = parser.parse_args()
if args.mode == 'vocab':
generate_vocab(args.file, args.vocab, cutoff=args.cutoff)
elif args.mode == 'graph':
# save the preprocessed data for generating graph
src_dataset, src_user, _, _, _ = load_data_cf(args.src, args.tgt, args.src_vocab, args.tgt_vocab, args.maxlen)
print(f'[!] load the cf mode dataset, prepare for preprocessing')
ppdataset = idx2sent(src_dataset, src_user, args.src_vocab)
print(f'[!] begin to create the graph')
generate_graph(ppdataset, args.graph, threshold=args.threshold, bidir=args.bidir)
elif args.mode == 'stat':
analyse_graph(args.graph, hops=args.hops)
else:
raise Exception('[!] wrong mode for running the utils script')