Fwd: 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 09:51:15 EDT 2012


On 09/02/2012 09:06 AM, John H. Li wrote:
> First, thanks very much for your kind help.
>
> 1)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']
> 2) I tested the text from nltk. It is list actually. See the following:
>>>> text = nltk.corpus.brown.words(categories = 'news')
>>>> text[:10]
> ['The', 'Fulton', 'County', 'Grand', 'Jury', 'said', 'Friday', 'an',
> 'investigation', 'of']
>
> How come python tells me that it is not a list by prompting "AttributeError:
> 'ConcatenatedCorpusView' object has no attribute 'insert'"? I am confused.
>
> Since we doubt text is not a list, I have to add one more line of code
> there as follows. Then it seems working.
>>>> text = nltk.corpus.brown.words(categories = 'news')
>>>> def hedge(text):
> text = list(text)
> for i in range(3,len(text),4):
>  text.insert(i, 'like')
> return text[:50]
>
>>>> hedge(text)
> ['The', 'Fulton', 'County', 'like', 'Grand', 'Jury', 'said', 'like',
> 'Friday', 'an', 'investigation', 'like', 'of', "Atlanta's", 'recent',
> 'like', 'primary', 'election', 'produced', 'like', '``', 'no', 'evidence',
> 'like', "''", 'that', 'any', 'like', 'irregularities', 'took', 'place',
> 'like', '.', 'The', 'jury', 'like', 'further', 'said', 'in', 'like',
> 'term-end', 'presentments', 'that', 'like', 'the', 'City', 'Executive',
> 'like', 'Committee', ',']
>
> Isn't it odd?
>
>

Without reading the documentation, or at least the help(), I can't
figure it to be odd.  If a class wants to support slicing semantics, all
it has to do is implement special methods like __getslice__ and
__setslice__.  If it doesn't document .insert(), then you shouldn't try
to call it.  Duck-typing.

What did you get when you tried type(), dir() and help() ?  Did they help.

-- 

DaveA




More information about the Python-list mailing list