diff --git a/lib/propbank_analyzer.py b/lib/propbank_analyzer.py index 52d8916d834f5c04d0c40095423a4df5ce7101c5..4a04bedf8bb9de2f08431e3924dcb7de6a9df71e 100644 --- a/lib/propbank_analyzer.py +++ b/lib/propbank_analyzer.py @@ -132,13 +132,60 @@ def find_role_in_roleset(roleset_data, role_number): return is_found, role_data + +#============================================================================== +# Main Function(s) +#============================================================================== + +def find_pb_role(amr_predicate, amr_role): + """ + Find the probbank role in PropBank frame corresponding to a given AMR + predicate and a given AMR role. + + Parameters + ---------- + amr_predicate : STRING + AMR predicate (example: 'include-01'). + amr_role : STRING + AMR core role (example: ':ARG0'). + + Returns + ------- + PropBank role (example: 'PAG'). + + """ + + # -- Intialize result + result = None + + # -- Analyze and adapt the target description + lemma = get_lemma_from_amr_predicate(amr_predicate) + roleset_id = get_roleset_id_from_amr_predicate(amr_predicate) + role_number = get_number_from_amr_role(amr_role) + + # -- Find the Frame XML data corresponding to a given lemma + frame_found, frame_filepath, frame_data = find_frame_of_lemma(lemma) + + if frame_found: + # -- Analyze frame data to find the target role + rs_found, rs_data = find_roleset_in_frame(frame_data, lemma, roleset_id) + nb_roles = -1 + + if rs_found: + nb_roles = len(rs_data.find_all('role')) + if role_number in range(nb_roles): + r_found, role_data = find_role_in_roleset(rs_data, role_number) + if r_found: + result = role_data.get('f') + + return result #============================================================================== -# Main function +# *** Dev Test *** #============================================================================== -def main(amr_predicate, amr_role): +def dev_analyze(amr_predicate, amr_role): print("\n" + "[CMT] PropBank Frame Analyzer") @@ -190,12 +237,16 @@ def main(amr_predicate, amr_role): else: print("----- role " + str(role_number) + " not found") + # -- Test for main function(s) + print("-- Test for main function(s)") + pb_role = find_pb_role(amr_predicate, amr_role) + print("----- find_pb_role(amr_predicate, amr_role) = " + pb_role) + # -- Ending print print("\n" + "[SSC] Done") - -if __name__ == "__main__": - main(sys.argv[1], sys.argv[2]) +def dev_test_1(): + dev_analyze('include-01', ':ARG0') diff --git a/lib/re_test.py b/lib/re_test.py index 8c7375de09ceb857399198d4a5ecd93e63bf95f3..73e4d186401aa993f745f47a8bd22420036db5a6 100644 --- a/lib/re_test.py +++ b/lib/re_test.py @@ -1,11 +1,12 @@ import regex as re +import propbank_analyzer as pba print("[DEV] Regular Expression Test") # -- Données de test print("\n-- Données de test") -graph = ''' (s / system +GRAPH_INIT = ''' (s / system :domain (p / planet :name (n / name :op1 "Solar" @@ -22,7 +23,7 @@ graph = ''' (s / system :op2 (d2 / direct-02 :polarity -))))))''' -print("----- graphe AMR traité : " + graph) +print("----- graphe AMR traité : " + GRAPH_INIT) substitutions = [] substitutions.append(('bind-01', ':ARG0', ':ARG0-AGT')) @@ -54,6 +55,7 @@ ARGOF_PATTERN = ':ARG\d-of' print("\n-- Recherche des relations (predicat, argument)") +graph_1 = GRAPH_INIT pred_arg_relation_list = [] @@ -70,7 +72,7 @@ def find_pred_arg_relations(graph, pred_arg_relation_list): arg_pos_start, arg_pos_end)) return pred_arg_relation_list -pred_arg_relation_list = find_pred_arg_relations(graph, pred_arg_relation_list) +pred_arg_relation_list = find_pred_arg_relations(graph_1, pred_arg_relation_list) # ----- prédicat pour chaque ARGi-of def find_argof_pred_relations(graph, pred_arg_relation_list): @@ -84,7 +86,7 @@ def find_argof_pred_relations(graph, pred_arg_relation_list): arg_pos_start, arg_pos_end)) return pred_arg_relation_list -find_argof_pred_relations(graph, pred_arg_relation_list) +find_argof_pred_relations(graph_1, pred_arg_relation_list) print("----- Resultat (matchs trouvés) :") for r in pred_arg_relation_list: @@ -95,6 +97,8 @@ for r in pred_arg_relation_list: print("\n-- Substitution des arguments dans le graphe") +graph_2 = GRAPH_INIT + def sub_betwenn_pos(text, start, end, new_str): result = text[:start] result += new_str @@ -103,8 +107,8 @@ def sub_betwenn_pos(text, start, end, new_str): # ----- argument pour chaque prédicat -def sub_pred_arg_relations(graph): - for (pred, old_arg, new_arg) in substitutions: +def sub_pred_arg_relations(graph, sub_list): + for (pred, old_arg, new_arg) in sub_list: for pred_match in re.finditer(PRED_PATTERN, graph): for arg_match in rx.finditer(graph[pred_match.end():]): arg_pos_start = pred_match.end() + arg_match.start() @@ -119,11 +123,11 @@ def sub_pred_arg_relations(graph): new_arg) return graph -graph = sub_pred_arg_relations(graph) +graph_2 = sub_pred_arg_relations(graph_2, substitutions) # ----- prédicat pour chaque ARGi-of -def sub_argof_pred_relations(graph): - for (pred, old_arg, new_arg) in substitutions: +def sub_argof_pred_relations(graph, sub_list): + for (pred, old_arg, new_arg) in sub_list: for arg_match in re.finditer(ARGOF_PATTERN, graph): pred_match = re.findall(PRED_PATTERN, graph[arg_match.end():]) arg_pos_start = arg_match.start() @@ -138,7 +142,40 @@ def sub_argof_pred_relations(graph): new_arg) return graph -graph = sub_argof_pred_relations(graph) +graph_2 = sub_argof_pred_relations(graph_2, substitutions) -print("----- Résultat (graphe après substitutions) :" + graph) \ No newline at end of file +print("----- Résultat (graphe après substitutions) :" + graph_2) + +# -- Substitution des arguments dans le graphe + +print("\n-- Test avec l'analyseur des cadres ProbBank (pba)") + +graph_3 = GRAPH_INIT + +init_relations = pred_arg_relation_list + +substitutions_from_pb = [] + +for (pred, orig_arg, _, _) in init_relations: + orig_role = orig_arg[0:5] + print("----- find pb role for: " + pred + " and " + orig_role) + new_role = pba.find_pb_role(pred, orig_role) + if new_role is not None: + print("----- pb role found: " + new_role) + new_arg = orig_arg[0:5] + '-' + new_role + if len(orig_arg) >= 8: + new_arg += orig_arg[5:8] + print("----- substitution add: " + pred + + ", " + orig_arg + ", " + new_arg) + substitutions_from_pb.append((pred, orig_arg, new_arg)) + else: + print("----- pb role not found") + +print("----- origin relations: " + str(init_relations)) +print("----- substitutions list: " + str(substitutions_from_pb)) + +graph_3 = sub_pred_arg_relations(graph_3, substitutions_from_pb) +result_graph = sub_argof_pred_relations(graph_3, substitutions_from_pb) + +print("----- Result: " + result_graph) \ No newline at end of file