-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_loader.py
131 lines (79 loc) · 2.76 KB
/
data_loader.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
import os
import nltk
import numpy as np
import tensorflow as tf
import pandas as pd
def load_dataset(dataset_path_train,word2idx):
with open(dataset_path_train,'r') as file1:
sentence_one = []
sentence_two = []
y_true = []
max1 = 0
max2 = 0
for index, line in enumerate(file1):
if index == 0:
continue
s_1 = []
s_2 = []
values = line.split("\t")
#Sentence 1
words = nltk.word_tokenize(values[3])
words = [word.lower() for word in words]
s_1.extend([word2idx.get(word,word2idx['UNK']) for word in words])
#Sentence 2
# words = values[4].split(" ")
words = nltk.word_tokenize(values[4])
words = [word.lower() for word in words]
s_2.extend([word2idx.get(word,word2idx['UNK']) for word in words])
if len(s_1) > max1:
max1 = len(s_1)
if len(s_2) > max2:
max2 = len(s_2)
y_true.append(np.asarray(values[0]))
sentence_one.append(np.pad(s_1,(0,41-len(s_1)),'constant',constant_values=(0)))
sentence_two.append(np.pad(s_2,(0,41-len(s_2)),'constant',constant_values=(0)))
# self.sentence_one.append(np.asarray(s_1[0:self.sen_len]))
# self.sentence_two.append(np.asarray(s_2[0:self.sen_len]))
sentence_one = np.stack(sentence_one)
sentence_two = np.stack(sentence_two)
y_true = np.stack(y_true)
#print self.weights
print "Max_train:",max1,max2
print sentence_one.shape,sentence_two.shape,y_true.shape
return sentence_one,sentence_two,y_true
def load_dataset_test(dataset_path_test,word2idx):
with open(dataset_path_test,'r') as file1:
sentence_one_test = []
sentence_two_test = []
y_true_test = []
max1 = 0
max2 = 0
for index, line in enumerate(file1):
if index == 0:
continue
s_1 = []
s_2 = []
values = line.split("\t")
#Sentence 1
# words = values[3].split(" ")
words = nltk.word_tokenize(values[3])
words = [word.lower() for word in words]
s_1.extend([word2idx.get(word,word2idx['UNK']) for word in words])
#Sentence 2
# words = values[4].split(" ")
words = nltk.word_tokenize(values[4])
words = [word.lower() for word in words]
s_2.extend([word2idx.get(word,word2idx['UNK']) for word in words])
if len(s_1) > max1:
max1 = len(s_1)
if len(s_2) > max2:
max2 = len(s_2)
y_true_test.append(np.asarray(values[0]))
sentence_one_test.append(np.pad(s_1,(0,41-len(s_1)),'constant',constant_values=(0)))
sentence_two_test.append(np.pad(s_2,(0,41-len(s_2)),'constant',constant_values=(0)))
print "Max_test:",max1,max2
sentence_one_test = np.stack(sentence_one_test)
sentence_two_test = np.stack(sentence_two_test)
y_true_test = np.stack(y_true_test)
print sentence_one_test.shape,sentence_two_test.shape,y_true_test.shape
return sentence_one_test,sentence_two_test,y_true_test