Re: The method of insert doesn't work with nltk texts: AttributeError: 'ConcatenatedCorpusView' object has no attribute 'insert'

Dave Angel d at davea.name
Sun Sep 2 07:10:29 EDT 2012


On 09/02/2012 05:39 AM, Token Type wrote:
> I wrote codes to add 'like' at the end of every 3 word in a nltk text as follows:
>
>>>> text = nltk.corpus.brown.words(categories = 'news')
>>>> def hedge(text):
> 	for i in range(3,len(text),4):
> 		new_text = text.insert(i, 'like')
> 	return new_text[:50]
>
>>>> hedge(text)
> Traceback (most recent call last):
>   File "<pyshell#77>", line 1, in <module>
>     hedge(text)
>   File "<pyshell#76>", line 3, in hedge
>     new_text = text.insert(i, 'like')
> AttributeError: 'ConcatenatedCorpusView' object has no attribute 'insert'
>
> Isn't text in the brown corpus above a list? why doesn't it has attribute 'insert'?
>
I tried to find online documentation for nltk, and although I found the
mention of a free online book, I didn't see it.  So, some generic comments.

The error message is telling you that the object 'text' is not a list,
but a "ConcatenatedCorpusView".  Perhaps you can look that up in your
docs for nltk.  But there's quite a bit you can do just with the
interpreter.

try   print type(text)   to see the type of text.

try   dir(text)   to see what attributes it has

try   help(text)  to see what  docstrings might be built in.

Incidentally, if you really think it's a list of words (or that it acts
like a list), then 'text' might not be the best name for it.  Any reason
you didn't just call it words ?

-- 

DaveA





More information about the Python-list mailing list