-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdata_utils.py
122 lines (86 loc) · 3.29 KB
/
data_utils.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
import json
import copy
class InputExample:
def __init__(self, src_tokens, tgt_tokens, ged_tags):
self.src_tokens = src_tokens
self.tgt_tokens = tgt_tokens
self.ged_tags = ged_tags
def __repr__(self):
return str(self.to_json_str())
def to_json_str(self):
return json.dumps(self.to_dict(), indent=2, ensure_ascii=False)
def to_dict(self):
output = copy.deepcopy(self.__dict__)
return output
class Dataset:
def __init__(self, raw_data_path):
self.examples = self.create_examples(raw_data_path)
def create_examples(self, raw_data_path):
examples = []
src_tokens = []
tgt_tokens = []
ged_tags = []
with open(raw_data_path) as f:
for i, line in enumerate(f.readlines()[1:]):
line = line.split('\t')
if len(line) == 3:
src, tgt, tag = line
# exclude insertions
if 'INSERT' in tag:
continue
src_tokens.append(src.strip())
tgt_tokens.append(tgt.strip())
ged_tags.append(tag.strip())
else:
examples.append(InputExample(src_tokens=src_tokens,
tgt_tokens=tgt_tokens,
ged_tags=ged_tags)
)
src_tokens, tgt_tokens, ged_tags = [], [], []
if src_tokens and tgt_tokens and ged_tags:
examples.append(InputExample(src_tokens=src_tokens,
tgt_tokens=src_tokens,
ged_tags=ged_tags)
)
return examples
def __getitem__(self, idx):
return self.examples[idx]
def __len__(self):
return len(self.examples)
def postprocess_src_ged(src_tokens, ged_tags):
"""
1) Replaces out-of-set tag combinations with UNK.
2) Projects a tag on a span of tokens.
This is important because we need to deal with a single source
token at a time.
"""
tag_combs = [
'REPLACE_OH+REPLACE_OM',
'REPLACE_OH+REPLACE_OT',
'REPLACE_OD+REPLACE_OR',
'REPLACE_OD+REPLACE_OG',
'REPLACE_XC+REPLACE_XN',
'REPLACE_OA+REPLACE_OH',
'REPLACE_OM+REPLACE_OR',
'REPLACE_OH+REPLACE_XC',
'REPLACE_OD+REPLACE_OH',
'REPLACE_XC+REPLACE_XG',
'REPLACE_MI+REPLACE_OH',
'REPLACE_OA+REPLACE_OR',
'REPLACE_OR+REPLACE_OT',
'REPLACE_OD+REPLACE_OM'
]
assert len(src_tokens) == len(ged_tags)
ged_tags_ = []
src_tokens_ = []
for token, tag in zip(src_tokens, ged_tags):
if ('+' in tag and tag not in tag_combs) or (tag == 'UNK'):
tag = 'UNK'
words, tags = project_span(token, tag)
src_tokens_ += words
ged_tags_ += tags
return src_tokens_, ged_tags_
def project_span(word, tag):
if 'MERGE' not in tag:
return word.split(), [tag for _ in range(len(word.split()))]
return word.split(), ['MERGE-B']+['MERGE-I' for _ in range(len(word.split()) - 1)]