ami / parse_text.py
patrickvonplaten's picture
up
28e6c6f
raw
history blame
1.29 kB
#!/usr/bin/env python3
import sys
import re
transcript = sys.argv[1]
new_transcript = sys.argv[2]
ref_look_up_dict_end = {}
with open(transcript, "r") as f:
ref_lines = f.readlines()
for line in ref_lines:
items = line.split(" ")
key = "_".join(items[:3] + items[4:5])
if key in ref_look_up_dict_end:
print("WARNING: Already there", "_".join(items))
ref_look_up_dict_end[key] = " ".join(items[5:])
for line in ref_lines:
items = line.split(" ")
key = "_".join(items[:3] + items[4:5])
start_time = items[3]
if " ," in line:
index = int(line.split(" ,")[1][0])
if index > 0:
new_key = "_".join(key.split("_")[:3] + [start_time])
if new_key in ref_look_up_dict_end and ref_look_up_dict_end[new_key].strip()[-1] not in [".", ",", "?"]:
ref_look_up_dict_end[new_key] = ref_look_up_dict_end[new_key].strip() + ",\n" # noqa: E501
with open(new_transcript, "w") as f:
for line in ref_lines:
items = line.split(" ")
key = "_".join(items[:3] + items[4:5])
if key in ref_look_up_dict_end:
line = ref_look_up_dict_end.pop(key)
line = re.sub(r" \,[0-9]+", ",", line)
f.write(" ".join(items[:5]) + " " + line)