Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'attribute': 0.05; 'method.': 0.05; 'list?': 0.07; "subject:' ": 0.07; 'try:': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:method': 0.09; 'def': 0.10; '"x")': 0.16; "'__doc__',": 0.16; "'the',": 0.16; '...]': 0.16; '3):': 0.16; 'hedge': 0.16; 'hint': 0.16; 'inserting': 0.16; 'range(0,': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'subject:object': 0.16; 'wrote:': 0.17; '>>>': 0.18; '"",': 0.22; 'header:User-Agent:1': 0.26; 'looks': 0.26; 'skip:[ 10': 0.26; 'wrote': 0.26; '(most': 0.27; 'list:': 0.27; "doesn't": 0.28; 'header:X-Complaints-To:1': 0.28; 'decide': 0.28; 'methods.': 0.29; 'workaround': 0.29; 'convert': 0.29; 'class': 0.29; "skip:' 10": 0.30; 'call.': 0.30; 'function': 0.30; 'error': 0.30; 'file': 0.32; "skip:' 20": 0.32; 'impression': 0.33; 'traceback': 0.33; 'to:addr:python-list': 0.33; 'text': 0.34; 'thanks': 0.34; 'list': 0.35; 'follows:': 0.35; 'list.': 0.35; 'add': 0.36; 'received:org': 0.36; 'but': 0.36; 'subject:with': 0.36; 'does': 0.37; 'why': 0.37; 'ones': 0.37; 'subject:: ': 0.38; 'growing': 0.38; 'object': 0.38; 'shows': 0.38; 'to:addr:python.org': 0.39; 'list,': 0.39; 'header:Received:5': 0.40; 'end': 0.40; 'your': 0.60; 'first': 0.61; 'free': 0.61; 'provide': 0.62; 'skip:n 10': 0.63; 'brown': 0.65; 'skip:n 30': 0.69; 'subject:The': 0.71; 'skip:n 40': 0.72; 'subjectcharset:utf-8': 0.72; 'subject::': 0.83; "'like'": 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: The method of insert doesn't work with nltk =?UTF-8?B?dGV4dHM644CAQXR0cmlidXRlRXJyb3I6?= 'ConcatenatedCorpusView' object has no attribute 'insert' Date: Sun, 02 Sep 2012 16:12:02 +0200 Organization: None References: <58dd6730-9c37-4b90-9dbc-2724300ff5f4@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Gmane-NNTP-Posting-Host: p5084af99.dip.t-dialin.net User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 83 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1346595132 news.xs4all.nl 6860 [2001:888:2000:d::a6]:52531 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:28275 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 "", line 1, in > hedge(text) > File "", 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) 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) >>> 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', '-', '-']