Person Profile

Optional Lesson

This lesson is optional and will not count towards your achievement. To view the completed code, check out the 15-person-profile branch.

If you click on a Person card anywhere on the website, you will be taken to a Person profile. This API call is the same regardless of whether the person is an actor, director, or both.

We have already implemented the methods that populate the methods in the MovieDAO to get a list of movies that the person has either acted in or directed.

But if you take a look at the PeopleDAO, you will see two methods that need to be implemented to complete this page:

find_by_id()

The find_by_id() method is currently hardcoded to return information about Al Pacino.

python
api/dao/people.py
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-chatbot-typescript/main/api/dao/people.py[tag="findById"]

You will update this method to run the following cypher statement in a read transaction.

cypher
Get Person information
MATCH (p:Person {tmdbId: $id})
RETURN p {
  .*,
  actedCount: count { (p)-[:ACTED_IN]->() },
  directedCount: count { (p)-[:DIRECTED]->() }
} AS person

The query will return the properties for the person with the corresponding tmdbId, along with a count of the number of movies that the person has acted in and directed.

Parameters in Neo4j Browser

To run this query in Neo4j Sandbox, you will have to define a parameter in your Neo4j Browser session. To do so, you can use the :param command before running the Cypher statement to set a parameter for the duration of your Neo4j Browser session.

For example, to view Kevin Bacon, you can set the id parameter to "4724":

cypher
:param id: "4724"

Your Task

  • Modify the find_by_id() method to query Neo4j and return details for the requested person.

  • The returned object should include counts of the number of movies that the person has acted in or directed.

  • If no records are returned, the method will raise a NotFoundException.

Click here to reveal the final find_by_id() method.
python
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-chatbot-typescript/15-person-profile/api/dao/people.py[tag="findById"]

get_similar_people()

The get_similar_people() method should return a paginated list of similar people based on their second degree connections - either people who have acted in or have directed the person.

python
api/dao/people.py
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-chatbot-typescript/main/api/dao/people.py[tag="getSimilarPeople"]

There could be a more clever algorithm for finding similar people by weighting the type of relationship differently, but for now, the following query will find a list of similar people based on the number of relationships in common.

cypher
Get Similar People
MATCH (:Person {tmdbId: $id})-[:ACTED_IN|DIRECTED]->(m)<-[r:ACTED_IN|DIRECTED]-(p)
WITH p, collect(m {.tmdbId, .title, type: type(r)}) AS inCommon
RETURN p {
  .*,
  actedCount: count { (p)-[:ACTED_IN]->() },
  directedCount: count {(p)-[:DIRECTED]->() },
  inCommon: inCommon
} AS person
ORDER BY size(person.inCommon) DESC
SKIP 0
LIMIT 6

Parameters in Neo4j Browser

To run this query in Neo4j Sandbox, you will have to define a parameter in your Neo4j Browser session. To do so, you can use the :param command before running the Cypher statement to set a parameter for the duration of your Neo4j Browser session.

For example, to view Kevin Bacon, you can set the id parameter to "4724":

cypher
:param id: "4724"

Your Task

  • Modify the get_similar_people() method to query Neo4j and return a list of similar people.

  • The returned objects should include a list of actors and an inCommon property to show how the two people are related.

Click here to reveal the final get_similar_people() method.
python
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-chatbot-typescript/15-person-profile/api/dao/people.py[tag="getSimilarPeople"]

Testing

To test that this functionality has been correctly implemented, run the following code in a new terminal session:

sh
Running the test
pytest -s tests/15_person_profile__test.py

The test file is located at tests/15_person_profile__test.py.

Are you stuck? Click here for help

If you get stuck, you can see a working solution by checking out the 15-person-profile branch by running:

sh
Check out the 15-person-profile branch
git checkout 15-person-profile

You may have to commit or stash your changes before checking out this branch. You can also click here to expand the Support pane.

Verifying the Test

According to our algorithm, who is the most similar person to Francis Ford Coppola?

As part of the test suite, the final test will log the name of the most common person to Francis Ford Coppola according to our Cypher statement.

Paste the name of the person the box below without quotes or whitespace and click Check Answer.

  • ✓ Frederic Forrest

Hint

You can also find the answer by running the following Cypher statement:

cypher
MATCH (:Person {tmdbId: '1776'})-[:ACTED_IN|DIRECTED]->(m)<-[r:ACTED_IN|DIRECTED]-(p)
WITH p, collect(m) AS inCommon
RETURN p.name
ORDER BY size(inCommon) DESC
LIMIT 1

Copy the answer without any double quotes or whitespace.

Lesson Summary

In this Challenge, you have implemented the final methods in the PeopleDAO.

Chatbot

Hi, I am an Educational Learning Assistant for Intelligent Network Exploration. You can call me E.L.A.I.N.E.

How can I help you today?