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.
Este exercício faz parte do curso
Graph RAG with LangChain and Neo4j
Instruções do exercício
- Append each
line_node
to thegraph_document
object. - Create
HAS_LINE
andSPOKEN_BY
relationships between eachscene
andline_node
, andline_node
andcharacter
, respectively, and add them tograph_document
.
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
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)