Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #28274
| Date | 2012-09-02 09:51 -0400 |
|---|---|
| From | Dave Angel <d@davea.name> |
| Subject | Re: Fwd: The method of insert doesn't work with nltk texts: AttributeError: 'ConcatenatedCorpusView' object has no attribute 'insert' |
| References | <58dd6730-9c37-4b90-9dbc-2724300ff5f4@googlegroups.com> <50433EA5.9090302@davea.name> <CAE5RzX=C7x-BXagCwANf+4wqb25eXKMkGs7TwMgSzciNFLDjew@mail.gmail.com> <CAE5RzXmbbOVd8ghoozrgAmQk1sgqVLAS=EVqpwvHPp-Tdiv3Cg@mail.gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.88.1346593882.27098.python-list@python.org> (permalink) |
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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
The method of insert doesn't work with nltk texts: AttributeError: 'ConcatenatedCorpusView' object has no attribute 'insert' Token Type <typetoken@gmail.com> - 2012-09-02 02:39 -0700 Re: The method of insert doesn't work with nltk texts: AttributeError: 'ConcatenatedCorpusView' object has no attribute 'insert' Dave Angel <d@davea.name> - 2012-09-02 07:10 -0400 Re: Fwd: The method of insert doesn't work with nltk texts: AttributeError: 'ConcatenatedCorpusView' object has no attribute 'insert' Dave Angel <d@davea.name> - 2012-09-02 09:51 -0400 Re: The method of insert doesn't work with nltk texts: AttributeError: 'ConcatenatedCorpusView' object has no attribute 'insert' Peter Otten <__peter__@web.de> - 2012-09-02 16:12 +0200
csiph-web