def read(fp, full = False, reify = False, text = None, alignment = None, quiet = False): n = 0; for id, snt, tokens, lemmas, pos_tags, ner_tags, abstract_map, amr_line, mapping in amr_lines(fp, alignment): amr = AMR.parse_AMR_line(amr_line) if not amr: raise Exception("failed to parse #{} ({}); exit." "".format(id, amr_line)); try: if id is not None: id = convert_amr_id(id) else: id = n; n += 1; except: pass graph, overlay = amr2graph(id, amr, full, reify, mapping); cid = None; if text: graph.add_input(text, quiet = quiet); elif snt: graph.add_input(snt, quiet = quiet); if tokens: graph.add_tokens(tokens, quiet = quiet); if lemmas: graph.add_lemmas(lemmas, quiet = quiet); if pos_tags: graph.add_pos_tags(pos_tags, quiet = quiet); if ner_tags: graph.add_ner_tags(ner_tags, quiet = quiet); if abstract_map: graph.add_abstract_map(abstract_map, quiet = quiet); yield graph, overlay;
def read(fp, full=False, reify=False, camr=False, text=None, alignment=None, quiet=False, trace=0): n = 0 for id, snt, amr_line, stash, mapping in amr_lines(fp, camr, alignment): if trace: print("{}: {}".format(id, amr_line), file=sys.stderr) amr = AMR.parse_AMR_line(amr_line) if not amr: raise Exception("failed to parse #{} ‘{}’; exit." "".format(id, amr_line)) if id is not None: try: id = convert_amr_id(id) except: pass else: id = n n += 1 graph, overlay = amr2graph(id, amr, text or snt, stash, camr, full, reify, quiet, mapping) yield graph, overlay
def from_parse(cls, annotation_line, comment_lines, consts_to_vars=False): metadata = {} for l in comment_lines: matches = re.findall(r'::(\S+)\s(([^:]|:(?!:))+)', l) for m in matches: metadata[m[0]] = m[1].strip() base_amr = AMR.parse_AMR_line(annotation_line, consts_to_vars=consts_to_vars) return cls(base_amr=base_amr, metadata=metadata)
def from_parse(cls, annotation_line, comment_lines, xlang=False): metadata = {} for l in comment_lines: matches = re.findall(r'::(\S+)\s(([^:]|:(?!:))+)', l) for m in matches: metadata[m[0]] = m[1].strip() base_amr = AMR.parse_AMR_line(annotation_line, xlang=xlang) return cls(base_amr=base_amr, metadata=metadata)
def generate_amr_lines(f1, f2): """ Read one AMR line at a time from each file handle :param f1: file handle (or any iterable of strings) to read AMR 1 lines from :param f2: file handle (or any iterable of strings) to read AMR 2 lines from :return: generator of cur_amr1, cur_amr2 pairs: one-line AMR strings """ while True: cur_amr1 = AMR.get_amr_line(f1) cur_amr2 = AMR.get_amr_line(f2) if not cur_amr1 and not cur_amr2: pass elif not cur_amr1: print("Error: File 1 has less AMRs than file 2", file=ERROR_LOG) print("Ignoring remaining AMRs", file=ERROR_LOG) elif not cur_amr2: print("Error: File 2 has less AMRs than file 1", file=ERROR_LOG) print("Ignoring remaining AMRs", file=ERROR_LOG) else: yield cur_amr1, cur_amr2 continue break
def read(fp, full=False, normalize=False, reify=False, text=None): for id, snt, amr_line in amr_lines(fp): amr = AMR.parse_AMR_line(amr_line) if not amr: raise Exception("failed to parse #{} ({}); exit." "".format(id, amr_line)) try: id = convert_wsj_id(id) except: pass graph = amr2graph(id, amr, full, normalize, reify) cid = None if text: graph.add_input(text) elif snt: graph.add_input(snt) yield graph
def read(fp, full=False, reify=False, text=None, alignment=None, quiet=False): n = 0 for id, snt, amr_line, mapping in amr_lines(fp, alignment): amr = AMR.parse_AMR_line(amr_line) if not amr: raise Exception("failed to parse #{} ({}); exit." "".format(id, amr_line)) try: if id is not None: id = convert_amr_id(id) else: id = n n += 1 except: pass graph, overlay = amr2graph(id, amr, full, reify, mapping) cid = None if text: graph.add_input(text, quiet=quiet) elif snt: graph.add_input(snt, quiet=quiet) yield graph, overlay
def get_amr_match(cur_amr1, cur_amr2, sent_num=1, justinstance=False, justattribute=False, justrelation=False, limit = None, instance1 = None, attributes1 = None, relation1 = None, prefix1 = None, instance2 = None, attributes2 = None, relation2 = None, prefix2 = None): global iteration_num if limit is not None: iteration_num = limit if cur_amr1 and cur_amr2: amr_pair = [] for i, cur_amr in (1, cur_amr1), (2, cur_amr2): try: amr_pair.append(AMR.parse_AMR_line(cur_amr)) except Exception as e: print("Error in parsing amr %d: %s" % (i, cur_amr), file=ERROR_LOG) print("Please check if the AMR is ill-formatted. Ignoring remaining AMRs", file=ERROR_LOG) print("Error message: %s" % e, file=ERROR_LOG) amr1, amr2 = amr_pair prefix1 = "a" prefix2 = "b" # Rename node to "a1", "a2", .etc amr1.rename_node(prefix1) # Renaming node to "b1", "b2", .etc amr2.rename_node(prefix2) (instance1, attributes1, relation1) = amr1.get_triples() (instance2, attributes2, relation2) = amr2.get_triples() if verbose: print("AMR pair", sent_num, file=DEBUG_LOG) print("============================================", file=DEBUG_LOG) print("AMR 1 (one-line):", cur_amr1, file=DEBUG_LOG) print("AMR 2 (one-line):", cur_amr2, file=DEBUG_LOG) print("Instance triples of AMR 1:", len(instance1), file=DEBUG_LOG) print(instance1, file=DEBUG_LOG) print("Attribute triples of AMR 1:", len(attributes1), file=DEBUG_LOG) print(attributes1, file=DEBUG_LOG) print("Relation triples of AMR 1:", len(relation1), file=DEBUG_LOG) print(relation1, file=DEBUG_LOG) print("Instance triples of AMR 2:", len(instance2), file=DEBUG_LOG) print(instance2, file=DEBUG_LOG) print("Attribute triples of AMR 2:", len(attributes2), file=DEBUG_LOG) print(attributes2, file=DEBUG_LOG) print("Relation triples of AMR 2:", len(relation2), file=DEBUG_LOG) print(relation2, file=DEBUG_LOG) # optionally turn off some of the node comparison doinstance = doattribute = dorelation = True if justinstance: doattribute = dorelation = False if justattribute: doinstance = dorelation = False if justrelation: doinstance = doattribute = False (best_mapping, best_match_num) = get_best_match(instance1, attributes1, relation1, instance2, attributes2, relation2, prefix1, prefix2, doinstance=doinstance, doattribute=doattribute, dorelation=dorelation) if verbose: print("best match number", best_match_num, file=DEBUG_LOG) print("best node mapping", best_mapping, file=DEBUG_LOG) print("Best node mapping alignment:", print_alignment(best_mapping, instance1, instance2), file=DEBUG_LOG) if justinstance: test_triple_num = len(instance1) gold_triple_num = len(instance2) elif justattribute: test_triple_num = len(attributes1) gold_triple_num = len(attributes2) elif justrelation: test_triple_num = len(relation1) gold_triple_num = len(relation2) else: test_triple_num = len(instance1) + len(attributes1) + len(relation1) gold_triple_num = len(instance2) + len(attributes2) + len(relation2) match_triple_dict.clear() if cur_amr1 and cur_amr2: return best_match_num, test_triple_num, gold_triple_num else: return best_match_num, test_triple_num, gold_triple_num, best_mapping