>> def simple_block_reader(stream): ... return stream.readline().split() This simple block reader reads a single line at a time, and returns a single token (consisting of a string) for each whitespace-separated substring on the line. When deciding how to define the block reader for a given corpus, careful consideration should be given to the size of blocks handled by the block reader. Smaller block sizes will increase the memory requirements of the corpus view's internal data structures (by 2 integers per block). On the other hand, larger block sizes may decrease performance for random access to the corpus. (But note that larger block sizes will *not* decrease performance for iteration.) Internally, ``CorpusView`` maintains a partial mapping from token index to file position, with one entry per block. When a token with a given index *i* is requested, the ``CorpusView`` constructs it as follows: 1. First, it searches the toknum/filepos mapping for the token index closest to (but less than or equal to) *i*. 2. Then, starting at the file position corresponding to that index, it reads one block at a time using the block reader until it reaches the requested token. The toknum/filepos mapping is created lazily: it is initially empty, but every time a new block is read, the block's initial token is added to the mapping. (Thus, the toknum/filepos map has one entry per block.) In order to increase efficiency for random access patterns that have high degrees of locality, the corpus view may cache one or more blocks. :note: Each ``CorpusView`` object internally maintains an open file object for its underlying corpus file. This file should be automatically closed when the ``CorpusView`` is garbage collected, but if you wish to close it manually, use the ``close()`` method. If you access a ``CorpusView``'s items after it has been closed, the file object will be automatically re-opened. :warning: If the contents of the file are modified during the lifetime of the ``CorpusView``, then the ``CorpusView``'s behavior is undefined. :warning: If a unicode encoding is specified when constructing a ``CorpusView``, then the block reader may only call ``stream.seek()`` with offsets that have been returned by ``stream.tell()``; in particular, calling ``stream.seek()`` with relative offsets, or with offsets based on string lengths, may lead to incorrect behavior. :ivar _block_reader: The function used to read a single block from the underlying file stream. :ivar _toknum: A list containing the token index of each block that has been processed. In particular, ``_toknum[i]`` is the token index of the first token in block ``i``. Together with ``_filepos``, this forms a partial mapping between token indices and file positions. :ivar _filepos: A list containing the file position of each block that has been processed. In particular, ``_toknum[i]`` is the file position of the first character in block ``i``. Together with ``_toknum``, this forms a partial mapping between token indices and file positions. :ivar _stream: The stream used to access the underlying corpus file. :ivar _len: The total number of tokens in the corpus, if known; or None, if the number of tokens is not yet known. :ivar _eofpos: The character position of the last character in the file. This is calculated when the corpus view is initialized, and is used to decide when the end of file has been reached. :ivar _cache: A cache of the most recently read block. It is encoded as a tuple (start_toknum, end_toknum, tokens), where start_toknum is the token index of the first token in the block; end_toknum is the token index of the first token not in the block; and tokens is a list of the tokens in the block. itutf8cCs|r||_ndg|_|g|_||_d|_||_d|_d|_d|_ yCt |jt r|jj |_ ntj|jj|_ Wn)tk r}td||fnXd|_dS(s Create a new corpus view, based on the file ``fileid``, and read with ``block_reader``. See the class documentation for more information. :param fileid: The path to the file that is read by this corpus view. ``fileid`` can either be a string or a ``PathPointer``. :param startpos: The file position at which the view will start reading. This can be used to skip over preface sections. :param encoding: The unicode encoding that should be used to read the file's contents. If no encoding is specified, then the file's contents will be read as a non-unicode string (i.e., a str). is!Unable to open or access %r -- %siN(iiN(t read_blockt_toknumt_filepost _encodingtNonet_lent_fileidt_streamt_current_toknumt_current_blocknumt isinstanceRt file_sizet_eofpostoststattst_sizet Exceptiont ValueErrort_cache(tselftfileidt block_readertstartpostencodingtexc((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pyt__init__~s$         cCs|jS(N(R(R$((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pytstdocsa The fileid of the file that is accessed by this view. :type: str or PathPointercCstddS(s Read a block from the input stream. :return: a block of tokens from the input stream :rtype: list(any) :param stream: an input stream :type stream: stream sAbstract MethodN(tNotImplementedError(R$tstream((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pyRs cCsst|jtr-|jj|j|_nB|jrZtt|jd|j|_nt|jd|_dS(s Open the file stream associated with this corpus view. This will be called performed if any value is read from the view while its file stream is closed. trbN(RRRtopenRRR (R$((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pyt_opens  !cCs,|jdk r|jjnd|_dS(ss Close the file stream associated with this corpus view. This can be useful if you are worried about running out of file handles (although the stream should automatically be closed upon garbage collection of the corpus view). If the corpus view is accessed after it is closed, it will be automatically re-opened. N(RRtclose(R$((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pyR2s cCs:|jdkr3x!|j|jdD]}q&Wn|jS(Ni(RRt iterate_fromR(R$ttok((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pyt__len__scCs2t|trzt||\}}|jd}||krj||jdkrj|jd||||!St|||S|dkr|t|7}n|dkrtdn|jd}||ko|jdknr|jd||Syt|j|SWnt k r-tdnXdS(Niiisindex out of range( RtsliceRR#R tlent IndexErrortnextR3t StopIteration(R$titstarttstoptoffset((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pyt __getitem__s"    # c cs*|jd|ko%|jdknrcx6|jd||jdD]}|V|d7}qGWn||jdkrtj|j|d}|j|}|j|}n-t|jd}|jd}|jd}|jdkr|jnx||j kr|jj |||_ ||_ |j |j}t|tttfsitd|j jt|}|jj}||kstd|j j|f|||t|f|_||jdkst|dkr|d7}||jdkrJ||jdks#t|jj||jj||q||j|ksitd|||j|kstdn||j kr|||_nx$|td||D] }|VqW||j kst||j krPn||7}|}qW|jdk s&tdS( Niiiis.block reader %s() should return list or tuple.s=block reader %s() should consume at least 1 byte (filepos=%d)s*inconsistent block reader (num chars read)s/inconsistent block reader (num tokens returned)(R#Rtbisectt bisect_rightRR7RRR1RtseekRRRRttupletlistR tAssertionErrort__name__ttelltappendRtmax( R$t start_tokR4t block_indexttoknumtfileposttokenstnum_tokst new_filepos((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pyR3s`*#              cCst||gS(N(tconcat(R$tother((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pyt__add__TscCst||gS(N(RQ(R$RR((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pyt__radd__VscCst|g|S(N(RQ(R$tcount((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pyt__mul__XscCst|g|S(N(RQ(R$RU((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pyt__rmul__ZsN(RFt __module__t__doc__RR*tpropertyR%RR1R2R5R?R3RSRTRVRW(((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pyR"s[ ;      O   tConcatenatedCorpusViewcBs2eZdZdZdZdZdZRS(s A 'view' of a corpus file that joins together one or more ``StreamBackedCorpusViews``. At most one file handle is left open at any time. cCs"||_dg|_d|_dS(Ni(t_piecest_offsetsRt _open_piece(R$t corpus_views((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pyR*cs   cCsMt|jt|jkrBx!|j|jdD]}q5Wn|jdS(Ni(R7R]R\R3(R$R4((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pyR5qscCs"x|jD]}|jq WdS(N(R\R2(R$tpiece((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pyR2xsccstj|j|d}x|t|jkr|j|}|j|}|j|k r|jdk ry|jjn||_nx)|jt d||D] }|VqW|dt|jkr|jj |jdt|n|d7}qWdS(Niii( R@RAR]R7R\R^RR2R3RIRH(R$RJtpiecenumR>R`R4((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pyR3|s   # $(RFRXRYR*R5R2R3(((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pyR[]s    cCst|dkr|dSt|dkr;tdntd|D}td|Drtdj|Sx1|D]}t|ttfs{Pq{q{Wt|Sx+|D]}t|tsPqqWt |St|dkrt |d}t|t rt d|gSt|t r<t d|d St j|rt jd }x|D]}|j|qaW|Sntd |d S( s Concatenate together the contents of multiple documents from a single corpus, using an appropriate concatenation function. This utility function is used by corpus readers when the user requests more than one document at a time. iis%concat() expects at least one object!css|]}|jVqdS(N(t __class__(t.0td((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pys scss|]}t|tVqdS(N(RR(RcR,((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pys stcSs||S(N((tatb((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pyR+scSs||S(N((RfRg((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pyR+st documentss'Don't know how to concatenate types: %rN((R7R"tsettalltjoint issubclassRR[R R RDRRCRt iselementtElementRH(tdocsttypesttyptxmltreeR,((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pyRQs8        tPickleCorpusViewcBsYeZdZdZdZedZdZdZe dZ e e dZ RS(s$ A stream backed corpus view for corpus files that consist of sequences of serialized Python objects (serialized using ``pickle.dump``). One use case for this class is to store the result of running feature detection on a corpus to disk. This can be useful when performing feature detection is expensive (so we don't want to repeat it); but the corpus is too large to store in memory. The following example illustrates this technique: >>> from nltk.corpus.reader.util import PickleCorpusView >>> from nltk.util import LazyMap >>> feature_corpus = LazyMap(detect_features, corpus) # doctest: +SKIP >>> PickleCorpusView.write(feature_corpus, some_fileid) # doctest: +SKIP >>> pcv = PickleCorpusView(some_fileid) # doctest: +SKIP idicCs||_tj||dS(s Create a new corpus view that reads the pickle corpus ``fileid``. :param delete_on_gc: If true, then ``fileid`` will be deleted whenever this object gets garbage-collected. N(t _delete_on_gcRR*(R$R%t delete_on_gc((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pyR*s cCsSg}xFt|jD]5}y|jtj|Wqtk rJPqXqW|S(N(tranget BLOCK_SIZERHtpickletloadtEOFError(R$R.tresultR;((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pyRs cCsit|drXtjj|jrXytj|jWqUttfk rQqUXqXn|jj dS(s If ``delete_on_gc`` was set to true when this ``PickleCorpusView`` was created, then delete the corpus view's fileid. (This method is called whenever a ``PickledCorpusView`` is garbage-collected. RtN( tgetattrRtpathtexistsRtremovetOSErrortIOErrort__dict__tclear(R$((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pyt__del__s cCsLt|tr!t|d}nx$|D]}tj|||jq(WdS(Ntwb(RRR0RxtdumptPROTOCOL(tclstsequencet output_filetitem((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pytwrites cCsyUtjdd\}}tj|d}|j|||jt||SWn)ttfk r}t d|nXdS(s6 Write the given sequence to a temporary file as a pickle corpus; and then return a ``PickleCorpusView`` view for that temporary corpus file. :param delete_on_gc: If true, then the temporary file will be deleted whenever this object gets garbage-collected. s.pcvsnltk-Rs"Error while creating temp file: %sN( ttempfiletmkstempRtfdopenRR2RsRRR"(RRRutfdtoutput_file_nameRte((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pytcache_to_tempfiles  ( RFRXRYRwRtFalseR*RRt classmethodRtTrueR(((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pyRss  cCs:g}x-tdD]}|j|jjqW|S(Ni(Rvtextendtreadlinetsplit(R.ttoksR;((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pytread_whitespace_blockscCs:g}x-tdD]}|jt|jqW|S(Ni(RvRRR(R.RR;((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pytread_wordpunct_block#scCsMg}x@tdD]2}|j}|s/|S|j|jdqW|S(Nis (RvRRHtrstrip(R.RR;tline((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pytread_line_block)s cCsjd}x]tre|j}|s5|r.|gSgSq |rX|j rX|rb|gSq ||7}q WdS(NRe(RRtstrip(R.tsR((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pytread_blankline_block1s   cCsd}xtr|j}|ddks |ddks |d dkrQq n|sk|rd|gSgSq ||7}tjd|dk r |gSq WdS(NReit=s is s^\d+-\d+(RRtretmatchR(R.RR((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pytread_alignedsent_block@s  0 cCsx6tr8|j}|sgStj||rPqqW|g}xtr|j}|j}|sydj|gS|dk rtj||rdj|gS|dkrtj||r|j|dj|gS|j|qEWdS(s Read a sequence of tokens from a stream, where tokens begin with lines that match ``start_re``. If ``end_re`` is specified, then tokens end with lines that match ``end_re``; otherwise, tokens end whenever the next line matching ``start_re`` or EOF is found. ReN( RRRRRGRkRRBRH(R.tstart_retend_reRtlinestoldpos((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pytread_regexp_blockPs$       i@c Cs|j}|j|}t|dd}|dk sNt|tsNt|d krzddl}|jd|n|rt j dt j |}nxt ry|r||j 7}t j|t|}nt|\}} t j dj|| j} |dkr,|j|| n$|j|t|| j||SWqtk r} | jdd kr|j|} | r|| 7}qq|jgSqqXqWdS( s- Read a sequence of s-expressions from the stream, and leave the stream's file position at the end the last complete s-expression read. This function will always return at least one s-expression, unless there are no more s-expressions in the file. If the file ends in in the middle of an s-expression, then that incomplete s-expression is returned when the end of the file is reached. :param block_size: The default block size for reading. If an s-expression is longer than one block, then more than one block will be read. :param comment_char: A character that marks comments. Any lines that begin with this character will be stripped out. (If spaces or tabs precede the comment character, then the line will not be stripped.) R(sutf-8iNsAParsing may fail, depending on the properties of the %s encoding!s (?m)^%s.*$s\s*isBlock too small(Nsutf-8(RGtreadR|RRRREtwarningstwarnRtcompiletescapeRRtsubt _sub_spacet_parse_sexpr_blocktsearchtendRBR7tencodeR"targsR( R.t block_sizet comment_charR<tblockR(RtCOMMENTRNR>Rt next_block((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pytread_sexpr_blockps: !     ! $ cCsd|j|jS(srHelper function: given a regexp match, return a string of spaces that's the same length as the matched string.t (RR<(tm((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pyRscCslg}d}}xO|t|kratjdj||}|sP||fS|j}|jdkrtjdj||}|r|j}qJ|r||fStdnd}xtjdj||D]K}|jdkr|d7}n |d8}|dkr|j}PqqW|r>||fStd|j |||!qW||fS(Nis\St(s[\s(]sBlock too smalls[()]i( R7RRRR<tgroupR"tfinditerRRH(RRNR<RRtm2tnesting((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pyRs6    "      c Cst|tstdn|d7}t|trg|jjD](}|jdsG|t|j^qG}g|D]}t j ||r||^q|}t |St|t rg}i}t sitd6}nxtj|j|D]\}}}djdt|j|D}|g|D]&} t j ||| r2|| ^q27}d|kr|jdqqWt |Std|dS( Ns+find_corpus_fileids: expected a PathPointert$t/t followlinksRecss|]}d|VqdS(s%s/N((Rctp((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pys ss.svnsDon't know how to handle %r(RRt TypeErrorR tzipfiletnamelisttendswithR7tentryRRtsortedRRRRtwalkR}Rkt _path_fromRRE( troottregexptnametfileidstitemstkwargstdirnametsubdirstprefixR%((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pytfind_corpus_fileidss* (+  %%'  cCstjj|ddkr5tjj|d}ng}xZ||krtjj|\}}|jd|tjj|d|ks>tq>W|S(NiRei(RR}RtinsertRE(tparenttchildR}R((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pyRs&cCs{d}xntrv|j}tjd|rC|jrs|gSq |dkri|jrb|gSgSq ||7}q WdS(NRes ======+\s*$(RRRRR(R.tparaR((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pyt!tagged_treebank_para_block_readers     (.RR@RRt functoolsRtcPickleRxt ImportErrort xml.etreeRRt nltk.compatRRt nltk.tokenizeRtnltk.internalsRt nltk.dataRRR R t nltk.utilR R R RRR[RQRsRRRRRRRRRRRRR(((si/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/util.pyt sF      "<7 7R      A  *