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>
Related
I am trying to run this code:
from find_job_titles import FinderAcora
finder=FinderAcora()
finder.findall('IT Audit & Governance')
But it gives me this error everytime:
---------------------------------------------------------------------------
StopIteration Traceback (most recent call last)
/usr/local/lib/python3.8/dist-packages/find_job_titles/__init__.py in longest_match(matches)
48 """
---> 49 longest = next(matches)
50
StopIteration:
The above exception was the direct cause of the following exception:
RuntimeError Traceback (most recent call last)
1 frames
<ipython-input-31-5b965ac3d7be> in <module>
----> 1 finder.findall('IT Audit & Governance')
/usr/local/lib/python3.8/dist-packages/find_job_titles/__init__.py in findall(self, string, use_longest)
82 else return all overlapping matches
83 :returns: list of matches of type `Match`
---> 84 """
85 return list(self.finditer(string, use_longest=use_longest))
86
RuntimeError: generator raised StopIteration
I tried using the suggestions from this Stack Overflow post but it didn't work.
I had this problem when trying to solve a optimization problem using pulp.
The code:
import pulp
import numpy as np
import math
prob = pulp.LpProblem("example", pulp.LpMaximize)
# Variable represent number of times device i is used
d = pulp.LpVariable("d", cat=pulp.LpContinuous,lowBound=0,upBound=np.inf)
var = pulp.LpVariable("var", cat=pulp.LpContinuous,lowBound=0,upBound=np.inf)
# The objective function that we want to maximize
n = len(y_arfima)
prob += -(n/2) * np.log(var) - np.sum([np.log((math.gamma(t)*math.gamma(t-2*d))/(math.gamma(t-d)**2)) for t in range(1,n+1)])/2 - 1/2
# Actually solve the problem, this calls GLPK so you need it installed
pulp.GLPK().solve(prob)
# Print out the results
for v in prob.variables():
print (v.name, "=", v.varValue)
The error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
AttributeError: 'LpVariable' object has no attribute 'log'
The above exception was the direct cause of the following exception:
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_12336/3027528854.py in <module>
11 # The objective function that we want to maximize
12 n = len(y_arfima)
---> 13 prob += -(n/2) * np.log(var) - np.sum([np.log((math.gamma(t)*math.gamma(t-2*d))/(math.gamma(t-d)**2)) for t in range(1,n+1)])/2 - 1/2
14
15 # Actually solve the problem, this calls GLPK so you need it installed
TypeError: loop of ufunc does not support argument 0 of type LpVariable which has no callable log method
AttributeError Traceback (most recent call last)
AttributeError: 'LpVariable' object has no attribute 'log'
The above exception was the direct cause of the following exception:
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_12336/3027528854.py in
11 # The objective function that we want to maximize
12 n = len(y_arfima)
---> 13 prob += -(n/2) * np.log(var) - np.sum([np.log((math.gamma(t)math.gamma(t-2d))/(math.gamma(t-d)**2)) for t in range(1,n+1)])/2 - 1/2
14
15 # Actually solve the problem, this calls GLPK so you need it installed
TypeError: loop of ufunc does not support argument 0 of type LpVariable which has no callable log method
Can you help me?
Thanks!
I am trying to slice each sentence in a list of sentences from [0:10] character of it.
Example of list of sentences: list name = sd_list
['I was born and brought up in Delhi.',
'I am using Dell Latitude E5140 laptop since 2012.',
'I work for ABC company since 2014.']
I tried to slice the first 10 characters of each sentence by running the below code and failed.
sent10 = [s[0:10] for s in sd_list]
By running this I encountered below TypeError
TypeError Traceback (most recent call last)
in ()
----> 1 [s[0:10] for s in sd_list]
in (.0)
----> 1 [s[0:10] for s in sd_list]
TypeError: 'float' object is not subscriptable
--> I even tried defining a function :
def sent_slice(text):
for s in range(0,len(text)):
text[s] = text[s][0:10]
return text
sent_slice(sd_list)
TypeError Traceback (most recent call last)
in ()
----> 1 sent_slice(sd_list)
in sent_slice(text)
1 def sent_slice(text):
2 for s in range(0,len(text)):
----> 3 text[s] = text[s][0:10]
4 return text
TypeError: 'float' object is not subscriptable
Could someone help me understand this "TypeError: 'float' object is not subscriptable" . How can I achieve my goal of slicing sentence?
it means that you have a float in sd_list. you can find it by doing something like:
print([f for f in sd_list if isinstance(f, float)])
NameError: name 'the_shape' is not defined
I get the following error when I try to import my KMeans module containing the class named KMeansClass. The KMeans.py module has the following structure:
import numpy as np
import scipy
class KMeansClass:
#takes in an npArray like object
def __init__(self,dataset,the_shape=5):
self.dataset=dataset
self.mu = np.empty(shape=the_shape)
and in ipython when I try
import KMeans
I get the NameError: name 'the_shape' is not defined
I am really new to python OOP and don't know why this is happening as all I'm doing is passing arguments to init and assigning those arguments to instance variables.
Any help would be appreciated.
Thanks in advance!
Full Traceback:
NameError Traceback (most recent call last)
<ipython-input-2-44169aae5584> in <module>()
----> 1 import kmeans
/Users/path to file/kmeans.py in <module>()
1 import numpy as np
2 import scipy
----> 3 class KMeansClass:
4 #takes in an npArray like object
5 def __init__(self,dataset,the_shape=5):
/Users/path_to_file/kmeans.py in KMeansClass()
5 def __init__(self,dataset,the_shape=5):
6 self.dataset=dataset
----> 7 self.mu = np.empty(shape=the_shape)
8
9
NameError: name 'the_shape' is not defined
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.