cannot reproduce bulbs/py2neo in python to create edges (relationships) - python

I am trying to sequentially insert vertices and edges in neo4j using python. The existing nodes aren't recognised as such when I add edges. Whether I use py2neo or bulbs I got a similar error message.
Note I am working with:
linux64
python2.7
bulbs0.3
py2neo1.5
neo4j-community1.8.2
With bulbs:
>>> from bulbs.neo4jserver import Graph
>>> g = Graph()
>>> g.vertices.create(name="James")
>>> g.vertices.create(name="Julie")
>>> g.edges.create(james, "knows", julie)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-46-9ba24256218d> in <module>()
----> 1 g.edges.create(james, "knows", julie)
NameError: name 'james' is not defined
With py2neo
from py2neo import neo4j
graph=neo4j.GraphDatabaseService()
node=graph.create({"name":'James'},{'name':'Julie'})
rel=graph.create((james,"knows",julie))
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-8-591f826cfd05> in <module>()
2 graph=neo4j.GraphDatabaseService()
3 node=graph.create({"name":'James'},{'name':'Julie'})
----> 4 rel=graph.create((james,"knows",julie))
NameError: name 'james' is not defined
Moreover I got the same error with bulbs if I use rexster instead of neo4j, i.e.
>>> from bulbs.rexster import Graph
>>> g = Graph()
>>> g.vertices.create(name="James")
>>> g.vertices.create(name="Julie")
>>> g.edges.create(james, "knows", julie)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-6-2cfb5faa42d1> in <module>()
3 g.vertices.create(name="James")
4 g.vertices.create(name="Julie")
----> 5 g.edges.create((james, "knows", julie))
NameError: name 'james' is not defined
What's wrong here?
Thanks

Your application variables james and julie won't automatically be created simply by creating nodes with a similar name property. You haven't shared any of your py2neo code and I'm not familiar with bulbs but within py2neo you will need to do something like:
from py2neo import neo4j
graph_db = neo4j.GraphDatabaseService()
james, julie = graph_db.create(node(name="James"), node(name="Julie"))
graph_db.create(rel(james, "KNOWS", julie))
You could of course instead create both nodes and relationship in the same statement (and batch) if you preferred:
from py2neo import neo4j
graph_db = neo4j.GraphDatabaseService()
james, julie, friendship = graph_db.create(
node(name="James"), node(name="Julie"), rel(0, "KNOWS", 1)
)

You're not setting the james or julie vars on your create statements.
Here's the proper code:
>>> from bulbs.neo4jserver import Graph
>>> g = Graph()
>>> james = g.vertices.create(name="James")
>>> julie = g.vertices.create(name="Julie")
>>> g.edges.create(james, "knows", julie)
See the Bulbs Quickstart for more examples.

Related

How to get a list of all tokens from Lucene 8.6.1 index using PyLucene?

I have got some direction from this question. I first make the index like below.
import lucene
from org.apache.lucene.analysis.standard import StandardAnalyzer
from org.apache.lucene.index import IndexWriterConfig, IndexWriter, DirectoryReader
from org.apache.lucene.store import SimpleFSDirectory
from java.nio.file import Paths
from org.apache.lucene.document import Document, Field, TextField
from org.apache.lucene.util import BytesRefIterator
index_path = "./index"
lucene.initVM()
analyzer = StandardAnalyzer()
config = IndexWriterConfig(analyzer)
if len(os.listdir(index_path))>0:
config.setOpenMode(IndexWriterConfig.OpenMode.APPEND)
store = SimpleFSDirectory(Paths.get(index_path))
writer = IndexWriter(store, config)
doc = Document()
doc.add(Field("docid", "1", TextField.TYPE_STORED))
doc.add(Field("title", "qwe rty", TextField.TYPE_STORED))
doc.add(Field("description", "uio pas", TextField.TYPE_STORED))
writer.addDocument(doc)
writer.close()
store.close()
I then try to get all the terms in the index for one field like below.
store = SimpleFSDirectory(Paths.get(index_path))
reader = DirectoryReader.open(store)
Attempt 1: trying to use the next() as used in this question which seems to be a method of BytesRefIterator implemented by TermsEnum.
for lrc in reader.leaves():
terms = lrc.reader().terms('title')
terms_enum = terms.iterator()
while terms_enum.next():
term = terms_enum.term()
print(term.utf8ToString())
However, I can't seem to be able to access that next() method.
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-47-6515079843a0> in <module>
2 terms = lrc.reader().terms('title')
3 terms_enum = terms.iterator()
----> 4 while terms_enum.next():
5 term = terms_enum.term()
6 print(term.utf8ToString())
AttributeError: 'TermsEnum' object has no attribute 'next'
Attempt 2: trying to change the while loop as suggested in the comments of this question.
while next(terms_enum):
term = terms_enum.term()
print(term.utf8ToString())
However, it seems TermsEnum is not understood to be an iterator by Python.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-48-d490ad78fb1c> in <module>
2 terms = lrc.reader().terms('title')
3 terms_enum = terms.iterator()
----> 4 while next(terms_enum):
5 term = terms_enum.term()
6 print(term.utf8ToString())
TypeError: 'TermsEnum' object is not an iterator
I am aware that my question can be answered as suggested in this question. Then I guess my question really is, how do I get all the terms in TermsEnum?
I found that the below works from here and from test_FieldEnumeration() in the test_Pylucene.py file which is in pylucene-8.6.1/test3/.
for term in BytesRefIterator.cast_(terms_enum):
print(term.utf8ToString())
Happy to accept an answer that has more explanation than this.

name 'FillMissing' is not defined

When I run following code:
df = pd.read_csv('../input/marketingrar/marketing.csv')
df.head()
dep_var = 'Revenue'
cat_names = ['Day_Name','Promo']
cont_names = ['Date','Week','Month','Month_ID','Year','Visitors','Marketing Spend']
procs = [FillMissing, Categorify, Normalize]
I got this error bellow:
NameError Traceback (most recent call
last) in
----> 1 procs = [FillMissing, Categorify, Normalize]
NameError: name 'FillMissing' is not defined
P.S. I'm using Kaggle notebook. Why this error occurs and how to solve it?
from fastai.tabular.all import *
is the only working solution for me
With this code, you are trying to initiate a list named procs with the 3 references to FillMissing, Categorify and Normalise, but you never created those references before.
Did you maybe want to create a list of 3 strings? Then you forgot the '', compare the other lists like cat_names or cont_names
Maybe it could also help to include
from fastai import *
from fastai.tabular import *

TypeError: 'module' object is not callable in python and pandas

I imported the module correctly into pandas and called it correctly using import main and then main.main(data, 1,10,2.5) but I am getting an error:
TypeError Traceback (most recent call last)
<ipython-input-52-e9913b227737> in <module>()
----> 1 main.main(data, 1, 10, 2.5)
38 dat_sh = data.shape[0]
39 #Z = random.sample(range(0,U),k_max)
---> 40 Z = cf.centroid_finder(data,sp_atr,k_max)
41
42 prototypes = {i: data[j:j+1].values.tolist()[0] for i,j in enumerate(Z)}
11 for i in range(dat_sh):
12 for j in range(dat_sh):
---> 13 D[i][j] = ed(sub_atr[i],sub_atr[j])
14
15
TypeError: 'module' object is not callable
ed is euclidean:
def ed(X2, X1):
return sqrt(sum(np.subtract(X1,X2)**2))
There may be some confusion regarding the importing of modules vs functions.
It's possible to recreate your error with a simple example, assuming that module/function importing has gotten mixed up somewhere along the way. Consider a case where we pass initial values a and b through a centroid_finder() function, which lives in cf.py:
# import cf.py as cf
import cf
a, b = ([1,2,3], [2,3,4])
cf.centroid_finder(a, b)
But centroid_finder() calls ed(), which lives in ed.py:
## cf.py
# import ed.py as ed
import ed
def centroid_finder(a, b):
print(ed(a, b))
## ed.py
from numpy import sqrt, sum
import numpy as np
def ed(X2, X1):
return sqrt(sum(np.subtract(X1,X2)**2))
Here, calling centroid_finder() will give the error you observed:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-1-e76ec6a0496c> in <module>()
3 a, b = ([1,2,3], [2,3,4])
4
----> 5 cf.centroid_finder(a, b)
cf.py in centroid_finder(a, b)
2
3 def centroid_finder(a, b):
----> 4 print(ed(a, b))
TypeError: 'module' object is not callable
That's because you imported a module, ed.py as ed...but what you wanted was to call the ed() function that lives inside of ed.py. That's ed.ed()!
Changing centroid_finder() to call ed.ed() produces the desired result:
# cf.py
import ed
def centroid_finder(a, b):
print(ed.ed(a, b))
Now, from the main script:
cf.centroid_finder(a, b)
# 1.73205080757
There's at least one undisclosed import shorthand in your example code, where you call sqrt in ed(). There isn't a natural sqrt() in Python, it most likely is imported from either math or numpy, e.g. from numpy import sqrt. That's not a problem, per se, but given the error you're getting about modules being non-callable, you might benefit from explicitly calling functions in your code from the modules they live in.
For example, using import numpy as np, call np.sqrt() instead of just importing sqrt() directly. This is a defensive programming posture that will prevent similar confusion in the future. (You do this already with np.subtract() in ed(); it's unclear why sqrt() doesn't get the same treatment.)

My freq-dist function keeps coming up undefined (python nltk) 3.4

I'm doing NLP with Python 3.4, and my frequency distribution function keeps returning as undefined, even after I call on "import nltk..." I appreciate any help. I am not having any other issues. I have Windows 7, 64 bit
Here is the code:
from nltk.book import *
text1
Out[39]: <Text: Moby Dick by Herman Melville 1851>
fdist1 = FreqDist(text1)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-40-a9ccb6c27929> in <module>()
----> 1 fdist1 = FreqDist(text1)
NameError: name 'FreqDist' is not defined
fdist1
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-41-f986ce66c258> in <module>()
----> 1 fdist1
NameError: name 'fdist1' is not defined
import nltk
text1 = nltk.book.text1
fdist1 = nltk.FreqDist(text1)
print(fdist1)
Result:
<FreqDist with 19317 samples and 260819 outcomes>

python and networkX keyerror

i have this problem in python, python keeps giving me an keyerror: weight
g.add_edge(1,3,weight=2.5)
g[1][2]['weight'] = 1.5
for n1,n2,attr in g.edges(data=True):
print n1,n2,attr['weight']
The output.
KeyError Traceback (most recent call last)
<ipython-input-56-832c29e7e1db> in <module>()
2 g[1][2]['weight'] = 1.5
3 for n1,n2,attr in g.edges(data=True):
----> 4 print n1,n2,attr['weight']
KeyError: 'weight'
0 1
i dont know why weight gives me an error?
My guess is that you have some other edges in your graph, and haven't set the weight attribute for all of them. Try the following:
for n1,n2,attr in g.edges(data=True):
print n1,n2,attr
See if attr contains a value for weight in every case.

Categories