-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_loader.py
executable file
·278 lines (240 loc) · 7.13 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
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
269
270
271
272
273
274
275
276
277
278
"""
Created by diesel
11/9/20
"""
import pandas as pd
import json
d = {
'movieMentions': {
'111776': 'Super Troopers (2001)',
'91481': 'Beverly Hills Cop (1984)',
'151656': 'Police Academy (1984)',
'134643': 'American Pie (1999)',
'192131': 'American Pie ',
'124771': '48 Hrs. (1982)',
'94688': 'Police Academy 2: Their First Assignment (1985)',
'101794': 'Lethal Weapon (1987)'
},
'respondentQuestions': {
'111776': {
'suggested': 0,
'seen': 1,
'liked': 1
},
'91481': {
'suggested': 1,
'seen': 2,
'liked': 2
},
'151656': {
'suggested': 1,
'seen': 0,
'liked': 1
},
'134643': {
'suggested': 0,
'seen': 1,
'liked': 1
},
'192131': {
'suggested': 0,
'seen': 1,
'liked': 1
},
'124771': {
'suggested': 1,
'seen': 2,
'liked': 2
}, '94688': {
'suggested': 1,
'seen': 0,
'liked': 1
},
'101794': {
'suggested': 1,
'seen': 0,
'liked': 2
}
},
'messages': [
{
'timeOffset': 0,
'text': 'Hi I am looking for a movie like @111776',
'senderWorkerId': 956,
'messageId': 204171
},
{
'timeOffset': 48,
'text': 'You should watch @151656',
'senderWorkerId': 957,
'messageId': 204172
},
{
'timeOffset': 90,
'text': 'Is that a great one? I have never seen it. I have seen @192131',
'senderWorkerId': 956,
'messageId': 204173
},
{
'timeOffset': 122,
'text': 'I mean @134643',
'senderWorkerId': 956,
'messageId': 204174
},
{
'timeOffset': 180,
'text': 'Yes @151656 is very funny and so is @94688',
'senderWorkerId': 957,
'messageId': 204175
},
{
'timeOffset': 199,
'text': 'It sounds like I need to check them out',
'senderWorkerId': 956,
'messageId': 204176
},
{
'timeOffset': 219,
'text': 'yes you will enjoy them',
'senderWorkerId': 957,
'messageId': 204177
},
{
'timeOffset': 253,
'text': 'I appreciate your time. I will need to check those out. Are there any others you would recommend?',
'senderWorkerId': 956,
'messageId': 204178
},
{
'timeOffset': 297,
'text': 'yes @101794',
'senderWorkerId': 957,
'messageId': 204179
},
{
'timeOffset': 311,
'text': 'Thank you i will watch that too',
'senderWorkerId': 956,
'messageId': 204180
},
{
'timeOffset': 312,
'text': 'and also @91481',
'senderWorkerId': 957,
'messageId': 204181
},
{
'timeOffset': 326,
'text': 'Thanks for the suggestions.',
'senderWorkerId': 956,
'messageId': 204182
},
{
'timeOffset': 341,
'text': 'you are welcome',
'senderWorkerId': 957,
'messageId': 204183
},
{
'timeOffset': 408,
'text': 'and also @124771',
'senderWorkerId': 957,
'messageId': 204184
},
{
'timeOffset': 518,
'text': 'thanks goodbye',
'senderWorkerId': 956,
'messageId': 204185
}
],
'conversationId': '20001',
'respondentWorkerId': 957,
'initiatorWorkerId': 956,
'initiatorQuestions': {
'111776': {
'suggested': 0, 'seen': 1, 'liked': 1},
'91481': {
'suggested': 1, 'seen': 2, 'liked': 2},
'151656': {
'suggested': 1, 'seen': 0, 'liked': 1},
'134643': {
'suggested': 0, 'seen': 1, 'liked': 1},
'192131': {
'suggested': 0, 'seen': 1, 'liked': 1},
'124771': {
'suggested': 1, 'seen': 2, 'liked': 2},
'94688': {
'suggested': 1, 'seen': 0, 'liked': 1},
'101794': {
'suggested': 0, 'seen': 2, 'liked': 2}}}
def get_messages(infile):
with open(infile, "r") as fin:
messages = []
for line in fin:
d = json.loads(line)
speaker_lookup = dict(zip(range(len("ABCDEF")), "ABCDEF"))
speakers = {}
sid = 0
for m in d["messages"]:
#assert m['senderWorkerId'] < 7
if m['senderWorkerId'] not in speakers:
speakers[m['senderWorkerId']] = speaker_lookup[sid]
sid += 1
messages.append({
"text": m["text"],
"speaker": speakers[m['senderWorkerId']]
})
return messages
class DataLoader(object):
def __init__(self, args, infile=None):
self.args = args
self.infile = infile
def load(self, infile=None):
if infile is None:
infile = self.infile
else:
self.infile = infile
messages = get_messages(infile)
return pd.DataFrame(messages)
from lexicon import Lexicon, LexBuilder
from rule_based_ner import RuleBasedNER
import file_utils as fu
import os
from collections import defaultdict
def load_ner_tagger(concept_path, lex_save_path):
df = pd.read_csv(concept_path, sep="\t")
builder = LexBuilder()
key_phrases = defaultdict(list)
for row in df.itertuples():
key_phrases[row.group.strip()].append(row.phrase.strip())
for standard_form, forms in key_phrases.items():
# entry is dictionary
builder.add_entry({
"category": "movie_genre",
"standard_form": standard_form,
"forms": forms,
"name": standard_form,
"full_name": standard_form,
})
builder.build_lexes()
#builder.save_lexes(os.path.join("./", f"{topic_name}-lex.json"))
builder.save_lexes(lex_save_path)
#print("entry keys:", builder.get_entry_key_set())
return RuleBasedNER(builder.lexicons)
def main():
infile = "redial/train_data.jsonl"
loader = DataLoader(None)
messages = loader.load(infile)
ner_tagger = load_ner_tagger("gez/genre-phrases.tsv", "gez/movie-lex.json")
mentions = []
for row in messages.itertuples():
#print("\ntext:", row.text)
toks = ner_tagger.tokenize_text(row.text)
mentions.append(json.dumps(ner_tagger.tag_tokens(toks)))
messages["genre_mentions"] = mentions
#df = pd.DataFrame(messages)
#df.to_csv("redial/train_data.messages.tsv")
messages.to_csv("redial/train-genre-mentions.tsv", sep="\t", index=False)
if __name__ == "__main__":
main()