-
Notifications
You must be signed in to change notification settings - Fork 0
/
word_tokenized.py
188 lines (136 loc) · 4.44 KB
/
word_tokenized.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
import spacy
import pickle
import argparse
import json
import os
nlp = spacy.blank('en')
def get_fixed_length_token( string, required_len,domain, q_a):
'''
this function create fixed length of string so make perfect input for tensorflow tensor
'''
string = string.replace('?', ' ')
string = string.replace('.', ' ')
string = string.lower()
string = nlp(string)
lst = []
for token in string:
lst.append(token.text)
#this if condition apply for question where question folppw by domain
if q_a == 'q':
lst.append(domain)
if len(lst) < required_len:
pad_data = required_len - len(lst)
lst = lst + ['<PAD>']*pad_data
else:
lst = lst[:required_len]
return lst
'''
Same dictionary for QA
def create_dictionary(tokenized_list):
word2id = {}
index = 2
word2id['<PAD>'] = 0
word2id['<S>'] = 1
for data in tokenized_list:
question = data['question']
answer = data['answer']
for token in question:
if token not in list(word2id.keys()):
word2id[token] = index
index +=1
for token in answer:
if token not in list(word2id.keys()):
word2id[token] = index
index +=1
return word2id'''
# its create different dictionary for question and annswer
def create_dictionary(tokenized_list):
'''
this fuction create different vocabulary for question and answer
'''
que_word2id = {}
que_index = 2
que_word2id['<PAD>'] = 0
que_word2id['<S>'] = 1
ans_word2id = {}
ans_index = 2
ans_word2id['<PAD>'] = 0
ans_word2id['<S>'] = 1
for data in tokenized_list:
question = data['question']
for token in question:
if token not in list(que_word2id.keys()):
que_word2id[token] = que_index
que_index += 1
answer = data['answer']
for token in answer:
if token not in list(ans_word2id.keys()):
ans_word2id[token] = ans_index
ans_index +=1
return que_word2id, ans_word2id
def tokenized_data(word2id, lst):
'''
this fucntion map word with it fixed index
'''
return_list = [word2id[word] if word in list(word2id.keys()) else word2id['unk'] for word in lst]
return return_list
def run(config):
file_name = config['data_type']+'.json'
with open(file_name, 'r') as fh:
data_list = json.load(fh)
lst = []
for data in data_list:
question = data['question']
answer = data['answer']
clean_data = {}
question = get_fixed_length_token(question, config['ques_len'], data['domain'],'q')
answer = get_fixed_length_token(answer,config['ans_len'], data['domain'],'a')
clean_data['question'] = question
clean_data['answer'] = answer
clean_data['token'] = data['domain']
clean_data['question_target']= question + ['<S>'] + answer + [data['domain']]
lst.append(clean_data)
'''if os.path.isfile('que_word2id_sport.json'):
with open('que_word2id_sport.json','r') as f:
que_word2id = json.load(f)
with open('ans_word2id_sport.json','r') as f:
ans_word2id = json.load(f)
#if word not in list(ans_word2id.keys()):
# ans_word2id[word] = ans_id
print('before ',len(que_word2id))
else:'''
que_word2id, ans_word2id = create_dictionary(lst)
print('before ',len(que_word2id))
word = '_unk'
que_id = len(que_word2id)
ans_id = len(ans_word2id)
if word not in list(que_word2id.keys()):
que_word2id[word] = que_id
que_id2word = dict(zip(list(que_word2id.values()), list(que_word2id.keys())))
ans_id2word = dict(zip(list(ans_word2id.values()), list(ans_word2id.keys())))
for data in lst :
data['question_id'] = tokenized_data(que_word2id, data['question'])
data['answer_id'] = tokenized_data(ans_word2id,data['answer'])
file_name = config['data_type']+'_clean_data.json'
print(len(lst))
with open(file_name,'w') as f:
json.dump(lst, f)
if config['data_type']=='train':
with open('que_word2id.json','w') as f:
json.dump(que_word2id, f)
with open('que_id2word.json','w') as f:
json.dump(que_id2word, f)
with open('ans_word2id.json','w') as f:
json.dump(ans_word2id, f)
with open('ans_id2word.json','w') as f:
json.dump(ans_id2word, f)
if __name__ == '__main__':
PARSER = argparse.ArgumentParser("Command line arguments")
PARSER.add_argument("-t", "--data_type", default='train',
type=str, dest="data_type", help="give me file name for tokenized data")
PARSER.add_argument('-ql','--ques_len',default = 15, type= int ,
dest ='ques_len',help = 'give me maximun number of word in question')
PARSER.add_argument('-al','--ans_len',default= 15, type= int,
dest='ans_len',help = 'give me maximum number of word in answer')
FLAGS = PARSER.parse_args()
run(vars(FLAGS))