Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PoliCheck GraphMatcher bug reproduce
graph [
directed 1
node [
id 0
label "(3, collect, <AnnotationType.COLLECT_VERB: 3>)"
tag "<AnnotationType.COLLECT_VERB: 3>"
lemma "'store,collect'"
lemmaList "'store'"
lemmaList "'collect'"
dep "'conj'"
pos "'VERB'"
neg 0
docStart 3
docEnd 3
startDoc 3
endDoc 5
]
node [
id 1
label "(1, measure, <AnnotationType.NONE: 0>)"
tag "<AnnotationType.NONE: 0>"
lemma "'measure'"
lemmaList "_networkx_list_start"
lemmaList "'measure'"
dep "'ROOT'"
pos "'VERB'"
neg 0
docStart 1
docEnd 1
]
node [
id 2
label "(0, We, <AnnotationType.ENTITY: 5>)"
tag "<AnnotationType.ENTITY: 5>"
lemma "'we'"
lemmaList "_networkx_list_start"
lemmaList "'we'"
dep "'nsubj'"
pos "'NOUN'"
neg 0
docStart 0
docEnd 0
]
node [
id 3
label "(6, your usage data, <AnnotationType.DATA_OBJ: 1>)"
tag "<AnnotationType.DATA_OBJ: 1>"
lemma "'your usage datum'"
lemmaList "_networkx_list_start"
lemmaList "'your usage datum'"
dep "'dobj'"
pos "'NOUN'"
neg 0
docStart 6
docEnd 6
]
edge [
source 0
target 3
label "'dobj'"
]
edge [
source 1
target 2
label "'nsubj'"
]
edge [
source 1
target 0
label "'conj'"
]
]
graph [
directed 1
node [
id 0
label "(4, obtain, <AnnotationType.COLLECT_VERB: 3>)"
tag "<AnnotationType.COLLECT_VERB: 3>"
lemma "'obtain'"
lemmaList "_networkx_list_start"
lemmaList "'obtain'"
dep "'xcomp'"
pos "'VERB'"
neg 0
docStart 4
docEnd 4
]
node [
id 1
label "(2, choose, <AnnotationType.NONE: 0>)"
tag "<AnnotationType.NONE: 0>"
lemma "'choose'"
lemmaList "_networkx_list_start"
lemmaList "'choose'"
dep "'ROOT'"
pos "'VERB'"
neg 0
docStart 2
docEnd 2
]
node [
id 2
label "(0, We, <AnnotationType.ENTITY: 5>)"
tag "<AnnotationType.ENTITY: 5>"
lemma "'we'"
lemmaList "_networkx_list_start"
lemmaList "'we'"
dep "'nsubj'"
pos "'NOUN'"
neg 0
docStart 0
docEnd 0
]
node [
id 3
label "(6, personal information, <AnnotationType.DATA_OBJ: 1>)"
tag "<AnnotationType.DATA_OBJ: 1>"
lemma "'personal information'"
lemmaList "_networkx_list_start"
lemmaList "'personal information'"
dep "'dobj'"
pos "'ADJ'"
neg 0
docStart 6
docEnd 6
]
edge [
source 0
target 3
label "'dobj'"
]
edge [
source 1
target 2
label "'nsubj'"
]
edge [
source 1
target 0
label "'xcomp'"
]
]
graph [
directed 1
node [
id 0
label "(2, choose, <AnnotationType.NONE: 0>)"
tag "<AnnotationType.NONE: 0>"
lemma "'choose'"
lemmaList "_networkx_list_start"
lemmaList "'choose'"
dep "'ROOT'"
pos "'VERB'"
neg 0
docStart 2
docEnd 2
]
node [
id 1
label "(0, We, <AnnotationType.ENTITY: 5>)"
tag "<AnnotationType.ENTITY: 5>"
lemma "'we'"
lemmaList "_networkx_list_start"
lemmaList "'we'"
dep "'nsubj'"
pos "'NOUN'"
neg 0
docStart 0
docEnd 0
]
node [
id 2
label "(4, obtain, <AnnotationType.COLLECT_VERB: 3>)"
tag "<AnnotationType.COLLECT_VERB: 3>"
lemma "'obtain'"
lemmaList "_networkx_list_start"
lemmaList "'obtain'"
dep "'xcomp'"
pos "'VERB'"
neg 0
docStart 4
docEnd 4
]
node [
id 3
label "(6, personal information, <AnnotationType.DATA_OBJ: 1>)"
tag "<AnnotationType.DATA_OBJ: 1>"
lemma "'personal information'"
lemmaList "_networkx_list_start"
lemmaList "'personal information'"
dep "'dobj'"
pos "'ADJ'"
neg 0
docStart 6
docEnd 6
]
edge [
source 0
target 1
label "'nsubj'"
]
edge [
source 0
target 2
label "'xcomp'"
]
edge [
source 2
target 3
label "'dobj'"
]
]
#!/usr/bin/env python3
from __future__ import print_function
import networkx as nx
pat1 = nx.read_gml('pattern_1.gml')
pat2 = nx.read_gml('pattern_2.gml')
depgraph = nx.read_gml('depgraph.gml')
match = nx.algorithms.isomorphism.GraphMatcher(
pat1, pat2,
edge_match=lambda a, b: a['label'] == b['label'])
print("Are pattern1 and pattern2 isomorphic?", match.is_isomorphic())
print("GraphMatcher(depgraph, pattern1):")
match1 = nx.algorithms.isomorphism.GraphMatcher(
depgraph, pat1,
node_match=lambda a, b: True,
edge_match=lambda a, b: a['label'] == b['label'])
for subgraph in match1.subgraph_isomorphisms_iter():
print(subgraph)
print("GraphMatcher(depgraph, pattern2):")
match2 = nx.algorithms.isomorphism.GraphMatcher(
depgraph, pat2,
node_match=lambda a, b: True,
edge_match=lambda a, b: a['label'] == b['label'])
for subgraph in match2.subgraph_isomorphisms_iter():
print(subgraph)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment