-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsentence_reader.py
370 lines (301 loc) · 12.6 KB
/
sentence_reader.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import os
import logging
from typing import List, TextIO
from utils import count_lines
from tqdm import tqdm
def count_number_of_sentences(input_path: str) -> int:
if not os.path.exists(input_path):
return -1
else:
num_sentences = 0
with open(input_path, "r", encoding="utf8") as file:
s, _, _ = get_sentence(file)
while s:
num_sentences += 1
s, _, _ = get_sentence(file)
return num_sentences
def get_sentence(file: TextIO) -> (List[str], List[List[int]], List[str]):
sentence: List[str] = []
tags: List[List[int]] = []
tags_types: List[str] = []
current_tag: List[int] = []
line: str = file.readline().rstrip().strip()
while line:
word: str
tag: str
try:
word, tag = line.split()
except ValueError:
try:
word, tag, q = line.split()
except ValueError:
raise ValueError(f"Error splitting line: {line}")
if tag == "O":
if len(current_tag) > 0:
tags.append(current_tag)
current_tag = []
elif tag.startswith("B"):
if len(current_tag) > 0:
tags.append(current_tag)
current_tag = []
current_tag.append(len(sentence))
try:
_, t = tag.split("-")
except ValueError:
raise ValueError(f"Unable to split tag: {tag} from line: {line}")
tags_types.append(t)
elif tag.startswith("I"):
current_tag.append(len(sentence))
else:
raise ValueError(f"Invalid tag. Word: {word}. Tag: {tag}")
sentence.append(word)
line = file.readline().rstrip().strip()
if len(current_tag) > 0:
tags.append(current_tag)
return sentence, tags, tags_types
def get_line(file: TextIO) -> (List[str], str):
# Multiple whitespace 2 whitespace and split
target_sentence_txt = " ".join(file.readline().rstrip().strip().split())
return target_sentence_txt.split(), target_sentence_txt
class SentenceReader:
source_file: TextIO
target_file: TextIO
output_path: str
sentence_no: int
source_sentence: List[str]
source_sentence_tags: List[List[int]]
target_sentence: List[str]
target_sentence_txt: str
target_sentence_tags: List[List[int]]
target_sentence_tags_types: List[str]
target_sentence_tags_quality: List[str]
current_tag: int
total_sentences: int
pbar: tqdm
def __init__(self, source_dataset: str, target_sentences: str, output_dataset: str):
self.output_path = output_dataset
self.total_sentences = count_number_of_sentences(source_dataset)
total_translated_sentences = count_lines(target_sentences)
if not os.path.exists(os.path.dirname(output_dataset)):
os.makedirs(os.path.dirname(output_dataset))
assert self.total_sentences == total_translated_sentences, (
f"Number of sentences in the source and target datasets must be the same. "
f"Number of lines in the source dataset: {self.total_sentences}. "
f"Number of lines in the target dataset: {total_translated_sentences}."
)
self.sentence_no = count_number_of_sentences(output_dataset)
assert self.sentence_no < self.total_sentences, (
f"There are more sentences in the output dataset that the "
f"number of sentences in the source dataset!!! Unable to restore "
f"from checkpoint, are you sure that you are using the correct"
f"dataset paths?"
)
self.source_file = open(source_dataset, "r", encoding="utf8")
self.target_file = open(target_sentences, "r", encoding="utf8")
if self.sentence_no >= 1:
logging.info(
"Output dataset found, we will restore from the last annotated sentence"
)
current_sentence: int = 0
while current_sentence < self.sentence_no:
while self.source_file.readline().rstrip().strip():
pass
next(self.target_file)
current_sentence += 1
self.pbar = tqdm(total=self.total_sentences - self.sentence_no)
(
self.source_sentence,
self.source_sentence_tags,
self.source_sentence_tags_types,
) = get_sentence(self.source_file)
self.current_tag = 0
self.target_sentence, self.target_sentence_txt = get_line(self.target_file)
self.target_sentence_tags = []
self.target_sentence_tags_types = []
self.target_sentence_tags_quality = []
while len(self.source_sentence_tags) == 0:
if not self.source_sentence:
break
self.write_sentence()
self.get_next_sentence()
self.skip_sentences()
def format_app(self):
source_sentence_start = " ".join(
self.source_sentence[: self.source_sentence_tags[self.current_tag][0]]
)
source_sentence_tag = " ".join(
self.source_sentence[
self.source_sentence_tags[self.current_tag][
0
] : self.source_sentence_tags[self.current_tag][-1]
+ 1
]
)
source_sentence_end = " ".join(
self.source_sentence[self.source_sentence_tags[self.current_tag][-1] + 1 :]
)
source_sentence_txt = (
source_sentence_start
+ " [ "
+ source_sentence_tag
+ " ] "
+ source_sentence_end
)
return (
source_sentence_txt,
self.target_sentence_txt,
len(source_sentence_start) + 1,
len(source_sentence_start) + len(source_sentence_tag) + 5,
)
def get_next_tag(self):
if self.current_tag + 1 >= len(self.source_sentence_tags):
self.write_sentence()
return self.get_next_sentence()
self.current_tag += 1
def skip_sentences(self):
# SKIP SENTENCE IF NO LABELS
if (
len(self.source_sentence_tags) == 0
and self.source_sentence
and self.target_sentence
):
self.write_sentence()
self.get_next_sentence()
# SKIP SENTENCE IF ALL THE SENTENCE IS A LABELLED SEQUENCE
if (
len(self.source_sentence_tags) == 1
and self.source_sentence_tags[0][0] == 0
and self.source_sentence_tags[0][-1] == len(self.source_sentence) - 1
):
label = self.source_sentence_tags_types[0]
self.target_sentence_tags_types.append(label)
self.target_sentence_tags_quality.append("HighQuality")
self.target_sentence_tags.append(list(range(len(self.target_sentence))))
self.write_sentence()
self.get_next_sentence()
def get_next_sentence(self):
self.pbar.update(1)
self.sentence_no += 1
(
self.source_sentence,
self.source_sentence_tags,
self.source_sentence_tags_types,
) = get_sentence(self.source_file)
self.current_tag = 0
self.target_sentence, self.target_sentence_txt = get_line(self.target_file)
self.target_sentence_tags = []
self.target_sentence_tags_types = []
self.target_sentence_tags_quality = []
self.skip_sentences()
def write_sentence(self):
with open(self.output_path, "a+", encoding="utf8") as output_file:
flat_target = [
item for sublist in self.target_sentence_tags for item in sublist
]
if len(flat_target) != len(set(flat_target)):
raise ValueError(
f"Collision between tags, entity overlap not allowed. "
f"A word cannot belong to more than one entity. "
f"target_sentence_tags: {self.target_sentence_tags}"
)
target_tags = ["O\t-"] * len(self.target_sentence)
for tag_indexes, target_type, target_quality in zip(
self.target_sentence_tags,
self.target_sentence_tags_types,
self.target_sentence_tags_quality,
):
first = True
for i in sorted(tag_indexes):
if first:
target_tags[i] = f"B-{target_type}\t{target_quality}"
first = False
else:
target_tags[i] = f"I-{target_type}\t{target_quality}"
print(
"\n".join(
[
f"{word}\t{tag}"
for word, tag in zip(self.target_sentence, target_tags)
]
)
+ "\n",
file=output_file,
)
def step(self, start_index: int, end_index: int, tag_quality: str):
if start_index != -1 and end_index != -1:
assert start_index < end_index, (
f"Start index should be < than end index. "
f"start_index: {start_index} "
f"end_index: {end_index}"
)
assert tag_quality in ["HighQuality", "LowQuality"]
# Get words
start_word = -1
end_word = -1
num_characters = 0
# Remove whitespaces from start_index
if start_index < 0:
start_index = 0
while self.target_sentence_txt[start_index] == " ":
start_index += 1
# Remove whitespaces from end_index
end_index -= (
1 # The cursor is always 1 postion after the last selected word
)
if end_index >= len(self.target_sentence_txt):
end_index = len(self.target_sentence_txt) - 1
while self.target_sentence_txt[end_index] == " ":
end_index -= 1
# Find span
for word_no, word in enumerate(self.target_sentence):
if num_characters <= start_index <= num_characters + len(word):
if start_word >= 0:
raise ValueError(
"Error getting selected word.\n"
f"target_sentence: {self.target_sentence}\n"
f"word: {word}\n"
f"word_no: {word_no}\n"
f"start_index*: {start_index}\n"
f"end_index: {end_index}\n"
f"start_word: {start_word}\n"
f"end_word: {end_word}\n"
f"num_characters: {num_characters}\n"
)
start_word = word_no
if num_characters <= end_index <= num_characters + len(word):
if end_word >= 0:
raise ValueError(
"Error getting selected word.\n"
f"target_sentence: {self.target_sentence}\n"
f"word: {word}\n"
f"word_no: {word_no}\n"
f"start_index: {start_index}\n"
f"end_index*: {end_index}\n"
f"start_word: {start_word}\n"
f"end_word: {end_word}\n"
f"num_characters: {num_characters}\n"
)
end_word = word_no
num_characters += len(word) + 1
assert (end_word >= 0 and start_word >= 0) and (start_word <= end_word), (
"Error getting selected word.\n"
f"target_sentence: {self.target_sentence}\n"
f"start_index: {start_index}\n"
f"end_index: {end_index}\n"
f"start_word: {start_word}\n"
f"end_word: {end_word}\n"
)
self.target_sentence_tags.append(list(range(start_word, end_word + 1)))
self.target_sentence_tags_types.append(
self.source_sentence_tags_types[self.current_tag]
)
self.target_sentence_tags_quality.append(tag_quality)
# print(f"target_sentence_tags: {self.target_sentence_tags}")
# print(f"target_sentence_tags_types: {self.target_sentence_tags_types}")
# print(f"target_sentence_tags_quality: {self.target_sentence_tags_quality}")
self.get_next_tag()
return (
self.format_app()
if self.source_sentence and self.target_sentence
else (None, None, None, None)
)