-
Notifications
You must be signed in to change notification settings - Fork 1
/
prepare_train_data.py
268 lines (242 loc) · 10.4 KB
/
prepare_train_data.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#encoding:utf-8
import os
import random
import csv
from pprint import pprint
import re
import pymongo
from pymongo import IndexModel, ASCENDING, DESCENDING
from tqdm import tqdm
from transliterate import translit
import pymorphy2
from nltk.tokenize import RegexpTokenizer
from stop_words import get_stop_words
TEST_RATIO = 0.00001
def get_content_collection():
mongo_client = pymongo.MongoClient()
db = mongo_client['tg_backup']
content_collection = db['content']
chat_id_index_name = 'chat_id'
chat_id_index = IndexModel([('chat_id', ASCENDING)], name=chat_id_index_name)
if chat_id_index_name not in content_collection.index_information():
content_collection.create_indexes([chat_id_index])
msg_date_index_name = 'date_index'
msg_date_index = IndexModel([('date', DESCENDING)], name=msg_date_index_name)
if msg_date_index_name not in content_collection.index_information():
content_collection.create_indexes([msg_date_index])
return content_collection
def prepare_text(text):
# return translit('{main}\n'.format(main=text.lower().replace('\n', ' ')), 'ru', reversed=True)
return '{main}\n'.format(main=text.lower().replace('\n', ' '))
def user_to_user(user_a, user_b, chat_id, filename_mask):
chat_id = '$' + chat_id
user_a = '$' + user_a
user_b = '$' + user_b
content_collection = get_content_collection()
coursor_a = content_collection.find({'chat_id': chat_id}).sort([('date', 1)])
coursor_b = content_collection.find({'chat_id': chat_id}).sort([('date', 1)])
next(coursor_b)
train_a = open(filename_mask + '.train.a', 'w')
train_b = open(filename_mask + '.train.b', 'w')
test_a = open(filename_mask + '.test.a', 'w')
test_b = open(filename_mask + '.test.b', 'w')
for phrase_a, phrase_b in tqdm(zip(coursor_a, coursor_b), total=(coursor_b.count() - 1)):
# Skip if no text in messages
if (not 'text' in phrase_a) or (not 'text' in phrase_b):
continue
# Skip if a is not user_a or b is not user_b
if (phrase_a['from']['id'] != user_a) or (phrase_b['from']['id'] != user_b):
# print(phrase_a['from']['id'], phrase_a['text'])
# print(phrase_b['from']['id'], phrase_b['text'])
continue
prepared_a = prepare_text(phrase_a['text'])
prepared_b = prepare_text(phrase_b['text'])
if random.uniform(0, 1) < TEST_RATIO:
# Then write to test
test_a.write(prepared_a)
test_b.write(prepared_b)
else:
# Else — write train
train_a.write(prepared_a)
train_b.write(prepared_b)
train_a.close()
train_b.close()
test_a.close()
test_b.close()
def every_to_user(user_b, chat_id, filename_mask):
chat_id = '$' + chat_id
user_b = '$' + user_b
content_collection = get_content_collection()
coursor_a = content_collection.find({'chat_id': chat_id}).sort([('date', 1)])
coursor_b = content_collection.find({'chat_id': chat_id}).sort([('date', 1)])
next(coursor_b)
train_a = open(filename_mask + '.train.a', 'w')
train_b = open(filename_mask + '.train.b', 'w')
test_a = open(filename_mask + '.test.a', 'w')
test_b = open(filename_mask + '.test.b', 'w')
for phrase_a, phrase_b in tqdm(zip(coursor_a, coursor_b), total=(coursor_b.count() - 1)):
# Skip if no text in messages
if (not 'text' in phrase_a) or (not 'text' in phrase_b):
continue
# Skip if a is not user_a or b is not user_b
if phrase_b['from']['id'] != user_b:
# print(phrase_a['from']['id'], phrase_a['text'])
# print(phrase_b['from']['id'], phrase_b['text'])
continue
prepared_a = prepare_text(phrase_a['text'])
prepared_b = prepare_text(phrase_b['text'])
if random.uniform(0, 1) < TEST_RATIO:
# Then write to test
test_a.write(prepared_a)
test_b.write(prepared_b)
else:
# Else — write train
train_a.write(prepared_a)
train_b.write(prepared_b)
train_a.close()
train_b.close()
test_a.close()
test_b.close()
def every_to_every(chat_id, filename_mask):
def prepare_and_write(f, phrase):
f.write(prepare_text(phrase))
def write_train_and_test(train_a, train_b, test_a, test_b, phrase_a, phrase_b):
if len(phrase_a) == 0 or len(phrase_b) == 0:
return
if random.uniform(0, 1) < TEST_RATIO:
# Then write to test
prepare_and_write(test_a, phrase_a)
prepare_and_write(test_b, phrase_b)
else:
# Else — write train
prepare_and_write(train_a, phrase_a)
prepare_and_write(train_b, phrase_b)
chat_id = '$' + chat_id
content_collection = get_content_collection()
coursor = content_collection.find({'chat_id': chat_id}).sort([('date', 1)])
train_a = open(filename_mask + '.train.a', 'w')
train_b = open(filename_mask + '.train.b', 'w')
test_a = open(filename_mask + '.test.a', 'w')
test_b = open(filename_mask + '.test.b', 'w')
prev_bundle_text = str()
prev_bundle_user = str()
curr_bundle_text = str()
curr_bundle_user = str()
prev_msg_text = str()
prev_msg_user = str()
prev_msg_time = 0
curr_msg_text = str()
curr_msg_user = str()
curr_msg_time = 0
for phrase in tqdm(coursor, total=(coursor.count())):
# Skip if no text in messages
if 'text' not in phrase:
continue
curr_msg_user = phrase['from']['id']
curr_msg_time = phrase['date']
curr_msg_text = phrase['text']
if curr_msg_user != prev_msg_user:
# diff users
if curr_msg_time - prev_msg_time < 3600:
# like an answer
if curr_bundle_user != prev_bundle_user:
write_train_and_test(train_a, train_b, test_a, test_b, prev_bundle_text, curr_bundle_text)
prev_bundle_user = curr_bundle_user
prev_bundle_text = curr_bundle_text
curr_bundle_text = curr_msg_text
curr_bundle_user = curr_msg_user
else:
# not like an answer
if curr_bundle_user != prev_bundle_user:
write_train_and_test(train_a, train_b, test_a, test_b, prev_bundle_text, curr_bundle_text)
prev_bundle_text = str()
prev_bundle_user = str()
curr_bundle_text = curr_msg_text
curr_bundle_user = curr_msg_user
else:
# same users
if curr_msg_time - prev_msg_time < 5 * 60:
# like same phrase, but in several messages
curr_bundle_text = '{curr} {new}'.format(curr=curr_bundle_text, new=curr_msg_text)
# curr_bundle_user = curr_user
else:
# like new message
if curr_bundle_user != prev_bundle_user:
write_train_and_test(train_a, train_b, test_a, test_b, prev_bundle_text, curr_bundle_text)
prev_bundle_text = str()
prev_bundle_user = str()
curr_bundle_text = curr_msg_text
curr_bundle_user = curr_msg_user
prev_msg_time = curr_msg_time
prev_msg_user = curr_msg_user
train_a.close()
train_b.close()
test_a.close()
test_b.close()
def all_replies(filename_mask):
def prepare_and_write(f, phrase):
f.write(prepare_text(phrase))
def write_train_and_test(train_a, train_b, test_a, test_b, phrase_a, phrase_b):
if len(phrase_a) == 0 or len(phrase_b) == 0:
return
if random.uniform(0, 1) < TEST_RATIO:
# Then write to test
prepare_and_write(test_a, phrase_a)
prepare_and_write(test_b, phrase_b)
else:
# Else — write train
prepare_and_write(train_a, phrase_a)
prepare_and_write(train_b, phrase_b)
content_collection = get_content_collection()
coursor = content_collection.find({
'reply_id': {'$exists': True},
'$or': [
{'$and': [
{'media.caption': {'$ne': ''}},
{'media.caption': {'$exists': True}}
]},
{'text': {'$exists': True}}
]
})
train_a = open(filename_mask + '.train.a', 'w')
train_b = open(filename_mask + '.train.b', 'w')
test_a = open(filename_mask + '.test.a', 'w')
test_b = open(filename_mask + '.test.b', 'w')
for phrase in tqdm(coursor, total=(coursor.count()), smoothing=0.01):
# Skip if no text in messages
if 'text' not in phrase:
continue
# Or if no repyl
if 'reply_id' not in phrase:
continue
# Get parent message
parent_message = content_collection.find_one({'_id': phrase['reply_id']})
# If nothing found – skip it
if parent_message is None:
continue
# If there is no explicit text in parent – skip it
if 'text' not in parent_message:
continue
# Ok, there is text in current and parent messages
# Looks like time to write it
write_train_and_test(train_a, train_b, test_a, test_b, parent_message['text'], phrase['text'])
train_a.close()
train_b.close()
test_a.close()
test_b.close()
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='Prepare data for chatbot training.')
parser.add_argument('--chat_id', type=str, default='no_chat_id')
parser.add_argument('--user_a', type=str)
parser.add_argument('--user_b', type=str)
parser.add_argument('--file', type=str)
args = parser.parse_args()
# user_to_user(args.user_a, args.user_b, args.chat_id, args.file)
# python prepare_train_data.py --chat_id 0100000099283b0542dd103c53c7a30c --user_a 01000000e4b71c00209bb3c1ac9e213c --user_b 0100000099283b0542dd103c53c7a30c --file tmp/tr
# every_to_user(args.user_b, args.chat_id, args.file)
# python prepare_train_data.py --chat_id 02000000a072bb000000000000000000 --user_a 0 --user_b 0100000006af22038ee504dc096c8596 --file tmp_chpok/
# every_to_every(args.chat_id, args.file)
# python prepare_train_data.py --chat_id 05000000ef461b4143b2772dd6c0a522 --user_a 0 --user_b 0 --file tmp_utro/
all_replies(args.file)
# python prepare_train_data.py --chat_id 05000000ef461b4143b2772dd6c0a522 --user_a 0 --user_b 0 --file tmp_utro/