Tuesday 5 July 2022

Graphs are everywhere - also in Religious Texts - part 4 - Connect the Hadiths with the scholars

Now, we will connect the Hadiths with the scholars:

:auto MATCH (h:Hadith)
WITH h, split(replace(h.chain_indx," ",""),",") as scholarlist
CALL
    {
    WITH h, scholarlist
    UNWIND scholarlist as scholar
        MATCH (s:Scholar {scholar_indx: scholar})
        MERGE (s)-[:NARRATED]->(h)
    } IN TRANSACTIONS OF 100 ROWS;

And then, here's where the magic happens: using the chain_indx field of the Hadith nodes, we can now connect pairs of Scholars together. The following query uses a super useful apoc.coll.pairsMin function to turn an array of scholar_indx properties into pairs of consecutive chain members.

:auto MATCH (h:Hadith)
WITH h, apoc.coll.pairsMin(apoc.coll.sort(toIntegerList(split(replace(h.chain_indx," ",""),",")))) as scholarpairs
CALL {
    WITH h, scholarpairs
    UNWIND scholarpairs as scholarpair
        MATCH (s1:Scholar {scholar_indx: toString(scholarpair[0])}), (s2:Scholar {scholar_indx: toString(scholarpair[1])})
        WHERE id(s1)<>id(s2)
        MERGE (s1)-[:HADITH_CHAIN {hadith_id: h.hadith_id}]->(s2)
} IN TRANSACTIONS OF 100 ROWS;

To supplement the individual HADITH_CHAIN relationships, I also decided to include an aggregated relationship that - instead of having a relationship between 2 scholars for every chain membership that they are part of - we just count the number of memberships and tally them up as a property on the AGGREGATED_HADITH_CHAIN relationship. So a weight on the relationship, so to speak, that would indicate the "strength" of the relationship between the scholars.

:auto MATCH (s1:Scholar)-[r:HADITH_CHAIN]->(s2:Scholar)
WITH  s1, s2, count(r) as count_of_hadiths
call {
    WITH s1, s2, count_of_hadiths
    create (s1)-[:AGGREGATED_HADITH_CHAIN {nr_of_hadiths: count_of_hadiths}]->(s2)
} IN TRANSACTIONS OF 100 ROWS;

This is what the result of these two queries looks like:

Now we have the graph ready for exploration.

Looking forward already!

Rik

PS: as always all the code/queries are available on github!

PPS: you can find all the parts in this blogpost on the following links

No comments:

Post a Comment