-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdata_converter.py
160 lines (140 loc) · 5.4 KB
/
data_converter.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
from utils import *
from transformers import *
from argparse import ArgumentParser
def xml_to_conll(xml_dir, conll_dir, doc_level, is_raw, segmenter, tokenizer):
train_scale = 1.0
with_dct = True
xml_list = [os.path.join(xml_dir, file) for file in sorted(os.listdir(xml_dir)) if file.endswith(".xml")]
print(f"total files: {len(xml_list)}")
if not os.path.exists(conll_dir):
os.makedirs(conll_dir)
if not is_raw:
batch_convert_document_to_conll(
xml_list,
os.path.join(
conll_dir,
f"single.conll"
),
sent_tag=True,
contains_modality=True,
with_dct=with_dct,
is_raw=is_raw,
morph_analyzer_name=segmenter,
bert_tokenizer=tokenizer,
is_document=doc_level
)
else:
for dir_file in xml_list:
file_name = dir_file.split('/')[-1].rsplit('.', 1)[0]
single_convert_document_to_conll(
dir_file,
os.path.join(
conll_dir,
f"{file_name}.conll"
),
sent_tag=True,
contains_modality=True,
with_dct=with_dct,
is_raw=is_raw,
morph_analyzer_name=segmenter,
bert_tokenizer=bert_tokenizer,
is_document=doc_level
)
def cross_validation(in_dir, out_dir, doc_level, is_raw, segmenter, cv_num, tokenizer):
train_scale = 1.0
with_dct = True
cv_data_split = doc_kfold(in_dir, train_scale=train_scale, cv=cv_num)
if not os.path.exists(out_dir):
os.makedirs(out_dir)
for cv_id, (train_files, dev_files, test_files) in enumerate(cv_data_split):
print(len(train_files), len(dev_files), len(test_files))
batch_convert_document_to_conll(
train_files,
os.path.join(
out_dir,
f"cv{cv_id}_train.conll"
),
sent_tag=True,
contains_modality=True,
with_dct=with_dct,
is_raw=is_raw,
morph_analyzer_name=segmenter,
bert_tokenizer=tokenizer,
is_document=doc_level
)
batch_convert_document_to_conll(
dev_files,
os.path.join(
out_dir,
f"cv{cv_id}_dev.conll"
),
sent_tag=True,
contains_modality=True,
with_dct=with_dct,
is_raw=False,
morph_analyzer_name=segmenter,
bert_tokenizer=tokenizer,
is_document=doc_level
)
batch_convert_document_to_conll(
test_files,
os.path.join(
out_dir,
f"cv{cv_id}_test.conll"
),
sent_tag=True,
contains_modality=True,
with_dct=with_dct,
is_raw=False,
morph_analyzer_name=segmenter,
bert_tokenizer=tokenizer,
is_document=doc_level
)
def conll_to_xml(conll_dir, xml_dir):
conll_list = [os.path.join(conll_dir, file) for file in sorted(os.listdir(conll_dir)) if file.endswith(".conll")]
print(f"total files: {len(conll_list)}")
if not os.path.exists(xml_dir):
os.makedirs(xml_dir)
for dir_conll in conll_list:
file_name = dir_conll.split('/')[-1].rsplit('.', 1)[0]
xml_out = os.path.join(xml_dir, f"{file_name}.xml")
doc_conll = data_objects.MultiheadConll(dir_conll)
doc_conll.doc_to_xml(xml_out)
parser = ArgumentParser(description='Convert xml to conll for training')
parser.add_argument("--mode", dest="mode",
help="convert_mode, xml2conll or conll2xml", metavar="CONVERT_MODE")
parser.add_argument("--xml", dest="xml_dir",
help="input xml dir")
parser.add_argument("--conll", dest="conll_dir",
help="output conll dir")
parser.add_argument("--doc_level",
action='store_true',
help="document-level extraction or sentence-level extraction")
parser.add_argument("--is_raw",
action='store_true',
help="whether the input xml is raw or annotated")
parser.add_argument("--cv_num", default=5, type=int,
help="k-fold cross-validation, 0 presents not to split data")
parser.add_argument("--segmenter", default='mecab', type=str,
help="segmenter: mecab (w/ MeCab package) and jumanpp (w/ pyknp package) ")
parser.add_argument("--bert_dir", type=str,
help="BERT dir for initializing tokenizer")
args = parser.parse_args()
if args.mode in ['xml2conll']:
bert_tokenizer = BertTokenizer.from_pretrained(
args.bert_dir,
do_lower_case=False,
do_basic_tokenize=False,
tokenize_chinese_chars=False
)
bert_tokenizer.add_tokens(['[JASP]'])
if args.cv_num == 0:
xml_to_conll(args.xml_dir, args.conll_dir, args.doc_level, args.is_raw, args.segmenter, bert_tokenizer)
elif args.cv_num > 0:
cross_validation(args.xml_dir, args.conll_dir, args.doc_level, args.is_raw, args.segmenter, args.cv_num, bert_tokenizer)
else:
raise Exception(f"Incorrect cv number {args.cv_num}...")
elif args.mode in ['conll2xml']:
conll_to_xml(args.conll_dir, args.xml_dir)
else:
raise Exception(f"Unknown converting mode {args.mode}...")