Not able to make relationship between previous and next node by neo4j-driver

110 Views Asked by At

i am storing the object of previous node and current node so that i can create relationship between previous and current node for example the string is "There is a cat", each word will be node in neo4j which will have relation ship of ":next" with next node so "there"-[]->"is" and so on but i am facing TypError : TypeError: Parameters of type Node are not supported i know there is problem with my node cypher variable but i can not figure it out, please be helpful i am stuck here

I have tried assigining previousNode.single()[0] to variable of cypher that is where the error is coming

import nltk
from neo4j import GraphDatabase
from nltk.corpus import wordnet as wn
from nltk.tokenize import sent_tokenize

class DefFromNltk:
#listToTokenize = input("Enter string for NEO4J")
text = "There is a cat. Cat is sitting on the couch. Cat is very dirty"
sentenses = sent_tokenize(text)

# Establishing Connection
graphdb = GraphDatabase.driver(uri="bolt://localhost:7687", auth=("neo4j", "huwaiza"))

# Making Session Object
session = graphdb.session()
print(sentenses)
counter1=0
counter2=0
previousNode=None
#Creating node of sentence
for sentense in sentenses:
    q1 = "CREATE(s:Sentence{label:$sentense}) return s"
    sentenceNode=session.run(q1, sentense=sentense)

    print(sentense)
    wordsFromSentence = nltk.word_tokenize(sentense)
    print(wordsFromSentence)
    counter1 = counter1 +1
    #Creating nodes of words
    for word in wordsFromSentence:
        print(word)
        q2 = "CREATE(w:Words{label:$word}) return w"
        ##################################
        wordNode = session.run(q2, word=word)
        #print(dict(wordNode.single()[0]))

        if previousNode is not None:
            print("what ther")
            #q2 = "MATCH (p:Words{label:$word}),(n:Words{label:$word}) create (p)-[:next]->(n)"
            #q2 = "MATCH (p:Words{label:$previousNode.label}),(n:Words{label:$wordNode.label}) create (p)-[:next]->(n)"
            #q2 = "MATCH (p:$previousNode),(n:$wordNode) CREATE (p)-[:next]->(n)"
            q2 = "CREATE ($previousNode)-[:next]->($wordNode)"
            session.run(q2,previousNode=previousNode.single()[0],wordNode=wordNode.single()[0])
        else:
            previousNode= wordNode
            #previousNode = wordNode
            #print(wordNode.single()[0])
            counter2 = counter2+1

print(counter1,counter2)
session.close()
deficlass  = DefFromNltk()

The error is raise TypeError(obj) TypeError:

During handling of the above exception, another exception occurred:

0

There are 0 best solutions below