Get startedGet started for free

Creating text chunks

The text from Romeo and Juliet has been loaded into a lexical graph with a structure of Acts and Scenes, but the text is still too large to generate meaningful embeddings. Complete the code to split the text from Act I, Scene I into smaller chunks for each line.

The visualize_graph() function will display the nodes and relationships in the graph document.

This exercise is part of the course

Graph RAG with LangChain and Neo4j

View Course

Exercise instructions

  • Append each line_node to the graph_document object.
  • Create HAS_LINE and SPOKEN_BY relationships between each scene and line_node, and line_node and character, respectively, and add them to graph_document.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

graph_document = GraphDocument(nodes=[scene], relationships=[])
characters = {}

for i, line in enumerate(lines[20:40]): 
    character, text = get_character_and_line(line)

    line_node = Node(type="Line", id=f"{scene.id}-line-{i}", properties={"raw": line,"character": character, "text": text, "caption": f"{text[:15]}..."})

    if character not in characters:
        character_node = Node(type="Character", id=character, properties={"name": character})
        characters[character] = character_node
        graph_document.nodes.append(character_node)

    # Add nodes to the array
    graph_document.____.append(____)

    # Add relationship to the array
    graph_document.____.extend(
        [Relationship(source=scene, target=line_node, type="____"),
        Relationship(source=line_node, target=characters[character], type="SPOKEN_BY")]
    )

visualize_graph(graph_document)
Edit and Run Code