Laurent Sarrazin

Software Engineer

Playing with neo4j and "Kaamelott" (french TV series)

After some experiments with neo4j, I've had the idea to graph the awesome "Kaamelott" universe. Here are the results of my first trials.

Retrieve the data and insert them into neo4j

This is not the most interesting part. I've simply created a little PHP script to extract episodes and characters information from the web, mostly from http://onenagros.com which is the reference regarding Kaamelott.

Once the data fetched, I've user the “neoxygen/neoclient” lib to create nodes and relationships in neo4j.

The structure is pretty simple :

(Person)-[:PLAYS_THE_ROLE_OF]->(Character)-[APPEARS_IN]->(Episode)

First queries

To begin, let's start with a really simple one : getting the characters and their number of appearances in the first season (Livre I) :

MATCH (c:Character)-[:APPEARS_IN]->(e:Episode) 
WHERE e.season="1" 
RETURN c.name, count(e) AS n 
ORDER BY n DESC;

Result :

Or for something more visual :

Playing with neo4j and "Kaamelott" (french TV series)

 

Of course, Arthur is the character who appears in almost every episodes as he's the main character. But there's one episode in which he does not appear as the first season consists of 100 episodes. Let's find which episode it is and which characters are involved :

MATCH (arthur:Character {name:"Arthur Pendragon"}),
      (e:Episode)<-[:APPEARS_IN]-(c:Character)
WHERE NOT (arthur)-[:APPEARS_IN]->(e) AND e.season="1"
RETURN e.title, cs.name

Result :

Playing with neo4j and &quot;Kaamelott&quot; (french TV series)

If you've seen this episode, you know that it takes place in a tavern and involves the 3 characters we just found, playing some absurd games. No Arthur in sight indeed.

Some more interesting queries

Let's take a step forward now and see the relationships between the characters.

First request : displaying all episodes and characters of the first season :

MATCH (c:Character)-[:APPEARS_IN]->(e:Episode {season:"1"}) RETURN c, e

Result :

Playing with neo4j and &quot;Kaamelott&quot; (french TV series)

Not really convincing... It might be more interesting to limit a bit our query. Let's try the same thing but with only two characters : Arthur and Karadoc.

MATCH (arthur:Character {name:"Arthur Pendragon"})-[:APPEARS_IN]->(e1:Episode{season:"1"}),
      (karadoc:Character {name:"Karadoc"})-[:APPEARS_IN]->(e2:Episode{season:"1"})
RETURN arthur,karadoc,e1,e2
Playing with neo4j and &quot;Kaamelott&quot; (french TV series)

It's better. But still not very useful. Let's try to add a third character :

 

Playing with neo4j and &quot;Kaamelott&quot; (french TV series)

This one is really interesting. We can see in a second that Perceval and Karadoc share a majority of the episodes in which they appear.

Moreover, we can see that Perceval shares more episodes with Arthur than Karadoc does and that only one episode does not feature Arthur, as we've already seen before.

Let's just try a last query. What about getting all the characters that never share an episode with Karadoc ?

MATCH (karadoc:Character{name:"Karadoc"}), (c:Character)
WHERE NOT EXISTS ((karadoc)-[:APPEARS_IN]->(:Episode{season:"1"})<-[:APPEARS_IN]-(c))
RETURN c.name ORDER BY c.name;
Result :

Conclusion

This was just a small preview of the awesome possibilities of neo4j (especially as I'm a newbie).

But you can still realize how quickly you can get some very interesting results.

I plan to keep playing with this great tool (and with Kaamelott). Please do not hesitate to give me your ideas and suggestions about it.

Previous post
Back to home page

Share this post

Repost 0

Comment on this post