Skip to content
Snippets Groups Projects
Commit 21290792 authored by Aurélien Lamercerie's avatar Aurélien Lamercerie
Browse files

Dev: some update

parent 22b4fc67
Branches
No related tags found
No related merge requests found
...@@ -133,12 +133,59 @@ def find_role_in_roleset(roleset_data, role_number): ...@@ -133,12 +133,59 @@ def find_role_in_roleset(roleset_data, role_number):
return is_found, role_data 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") print("\n" + "[CMT] PropBank Frame Analyzer")
...@@ -190,12 +237,16 @@ def main(amr_predicate, amr_role): ...@@ -190,12 +237,16 @@ def main(amr_predicate, amr_role):
else: else:
print("----- role " + str(role_number) + " not found") 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 # -- Ending print
print("\n" + "[SSC] Done") print("\n" + "[SSC] Done")
def dev_test_1():
if __name__ == "__main__": dev_analyze('include-01', ':ARG0')
main(sys.argv[1], sys.argv[2])
......
import regex as re import regex as re
import propbank_analyzer as pba
print("[DEV] Regular Expression Test") print("[DEV] Regular Expression Test")
# -- Données de test # -- Données de test
print("\n-- Données de test") print("\n-- Données de test")
graph = ''' (s / system GRAPH_INIT = ''' (s / system
:domain (p / planet :domain (p / planet
:name (n / name :name (n / name
:op1 "Solar" :op1 "Solar"
...@@ -22,7 +23,7 @@ graph = ''' (s / system ...@@ -22,7 +23,7 @@ graph = ''' (s / system
:op2 (d2 / direct-02 :op2 (d2 / direct-02
:polarity -))))))''' :polarity -))))))'''
print("----- graphe AMR traité : " + graph) print("----- graphe AMR traité : " + GRAPH_INIT)
substitutions = [] substitutions = []
substitutions.append(('bind-01', ':ARG0', ':ARG0-AGT')) substitutions.append(('bind-01', ':ARG0', ':ARG0-AGT'))
...@@ -54,6 +55,7 @@ ARGOF_PATTERN = ':ARG\d-of' ...@@ -54,6 +55,7 @@ ARGOF_PATTERN = ':ARG\d-of'
print("\n-- Recherche des relations (predicat, argument)") print("\n-- Recherche des relations (predicat, argument)")
graph_1 = GRAPH_INIT
pred_arg_relation_list = [] pred_arg_relation_list = []
...@@ -70,7 +72,7 @@ def find_pred_arg_relations(graph, pred_arg_relation_list): ...@@ -70,7 +72,7 @@ def find_pred_arg_relations(graph, pred_arg_relation_list):
arg_pos_start, arg_pos_end)) arg_pos_start, arg_pos_end))
return pred_arg_relation_list 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 # ----- prédicat pour chaque ARGi-of
def find_argof_pred_relations(graph, pred_arg_relation_list): def find_argof_pred_relations(graph, pred_arg_relation_list):
...@@ -84,7 +86,7 @@ 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)) arg_pos_start, arg_pos_end))
return pred_arg_relation_list 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) :") print("----- Resultat (matchs trouvés) :")
for r in pred_arg_relation_list: for r in pred_arg_relation_list:
...@@ -95,6 +97,8 @@ 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") print("\n-- Substitution des arguments dans le graphe")
graph_2 = GRAPH_INIT
def sub_betwenn_pos(text, start, end, new_str): def sub_betwenn_pos(text, start, end, new_str):
result = text[:start] result = text[:start]
result += new_str result += new_str
...@@ -103,8 +107,8 @@ def sub_betwenn_pos(text, start, end, new_str): ...@@ -103,8 +107,8 @@ def sub_betwenn_pos(text, start, end, new_str):
# ----- argument pour chaque prédicat # ----- argument pour chaque prédicat
def sub_pred_arg_relations(graph): def sub_pred_arg_relations(graph, sub_list):
for (pred, old_arg, new_arg) in substitutions: for (pred, old_arg, new_arg) in sub_list:
for pred_match in re.finditer(PRED_PATTERN, graph): for pred_match in re.finditer(PRED_PATTERN, graph):
for arg_match in rx.finditer(graph[pred_match.end():]): for arg_match in rx.finditer(graph[pred_match.end():]):
arg_pos_start = pred_match.end() + arg_match.start() arg_pos_start = pred_match.end() + arg_match.start()
...@@ -119,11 +123,11 @@ def sub_pred_arg_relations(graph): ...@@ -119,11 +123,11 @@ def sub_pred_arg_relations(graph):
new_arg) new_arg)
return graph return graph
graph = sub_pred_arg_relations(graph) graph_2 = sub_pred_arg_relations(graph_2, substitutions)
# ----- prédicat pour chaque ARGi-of # ----- prédicat pour chaque ARGi-of
def sub_argof_pred_relations(graph): def sub_argof_pred_relations(graph, sub_list):
for (pred, old_arg, new_arg) in substitutions: for (pred, old_arg, new_arg) in sub_list:
for arg_match in re.finditer(ARGOF_PATTERN, graph): for arg_match in re.finditer(ARGOF_PATTERN, graph):
pred_match = re.findall(PRED_PATTERN, graph[arg_match.end():]) pred_match = re.findall(PRED_PATTERN, graph[arg_match.end():])
arg_pos_start = arg_match.start() arg_pos_start = arg_match.start()
...@@ -138,7 +142,40 @@ def sub_argof_pred_relations(graph): ...@@ -138,7 +142,40 @@ def sub_argof_pred_relations(graph):
new_arg) new_arg)
return graph 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_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("----- Résultat (graphe après substitutions) :" + graph) print("----- Result: " + result_graph)
\ No newline at end of file \ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment