convert_dyiepp_to_sentence.py (1609B)
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 import sys 4 from os import path 5 import json 6 import collections 7 8 9 def main(): 10 11 output_dir = sys.argv[1] 12 for fold in ["train", "dev", "test"]: 13 g_convert = open(path.join(output_dir, fold + "_convert.json"), "w") 14 with open(path.join(output_dir, fold + ".json"), "r") as g: 15 print('convert %s to %s' % ( 16 path.join(output_dir, fold + ".json"), 17 path.join(output_dir, fold + "_convert.json") 18 )) 19 for line in g: 20 line = json.loads(line) 21 sentences = line["sentences"] 22 ner = line["ner"] 23 relations = line["relations"] 24 events = line["events"] 25 sentence_start = line["_sentence_start"] 26 doc_key = line["doc_key"] 27 28 assert len(sentence_start) == len(ner) == len( 29 relations) == len(events) == len(sentence_start) 30 31 for sentence, ner, relation, event, s_start in zip(sentences, ner, relations, events, sentence_start): 32 sentence_annotated = collections.OrderedDict() 33 sentence_annotated["sentence"] = sentence 34 sentence_annotated["s_start"] = s_start 35 sentence_annotated["ner"] = ner 36 sentence_annotated["relation"] = relation 37 sentence_annotated["event"] = event 38 39 g_convert.write(json.dumps( 40 sentence_annotated, default=int) + "\n") 41 42 43 if __name__ == "__main__": 44 main()