Find answers from the community

Updated 4 months ago

Nodes

hello there, I am trying to replace text within a node of a markdown document however having some trouble. Is there anything wrong with the code below? the text are just not changed.

sample document:
Plain Text
# A
- text for A

# B
- text for b


code:
Plain Text
from llama_index.core.node_parser import MarkdownNodeParser
parser = MarkdownNodeParser()

for page in document:       
    page_nodes = parser.get_nodes_from_documents([page])
    for node in page_nodes:
        if node.metadata['Header_1'] == 'B':
            node.text = 'New text for B'
        print(node.text) # text is changed
    print(page.text) # text is not changed!
L
g
7 comments
page and page_nodes are two separate objects.

You are modifying only the nodes, but that has nothing to do with the page
ok so what I did was reconstruct a new page out of all old and new node text
Hmm? I don't think so?

In this code sample you modified the page_nodes for each page, but you did not modify the page
haha i meant i already fix my issue by doing what i said in my previous text
sorry wasn't being clear
Plain Text
reconstructed_nodes_text = ''
for page in document:       
    page_nodes = parser.get_nodes_from_documents([page])
    for node in page_nodes:
        if node.metadata['Header_1'] == 'B':
             reconstructed_nodes_text += '# B' + 'New text for B'
        else:
             reconstructed_nodes_text += '# ' + node.text + '\n\n'

reconstructed_page = Document(text=reconstructed_nodes_text)
Oh great! 😁
Add a reply
Sign up and join the conversation on Discord