Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #28275
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Subject | Re: The method of insert doesn't work with nltk texts: AttributeError: 'ConcatenatedCorpusView' object has no attribute 'insert' |
| Date | 2012-09-02 16:12 +0200 |
| Organization | None |
| References | <58dd6730-9c37-4b90-9dbc-2724300ff5f4@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.89.1346595132.27098.python-list@python.org> (permalink) |
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'? > > Thanks much for your hints. The error message shows that text is not a list. It looks like a list, >>> text = nltk.corpus.brown.words(categories="news") >>> text ['The', 'Fulton', 'County', 'Grand', 'Jury', 'said', ...] but it is actually a nltk.corpus.reader.util.ConcatenatedCorpusView: >>> type(text) <class 'nltk.corpus.reader.util.ConcatenatedCorpusView'> The implementer of a class is free to decide what methods he wants to implement. You can get a first impression of the available ones with dir(): >>> dir(text) ['_MAX_REPR_SIZE', '__add__', '__class__', '__cmp__', '__contains__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__getitem__', '__hash__', '__init__', '__iter__', '__len__', '__module__', '__mul__', '__new__', '__radd__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_offsets', '_open_piece', '_pieces', 'close', 'count', 'index', 'iterate_from'] As you can see insert() is not among these methods. However, __iter__() is a hint that you can convert the ConcatenatedCorpusView to a list, and that does provide an insert() method. Let's try: >>> text = list(text) >>> type(text) <type 'list'> >>> text.insert(0, "yadda") >>> text[:5] ['yadda', 'The', 'Fulton', 'County', 'Grand'] Note that your hedge() function may still not work as you expect: >>> text = ["-"] * 20 >>> text ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-'] >>> for i in range(0, len(text), 3): ... text.insert(i, "X") ... >>> text ['X', '-', '-', 'X', '-', '-', 'X', '-', '-', 'X', '-', '-', 'X', '-', '-', 'X', '-', '-', 'X', '-', '-', '-', '-', '-', '-', '-', '-'] That is because the list is growing with every insert() call. One workaround is to start inserting items at the end of the list: >>> text = ["-"] * 20 >>> for i in reversed(range(0, len(text), 3)): ... text.insert(i, "X") ... >>> text ['X', '-', '-', '-', 'X', '-', '-', '-', 'X', '-', '-', '-', 'X', '-', '-', '-', 'X', '-', '-', '-', 'X', '-', '-', '-', 'X', '-', '-']
Back to comp.lang.python | Previous | Next — Previous 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