Re: [nltk-users] The Method of Insert doesn't work. AttributeError: 'ConcatenatedCorpusView' object has no attribute 'insert'

144 views
Skip to first unread message

Steven Bird

unread,
Sep 2, 2012, 10:18:37 PM9/2/12
to nltk-...@googlegroups.com
The problem here is that you are trying to access an NLTK corpus
object as if it was a list object. However, you can convert it to a
list like this:

text = list(nltk.corpus.brown.words(categories = 'news'))

-Steven Bird

On 2 September 2012 14:32, typetoken <type...@gmail.com> wrote:
> 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']
>>>>
>
> Thanks for your instructions!
>
> --
>
>

John H. Li

unread,
Sep 2, 2012, 10:44:35 PM9/2/12
to nltk-...@googlegroups.com
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']



Steven Bird

unread,
Sep 2, 2012, 10:47:52 PM9/2/12
to nltk-...@googlegroups.com
No, an NLTK corpus reader is not a list, but you can convert it to a list:

>>> text = nltk.corpus.brown.words(categories = 'news')
>>> type(text)
<class 'nltk.corpus.reader.util.ConcatenatedCorpusView'>
>>> text = list(nltk.corpus.brown.words(categories = 'news'))
>>> type(text)
<type 'list'>
> --
>
>

peter ljunglöf

unread,
Sep 3, 2012, 3:23:10 AM9/3/12
to nltk-...@googlegroups.com
You did just implicitly convert it to a list:

>>> type(text)
<class 'nltk.corpus.reader.util.ConcatenatedCorpusView'>

>>> type(text[:10])
<type 'list'>

/Peter

John H. Li

unread,
Sep 2, 2012, 11:21:12 PM9/2/12
to nltk-...@googlegroups.com
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?

--



Michael Deeringer

unread,
Sep 7, 2012, 2:24:30 PM9/7/12
to nltk-...@googlegroups.com
Per the reply from Peter Ljunglöf, using slice notation on that object implicitly converts it to a list.


--
 
 

Michael Deeringer

unread,
Sep 7, 2012, 2:29:10 PM9/7/12
to nltk-...@googlegroups.com
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
Reply all
Reply to author
Forward
0 new messages