cjvt-srl-tagging/tools/gen_json.py

103 lines
2.6 KiB
Python
Raw Normal View History

2019-02-25 23:22:15 +00:00
from pathlib import Path
from parser.parser import Parser
2019-02-27 08:15:40 +00:00
import configparser
2019-02-27 16:32:19 +00:00
import json
import sys
2019-02-25 23:22:15 +00:00
2019-02-27 08:15:40 +00:00
# defaults
2019-02-25 23:22:15 +00:00
ORIGPATH = Path("../data/kres_example") # we need the IDs
INPATH = Path("../data/kres_example_srl")
OUTPATH = Path("../data/kres_example_json")
2019-02-27 15:58:04 +00:00
DEBUG = False
2019-02-25 23:22:15 +00:00
2019-02-27 08:15:40 +00:00
# parse config
config = configparser.ConfigParser()
config.read("tools.cfg")
ORIGPATH = Path(config["tools"]["kres_orig"])
INPATH = Path(config["tools"]["kres_srl"])
OUTPATH = Path(config["tools"]["kres_json"])
2019-02-27 16:32:19 +00:00
DEBUG = config["tools"]["debug"] == "True"
2019-02-27 08:15:40 +00:00
2019-02-25 23:22:15 +00:00
def get_origfile(filename):
for origfile in ORIGPATH.iterdir():
if filename.name.split('.')[0] == origfile.name.split('.')[0]:
return origfile
raise FileNotFoundError
def extract_sentences(line_reader):
acc = []
2019-02-27 15:58:04 +00:00
# last char in line is \n, remove it
for line in [x.decode("utf-8")[:-1].split('\t') for x in line_reader]:
if len(line) == 1: # empty line
2019-02-25 23:22:15 +00:00
tmp = acc
acc = []
yield tmp
else:
acc.append(line)
2019-02-27 15:58:04 +00:00
def to_sentence(sentence_arr):
return " ".join([token[1] for token in sentence_arr])
def match_sentence_id(sentence, orig_dict):
for k, e in orig_dict.items():
orig_sentence = " ".join(token[2] for token in e["tokens"])
if sentence == orig_sentence:
2019-02-25 23:22:15 +00:00
return k
raise KeyError
2019-02-27 15:58:04 +00:00
def get_dep_rel(token):
if DEBUG:
print(token)
for i, field in enumerate(token[14:]):
if field != "_":
return {
"arg": field,
"from": i, # i-th predicate in sentence
"dep": token[0],
}
return None
2019-02-25 12:44:24 +00:00
2019-02-27 16:04:03 +00:00
par = Parser()
OUTPATH.mkdir(exist_ok=True)
2019-02-25 23:22:15 +00:00
2019-02-28 07:20:21 +00:00
print("Start generating .josn files.")
2019-02-27 16:04:03 +00:00
for infile in [x for x in INPATH.iterdir() if x.is_file()]:
origfile = get_origfile(infile)
orig_dict = par.parse_tei(origfile)
2019-02-25 23:22:15 +00:00
2019-02-27 16:32:19 +00:00
with infile.open("rb") as fp:
outdata = {}
for sentence_arr in extract_sentences(fp.readlines()):
# tsv dropped sentence ids, match the ID, using original data
sid = match_sentence_id(to_sentence(sentence_arr), orig_dict)
2019-02-27 15:58:04 +00:00
2019-02-27 16:32:19 +00:00
outdata[sid] = []
2019-02-27 15:58:04 +00:00
2019-02-27 16:32:19 +00:00
# find all predicate indices in the sentence
predicates = []
for token in sentence_arr:
if token[12] == "Y":
predicates += [token[0]] # idx
2019-02-27 15:58:04 +00:00
2019-02-27 16:32:19 +00:00
deprel = get_dep_rel(token)
if deprel is not None:
outdata[sid].append(deprel)
2019-02-27 15:58:04 +00:00
2019-02-27 16:32:19 +00:00
# deprel["from"] points to n-th predicate
# replace with predicate's token index
for deprel in outdata[sid]:
deprel["from"] = predicates[deprel["from"]]
2019-02-25 23:22:15 +00:00
2019-02-27 16:32:19 +00:00
if DEBUG:
print(to_sentence(sentence_arr))
print(outdata[sid])
print(sid)
print()
print()
2019-02-25 23:22:15 +00:00
2019-02-27 16:04:03 +00:00
outfile = (OUTPATH / infile.name).with_suffix(".json")
2019-02-27 16:32:19 +00:00
with outfile.open("w") as fp:
json.dump(outdata, fp)
2019-02-28 07:20:21 +00:00
print("SRL relations written to: ", outfile)
print("Finished generating .json files.")