-
Notifications
You must be signed in to change notification settings - Fork 4
/
convert_dyiepp_to_sentence.py
44 lines (36 loc) · 1.57 KB
/
convert_dyiepp_to_sentence.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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import sys
from os import path
import json
import collections
def main():
output_dir = sys.argv[1]
for fold in ["train", "dev", "test"]:
g_convert = open(path.join(output_dir, fold + "_convert.json"), "w")
with open(path.join(output_dir, fold + ".json"), "r") as g:
print('convert %s to %s' % (
path.join(output_dir, fold + ".json"),
path.join(output_dir, fold + "_convert.json")
))
for line in g:
line = json.loads(line)
sentences = line["sentences"]
ner = line["ner"]
relations = line["relations"]
events = line["events"]
sentence_start = line["_sentence_start"]
doc_key = line["doc_key"]
assert len(sentence_start) == len(ner) == len(
relations) == len(events) == len(sentence_start)
for sentence, ner, relation, event, s_start in zip(sentences, ner, relations, events, sentence_start):
sentence_annotated = collections.OrderedDict()
sentence_annotated["sentence"] = sentence
sentence_annotated["s_start"] = s_start
sentence_annotated["ner"] = ner
sentence_annotated["relation"] = relation
sentence_annotated["event"] = event
g_convert.write(json.dumps(
sentence_annotated, default=int) + "\n")
if __name__ == "__main__":
main()