Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
05-06-2022 03:50 AM
Hi All
I am new to cypher or neo4j and still trying to learn the craft. I am stuck on an issue. I have a dataframe which I want to transfer to neo4j graph using cypher. Here are the details
What I am trying to achieve via cypher query
import pandas as pd
from py2neo import Graph,Node,Relationship
from neo4j import GraphDatabase, basic_auth
for index, row in df.iterrows():
tx = graph.begin()
tx.evaluate('''
Merge (W1:word{term:row.Word1)
Merge(W2:word{term:row.Word2)
Merge (W1)-[r:Next]->(W2)
set r.start = row.POS1
set r.end = row.POS2
''',parameters = {row.Word1, row.Word2})
tx.commit()
Objective: The "next" relationship has a direction and I am attaching the POS values into the specific relationships. I would then create the word nodes with the unique property being term. In this way I wish to achieve the words are connected and I have the POS in the relation properties.
05-08-2022 12:42 AM
parameters = {"row" : row}
05-08-2022 11:30 AM
Hey Sanjay thanks for you response I tried your suggestion but I am still getting error stating----> TypeError: Neo4j does not support JSON parameters of type Series
05-09-2022 12:38 AM
Sorry. Try this.
tx.evaluate('''
Merge (W1:word{term:$Word1)
Merge(W2:word{term:$Word2)
Merge (W1)-[r:Next]->(W2)
set r.start = $POS1
set r.end = $POS2
''',parameters = {"Word1":row.Word1, "Word2": row.Word2,"POS1":row.POS1, "POS2":row.POS2})
tx.commit()
05-11-2022 04:14 PM
This failed again. I tried couple of more things but didn't work for me.
I am attaching the query so that you can replicate this at your end
from py2neo import Graph,Node,Relationship
from neo4j import GraphDatabase, basic_auth
for index, row in df.iterrows():
tx = graph.begin()
tx.evaluate('''
Merge (W1:word{term:$Word1)
Merge(W2:word{term:$Word2)
Merge (W1)-[r:Next]->(W2)
set r.start = $POS1
set r.end = $POS2
''',parameters = {"Word1":row.Word1, "Word2": row.Word2,"POS1":row.POS1, "POS2":row.POS2})
tx.commit()
Dataframe is like
05-11-2022 08:28 PM
It’s a simple typo. There’s a missing closing curly brace at the location pointed in error message. In the next line too.
05-12-2022 01:09 AM
@sanjaysingh13 .Thanks for pointing that out. It was a miss from my end apologies. I was able to execute that now. However, looking at the results the POS did not get populated. The output I got has
{
"identity": 26,
"labels": [
"word"
],
"properties": {
"term": "his"
}
}
{
"identity": 27,
"labels": [
"word"
],
"properties": {
"term": "dog"
}
}
{
"identity": 28,
"labels": [
"word"
],
"properties": {
"term": "eats"
}
}
{
"identity": 29,
"labels": [
"word"
],
"properties": {
"term": "turkey"
}
}
{
"identity": 30,
"labels": [
"word"
],
"properties": {
"term": "on"
}
}
{
"identity": 31,
"labels": [
"word"
],
"properties": {
"term": "tuesday"
}
}
The idea is to attach the POS in properties and label as words
05-12-2022 03:57 AM
Hi,
Can you mail me a copy of df? Just pickle it.
05-12-2022 05:11 AM
05-12-2022 07:48 AM
Are you sure you want Nodes like (:his {prop:"PRON"}), (:dog {prop:"NOUN"})? Please share what you expect your Graph to look like. Even a pencil-sketch would be fine.
05-12-2022 10:23 AM
@sanjaysingh13
Sure I will try to explain here.
Main task mentioned in the bullets
The final goal is to make a graph in such way where I search a word and I get what all POS it has been used in basically to get the context.
For now I just want 2 things
05-12-2022 11:50 PM
The ideal way to model your requirements would be to have Sentence, Word and Function (POS). You don't want to repeat POS as a property because POS is generic.
(:Word {term:"dog")-[:APPEARS_AS]-(p1:POS)-[:IN]-(:Sentence {phrase:"The dog ate the book")
(:Word {term:"ate")-[:APPEARS_AS]-(p2:POS)-[:IN]-(:Sentence {phrase:"The dog ate the book")
(:Word {term:"dog")-[:NEXT]-(:Word {term:"ate")
(p1)-[:IS_PART_OF_SPEECH]-(:Function {pos:"Subject"})
(p2)-[:IS_PART_OF_SPEECH]-(:Function {pos:"Verb"})
All the sessions of the conference are now available online