cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

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
3X_3_8_383182ece6cda0e6adce11043d7aff323060cf4e.png

And the other one which is actually a pattern
3X_3_d_3db395e8cdb6137297c234a58b638b65981104fe.png

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

8 REPLIES 8

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?

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

Similar like this I have other patterns such as,

3X_7_1_7121c6f50e03b7e33f86ad27032dce23bf2730ec.png

3X_1_d_1dc4017bb3caa5fa4f333a59b66a26db47c6470e.png
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.

Thanks for your reply 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

Hi @koji Thanks for your reply 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

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