Skip to content

Commit

Permalink
Merge pull request #62 from adaamko/main
Browse files Browse the repository at this point in the history
OpenIE graph matcher
  • Loading branch information
adaamko authored Mar 9, 2022
2 parents 170355a + db92c67 commit 57a7454
Showing 1 changed file with 34 additions and 21 deletions.
55 changes: 34 additions & 21 deletions tuw_nlp/graph/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,29 +137,42 @@ 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:
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

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.
Expand Down

0 comments on commit 57a7454

Please sign in to comment.