How to find the common relationship pattern between two different graphs?

Hello I have been working on NLP project for that I have to find the common relationship patterns in the two different graphs.

This is one graph
image

And the other one which is actually a pattern
image

Both graph has the common relationship pattern i.e., NN<-[:nsubj]-AUX->[:dobj].So,how to match with this similarity and show that the graph1 matches the graph2 patterns?

Could anyone know about this please?
I tried the adjacency matrix concept but it didn't work out.
Thanks
Vigneshwar

Hi @imvigneshwar1104

I created the data.

CREATE (each:DT {word:"Each"}),
       (confirmation:NN {word:"confirmation"}),
       (window:NN {word:"window"}),
       (has:AUX {word:"has"}),
       (a:DT {word:"a"}),
       (close:JJ {word:"close"}),
       (button:NN {word:"button"})
CREATE (each)<-[:det]-(window),
       (confirmation)<-[:compound]-(window),
       (window)<-[:nsubj]-(has),
       (has)-[:dobj]->(button),
       (a)<-[:dobj]-(button),
       (close)<-[:amod]-(button)

Search for this in the pattern and it will match.

MATCH (nn1:NN)<-[:nsubj]-(aux:AUX)-[:dobj]->(nn2:NN)
RETURN *

Is this an answer to your question?

1 Like

Hello @koji Yes! Thanks for your reply and an idea. :smiley:

Similar like this I have other patterns such as,

image

image
In this one pattern can't match with the main graph.
Because the main graph don't have relationship from POS tag NN<-[:nsubj]-VB-[:dobj]->NN

And,I might have to check with many patterns like this.Each patterns has the number like 1,2,3, so on ... Which patterns get matched ,the number or id of it should be returned.
So,is it possible to check with all the patterns simultaneously whether it matches the relationship with the main graph?
Please let me know..
Thanks
Vigneshwar

Hi @imvigneshwar1104

I made this Cypher.
It's not cool codes.

OPTIONAL MATCH (nn1:NN)<-[:nsubj]-(aux:AUX)-[:dobj]->(nn2:NN)
WITH "Pattern1" + collect(id(nn1)) + collect(id(aux)) + collect(id(nn2)) AS list1
OPTIONAL MATCH (nn1:NN)<-[:nsubj]-(vb:VB)-[:dobj]->(nn2:NN)
RETURN list1, "Pattern2" + collect(id(nn1)) + collect(id(vb)) + collect(id(nn2)) AS list2

Pattern 1 shows the id of the node.
Pattern 2 has no id because there is no match.

1 Like

Thanks for your reply :smiley: This solution works for me.
However, By using this I can match the main graph with few patterns manually.But,I will have many patterns which is actually the subgraphs. So, dynamically need to find the common relationship between the main graph and the many other patterns.
Is it possible? Can you give any suggestions please?

Thanks
VIGNESHWAR

Hi @imvigneshwar1104

I think this code is better than the previous one.
Now you just add more MATCH patterns with UNION ALL.
If you know the id of the nodes used in the main graph, you can add it to the Where clause to make the search faster.
Since the main graph is a sentence, you can repeat this process.

OPTIONAL MATCH (nn1:NN)<-[:nsubj]-(aux:AUX)-[:dobj]->(nn2:NN)
RETURN 1 AS Pattern, collect(id(nn1)) + collect(id(aux)) + collect(id(nn2)) AS idList
UNION ALL
OPTIONAL MATCH (nn1:NN)<-[:nsubj]-(vb:VB)-[:dobj]->(nn2:NN)
RETURN 2 AS Pattern, collect(id(nn1)) + collect(id(vb)) + collect(id(nn2)) AS idList

1 Like

Hi @koji Thanks for your reply :smiley: This code is good and helpful.Actually,I'm trying to MATCH my main graph with the other patterns to find the common relationship i.e., Whether it's a subgraph or not.So,manually I can do as you suggested say for few patterns.But,I will have nearly 100 patterns to be check with the main graph.

All that patterns will be saved in the same database as different labeled nodes.So,I'm searching for the idea to find the match between the main graph with the hundreds of patterns.
It will return which pattern has been matched.Is there any possible way for that to make the main graph itself check automatically ?
Please let me know.

Thanks
VIGNESHWAR

Hi @imvigneshwar1104

How about using the Triggers to check when a document is added?
https://neo4j-contrib.github.io/neo4j-apoc-procedures/3.5/operational/triggers/

Thanks for the idea @koji . I will try it. :smiley:

1 Like