I want to add 'like' at the end of every other 3 words in a text. Then I try to use insert as follows. However, it told me that "AttributeError: 'ConcatenatedCorpusView' object has no attribute 'insert'". Quite puzzled? Isn't text a list here? If it is a list, why doesn't not insert work here?
>>> text = nltk.corpus.brown.words(categories = 'news') >>> def hedge(text):
for i in range(0, len(text),3): text.insert(i,'like')
>>> hedge(text)
Traceback (most recent call last): File "<pyshell#33>", line 1, in <module> hedge(text) File "<pyshell#32>", line 3, in hedge text.insert(i,'like') AttributeError: 'ConcatenatedCorpusView' object has no attribute 'insert'
Further more, I test the function of insert. It did work as follows:
>>> text = ['The', 'Fulton', 'County', 'Grand'] >>> text.insert(3,'like') >>> text
> I want to add 'like' at the end of every other 3 words in a text. Then I
> try to use insert as follows. However, it told me that "AttributeError:
> 'ConcatenatedCorpusView' object has no attribute 'insert'". Quite puzzled?
> Isn't text a list here? If it is a list, why doesn't not insert work here?
>>>> text = nltk.corpus.brown.words(categories = 'news')
>>>> def hedge(text):
> for i in range(0, len(text),3):
> text.insert(i,'like')
>>>> hedge(text)
> Traceback (most recent call last):
> File "<pyshell#33>", line 1, in <module>
> hedge(text)
> File "<pyshell#32>", line 3, in hedge
> text.insert(i,'like')
> AttributeError: 'ConcatenatedCorpusView' object has no attribute 'insert'
> Further more, I test the function of insert. It did work as follows:
>>>> text = ['The', 'Fulton', 'County', 'Grand']
>>>> text.insert(3,'like')
>>>> text
> ['The', 'Fulton', 'County', 'like', 'Grand']
> Isn't text in NLTK corpus in a list format already? I am puzzled. I tested
> the text from nltk as follows. It is a list actually. See the following:
>>>> text = nltk.corpus.brown.words(categories = 'news')
>>>> text[:10]
> ['The', 'Fulton', 'County', 'Grand', 'Jury', 'said', 'Friday', 'an',
> 'investigation', 'of']
> Isn't text in NLTK corpus in a list format already? I am puzzled. I tested the text from nltk as follows. It is a list actually. See the following:
> >>> text = nltk.corpus.brown.words(categories = 'news')
> >>> text[:10]
> ['The', 'Fulton', 'County', 'Grand', 'Jury', 'said', 'Friday', 'an', 'investigation', 'of']
Thanks indeed for your enlightenment. type(text) does show the truth.
Though NLTK corpus reader is not a list, the slice of the text from the
nltk corpus is still in list format. say,
On Mon, Sep 3, 2012 at 10:47 AM, Steven Bird <stevenbi...@gmail.com> wrote:
> No, an NLTK corpus reader is not a list, but you can convert it to a list:
> On 3 September 2012 12:44, John H. Li <typeto...@gmail.com> wrote:
> > Thanks very much indeed.
> > Isn't text in NLTK corpus in a list format already? I am puzzled. I
> tested
> > the text from nltk as follows. It is a list actually. See the following:
> >>>> text = nltk.corpus.brown.words(categories = 'news')
> >>>> text[:10]
> > ['The', 'Fulton', 'County', 'Grand', 'Jury', 'said', 'Friday', 'an',
> > 'investigation', 'of']
> Thanks indeed for your enlightenment. type(text) does show the truth.
> Though NLTK corpus reader is not a list, the slice of the text from the
> nltk corpus is still in list format. say,
> >>> import nltk
> >>> text = nltk.corpus.brown.words(categories = 'news')
> >>> type(text)
> <class 'nltk.corpus.reader.util.ConcatenatedCorpusView'>
> >>> type(text[:10])
> <type 'list'>
> Isn't it contradictory?
> On Mon, Sep 3, 2012 at 10:47 AM, Steven Bird <stevenbi...@gmail.com>wrote:
>> No, an NLTK corpus reader is not a list, but you can convert it to a list:
>> On 3 September 2012 12:44, John H. Li <typeto...@gmail.com> wrote:
>> > Thanks very much indeed.
>> > Isn't text in NLTK corpus in a list format already? I am puzzled. I
>> tested
>> > the text from nltk as follows. It is a list actually. See the following:
>> >>>> text = nltk.corpus.brown.words(categories = 'news')
>> >>>> text[:10]
>> > ['The', 'Fulton', 'County', 'Grand', 'Jury', 'said', 'Friday', 'an',
>> > 'investigation', 'of']
When in doubt, help(ClassName) or help(variable_name) at the Python prompt
will tell you all about that class/object, pulling straight from the
docstring. For a quick list of attributes, you can use dir instead of help.
~Michael
On Fri, Sep 7, 2012 at 2:24 PM, Michael Deeringer <mdeerin...@gmail.com>wrote:
> Per the reply from Peter Ljunglöf, using slice notation on that object
> implicitly converts it to a list.
> On Sun, Sep 2, 2012 at 11:21 PM, John H. Li <typeto...@gmail.com> wrote:
>> Thanks indeed for your enlightenment. type(text) does show the truth.
>> Though NLTK corpus reader is not a list, the slice of the text from the
>> nltk corpus is still in list format. say,
>> >>> import nltk
>> >>> text = nltk.corpus.brown.words(categories = 'news')
>> >>> type(text)
>> <class 'nltk.corpus.reader.util.ConcatenatedCorpusView'>
>> >>> type(text[:10])
>> <type 'list'>
>> Isn't it contradictory?
>> On Mon, Sep 3, 2012 at 10:47 AM, Steven Bird <stevenbi...@gmail.com>wrote:
>>> No, an NLTK corpus reader is not a list, but you can convert it to a
>>> list:
>>> On 3 September 2012 12:44, John H. Li <typeto...@gmail.com> wrote:
>>> > Thanks very much indeed.
>>> > Isn't text in NLTK corpus in a list format already? I am puzzled. I
>>> tested
>>> > the text from nltk as follows. It is a list actually. See the
>>> following:
>>> >>>> text = nltk.corpus.brown.words(categories = 'news')
>>> >>>> text[:10]
>>> > ['The', 'Fulton', 'County', 'Grand', 'Jury', 'said', 'Friday', 'an',
>>> > 'investigation', 'of']