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

John H. Li typetoken at gmail.com
Sun Sep 2 09:06:11 EDT 2012


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?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120902/a762ae48/attachment.html>


More information about the Python-list mailing list