3

So I just started with Neo4j, and I'm trying to figure out how I might populate my DataFrame. I have a dictionary of words as keys and synonyms as values in a list and I want to populate Neo4j that seems like it would be an interesting way to learn how to use the database.

An example would be:

'CRUNK' : [u'drunk', u'wasted', u'high', u'crunked', u'crazy', u'hammered', u'alcohol', u'hyphy', u'party']

The lists are not going to be of equal length so converting it to a more typical csv format is not an option, and I haven't found an explanation of how I could populate the database like I would for the SQL database in a Django app. I want to do something like this:

for each k,v in dictionary:
    add k and add relationship to each value in v

Does anyone have any tutorials, documentation or answers that could help point me in the right direction?

Community
  • 1
  • 1
Rob
  • 3,333
  • 5
  • 28
  • 71
  • http://stackoverflow.com/questions/18931614/how-to-insert-bulk-data-into-neo4j-using-python this link has a lot of the information I need, I'm going to try what they said and hopefully post an answer in a little bit. – Rob Sep 21 '15 at 19:26

2 Answers2

5

I think what you want to do you can do in Cypher directly:

MERGE (w:Word {text:{root}})
UNWIND {words} as word
MERGE (w2:Word {text:word})
MERGE (w2)-[:SYNONYM]->(w)

You would then run this statement with http://py2neo.org's cypher-session API and the two parameters, a single root word and a list of words.

you can also use foreach instead of unwind

MERGE (w:Word {text:{root}})
FOREACH (word IN {words} |
  MERGE (w2:Word {text:word})
  MERGE (w2)-[:SYNONYM]->(w)
)
Nicole White
  • 7,720
  • 29
  • 31
Michael Hunger
  • 41,339
  • 3
  • 57
  • 80
  • lol at 'words' in quotes. So I just read up on merge and I appreciate this, this is exactly what I was looking for. Thanks for the help!!!! – Rob Sep 22 '15 at 00:15
0

FINAL EDIT INCORPORATING MERGE:

This uses a dictionary to checks to make sure their output isn't NoneType or 'NOT FOUND', and populates the graph with a 'SYNONYM' relationship using the merge function to ensure their aren't duplicates.

import pickle
from py2neo import Graph
from py2neo import Node, Relationship

import random

graph = Graph(f'http://neo4j:{pw}@localhost:7474/db/data/'))
udSyn = pickle.load(open('lookup_ud', 'rb'))
myWords = udSyn.keys()

for key in myWords:
    print(key)
    values = udSyn[key]
    if values in [None, 'NOT FOUND']:
        continue
    node = graph.merge_one('WORD', 'name', key)
 
    for value in values:
        node2 = graph.merge_one('WORD', 'name', value)
        synOfNode = Relationship(node, 'SYNONYM', node2)
        graph.create(synOfNode)
        graph.push()
Rob
  • 3,333
  • 5
  • 28
  • 71