def read_amr(file_path): # read all lines ref_amr_lines = [] with open(file_path, encoding='utf8') as fid: line = AMR.get_amr_line(fid) while line: ref_amr_lines.append(line) line = AMR.get_amr_line(fid) return ref_amr_lines
def main(data): logging.basicConfig(level=logging.ERROR) logger = logging.getLogger(__name__) test = codecs.open(data.test, 'r', 'utf-8') gold = codecs.open(data.gold, 'r', 'utf-8') flag = False sema = Sema() while True: cur_amr1 = AMR.get_amr_line(test) cur_amr2 = AMR.get_amr_line(gold) if cur_amr1 == '' and cur_amr2 == '': break if cur_amr1 == '': logger.error('Error: File 1 has less AMRs than file 2') logger.error('Ignoring remaining AMRs') flag = True break if cur_amr2 == '': logger.error('Error: File 2 has less AMRs than file 1') logger.error('Ignoring remaining AMRs') flag = True break try: amr1 = AMR.parse_AMR_line(cur_amr1) except Exception as e: logger.error('Error in parsing amr 1: %s' % cur_amr1) logger.error( "Please check if the AMR is ill-formatted. Ignoring remaining AMRs" ) logger.error("Error message: %s" % str(e)) flag = True break try: amr2 = AMR.parse_AMR_line(cur_amr2) except Exception as e: logger.error("Error in parsing amr 2: %s" % cur_amr2) logger.error( "Please check if the AMR is ill-formatted. Ignoring remaining AMRs" ) logger.error("Error message: %s" % str(e)) flag = True break prefix_test = 'a' prefix_gold = 'b' amr1.rename_node(prefix_test) amr2.rename_node(prefix_gold) sema.compute_sema(amr1, amr2) if not flag: precision, recall, f1 = sema.get_sema_value() print(f'SEMA: P {precision:.2f} R {recall:.2f} F1 {f1:.2f}')
def get_amr(tokens, actions, entity_rules): # play state machine to get AMR state_machine = AMRStateMachine(tokens, entity_rules=entity_rules) for action in actions: # FIXME: It is unclear that this will be allways the right option # manual exploration of dev yielded 4 cases and it works for the 4 if action == "<unk>": action = f'PRED({state_machine.get_top_of_stack()[0].lower()})' state_machine.applyAction(action) # sanity check: foce close if not state_machine.is_closed: alert_str = yellow_font('Machine not closed!') print(alert_str) state_machine.CLOSE() # TODO: Probably waisting ressources here amr_str = state_machine.amr.toJAMRString() return AMR.get_amr_line(amr_str.split('\n'))