I know the Cypher query. My requirement is I want the POS column rows attached as a property to each word. Example Node Dog has has POS NOUN so NOUN should be attached as a property to that node and the NEXT relationship should be maintained as shown above.
How can I write the query in python notebook and see the same results in Neo4j graph? Please assist me with the syntax as I am pretty new to Neo4j and Cypher?
Here is what I have done so far.
I have py2neo and neo4j installed in my PC.
I wish to run the cypher query from my python notebook and the changes should reflect in NEO4j graph
Already know some basic like
import pandas as pd
from py2neo import Graph,Node,Relationship
from neo4j import GraphDatabase, basic_auth
graph = Graph("http://localhost:7474/browser/", auth=("neo4j", "*****"))
for index, row in df.iterrows():
tx = graph.begin()
tx.evaluate('''cypher query goes here''')
tx.commit()
From python notebook by using a dataframe putting the value of second column POS as property and maintaining the Next relationship in the first column as shown above
@koji No, I am not looking for this. The challenge I am facing is on jupyter notebook how do I perform the same operation via pandas dataframe from python notebook
import pandas as pd
from py2neo import Graph,Node,Relationship
from neo4j import GraphDatabase, basic_auth
graph = Graph("http://localhost:7474/browser/", auth=("neo4j", "*****"))
for index, row in df.iterrows():
tx = graph.begin()
tx.evaluate('''cypher query goes here''')
tx.commit()
neointerface package has load_df method, however in order to create NEXT relationships btw words you need to load the dataframe with index column and run an additional query. try something like:
The load_df will not work for you in this specific use case. Solved your problem as follows. Note that you need to install apoc library to make it work:
import spacy
import en_core_web_sm
import pandas as pd
nlp = spacy.load("en_core_web_sm")
text = "The wild is dangerous"
doc = nlp(text)
cols = ("text", "POS")
rows = []
for t in doc:
row = [t.text, t.pos_]
rows.append(row)
df = pd.DataFrame(rows, columns = cols)
#clean-up
db.query("MATCH (w:Word) detach delete w")
db.create_index("Word", "index")
q = """
UNWIND $data as row
MERGE (w:Word{text:row.text})
ON CREATE SET w.POS = [row.POS]
ON MATCH SET w.POS = CASE WHEN row.POS in w.POS THEN w.POS ELSE w.POS + [row.POS] END
WITH collect(w) as coll
WITH apoc.coll.pairsMin(coll) as pairs
UNWIND pairs as pair
WITH pair[0] as node1, pair[1] as node2
MERGE (node1)-[:NEXT]->(node2)
"""
db.query(q, {'data': df.to_dict(orient='records')})
text = "The rockstar is wild"
doc = nlp(text)
cols = ("text", "POS")
rows = []
for t in doc:
row = [t.text, t.pos_]
rows.append(row)
df_2 = pd.DataFrame(rows, columns = cols)
db.query(q, {'data': df_2.to_dict(orient='records')})