How do I generate random text in NLTK 3.0? - python

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.

Related

Attribute error from SoCo class MusicLibrary that documentation appears to say is not an error

I am doing initial exploration in the SoCo module which is a Python API for Sonos. Following the tutorial I do
>>> speakers = soco.discover()
>>> speaker = speakers.pop()
>>> speaker.player_name
'Portable'
>>> speaker.ip_address
'192.168.0.11'
>>> speaker.get_current_transport_info()['current_transport_state']
'STOPPED'
So far, so good. Now, still following the documentation, I do
>>> speaker.music_library.list_library_shares()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'MusicLibrary' object has no attribute 'list_library_shares'
and, sure enough, dir(speaker.music_library) confirms there is no such method.
But the documentation for this class says:
Listing and deleting music library shares
Music library shares are the local network drive shares connected to Sonos, which host the
audio content in the Sonos Music Library.
To list the shares connected to Sonos, use the list_library_shares()
method as follows:
››› device.music_library.list_library_shares()
['//share_host_01/music', '//share_host_02/music']
The result is a
list of network share locations.
Now either the documentation is badly out of step with the version I just downloaded (0.18.1; it says not), or I need another pair of eyes to point out what I am doing wrong.
I asked this question also on the SoCo Google group and this is the answer I got:
This function is not included in v0.18.1, but will be in the upcoming
v0.19 release. It's unintuitive but you need to look at the v0.18.1
documentation at:
http://docs.python-soco.com/en/v0.18.1/api/soco.music_library.html
... not the 'latest' version of the documentation.
I forbear to remark on the wisdom of pointing readers of the Getting Started page in the wrong direction.

AttributeError: 'English' object has no attribute 'vocal'

Im running this code with en_core_web_sm 2.2.5
>>> import spacy
>>> nlp = spacy.load('en_core_web_sm', parser=False)
>>> print(nlp.vocal[u'fun'].similarity(nlp.vocal[u'humour']))
Traceback (most recent call last): File "", line 1, in
AttributeError: 'English' object has no attribute 'vocal'
First of all, I think you meant vocab instead of vocal.
Second of all, you are trying to access the word-vector and vocab has nothing to do with that.
Finally, you are using the en_core_web_sm model which doesn't support word-vectors according to spaCy official documentation here.
My suggestion is to use en_core_web_md instead. You can download it using the following command:
python -m spacy download en_core_web_md
And you can change your code to be:
>>> import spacy
>>> nlp = spacy.load('en_core_web_md', parser=False)
>>> nlp.(u'fun').similarity(nlp(u'humour'))
0.43595678034197044

'RiakBucket' object has no attribute 'new_binary'

As I'm trying to store a PNG image file into my riakBucket. As per https://riak-python-client.readthedocs.io/en/1.5-stable/tutorial.html documentation described here actually using riakBucketObject.new_binary().
But when I'm trying to do this over my system, this error is pop-up:
My python script is :
>>> import riak
>>> myClient = riak.RiakClient(pb_port=8087, protocol='pbc')
>>> photo_bucket = myClient.bucket('photo-bucket')
>>> file_data = open('/home/kamli/Pictures/Store3.png','rb').read()
>>> key = photo_bucket.new_binary('myphoto', data=file_data, content_type='image/png')
But error is :
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'RiakBucket' object has no attribute 'new_binary'
System Configuration:
Python version - 2.7.6
Riak Version - 2.2.0
Riak 2.2 has changed since 1.5 and the current method to get a new RiakObject from a bucket is simply using RiakBucket.new() or RiakBucket.new_from_file(). The documentation can be found on their readthedocs website. Look for the version switcher near the bottom to look at documentation for each major release.
In addition to what Aaron3468 said, I would like to mention that the Riak Python Client's version does not match Riak's version. The client is versioned according to semver and the latest release is available here.
Please use the latest documentation.

Searching Sonos Music Library with SoCo in Python

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')

Error when accessing synonyms in python using nltk?

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

Categories

Resources