-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathload_data_for_cnn_multi.py
More file actions
213 lines (176 loc) · 7.86 KB
/
load_data_for_cnn_multi.py
File metadata and controls
213 lines (176 loc) · 7.86 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import sys
sys.path.append('..')
from utils.mongodb_client import Mongo
import os
import nlp_property
import copy
import json
from tools.word_cut import WordCutHelper
wh = WordCutHelper(1)
punctuations = ['~', '!', '(', ')', '{', '}', '【', '】', '、', ';', ':', '“', '”', '‘', '’',
'《', '》', ',', '。', '?', '~', '!', '(', ')', '{', '}', '[', ']', ';', ':',
'"', '"', '<', '>', ',', '.', '?', "'"]
def gather_data(am_data, cut_sentence, expand_punct=True):
labels = set()
data_train = dict()
data_test = dict()
n_train_sample = 0
n_test_sample = 0
for data in am_data:
questions = data['questions']
# questions_test = data['questions_test']
questions_test = data.get('questions_test', None)
intent = data['intent']
if intent:
pass
else:
intent = 'null'
entities = data['entities']
if not entities:
label = intent
elif isinstance(entities, list):
label = intent + '|' + '#'.join(entities)
else:
label = intent + '|' + entities
labels.add(label)
for question in questions:
# line = question + '##' + intent
if cut_sentence:
value = wh.getTagAndWord(question)['word']
question = ' '.join(value)
data_train[question] = {'label': [label]}
n_train_sample += 1
if expand_punct:
expanded_question = copy.deepcopy(question)
flag = False
for char in question:
if char in punctuations:
flag = True
expanded_question = expanded_question.replace(char, '')
if flag:
if cut_sentence:
value = wh.getTagAndWord(expanded_question)['word']
expanded_question = ' '.join(value)
data_train[expanded_question] = {'label': [label]}
n_train_sample += 1
if questions_test:
for question in questions_test:
if cut_sentence:
value = wh.getTagAndWord(question)['word']
question = ' '.join(value)
data_test[question] = {'label': [label]}
n_test_sample += 1
else:
pass
return data_train, data_test, labels, n_train_sample, n_test_sample
def load_scene_data_cnn(scene_id, cut_sentence=True, load_qa=False, expand_punct=True):
"""
:param scene_id:场景名称
:param cut_sentence:预先分词
:param load_qa:
:param expand_punct: 通过去标点符号来扩充语料
:return:
"""
print('downloading data {} from mongodb...'.format(scene_id))
mongodb = Mongo(ip=nlp_property.NLP_FRAMEWORK_DATA_IP, db_name=scene_id)
# mongodb_auto = Mongo(ip=nlp_property.NLP_FRAMEWORK_IP, db_name='automata')
# automata
# shangyongjieshao: query={"category": {"$nin":["&", "&1","&2", "&3","&4", "&&"]}}
query = {}
if scene_id == 'shangyongjieshao' or scene_id == 'jieshaoshangyong':
query = {"category": {"$nin": ["&", "&1", "&2", "&3", "&4", "&&"]}}
am_data = mongodb.search(collection='automata', query=query, field={
"intent": 1, "entities": 1, "questions": 1, "questions_test": 1})
frame_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# directory = context.train_data_path_prefix.format(scene_id)
data_save_dir = os.path.join(frame_dir, 'work_space', scene_id, 'dataset')
if os.path.exists(data_save_dir):
pass
else:
os.makedirs(data_save_dir)
# load data
if cut_sentence:
print('cut sentences')
data_train, data_test, labels, n_train, n_test = gather_data(am_data, cut_sentence, expand_punct=expand_punct)
if load_qa:
q_data = mongodb.search(collection='qa', field={"equal_questions": 1})
if len(q_data) > 0:
labels.add('qa')
# write_qa(data_save_dir + "/questions_train", q_data)
for data in q_data:
questions = data['equal_questions']
for question in questions:
if cut_sentence:
value = wh.getTagAndWord(question)['word']
question = ' '.join(value)
data_train[question] = {'label': ['qa']}
n_train += 1
with open(os.path.join(data_save_dir, 'questions_train.json'), 'w') as f_train:
# for data in data_train:
# f_train.write(json.dumps(data, ensure_ascii=False))
json.dump(data_train, f_train, ensure_ascii=False)
with open(os.path.join(data_save_dir, 'questions_test.json'), 'w') as f_test:
json.dump(data_test, f_test, ensure_ascii=False)
with open(os.path.join(data_save_dir, 'labels'), 'w') as f:
for label in labels:
f.write(label + '\n')
print('download completed data {} from mongodb...'.format(scene_id))
print('number of training/test data is: {}/ {} '.format(n_train, n_test))
def load_shared_data(cut_sentence=True, load_qa=False, expand_punct=True):
print('downloading data of all shared scene from mongodb')
db_automata = Mongo(ip=nlp_property.NLP_FRAMEWORK_IP, db_name='automata')
docs = db_automata.search(collection='machines', query={'shared': 'True'})
frame_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
data_save_dir = os.path.join(frame_dir, 'work_space/common/dataset')
if os.path.exists(data_save_dir):
pass
else:
os.makedirs(data_save_dir)
train_set = dict()
test_set = dict()
labels = set()
n_train = 0
n_test = 0
if cut_sentence:
print('cut sentence')
for doc in docs:
scene_id = doc['scene_id']
mongodb = Mongo(ip=nlp_property.NLP_FRAMEWORK_IP, db_name=scene_id)
am_data = mongodb.search(collection='automata',
field={"intent": 1, "entities": 1, "questions": 1})
data_train, data_test, labels_doc, num_train, num_test = gather_data(am_data, cut_sentence, expand_punct=expand_punct)
train_set.update(data_train)
test_set.update(data_test)
labels.update(labels_doc)
n_train += num_train
n_test += num_test
if load_qa:
q_data = mongodb.search(collection='qa', field={"equal_questions": 1})
if len(q_data) > 0:
labels.add('qa')
# write_qa(data_save_dir + "/questions_train", q_data)
for data in q_data:
questions = data['equal_questions']
for question in questions:
if cut_sentence:
value = wh.getTagAndWord(question)['word']
question = ' '.join(value)
train_set[question] = {'label': ['qa']}
n_train += 1
with open(os.path.join(data_save_dir, 'questions_train.json'), 'w') as f_train:
json.dump(train_set, f_train, ensure_ascii=False)
with open(os.path.join(data_save_dir, 'questions_test.json'), 'w') as f_test:
json.dump(test_set, f_test, ensure_ascii=False)
with open(os.path.join(data_save_dir, 'labels'), 'w') as f:
for label in labels:
f.write(label + '\n')
print('Download data of all shared scene from mongodb')
print('number of training/test data is: {}/ {} '.format(n_train, n_test))
if __name__ == "__main__":
# 1 下载某一场景的数据
# bookstore/suzhou_gov_centre
scene = 'banktest'
load_scene_data_cnn(scene, cut_sentence=True, load_qa=False, expand_punct=True)
# 2 下载共享场景数据(所有shared为True场景的数据),对应于common场景
# load_shared_data(cut_sentence=True)
print('Done')