Skip to content

Instantly share code, notes, and snippets.

@seandavi
Created January 10, 2012 20:49
Show Gist options
  • Select an option

  • Save seandavi/1591102 to your computer and use it in GitHub Desktop.

Select an option

Save seandavi/1591102 to your computer and use it in GitHub Desktop.
import csv
import os
os.environ['NEO4J_PYTHON_JVMARGS'] = '-Xmx2g'
from neo4j import GraphDatabase,INCOMING,OUTGOING
db = GraphDatabase('testing2')
idx = None
try:
idx = db.node.indexes.get('terms')
except ValueError:
idx = db.nodes.indexes.create('terms')
rows = []
with open('/Users/sdavis/Downloads/Thesaurus.txt','r') as thesaurus:
reader = csv.reader(thesaurus,delimiter="\t")
s = False
k=0
row = reader.next()
while(not s):
j=0
with db.transaction:
while(j<1000):
term = db.node(cid = row[0],name=row[1])
idx['name'][row[1]]=term
idx['cid'][row[0]]=term
j+=1
k+=1
try:
row = reader.next()
except StopIteration:
s = True
break
j=0
print k
with open('/Users/sdavis/Downloads/Thesaurus.txt','r') as thesaurus:
reader = csv.reader(thesaurus,delimiter="\t")
s = False
k=0
row = reader.next()
while(not s):
j=0
with db.transaction:
while(j<200):
cnode = idx['name'][row[1]].single
parents = row[2].split('|')
for parent in parents:
pnode = idx['name'][parent].single
if(pnode is None):
pnode = db.node(name=parent)
pnode.Parent_of(cnode)
cnode.Child_of(pnode)
j+=1
k+=1
try:
row = reader.next()
except StopIteration:
s = True
break
j=0
print k
db.shutdown()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment