forked from VILAN-Lab/MLGCN-DP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
167 lines (134 loc) · 5.19 KB
/
dataset.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
import numpy as np
import torch
import torch.utils.data
from transformer import Constants
def paired_collate_fn(insts):
src1_insts, src2_insts, src3_insts, src4_insts, \
scr1_emo, scr2_emo, scr3_emo, scr4_emo, tgt_insts = list(zip(*insts))
src1_insts = collate_fn(src1_insts, 'src1')
src2_insts = collate_fn(src2_insts, 'src2')
src3_insts = collate_fn(src3_insts, 'src3')
src4_insts = collate_fn(src4_insts, 'src4')
scr1_emo = collate_fn_emotion(scr1_emo)
scr2_emo = collate_fn_emotion(scr2_emo)
scr3_emo = collate_fn_emotion(scr3_emo)
scr4_emo = collate_fn_emotion(scr4_emo)
tgt_insts = collate_fn(tgt_insts, 'src4')
return (*src1_insts, *src2_insts, *src3_insts, *src4_insts, *scr1_emo,
*scr2_emo, *scr3_emo, *scr4_emo, *tgt_insts)
def collate_fn_emotion(insts):
''' Pad the instance to the max seq length in batch '''
# max_len = max(len(inst) for inst in insts)
max_len = 1
# print('insts:', insts)
batch_seq = np.array([inst for inst in insts])
# batch_pos = np.array(
# [pos_i+1 if w_i != Constants.PAD else 0
# for pos_i, w_i in enumerate(batch_seq)])
# # print(batch_seq)
batch_seq = torch.LongTensor(batch_seq)
# batch_pos = torch.LongTensor(batch_pos)
return batch_seq,
def collate_fn(insts, sent):
''' Pad the instance to the max seq length in batch '''
# max_len = max(len(inst) for inst in insts)
if sent == 'src1':
max_len = 20
batch_seq = np.array([inst[:max_len] + [Constants.PAD] * (max_len - len(inst[:max_len]))
for inst in insts])
elif sent == 'src2':
max_len = 20
batch_seq = np.array([inst[:max_len] + [Constants.PAD] * (max_len - len(inst[:max_len])) + [4]
for inst in insts])
elif sent == 'src3':
max_len = 20
batch_seq = np.array([inst[:max_len] + [Constants.PAD] * (max_len - len(inst[:max_len])) + [4, 4]
for inst in insts])
else:
max_len = 20
batch_seq = np.array([inst[:max_len] + [Constants.PAD] * (max_len - len(inst[:max_len])) + [4, 4, 4]
for inst in insts])
batch_pos = np.array([
[pos_i+1 if w_i != Constants.PAD else 0
for pos_i, w_i in enumerate(inst)] for inst in batch_seq])
# print(batch_seq)
batch_seq = torch.LongTensor(batch_seq)
batch_pos = torch.LongTensor(batch_pos)
return batch_seq, batch_pos
class SEGDataset(torch.utils.data.Dataset):
def __init__(self, src_word2idx, tgt_word2idx,
src1_insts=None,
src2_insts=None,
src3_insts=None,
src4_insts=None,
src1_emo=None,
src2_emo=None,
src3_emo=None,
src4_emo=None,
tgt_insts=None):
# assert src_insts
# print(len(src1_insts), len(tgt_insts))
assert not tgt_insts or (len(src1_insts) == len(tgt_insts))
src_idx2word = {idx: word for word, idx in src_word2idx.items()}
self._src_word2idx = src_word2idx
self._src_idx2word = src_idx2word
self._src1_insts = src1_insts
self._src2_insts = src2_insts
self._src3_insts = src3_insts
self._src4_insts = src4_insts
self.src1_emo = src1_emo
self.src2_emo = src2_emo
self.src3_emo = src3_emo
self.src4_emo = src4_emo
tgt_idx2word = {idx: word for word, idx in tgt_word2idx.items()}
self._tgt_word2idx = tgt_word2idx
self._tgt_idx2word = tgt_idx2word
self._tgt_insts = tgt_insts
@property
def n_insts(self):
''' Property for dataset size '''
return len(self._src1_insts)
@property
def src_vocab_size(self):
''' Property for vocab size '''
return len(self._src_word2idx)
@property
def tgt_vocab_size(self):
''' Property for vocab size '''
return len(self._tgt_word2idx)
@property
def src_word2idx(self):
''' Property for word dictionary '''
return self._src_word2idx
@property
def tgt_word2idx(self):
''' Property for word dictionary '''
return self._tgt_word2idx
@property
def src_idx2word(self):
''' Property for index dictionary '''
return self._src_idx2word
def tgt_idx2word(self):
''' Property for index dictionary '''
return self._tgt_idx2word
def __len__(self):
return self.n_insts
def __getitem__(self, idx):
if self._tgt_insts:
return self._src1_insts[idx],\
self._src2_insts[idx],\
self._src3_insts[idx],\
self._src4_insts[idx],\
self.src1_emo[idx],\
self.src2_emo[idx],\
self.src3_emo[idx],\
self.src4_emo[idx],\
self._tgt_insts[idx]
return self._src1_insts[idx], \
self._src2_insts[idx],\
self._src3_insts[idx],\
self._src4_insts[idx], \
self.src1_emo[idx], \
self.src2_emo[idx], \
self.src3_emo[idx], \
self.src4_emo[idx],\