ó <¿CVc@sÃdZddlmZddlZddlZddlmZddlmZddl m Z m Z m Z ddl Tejdefd „ƒYƒZd efd „ƒYZd efd „ƒYZdS(u API for corpus readers. iÿÿÿÿ(tunicode_literalsN(t defaultdict(tcompat(t PathPointertFileSystemPathPointertZipFilePathPointer(t*t CorpusReadercBs›eZdZddd„Zd„Zd„Zd„Zd„Zd„Z d„Z d „Z de e d „Z d „Zd „Zd „ZeeddƒZRS(u® A base class for "corpus reader" classes, each of which can be used to read a specific corpus format. Each individual corpus reader instance is used to read a specific corpus, consisting of one or more files under a common root directory. Each file is identified by its ``file identifier``, which is the relative path to the file from the root directory. A separate subclass is be defined for each corpus format. These subclasses define one or more methods that provide 'views' on the corpus contents, such as ``words()`` (for a list of words) and ``parsed_sents()`` (for a list of parsed sentences). Called with no arguments, these methods will return the contents of the entire corpus. For most corpora, these methods define one or more selection arguments, such as ``fileids`` or ``categories``, which can be used to select which portion of the corpus should be returned. uutf8c CsFt|tjƒrmt|tƒ rmtjd|ƒ}|jƒ\}}|r^t||ƒ}q‹t|ƒ}nt|tƒs‹t dƒ‚nt|tjƒr¯t ||ƒ}n||_ ||_ t|t ƒr0i}xN|j D]C} x:|D]2} | \} } tj| | ƒrí| || ( RRRRtfilenametentrytpatht __class__t__name__(RR$((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/api.pyt__repr__rs"cCsdS(uü Load this corpus (if it has not already been loaded). This is used by LazyCorpusLoader as a simple method that can be used to make sure a corpus is loaded -- e.g., in case a user wants to do help(some_corpus). N((R((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/api.pyt ensure_loadedyscCs|jdƒjƒS(uN Return the contents of the corpus README file, if it exists. uREADME(topentread(R((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/api.pytreadme‚scCs|jdƒjƒS(uO Return the contents of the corpus LICENSE file, if it exists. uLICENSE(R)R*(R((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/api.pytlicenseˆscCs|jdƒjƒS(uT Return the contents of the corpus citation.bib file, if it exists. u citation.bib(R)R*(R((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/api.pytcitationŽscCs|jS(ue Return a list of file identifiers for the fileids that make up this corpus. (R(R((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/api.pyR”scCs|jj|ƒS(uØ Return the absolute path for the given file. :type fileid: str :param fileid: The file identifier for the file whose path should be returned. :rtype: PathPointer (Rtjoin(RR((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/api.pytabspath›s cCsï|dkr|j}nt|tjƒr6|g}ng|D]}|jj|ƒ^q=}|r™|r™tt|g|D]}|j |ƒ^qw|ƒƒS|r²tt||ƒƒS|rçtt|g|D]}|j |ƒ^qȃƒS|SdS(uÖ Return a list of the absolute paths for all fileids in this corpus; or for the given list of fileids, if specified. :type fileids: None or str or list :param fileids: Specifies the set of fileids for which paths should be returned. Can be None, for all fileids; a list of file identifiers, for a specified set of fileids; or a single file identifier, for a single file. Note that the return value is always a list of paths, even if ``fileids`` is a single file identifier. :param include_encoding: If true, then return a list of ``(path_pointer, encoding)`` tuples. :rtype: list(PathPointer) N( tNoneRRRR RR.RtzipR(RRtinclude_encodingtinclude_fileidtftpaths((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/api.pytabspaths¦s   % 2/cCs.|j|ƒ}|jj|ƒj|ƒ}|S(u Return an open stream that can be used to read the given file. If the file's encoding is not None, then the stream will automatically decode the file's contents into unicode. :param file: The file identifier of the file to read. (RRR.R)(RtfileRtstream((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/api.pyR)ÉscCs-t|jtƒr"|jj|ƒS|jSdS(uÓ Return the unicode encoding for the given corpus file, if known. If the encoding is unknown, or if the given file should be processed using byte strings (str), then return None. N(RRtdicttget(RR7((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/api.pyRÕscCs|jS(N(R(R((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/api.pyt _get_rootàstdocuO The directory where this corpus is stored. :type: PathPointerN(R&t __module__t__doc__R0R!R'R(R+R,R-RR/tFalseR6R)RR;tpropertyR(((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/api.pyRs G       "  tCategorizedCorpusReadercBsAeZdZd„Zd„Zd„Zdd„Zdd„ZRS(u™ A mixin class used to aid in the implementation of corpus readers for categorized corpora. This class defines the method ``categories()``, which returns a list of the categories for the corpus or for a specified set of fileids; and overrides ``fileids()`` to take a ``categories`` argument, restricting the set of fileids to be returned. Subclasses are expected to: - Call ``__init__()`` to set up the mapping. - Override all view methods to accept a ``categories`` parameter, which can be used *instead* of the ``fileids`` parameter, to select which fileids should be included in the returned view. cCsd|_d|_d|_d|_d|_d|_d|krY|d|_|d=nud|kr||d|_|d=nRd|krÂ|d|_|d=d|krÎ|d|_|d=qÎn tdƒ‚d|ksòd|ksòd|krtdƒ‚ndS(u/ Initialize this mapping based on keyword arguments, as follows: - cat_pattern: A regular expression pattern used to find the category for each file identifier. The pattern will be applied to each file identifier, and the first matching group will be used as the category label for that file. - cat_map: A dictionary, mapping from file identifiers to category labels. - cat_file: The name of a file that contains the mapping from file identifiers to categories. The argument ``cat_delimiter`` can be used to specify a delimiter. The corresponding argument will be deleted from ``kwargs``. If more than one argument is specified, an exception will be raised. u cat_patternucat_mapucat_fileu cat_delimiteru=Expected keyword argument cat_pattern or cat_map or cat_file.u7Specify exactly one of: cat_pattern, cat_map, cat_file.N(R0t_f2ct_c2ft_patternt_mapt_filet _delimitert ValueError(Rtkwargs((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/api.pyR!ýs,                   cCs€ttƒ|_ttƒ|_|jdk rrxL|jD]4}tj|j|ƒj dƒ}|j ||ƒq7Wn |j dk rÅxø|j j ƒD]-\}}x|D]}|j ||ƒq¤Wq‘Wn·|j dk r|x¥|j|j ƒjƒD]ˆ}|jƒ}|j|jdƒ\}}||jƒkrHtd|j |fƒ‚nx*|j|jƒD]}|j ||ƒq[WqíWndS(Niu)In category mapping file %s: %s not found(RtsetRBRCRDR0RR R tgroupt_addREtitemsRFR)t readlineststriptsplitRGRRH(Rtfile_idtcategoryt categoriestline((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/api.pyt_init0s&  cCs,|j|j|ƒ|j|j|ƒdS(N(RBtaddRC(RRQRR((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/api.pyRLHscCs€|jdkr|jƒn|dkr5t|jƒSt|tjƒrS|g}nttj g|D]}|j|^qcŒƒS(u~ Return a list of the categories that are defined for this corpus, or for the file(s) if it is given. N( RBR0RUtsortedRCRRR RJtunion(RRtd((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/api.pyRSLs    cCsÍ|dkrtt|ƒjƒSt|tjƒr€|jdkrM|jƒn||j krmt |j |ƒSt d|ƒ‚nI|jdkrœ|jƒnt t j g|D]}|j |^q¬ŒƒSdS(u— Return a list of file identifiers for the files that make up this corpus, or that make up the given category(s) if specified. uCategory %s not foundN(R0tsuperRARRRR RBRURCRWRHRJRX(RRStc((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/api.pyRYs   N( R&R=R>R!RURLR0RSR(((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/api.pyRAës  3   tSyntaxCorpusReadercBs³eZdZd„Zd„Zd„Zd„Zdd„Zdd„Z ddd„Z dd„Z ddd „Z dd „Z d „Zdd „Zd „Zdd„Zd„ZRS(uH An abstract base class for reading corpora consisting of syntactically parsed text. Subclasses should define: - ``__init__``, which specifies the location of the corpus and a method for detecting the sentence blocks in corpus files. - ``_read_block``, which reads a block from the input stream. - ``_word``, which takes a block and returns a list of list of words. - ``_tag``, which takes a block and returns a list of list of tagged words. - ``_parse``, which takes a block and returns a list of parsed sentences. cCs tƒ‚dS(N(tNotImplementedError(Rts((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/api.pyt_parsescCs tƒ‚dS(N(R](RR^((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/api.pyt_wordscCs tƒ‚dS(N(R](RR^((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/api.pyt_tagƒscCs tƒ‚dS(N(R](RR8((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/api.pyt _read_block…scCsb|dkr|j}nt|tjƒr6|g}ntg|D]}|j|ƒjƒ^q@ƒS(N(R0RRRR tconcatR)R*(RRR4((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/api.pytrawˆs   cCsG|j}tg|j|tƒD]!\}}t||d|ƒ^qƒS(NR(t_read_parsed_sent_blockRcR6tTruetStreamBackedCorpusView(RRtreaderRR ((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/api.pyt parsed_sentss csP‡‡fd†}tgˆj|tƒD]!\}}t||d|ƒ^q(ƒS(Ncsˆj|ˆƒS(N(t_read_tagged_sent_block(R8(RR(sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/api.pyRh“sR(RcR6RfRg(RRRRhRR ((RRsh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/api.pyt tagged_sents’scCsG|j}tg|j|tƒD]!\}}t||d|ƒ^qƒS(NR(t_read_sent_blockRcR6RfRg(RRRhRR ((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/api.pytsents˜s csP‡‡fd†}tgˆj|tƒD]!\}}t||d|ƒ^q(ƒS(Ncsˆj|ˆƒS(N(t_read_tagged_word_block(R8(RR(sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/api.pyRhžsR(RcR6RfRg(RRRRhRR ((RRsh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/api.pyt tagged_wordsscCsAtg|j|tƒD]$\}}t||jd|ƒ^qƒS(NR(RcR6RfRgt_read_word_block(RRRR ((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/api.pytwords£scCst|j|ƒgƒS(N(tsumRl(RR8((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/api.pyRp¬scCst|j||ƒgƒS(N(RrRj(RR8R((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/api.pyRn¯scCs8ttdg|j|ƒD]}|j|ƒ^qƒƒS(N(RtfilterR0RbR`(RR8tt((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/api.pyRl²scCs;ttdg|j|ƒD]}|j||ƒ^qƒƒS(N(RRsR0RbRa(RR8RRt((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/api.pyRjµs cCs8ttdg|j|ƒD]}|j|ƒ^qƒƒS(N(RRsR0RbR_(RR8Rt((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/api.pyRe¹sN(R&R=R>R_R`RaRbR0RdRiRkRmRoRqRpRnRlRjRe(((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/api.pyR\qs             (R>t __future__RtosR t collectionsRtnltkRt nltk.dataRRRtnltk.corpus.reader.utiltpython_2_unicode_compatibletobjectRRAR\(((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/corpus/reader/api.pyt s    Ó†