forked from jadore801120/attention-is-all-you-need-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
matts_preprocess.py
182 lines (152 loc) · 6.25 KB
/
matts_preprocess.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
import csv
import argparse
import re
import os
import string
import json
from tqdm import tqdm
SEPERATOR_TOKEN = "<s>"
def get_history_response(dialogue):
sentences = [" ".join(d) for d in dialogue]
hisory = " <s> ".join(sentences[:-1])
hisory = "<cls> " + hisory
return hisory, "<cls> " +sentences[-1]
def get_line(soup):
non_tokens = {'"', '\"', '\'', '\' ', '-'}
line = list()
for token in soup.find_all("w"):
valid = 1
text = token.text
text = text.lower()
if text == 're': text = 'are'
if text == 's': text = 'is'
if text == 'm': text = 'am'
if text == 't': text = 'not'
if text == 've': text = 'have'
if text == 'il': text = 'will'
text = text.replace('n\'', '')
text = text.replace('\' ', '')
text = text.replace('\'', '')
text = text.replace('"', '')
text = text.replace('\"', '')
if text.isdigit():
line.append("<NUM>")
continue
if not text in non_tokens:
line.append(text)
return line
def create_dialogues(filename, max_len):
"""
partitions the lines of the movie into dialogues based on the time
the lines happen, and the maximum length
:param filename:
:param max_len(int): the maximum number of tokens in the history
:param max_time_interval(int): the maximum time between lines in a dialogue
:param movie_id(int): the id of the movie
:return:
List: [(movie id, history, response)]
"""
import pdb; pdb.set_trace()
chat_id = 0
history = list()
chat_ids = list()
response = list()
dialogue = list()
with open(filename, 'r') as f:
for line in f:
# check if this is the start of a new dialogue
if line[0] == '1':
dialogue = list()
chat_id += 1
# add current line in file to growing dialogue
dialogue.append((line.split("\t")[0][1:].strip()).split(' '))
# trim dialogue to maximum length
while (sum([len(sentence) for sentence in dialogue[:-1]]) + len(
dialogue) - 1 > max_len):
dialogue = dialogue[1:]
# for every response in the conversation, treat previous responses as history and add conversation
if len(dialogue) >= 2:
h, r = get_history_response(dialogue)
history.append(h)
response.append(r)
chat_ids.append(chat_id)
dialogue.append((line.split("\t")[1].strip()).split(' '))
while (sum([len(sentence) for sentence in dialogue[:-1]]) + len(
dialogue) - 1 > max_len):
dialogue = dialogue[1:]
h, r = get_history_response(dialogue)
history.append(h)
response.append(r)
chat_ids.append(chat_id)
return [(c, h, r) for c, h, r in zip(chat_ids, history, response)]
def main():
"""
here is the plan: for each dialogue create a history sequence of sentences
seperated by <s>. The sentences in the history must occur in a short time
span from another so they are relevant. The last sentence becomes the response
where the response must also be in the span
:return:
"""
parser = argparse.ArgumentParser()
parser.add_argument("-dataset_filename",
default="./datasets/personachat/raw",
type=str,
required=False,
help="The input data dir. Should contain the xml for the task.")
parser.add_argument("-output_dir",
default="./datasets/personachat/processed",
type=str,
required=False,
help="The output data dir.")
parser.add_argument("-type",
default="none_original",
type=str,
required=False,
help="The genres you would like to use.")
parser.add_argument("-max_history_tokens",
default=100,
type=int,
help="the maximum amout of history tokens")
parser.add_argument("-a_nice_note",
default="only dialogues 1-10",
type=str,
required=False,
help="leave a nice lil note for yourself in the future")
args = parser.parse_args()
if not os.path.exists(args.output_dir):
os.mkdir(args.output_dir)
meta_data = dict()
meta_data["args"] = dict()
for arg in vars(args):
meta_data["args"][arg] = getattr(args, arg)
train_set = create_dialogues(os.path.join(args.dataset_filename,
"train_{}.txt".format(args.type)),
args.max_history_tokens)
val_set = create_dialogues(os.path.join(args.dataset_filename,
"valid_{}.txt".format(args.type)),
args.max_history_tokens)
test_set = create_dialogues(os.path.join(args.dataset_filename,
"test_{}.txt".format(args.type)),
args.max_history_tokens)
meta_data["train_len"] = len(train_set)
meta_data["val_len"] = len(val_set)
meta_data["test_len"] = len(test_set)
with open(os.path.join(args.output_dir, "meta_data"), 'w') as fp:
json.dump(meta_data, fp, indent=4, sort_keys=True)
with open(os.path.join(args.output_dir, "train.csv"), 'w') as out:
csv_out = csv.writer(out)
csv_out.writerow(['movie_id', 'history', 'response'])
for row in train_set:
csv_out.writerow(row)
with open(os.path.join(args.output_dir, "val.csv"), 'w') as out:
csv_out = csv.writer(out)
csv_out.writerow(['movie_id', 'history', 'response'])
for row in val_set:
csv_out.writerow(row)
with open(os.path.join(args.output_dir, "test.csv"), 'w') as out:
csv_out = csv.writer(out)
csv_out.writerow(['movie_id', 'history', 'response'])
for row in test_set:
csv_out.writerow(row)
if __name__ == '__main__':
main()