-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
180 lines (146 loc) · 5.83 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
168
169
170
171
172
173
174
175
176
177
178
179
import os
from copy import deepcopy
from tqdm import tqdm
import torch
import torch.utils.data as data
from torch.autograd import Variable as Var
from tree import Tree
from vocab import Vocab
import Constants
import utils
from nltk import word_tokenize
# Dataset class for SICK dataset
class SICKDataset(data.Dataset):
def __init__(self, path, vocab, num_classes):
super(SICKDataset, self).__init__()
self.vocab = vocab
self.num_classes = num_classes
self.lsentences = self.read_sentences(os.path.join(path,'a.toks'))
self.rsentences = self.read_sentences(os.path.join(path,'b.toks'))
self.ltrees = self.read_trees(os.path.join(path,'a.parents'))
self.rtrees = self.read_trees(os.path.join(path,'b.parents'))
self.labels = self.read_labels(os.path.join(path,'sim.txt'))
self.size = self.labels.size(0)
def __len__(self):
return self.size
def __getitem__(self, index):
ltree = deepcopy(self.ltrees[index])
rtree = deepcopy(self.rtrees[index])
lsent = deepcopy(self.lsentences[index])
rsent = deepcopy(self.rsentences[index])
label = deepcopy(self.labels[index])
return (ltree,lsent,rtree,rsent,label)
def read_sentences(self, filename):
with open(filename,'r') as f:
sentences = [self.read_sentence(line) for line in tqdm(f.readlines())]
return sentences
def read_sentence(self, line):
indices = self.vocab.convertToIdx(line.split(), Constants.UNK_WORD)
return torch.LongTensor(indices)
def read_trees(self, filename):
with open(filename,'r') as f:
trees = [self.read_tree(line) for line in tqdm(f.readlines())]
return trees
def read_tree(self, line):
parents = map(int,line.split())
trees = dict()
root = None
for i in xrange(1,len(parents)+1):
#if not trees[i-1] and parents[i-1]!=-1:
if i-1 not in trees.keys() and parents[i-1]!=-1:
idx = i
prev = None
while True:
parent = parents[idx-1]
if parent == -1:
break
tree = Tree()
if prev is not None:
tree.add_child(prev)
trees[idx-1] = tree
tree.idx = idx-1
#if trees[parent-1] is not None:
if parent-1 in trees.keys():
trees[parent-1].add_child(tree)
break
elif parent==0:
root = tree
break
else:
prev = tree
idx = parent
return root
def read_labels(self, filename):
with open(filename,'r') as f:
labels = map(lambda x: float(x), f.readlines())
labels = torch.Tensor(labels)
return labels
class WebKbbDataset(data.Dataset):
def __init__(self, vocab, num_classes,data_dir,label_dir):
super(WebKbbDataset, self).__init__()
self.vocab = vocab
self.num_classes = num_classes
#self.word_set=[line.strip() for line in open('../data/vocab.txt','r',encoding='latin-1').readlines()]
#self.tsentences = self.read_sentences(os.path.join(path,'a.toks'))
self.labels = self.read_labels(label_dir)
self.texts = self.read_texts(data_dir)
#self.trees = self.read_trees(os.path.join(path,'parents.txt'))
self.size = self.labels.size(0)
def __len__(self):
return self.size
def __getitem__(self, index):
#tree = deepcopy(self.trees[index])
text = deepcopy(self.texts[index])
label = deepcopy(self.labels[index])
#return (tree,text,label)
return (text,label)
def read_texts(self, filename):
with open(filename,'r',encoding='latin-1') as f:
texts = [self.read_text(line) for line in tqdm(f.readlines())]
return texts
def read_text(self, line):
blocks=line.strip().split('|||')
blocks=[block.split(' ') for block in blocks]
indices=[]
for block in blocks:
idx=self.vocab.convertToIdx(block, Constants.UNK_WORD)
indices.append(Var(torch.LongTensor(idx)))
return indices
def read_trees(self, filename):
with open(filename,'r') as f:
trees = [self.read_tree(line) for line in tqdm(f.readlines())]
return trees
def read_tree(self, line):
parents =list(map(int,line.split()))
trees = dict()
root = None
for i in range(1,len(parents)+1):
#if not trees[i-1] and parents[i-1]!=-1:
if i-1 not in trees.keys() and parents[i-1]!=-1:
idx = i
prev = None
while True:
parent = parents[idx-1]
if parent == -1:
break
tree = Tree()
if prev is not None:
tree.add_child(prev)
trees[idx-1] = tree
tree.idx = idx-1
#if trees[parent-1] is not None:
if parent-1 in trees.keys():
trees[parent-1].add_child(tree)
break
elif parent==0:
root = tree
break
else:
prev = tree
idx = parent
return root
def read_labels(self, filename):
with open(filename,'r') as f:
labels = list(map(lambda x: float(x), f.readlines()))
labels = torch.Tensor(labels)
return labels