I have written a very simple piece of code to try and print the synonyms associated with a word.
import nltk
from nltk.corpus import wordnet as wn
wordNetSynset = wn.synsets('small')
for synSet in wordNetSynset:
for synWords in synSet.lemma_names:
synonymList.add(synWords)
print synonymList
However, I get the following error:
Traceback (most recent call last):
File "test.py", line 6, in <module>
for synWords in synSet.lemma_names:
TypeError: 'instancemethod' object is not iterable
Does anyone know what the problem could be?
In Nltk 3, the lemma_names has been changed to a method from an attribute.
So you have to call the method
for synWords in synSet.lemma_names():
Other minor changes required are:
synonymList is not defined
List will not have an add method even if synonymList is defined
You better name your variable synonymSet
Related
This is my code
w2v = Word2Vec(vector_size=150,min_count = 10)
w2v.build_vocab(x_train)
w2v.train(x_train)
def average_vec(text):
vec = np.zeros(300).reshape((1,300))
for word in text:
try:
vec += w2v[word].reshape((1,300))
except KeyError:
continue
return vec
And this throws the following error:
Traceback (most recent call last): File
"C:/Users/machao/Desktop/svm-master/word2vec.py", line 27, in <module>
train_vec = np.concatenate([average_vec(z) for z in x_train]) File "C:/Users/machao/Desktop/svm-master/word2vec.py", line 27, in
<listcomp>
train_vec = np.concatenate([average_vec(z) for z in x_train]) File "C:/Users/machao/Desktop/svm-master/word2vec.py", line 21, in
average_vec
vec += w2v[word] TypeError: 'Word2Vec' object is not subscriptable
Process finished with exit code 1
The Word2Vec model object itself – w2v in your code – no longer supports direct access to individual vectors by lookup word key, in Gensim 4.0 and above.
Instead, you should use the subsidiary object in its .wv property - an object of type KeyedVectors which can be used to work with the set of word-vectors separately. (Separating functionality like this helps in cases where you only want the word-vectors, or only have the word-vectors from someone else, but not the full model's overhead.)
So, everywhere you might use w2v[word], try w2v.wv[word] instead.
Or perhaps, name things more like the following, and hold a different variable reference to the word-vectors:
w2v_model = Word2Vec(...)
word_vectors = w2v_model.wv
print(word_vectors[word])
For other tips in adapting your own older code, or examples online, to Gensim 4.0, the following project wiki page may be helpful:
Migrating from Gensim 3.x to 4
I have another SoCo questions and I really hope someone can get me started.
I'm really pulling my hair out here. What am I doing wrong?
>>> from soco.music_library import MusicLibrary
>>> MusicLibrary.get_music_library_information('artists', search_term='Metallica')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: get_music_library_information() missing 1 required positional argument: 'search_type'
I copied the get_music_library_information('artists', search_term='Metallica') straight out of the docs.
Thanks for having a go Bahrom. I got it to take action in the following way:
First i got the list of speakers with a
speakers=soco.discover()
and then I selected one of the speakers, calling it 'speaker'.
>>> for speaker in speakers:
... if speaker.player_name == 'Office':
... break
Then I appended the get command on to the speaker e.g.
>>> from soco.music_library import MusicLibrary
>>> speaker.get_music_library_information('genres')
and this works :)
Haven't tested this, but looking at music_library.py on github, I think you just need to instantiate MusicLibrary first:
>>> from soco.music_library import MusicLibrary
>>> MusicLibrary().get_music_library_information('artists', search_term='Metallica')
I'm new to Python (and am using Spyder) and am trying to create some histograms of when the top movies from IMDB were created. I have imported the matplotlib, numpy and pandas, as well as the .txt file, but when I run the following lines of code:
plt.hist(data.year, bins=np.arange(1950, 2013), color='#cccccc')
I receive an error message:
Traceback (most recent call last):
File "stdin", line 1, in <module>
AttributeError: 'module' object has no attribute 'hist'
What am I doing wrong?
Your question provide very poor information and insight in your code. More data..
Meanwhile check if you actually import your modules correctly, should have:
import matplotlib.pyplot as plt
in order to use hist function
The generate method of nltk.text.Text seems to have been removed in NLTK 3.0.
For example:
>>> bible = nltk.corpus.gutenberg.words(u'bible-kjv.txt')
>>> bibleText = nltk.Text(bible)
>>> bibleText.generate()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Text' object has no attribute 'generate'
It may just be that I'm remembering wrongly how to do this, but everything I can find online seems to support the above method. Any ideas what I'm doing wrong?
A note in the first online chapter of the NLTK book says that:
The generate() method is not available in NLTK 3.0 but will be
reinstated in a subsequent version.
I am working on map automation using arcpy.
I need to add a legend on the map layout based on the layers added to the mxd.I am using the code below (as given on the tutorial):
import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]
lyrFile = arcpy.mapping.Layer(r"C:\Project\Data\Rivers.lyr")
arcpy.mapping.AddLayer(df, lyrFile, "TOP")
styleItem = arcpy.mapping.ListStyleItems("USER_STYLE", "Legend Items", "NewDefaultLegendStyle")[0]
legend = arcpy.mapping.ListLayoutElements(mxd, "LEGEND_ELEMENT")[0]
legend.updateItem(lyrFile, styleItem)
But everytime I run this code i get the following error:
Runtime error
Traceback (most recent call last):
File "<string>", line 1, in <module>
AttributeError: 'list' object has no attribute 'updateItem'
What could cause this error to appear?
What could cause this error to appear?
Well, I am not familiar with arcpy, but it seems the 0th element of whatever ListLayoutElements() returns is a list which indeed has no updateItem() method.
You might want to .append() to the list, or you might want to have a different type of object.
Your code is the same as ArcGIS Help example,
http://resources.arcgis.com/zh-cn/help/main/10.2/index.html#//00s30000006z000000
I tested the example code and it ran correctly.
By the way, I am wondering if you had pasted your own code. Otherwise you probably encounter problem in line 2,4,6 rather than the last line.
As the user2357112 suggested, you'd better try it again with clean code. Or you can confirm the type of the variable "legend" just by print type(legend)before the line
legend.updateItem(lyrFile, styleItem)