-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathdata_stream.py
149 lines (115 loc) · 3.6 KB
/
data_stream.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
from __future__ import print_function
import re
import numpy as np
import json
import re
import pickle
from nltk.tokenize import RegexpTokenizer
tokenizer = RegexpTokenizer(r'\w+')
def find_all(a_str, sub):
start = 0
while True:
start = a_str.find(sub, start)
if start == -1: return
yield start
start += len(sub) # use start += 1 to find overlapping matches
def get_batches(inpath=None,word_vocab = None, char_vocab = None, batch_size = 60,
isShuffle=False, isLoop=False, isSort=True, max_char_per_word=10, max_sent_length=200):
with open('train-v1.1.json', 'rb') as f:
dataset = json.load(f)
for article in dataset['data']:
for para in article['paragraphs']:
para['context'] = para['context'].replace(u'\u000A', '')
para['context'] = para['context'].replace(u'\u00A0', ' ')
try:
context = para['context'].encode('utf-8').lower()
context_wp = re.sub(r'[^\w\s]','',context)
except:
continue
questions = []
passages = []
starts = []
stops = []
for qa in para['qas']:
for answer in qa['answers']:
answer['text'] = answer['text'].replace(u'\u00A0', ' ')
text = answer['text']
try:
text = text.encode('utf-8').lower()
text_wp = re.sub(r'[^\w\s]','',text)
except:
continue
if(text_wp in context_wp):
a_start = context_wp.find(text_wp)
a_stop = context_wp.find(text_wp) + len(text_wp)
else:
continue
questions.append(get_vectors_q(qa['question']))
passages.append(get_vectors(context_wp))
starts.append(a_start)
stops.append(a_stop)
yield [questions,passages,starts,stops]
def get_vectors_q(string):
words = tokenizer.tokenize(string)
z = [0]*300
if(len(words)>=15):
return get_vectors(words[:15])
else:
vect = list(get_vectors(words))
for i in range(15-len(words)):
vect.append(np.array(z))
return np.asarray(vect)
def get_vectors(words):
embedding = load_embedding()
z = [0]*300
zeros = np.array(z)
vect = []
for word in words:
try:
vect.append(np.array(embedding[word]))
except:
vect.append(np.array(zeros))
return np.asarray(vect)
def load_embedding():
with open('embedding.pkl', 'r') as f:
embedding = pickle.load(f)
return embedding
"""
def get_batches(batch_size):
questions , passages,starts,stops , vocab_size = prepare_data()
length = len(questions)
for i in range(0:int(length//batch_size)):
yield questions[i*batch_size : (i+1)*batch_size] , passages[i*batch_size : (i+1)*batch_size],starts[i*batch_size : (i+1)*batch_size], stops[i*batch_size : (i+1)*batch_size]
if context[answer_start : answer_start + len(text)] == text:
if text.lstrip() == text:
pass
else:
answer_start += len(text) - len(text.lstrip())
answer['answer_start'] = answer_start
text = text.lstrip()
answer['text'] = text
else:
text = text.lstrip()
answer['text'] = text
starts = list(find_all(context, text))
if len(starts) == 1:
answer_start = starts[0]
elif len(starts) > 1:
new_answer_start = min(starts, key=lambda s: abs(s - answer_start))
loc_diffs.append(abs(new_answer_start - answer_start))
answer_start = new_answer_start
else:
continue # CHECK THIS
answer['answer_start'] = answer_start
with open('', 'r') as f:
data = json.load(f)
i=0
for article in data['data']:
for para in article['paragraphs']:
for k in para['qas']:
questions.append(k['question'])
answers.append(k['answers'][0]['text'])
start_index.append(k['answers'][0]['answer_start'])
passages.append(para['context'])
print(h.split_doc(para['context']))
"""