From b8f7031878dd0fba0059939ddfef7c27b50cc981 Mon Sep 17 00:00:00 2001 From: Adam Kovacs Date: Tue, 1 Mar 2022 15:40:13 +0100 Subject: [PATCH 1/3] Prepare matcher for OpenIE --- tuw_nlp/graph/utils.py | 54 ++++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/tuw_nlp/graph/utils.py b/tuw_nlp/graph/utils.py index 4af6342..213b96a 100644 --- a/tuw_nlp/graph/utils.py +++ b/tuw_nlp/graph/utils.py @@ -1,3 +1,4 @@ +from nis import match import re from copy import deepcopy from itertools import chain @@ -137,29 +138,40 @@ def __init__(self, patterns, converter): neg_graphs = [converter(neg_patt)[0] for neg_patt in negs] self.patts.append((pos_patts, neg_graphs, key)) - def match(self, graph): + def _neg_match(self, graph, negs): + for neg_graph in negs: + matcher = DiGraphMatcher( + graph, neg_graph, node_match=GraphFormulaMatcher.node_matcher, edge_match=GraphFormulaMatcher.edge_matcher) + if matcher.subgraph_is_monomorphic(): + return True + return False + + def match(self, graph, return_subgraphs=False): for i, (patt, negs, key) in enumerate(self.patts): logger.debug(f'matching this: {self.patts[i]}') - - neg_match = False - for neg in negs: - matcher = DiGraphMatcher( - graph, neg, node_match=GraphFormulaMatcher.node_matcher, edge_match=GraphFormulaMatcher.edge_matcher) - if matcher.subgraph_is_monomorphic(): - neg_match = True - break - - pos_match = True - for p in patt: - matcher = DiGraphMatcher( - graph, p, node_match=GraphFormulaMatcher.node_matcher, edge_match=GraphFormulaMatcher.edge_matcher) - if not matcher.subgraph_is_monomorphic(): - pos_match = False - break - - if pos_match and not neg_match: - yield key, i - + neg_match = self._neg_match(graph, negs) + + if not neg_match: + pos_match = True + subgraphs = [] + for p in patt: + matcher = DiGraphMatcher( + graph, p, node_match=GraphFormulaMatcher.node_matcher, edge_match=GraphFormulaMatcher.edge_matcher) + + monomorphic_subgraphs = list(matcher.subgraph_monomorphisms_iter()) + if not len(monomorphic_subgraphs) == 0: + subgraph = monomorphic_subgraphs[0].keys() + subgraphs.append(graph.subgraph(subgraph)) + else: + pos_match = False + break + + if pos_match: + if return_subgraphs: + yield key, i, subgraphs + else: + yield key, i + def gen_subgraphs(M, no_edges): """M must be dict of dicts, see networkx.convert.to_dict_of_dicts. From 588fde471b28bba67c299cdff17f63f84fa48a77 Mon Sep 17 00:00:00 2001 From: Adam Kovacs Date: Wed, 2 Mar 2022 11:35:32 +0100 Subject: [PATCH 2/3] Add node mapping --- tuw_nlp/graph/utils.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tuw_nlp/graph/utils.py b/tuw_nlp/graph/utils.py index 213b96a..dc3f901 100644 --- a/tuw_nlp/graph/utils.py +++ b/tuw_nlp/graph/utils.py @@ -160,8 +160,10 @@ def match(self, graph, return_subgraphs=False): monomorphic_subgraphs = list(matcher.subgraph_monomorphisms_iter()) if not len(monomorphic_subgraphs) == 0: - subgraph = monomorphic_subgraphs[0].keys() - subgraphs.append(graph.subgraph(subgraph)) + mapping = monomorphic_subgraphs[0] + subgraph = graph.subgraph(mapping.keys()) + nx.set_node_attributes(subgraph, mapping, name="mapping") + subgraphs.append(subgraph) else: pos_match = False break From db92c67ec361dad03e00310719e359acba900c65 Mon Sep 17 00:00:00 2001 From: Adam Kovacs Date: Wed, 9 Mar 2022 10:20:25 +0100 Subject: [PATCH 3/3] Remove unnecessary import --- tuw_nlp/graph/utils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tuw_nlp/graph/utils.py b/tuw_nlp/graph/utils.py index dc3f901..a02fc51 100644 --- a/tuw_nlp/graph/utils.py +++ b/tuw_nlp/graph/utils.py @@ -1,4 +1,3 @@ -from nis import match import re from copy import deepcopy from itertools import chain