-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproxydata.py
317 lines (251 loc) · 11.4 KB
/
proxydata.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import json
import os
import spacy
import random
import csv
def read_UD(file):
sentences = []
with open(file, "r") as input:
lines = input.readlines()
for line in lines:
if line.startswith("# text"):
clean_line = line[9:].strip()
sentences.append(clean_line)
return sentences
def read_NaijaUD(src_dir):
naija_sentences = []
english_sentences = []
filenames = [f for f in os.listdir(src_dir) if f.endswith(".conllu")]
for f in filenames:
with open(os.path.join(src_dir, f), "r") as input:
lines = input.readlines()
for line in lines:
if line.startswith("# text_ortho"):
clean_line = line[15:].strip() # take off '# text_ortho = '
naija_sentences.append(clean_line)
if line.startswith("# text_en"):
clean_line = line[12:].strip() # take off '# text_en = '
english_sentences.append(clean_line)
return naija_sentences, english_sentences
def read_SinglishUD(full_path):
sentences = []
with open(full_path, "r") as indata:
lines = indata.readlines()
stack = []
for line in lines:
if line != "\n":
elems = line.split("\t")
token = elems[1]
stack.append(token.strip())
if line == "\n":
sent = " ".join(stack)
sentences.append(sent)
stack = []
return sentences
def read_HaitianExtra(full_path):
sentences = []
# evaluating haitian datasets sepperately ... >_>
with open(full_path, "r") as indata:
lines = indata.readlines()
for line in lines: # clean out examples length 1?
if len(line.split(" ")) > 1:
sentences.append(line.strip("\n").strip()) # already in sentences
return sentences
def split_sents(base_language, text_lines):
"""
:param base_language: "en" or "fr" for English or French
:param text_lines: list['sentences', 'sentences']
:return: list['sent', 'sent', 'sent']
"""
sentences = []
if base_language == "en":
nlp = spacy.load("en_core_web_sm") #spacy.load("en_cor_web_sm")
for text in text_lines:
doc = nlp(text.strip())
[sentences.append(s.text.strip()) for s in doc.sents]
else:
nlp = spacy.load("xx_sent_ud_sm")
for text in text_lines:
text = text.strip()
text = text.replace('\n', '')
doc = nlp(text)
[sentences.append(s.text) for s in doc.sents]
return sentences
def load_news(full_path):
"""
for reading "news.LANG" WMT2020 files
"""
with open(full_path, "r") as infile:
base_language = full_path[-2:]
data = infile.readlines()
sentences = split_sents(base_language, data)
return sentences
def readJson(file, lang):
sentences = []
with open(file, "r") as injson:
data = json.load(injson, encoding="utf-8")
for lil_d in data:
for sent, label in lil_d.items():
if label == lang:
sentences.append(sent)
return sentences
def makeVocab(list_of_sents, language="en"):
vocab = set()
vocab.add("<unk>")
"""
#This is the better way to build the vocab, but the proxy-a-distance code just splits on white space
if language in ["haitian", "naija", "singlish"]:
nlp = spacy.load("xx_sent_ud_sm")
elif language == "en":
nlp = spacy.load("en_core_web_sm") # spacy.load("en_cor_web_sm")
elif language == "fr":
nlp = spacy.load("fr_core_news_sm")
else:
print("Youve supplied a language that is not supported by the tokenizer")
raise NotImplementedError
tokenizer = nlp.tokenizer
for sent in list_of_sents:
tokens = tokenizer(sent)
[vocab.add(t.text) for t in tokens if t.text!= " "]
"""
for sent in list_of_sents:
words = sent.split(" ")
[vocab.add(w.strip()) for w in words if w != " "]
return list(vocab)
def output_file(list_of_stuff, filename):
out_dir = "/Users/plq360/Desktop/data/creoledata/proxydata"
with open(os.path.join(out_dir, filename), "w") as output:
for i, stuff in enumerate(list_of_stuff):
if stuff != '':
if i+1 != len(list_of_stuff):
output.write(f"{stuff}\n")
else:
output.write(f"{stuff}")
def main():
num_src = 3050
domain_3_src = [] #parallel to Haitian-1
domain_4_src = [] #parallel to Naija-2
domain_5_src = [] #parallel to Haitian-2
for language in ["naija", "singlish", "haitian", "en", "fr"]:
if language == "naija":
domain_1 = readJson("/Users/plq360/Desktop/data/creoledata/train/naija/naija_and_all.train.json", "naija")
domain_1_src = domain_1[:num_src]
domain_2_naija, domain_4_english = read_NaijaUD("/Users/plq360/Desktop/data/creoledata/eval/naija/SUD_Naija-NSC")
combo = list(zip(domain_2_naija, domain_4_english))
random.shuffle(combo)
combo_clipped = combo[:num_src]
domain_2_src = [p[0] for p in combo_clipped]
domain_4_src = [p[1] for p in combo_clipped]
all_sentences = domain_1_src + domain_2_src
print(f"len all sentences: {len(all_sentences)}")
vocab_list = makeVocab(list_of_sents=all_sentences, language="naija")
print(f"Naija d1 src [pidgin corpus]: {len(domain_1_src)}")
print(f"Naija d2 src [NUD]: {len(domain_2_src)}")
print(f"English d4 src [NUD - parallel to Naija d1 NUD]: {len(domain_4_src)}")
print(f"len vocab: {len(vocab_list)}")
output_file(domain_1_src, "naija-corpus.src")
output_file(domain_2_src, "naija-NUD.src")
output_file(domain_4_src, "english-NUD.src")
output_file(vocab_list, "naija.vocab")
if language == "singlish":
domain_1 = readJson("/Users/plq360/Desktop/data/creoledata/train/singlish/singlish_and_all.train.json", "singlish")
domain_1_src = domain_1[:num_src]
domain_2 = read_SinglishUD("/Users/plq360/Desktop/data/creoledata/eval/singlish/TALLIP19_UD_dataset/gold_pos/train.ext.conll")
random.shuffle(domain_2)
domain_2_src = domain_2[:num_src]
print(f"Singlish d1 src [SMS]: {len(domain_1_src)}")
print(f"Singlish d2 src [SUD]: {len(domain_2_src)}")
all_sentences = domain_1_src + domain_2_src
vocab_list = makeVocab(list_of_sents=all_sentences, language="singlish")
print(f"len vocab: {len(vocab_list)}")
output_file(domain_1_src, "singlish-SMS.src")
output_file(domain_2_src, "singlish-SUD.src")
output_file(vocab_list, "singlish.vocab")
if language == "haitian":
# domain_1 = readJson("/Users/plq360/Desktop/data/creoledata/train/haitian/haitian_and_all.train.json", "haitian")
# domain_1_src = domain_1[:num_src]
domain_1 = []
domain_3 = [] #PARALLEL ENGLISH DOMAIN 3
# load haitian train and dev
path_to_haitian = "/Users/plq360/Desktop/data/creoledata/train/haitian"
train_path = "disaster_response_messages_training.csv"
with open(os.path.join(path_to_haitian, train_path), "r") as csvfile:
rows = csv.reader(csvfile, delimiter=",", quotechar='"')
for r in rows:
en = r[2]
h = r[3]
if h != '':
domain_1.append(h)
if en != '':
domain_3.append(en)
domain_1_src = domain_1[:num_src]
domain_3_src = domain_3[:num_src]
domain_2 = read_HaitianExtra("/Users/plq360/Desktop/data/creoledata/eval/haitian/newswire-all.ht")
domain_5 = read_HaitianExtra("/Users/plq360/Desktop/data/creoledata/eval/haitian/newswire-all.en")
combo = list(zip(domain_2, domain_5))
random.shuffle(combo)
combo_clipped = combo[:num_src]
domain_2_src = [p[0] for p in combo_clipped] #haitian
domain_5_src = [p[1] for p in combo_clipped] #english
print(f"Haitian d1 src [emergency]: {len(domain_1_src)}")
print(f"Haitian d2 src [newswire]: {len(domain_2_src)}")
print(f"English d3 src [parallel to Haitian emergency]: {len(domain_3_src)}")
print(f"English d5 src [parallel to Haitain newswire]: {len(domain_5_src)}")
all_sentences = domain_1_src + domain_2_src
vocab_list = makeVocab(list_of_sents=all_sentences, language="haitian")
print(f"len vocab: {len(vocab_list)}")
output_file(domain_1_src, "haitian-emergency.src")
output_file(domain_2_src, "haitian-newswire.src")
output_file(domain_3_src, "english-emergency.src")
output_file(domain_5_src, "english-newswire.src")
output_file(vocab_list, "haitian.vocab")
if language == "en":
domain_1 = readJson("/Users/plq360/Desktop/data/creoledata/train/naija/naija_and_all.train.json", "en")
domain_1_src = domain_1[:num_src]
domain_2 = read_UD("/Users/plq360/Desktop/data/creoledata/train/other/en_ewt-ud-train.conllu")
random.shuffle(domain_2)
domain_2_src = domain_2[:num_src]
all_sentences = domain_1_src + domain_2_src + domain_3_src + domain_4_src + domain_5_src
vocab_list = makeVocab(list_of_sents=all_sentences, language="en")
print(f"english d1 src [wmt-news]: {len(domain_1_src)}")
print(f"english d2 src [ewt-UD]: {len(domain_2_src)}")
print(f"len vocab: {len(vocab_list)}")
output_file(domain_1_src, "english-wmt-news.src")
output_file(domain_2_src, "english-ewt-UD.src")
output_file(vocab_list, "english.vocab")
if language == "fr":
domain_1 = readJson("/Users/plq360/Desktop/data/creoledata/train/haitian/haitian_and_all.train.json", "fr")
domain_1_src = domain_1[:num_src]
domain_2 = read_UD("/Users/plq360/Desktop/data/creoledata/train/other/fr_gsd-ud-train.conllu")
random.shuffle(domain_2)
domain_2_src = domain_2[:num_src]
all_sentences = domain_1_src + domain_2_src
vocab_list = makeVocab(list_of_sents=all_sentences, language="fr")
print(f"French d1 src [wmt-news]: {len(domain_1_src)}")
print(f"French d2 src [french UD]: {len(domain_2_src)}")
print(f"len vocab: {len(vocab_list)}")
output_file(domain_1_src, "french-wmt-news.src")
output_file(domain_2_src, "french-FUD.src")
output_file(vocab_list, "french.vocab")
main()
"""
len all sentences: 6100
Naija d1 src [pidgin corpus]: 3050
Naija d2 src [NUD]: 3050
English d4 src [NUD - parallel to Naija d1 NUD]: 3050
len vocab: 13353
Singlish d1 src [SMS]: 3050
Singlish d2 src [SUD]: 3050
len vocab: 13839
Haitian d1 src [emergency]: 3050
Haitian d2 src [newswire]: 3050
English d3 src [parallel to Haitian emergency]: 3050
English d5 src [parallel to Haitain newswire]: 3050
len vocab: 20443
english d1 src [wmt-news]: 3050
english d2 src [ewt-UD]: 3050
len vocab: 38759
French d1 src [wmt-news]: 3050
French d2 src [french UD]: 3050
len vocab: 31491
"""