d5Z?d6Z@d7ZAd8ZBeCd9krKeBnd:d;d<d=d>d?d@dAdBdCdDg ZDdS(Eu Basic data classes for representing context free grammars. A "grammar" specifies which trees can represent the structure of a given text. Each of these trees is called a "parse tree" for the text (or simply a "parse"). In a "context free" grammar, the set of parse trees for any piece of a text can depend only on that piece, and not on the rest of the text (i.e., the piece's context). Context free grammars are often used to find possible syntactic structures for sentences. In this context, the leaves of a parse tree are word tokens; and the node values are phrasal categories, such as ``NP`` and ``VP``. The ``CFG`` class is used to encode context free grammars. Each ``CFG`` consists of a start symbol and a set of productions. The "start symbol" specifies the root node value for parse trees. For example, the start symbol for syntactic parsing is usually ``S``. Start symbols are encoded using the ``Nonterminal`` class, which is discussed below. A Grammar's "productions" specify what parent-child relationships a parse tree can contain. Each production specifies that a particular node can be the parent of a particular set of children. For example, the production `` -> `` specifies that an ``S`` node can be the parent of an ``NP`` node and a ``VP`` node. Grammar productions are implemented by the ``Production`` class. Each ``Production`` consists of a left hand side and a right hand side. The "left hand side" is a ``Nonterminal`` that specifies the node type for a potential parent; and the "right hand side" is a list that specifies allowable children for that parent. This lists consists of ``Nonterminals`` and text types: each ``Nonterminal`` indicates that the corresponding child may be a ``TreeToken`` with the specified node type; and each text type indicates that the corresponding child may be a ``Token`` with the with that type. The ``Nonterminal`` class is used to distinguish node values from leaf values. This prevents the grammar from accidentally using a leaf value (such as the English word "A") as the node of a subtree. Within a ``CFG``, all node values are wrapped in the ``Nonterminal`` class. Note, however, that the trees that are specified by the grammar do *not* include these ``Nonterminal`` wrappers. Grammars can also be given a more procedural interpretation. According to this interpretation, a Grammar specifies any tree structure *tree* that can be produced by the following procedure: | Set tree to the start symbol | Repeat until tree contains no more nonterminal leaves: | Choose a production prod with whose left hand side | lhs is a nonterminal leaf of tree. | Replace the nonterminal leaf with a subtree, whose node | value is the value wrapped by the nonterminal lhs, and | whose children are the right hand side of prod. The operation of replacing the left hand side (*lhs*) of a production with the right hand side (*rhs*) in a tree (*tree*) is known as "expanding" *lhs* to *rhs* in *tree*. i(tprint_functiontunicode_literalsN(ttransitive_closuret invert_graph(t string_typesttotal_orderingt text_typetpython_2_unicode_compatiblet unicode_repr(traise_unorderable_types(tImmutableProbabilisticMixIn(t FeatStructtFeatDicttFeatStructReadertSLASHtTYPEt NonterminalcBs_eZdZdZdZdZdZdZdZdZ dZ d Z RS( u. A non-terminal symbol for a context free grammar. ``Nonterminal`` is a wrapper class for node values; it is used by ``Production`` objects to distinguish node values from leaf values. The node value that is wrapped by a ``Nonterminal`` is known as its "symbol". Symbols are typically strings representing phrasal categories (such as ``"NP"`` or ``"VP"``). However, more complex symbol types are sometimes used (e.g., for lexicalized grammars). Since symbols are node values, they must be immutable and hashable. Two ``Nonterminals`` are considered equal if their symbols are equal. :see: ``CFG``, ``Production`` :type _symbol: any :ivar _symbol: The node value corresponding to this ``Nonterminal``. This value must be immutable and hashable. cCs||_t||_dS(u Construct a new non-terminal from the given symbol. :type symbol: any :param symbol: The node value corresponding to this ``Nonterminal``. This value must be immutable and hashable. N(t_symbolthasht_hash(tselftsymbol((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyt__init__ks cCs|jS(ue Return the node value corresponding to this ``Nonterminal``. :rtype: (any) (R(R((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRwscCs(t|t|ko'|j|jkS(u Return True if this non-terminal is equal to ``other``. In particular, return True if ``other`` is a ``Nonterminal`` and this non-terminal's symbol is equal to ``other`` 's symbol. :rtype: bool (ttypeR(Rtother((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyt__eq__scCs ||k S(N((RR((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyt__ne__scCs2t|ts"td||n|j|jkS(Nu<(t isinstanceRR R(RR((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyt__lt__scCs|jS(N(R(R((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyt__hash__scCs2t|jtrd|jSdt|jSdS(u_ Return a string representation for this ``Nonterminal``. :rtype: str u%sN(RRRR(R((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyt__repr__s cCs2t|jtrd|jSdt|jSdS(u_ Return a string representation for this ``Nonterminal``. :rtype: str u%sN(RRRR(R((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyt__str__s cCstd|j|jfS(uA Return a new nonterminal whose symbol is ``A/B``, where ``A`` is the symbol for this nonterminal, and ``B`` is the symbol for rhs. :param rhs: The nonterminal used to form the right hand side of the new nonterminal. :type rhs: Nonterminal :rtype: Nonterminal u%s/%s(RR(Rtrhs((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyt__div__s ( t__name__t __module__t__doc__RRRRRRRRR!(((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRWs     cCsMd|kr|jd}n |j}g|D]}t|j^q1S(u Given a string containing a list of symbol names, return a list of ``Nonterminals`` constructed from those symbols. :param symbols: The symbol name string. This string can be delimited by either spaces or commas. :type symbols: str :return: A list of ``Nonterminals`` constructed from the symbol names given in ``symbols``. The ``Nonterminals`` are sorted in the same order as the symbols names. :rtype: list(Nonterminal) u,(tsplitRtstrip(tsymbolst symbol_listts((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyt nonterminalss  tFeatStructNonterminalcBs eZdZdZdZRS(u|A feature structure that's also a nonterminal. It acts as its own symbol, and automatically freezes itself when hashed.cCs|jtj|S(N(tfreezeR R(R((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRs cCs|S(N((R((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRs(R"R#R$RR(((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyR+s cCs t|tS(uJ :return: True if the item is a ``Nonterminal``. :rtype: bool (RR(titem((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pytis_nonterminalscCst|dot|t S(u Return True if the item is a terminal, which currently is if it is hashable and not a ``Nonterminal``. :rtype: bool u__hash__(thasattrRR(R-((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyt is_terminalst ProductioncBszeZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d ZRS( u A grammar production. Each production maps a single symbol on the "left-hand side" to a sequence of symbols on the "right-hand side". (In the case of context-free productions, the left-hand side must be a ``Nonterminal``, and the right-hand side is a sequence of terminals and ``Nonterminals``.) "terminals" can be any immutable hashable object that is not a ``Nonterminal``. Typically, terminals are strings representing words, such as ``"dog"`` or ``"under"``. :see: ``CFG`` :see: ``DependencyGrammar`` :see: ``Nonterminal`` :type _lhs: Nonterminal :ivar _lhs: The left-hand side of the production. :type _rhs: tuple(Nonterminal, terminal) :ivar _rhs: The right-hand side of the production. cCsUt|trtdn||_t||_t|j|jf|_dS(u  Construct a new ``Production``. :param lhs: The left-hand side of the new ``Production``. :type lhs: Nonterminal :param rhs: The right-hand side of the new ``Production``. :type rhs: sequence(Nonterminal and terminal) u9production right hand side should be a list, not a stringN(RRt TypeErrort_lhsttuplet_rhsRR(RtlhsR ((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRs  cCs|jS(u` Return the left-hand side of this ``Production``. :rtype: Nonterminal (R3(R((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyR6scCs|jS(ux Return the right-hand side of this ``Production``. :rtype: sequence(Nonterminal and terminal) (R5(R((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyR scCs t|jS(uP Return the length of the right-hand side. :rtype: int (tlenR5(R((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyt__len__ scCstd|jDS(ui Return True if the right-hand side only contains ``Nonterminals`` :rtype: bool css|]}t|VqdS(N(R.(t.0tn((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pys .s(tallR5(R((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyt is_nonlexical(scCs |j S(uj Return True if the right-hand contain at least one terminal token. :rtype: bool (R<(R((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyt is_lexical0scCs7dt|j}|djd|jD7}|S(ud Return a verbose string representation of the ``Production``. :rtype: str u%s -> u css|]}t|VqdS(N(R(R9tel((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pys ?s(RR3tjoinR5(Rtresult((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyR8s cCsd|S(ud Return a concise string representation of the ``Production``. :rtype: str u%s((R((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRBscCs:t|t|ko9|j|jko9|j|jkS(ua Return True if this ``Production`` is equal to ``other``. :rtype: bool (RR3R5(RR((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRJscCs ||k S(N((RR((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRTscCsDt|ts"td||n|j|jf|j|jfkS(Nu<(RR1R R3R5(RR((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRWscCs|jS(uR Return a hash value for the ``Production``. :rtype: int (R(R((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyR\s(R"R#R$RR6R R8R<R=RRRRRR(((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyR1s         tDependencyProductioncBseZdZdZRS(u A dependency grammar production. Each production maps a single head word to an unordered list of one or more modifier words. cCs9d|jf}x"|jD]}|d|f7}qW|S(un Return a verbose string representation of the ``DependencyProduction``. :rtype: str u'%s' ->u '%s'(R3R5(RR@telt((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRks(R"R#R$R(((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRAestProbabilisticProductioncBs;eZdZdZdZdZdZdZRS(u A probabilistic context free grammar production. A PCFG ``ProbabilisticProduction`` is essentially just a ``Production`` that has an associated probability, which represents how likely it is that this production will be used. In particular, the probability of a ``ProbabilisticProduction`` records the likelihood that its right-hand side is the correct instantiation for any given occurrence of its left-hand side. :see: ``Production`` cKs'tj||tj|||dS(u Construct a new ``ProbabilisticProduction``. :param lhs: The left-hand side of the new ``ProbabilisticProduction``. :type lhs: Nonterminal :param rhs: The right-hand side of the new ``ProbabilisticProduction``. :type rhs: sequence(Nonterminal and terminal) :param prob: Probability parameters of the new ``ProbabilisticProduction``. N(R RR1(RR6R tprob((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRs cCs3tj||jdkr$dn d|jS(Ng?u [1.0]u [%g](R1t __unicode__RD(R((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRs cCsRt|t|koQ|j|jkoQ|j|jkoQ|j|jkS(N(RR3R5RD(RR((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRscCs ||k S(N((RR((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRscCst|j|j|jfS(N(RR3R5RD(R((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRs(R"R#R$RRRRR(((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRCws     tCFGcBseZdZedZdZdZeddZ dZ dde dZ dZ dZd Zd Zd Zd Zd ZdZdZdZdZdZdZdZdZRS(u# A context-free grammar. A grammar consists of a start state and a set of productions. The set of terminals and nonterminals is implicitly specified by the productions. If you need efficient key-based access to productions, you can use a subclass to implement it. cCs~t|s(tdt|jn||_||_td|D|_|j|j |rz|j ndS(u Create a new context-free grammar, from the given start state and set of ``Production``s. :param start: The start symbol :type start: Nonterminal :param productions: The list of productions that defines the grammar :type productions: list(Production) :param calculate_leftcorners: False if we don't want to calculate the leftcorner relation. In that case, some optimized chart parsers won't work. :type calculate_leftcorners: bool u.start should be a Nonterminal object, not a %scss|]}|jVqdS(N(R6(R9tprod((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pys sN( R.R2RR"t_startt _productionstsett _categoriest_calculate_indexest_calculate_grammar_formst_calculate_leftcorners(Rtstartt productionstcalculate_leftcorners((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRs     cCsi|_i|_i|_i|_x|jD]}|j}||jkr\g|j|scss|]}|tfVqdS(N(RJ(R9R[((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pys sit reflexivei'(tdictRKt _immediate_leftcorner_categoriest_immediate_leftcorner_wordsRPR7R6R R.RXRtTruet _leftcornersRt_leftcorner_parentstsumtmaptvaluestNonet_leftcorner_wordsRJtupdatetget(RRGR[tlefttlctnr_leftcorner_categoriestnr_leftcorner_wordstlefts((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRNs,      cCs(t|td|\}}t||S(u Return the ``CFG`` corresponding to the input string(s). :param input: a grammar, either in the form of a string or as a list of strings. tencoding(t read_grammartstandard_nonterm_parserRF(tclstinputRoRORP((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyt fromstrings cCs|jS(uU Return the start symbol of the grammar :rtype: Nonterminal (RH(R((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRO scCs|r|rtdn| rF| rF|s6|jS|jjSn|r| r|sl|jj|gS||jkr|j|gSgSna|r| r|jj|gSg|jj|gD]'}||jj|gkr|^qSdS(u Return the grammar productions, filtered by the left-hand side or the first item in the right-hand side. :param lhs: Only return productions with the given left-hand side. :param rhs: Only return productions with the given first item in the right-hand side. :param empty: Only return productions with an empty right-hand side. :return: A list of productions matching the given constraints. :rtype: list(Production) uCYou cannot select empty and non-empty productions at the same time.N(t ValueErrorRIRTReRRRiRS(RR6R temptyRG((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRPs   cCs|jj|t|gS(u Return the set of all nonterminals that the given nonterminal can start with, including itself. This is the reflexive, transitive closure of the immediate leftcorner relation: (A > B) iff (A -> B beta) :param cat: the parent of the leftcorners :type cat: Nonterminal :return: the set of all leftcorners :rtype: set(Nonterminal) (RaRiRJ(RR[((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyt leftcorners=s csntrj|kSjrDjj|tkStfdj|DSdS(u- True if left is a leftcorner of cat, where left can be a terminal or a nonterminal. :param cat: the parent of the leftcorner :type cat: Nonterminal :param left: the suggested leftcorner :type left: Terminal or Nonterminal :rtype: bool c3s-|]#}jj|tkVqdS(N(R_RiRJ(R9tparent(RjR(s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pys \sN(R.RwRgRiRJtany(RR[Rj((RjRs^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyt is_leftcornerLs  cCs|jj|t|gS(uC Return the set of all nonterminals for which the given category is a left corner. This is the inverse of the leftcorner relation. :param cat: the suggested leftcorner :type cat: Nonterminal :return: the set of all parents to the leftcorner :rtype: set(Nonterminal) (RbRiRJ(RR[((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pytleftcorner_parents_s cCsag|D]}|jj|s|^q}|r]djd|D}td|ndS(u Check whether the grammar rules cover the given list of tokens. If not, then raise an exception. :type tokens: list(str) u, css|]}d|fVqdS(u%rN((R9tw((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pys usu3Grammar does not cover some of the input words: %r.N(RURiR?Ru(Rttokensttoktmissing((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pytcheck_coverageks  cCs|j}td|D|_td|D|_td|D|_td|D|_td|D|_dS(u@ Pre-calculate of which form(s) the grammar is. css|]}|jVqdS(N(R=(R9tp((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pys ~scss-|]#}t|dkr|jVqdS(iN(R7R<(R9R((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pys scss|]}t|VqdS(N(R7(R9R((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pys scss|]}t|VqdS(N(R7(R9R((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pys scss-|]#}t|dkr|jVqdS(iN(R7R=(R9R((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pys sN( RIR;t _is_lexicalt_is_nonlexicaltmint_min_lentmaxt_max_lent_all_unary_are_lexical(Rtprods((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRMys  cCs|jS(uA Return True if all productions are lexicalised. (R(R((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyR=scCs|jS(u Return True if all lexical rules are "preterminals", that is, unary rules which can be separated in a preprocessing step. This means that all productions are of the forms A -> B1 ... Bn (n>=0), or A -> "s". Note: is_lexical() and is_nonlexical() are not opposites. There are grammars which are neither, and grammars which are both. (R(R((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyR<s cCs|jS(uW Return the right-hand side length of the shortest grammar production. (R(R((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pytmin_lenscCs|jS(uV Return the right-hand side length of the longest grammar production. (R(R((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pytmax_lenscCs |jdkS(u@ Return True if there are no empty productions. i(R(R((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyt is_nonemptyscCs |jdkS(u Return True if all productions are at most binary. Note that there can still be empty and unary productions. i(R(R((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyt is_binarisedscCs"|jo!|jo!|jS(uh Return True if all productions are of the forms A -> B C, A -> B, or A -> "s". (RR<R(R((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pytis_flexible_chomsky_normal_formscCs|jo|jS(u Return True if the grammar is of Chomsky Normal Form, i.e. all productions are of the form A -> B C, or A -> "s". (RR(R((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pytis_chomsky_normal_forms cCsdt|jS(Nu(R7RI(R((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRscCsJdt|j}|d|j7}x|jD]}|d|7}q.W|S(NuGrammar with %d productionsu (start state = %r)u %s(R7RIRH(RR@t production((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRs N(R"R#R$R`RRLRNt classmethodRfRtROtFalseRPRwRzR{RRMR=R<RRRRRRRR(((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRFs.     )           tFeatureGrammarcBsheZdZdZdZedddddZddedZ dZ dZ dZ RS( u A feature-based grammar. This is equivalent to a ``CFG`` whose nonterminals are all ``FeatStructNonterminal``. A grammar consists of a start state and a set of productions. The set of terminals and nonterminals is implicitly specified by the productions. cCstj|||dS(u@ Create a new feature-based grammar, from the given start state and set of ``Productions``. :param start: The start symbol :type start: FeatStructNonterminal :param productions: The list of productions that defines the grammar :type productions: list(Production) N(RFR(RRORP((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRs cCsdi|_i|_i|_g|_i|_x0|jD]%}|j|j}||jkrng|j|(R(R((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRjscCs(t|t|ko'|j|jkS(N(RR(RR((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRmscCs ||k S(N((RR((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRpscCs2t|ts"td||n|j|jkS(Nu<(RRR R(RR((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRsscCs|jS(N(R(R((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRxs( R"R#R$RRRRRR(((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyR^s     tDependencyGrammarcBsJeZdZdZedZdZdZdZdZ RS(u A dependency grammar. A DependencyGrammar consists of a set of productions. Each production specifies a head/modifier relationship between a pair of words. cCs ||_dS(u Create a new dependency grammar, from the set of ``Productions``. :param productions: The list of productions that defines the grammar :type productions: list(Production) N(RI(RRP((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRscCsg}xt|jdD]w\}}|j}|jds|dkrUqny|t|7}Wqtk rtd||fqXqWt|dkrtdnt|S(Nu u#uuUnable to parse line %s: %siuNo productions found!(t enumerateR%R&t startswitht_read_dependency_productionRuR7R(RrRsRPtlinenumtline((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRts"  cCsKxD|jD]9}x0|jD]%}|j|kr||krtSqWq WtS(u. :param head: A head word. :type head: str :param mod: A mod word, to test as a modifier of 'head'. :type mod: str :return: true if this ``DependencyGrammar`` contains a ``DependencyProduction`` mapping 'head' to 'mod'. :rtype: bool (RIR5R3R`R(RtheadtmodRt possibleMod((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pytcontainss  cCsKxD|jD]9}x0|jD]%}|j|kr||krtSqWq WtS(u( Return True if this ``DependencyGrammar`` contains a ``DependencyProduction`` mapping 'head' to 'mod'. :param head: A head word. :type head: str :param mod: A mod word, to test as a modifier of 'head'. :type mod: str :rtype: bool (RIR5R3R`R(RRRRR((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyt __contains__s  cCs9dt|j}x|jD]}|d|7}qW|S(uj Return a verbose string representation of the ``DependencyGrammar`` :rtype: str u&Dependency grammar with %d productionsu %s(R7RI(RtstrR((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRscCsdt|jS(uU Return a concise string representation of the ``DependencyGrammar`` u&Dependency grammar with %d productions(R7RI(R((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRs( R"R#R$RRRtRRRR(((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyR|s    tProbabilisticDependencyGrammarcBs2eZdZdZdZdZdZRS(u cCs||_||_||_dS(N(RIt_eventst_tags(RRPteventsttags((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRs  cCsKxD|jD]9}x0|jD]%}|j|kr||krtSqWq WtS(u( Return True if this ``DependencyGrammar`` contains a ``DependencyProduction`` mapping 'head' to 'mod'. :param head: A head word. :type head: str :param mod: A mod word, to test as a modifier of 'head'. :type mod: str :rtype: bool (RIR5R3R`R(RRRRR((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRs  cCsdt|j}x|jD]}|d|7}qW|d7}x,|jD]!}|d|j||f7}qIW|d7}x,|jD]!}|d||j|f7}qW|S(uw Return a verbose string representation of the ``ProbabilisticDependencyGrammar`` :rtype: str u2Statistical dependency grammar with %d productionsu %su Events:u %d:%su Tags:u %s: (%s)(R7RIRR(RRRteventttag_word((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRs  cCsdt|jS(ub Return a concise string representation of the ``ProbabilisticDependencyGrammar`` u2Statistical Dependency grammar with %d productions(R7RI(R((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRs(R"R#R$RRRR(((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRs    tPCFGcBs2eZdZdZedZeddZRS(u A probabilistic context-free grammar. A PCFG consists of a start state and a set of productions with probabilities. The set of terminals and nonterminals is implicitly specified by the productions. PCFG productions use the ``ProbabilisticProduction`` class. ``PCFGs`` impose the constraint that the set of productions with any given left-hand-side must have probabilities that sum to 1 (allowing for a small margin of error). If you need efficient key-based access to productions, you can use a subclass to implement it. :type EPSILON: float :cvar EPSILON: The acceptable margin of error for checking that productions with a given left-hand side have probabilities that sum to 1. g{Gz?cCstj||||i}x:|D]2}|j|jd|j||j B C in a PCFG is: | count(A -> B C) | P(B, C | A) = --------------- where \* is any right hand side | count(A -> \*) :param start: The start symbol :type start: Nonterminal :param productions: The list of productions that defines the grammar :type productions: list(Production) iiRD(RiR6RCR tfloatR(RORPtpcounttlcountRGRR((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyt induce_pcfgSs &IcCs t|tS(u8 Return a list of context-free ``Productions``. (t_read_productionRq(Rs((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyt_read_cfg_productionvscCst|tdtS(u= Return a list of PCFG ``ProbabilisticProductions``. R(RRqR`(Rs((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyt_read_pcfg_production|scCs t||S(u9 Return a list of feature-based ``Productions``. (R(RsR((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyt_read_fcfg_productionsu \s* -> \s*u( \[ [\d\.]+ \] ) \s*u( "[^"]+" | \'[^\']+\' ) \s*u\| \s*c Cs'd}|||\}}tj||}|sBtdn|j}dg}gg}xe|t|krtj||}|r|r|j}t|jddd!|d<|ddkrtd|dfqqc||dkrStj||}|s#td n|dj |jddd!|j}qc||d krt j||}|j d|j g|j}qc|||\}}|dj |qcW|rgt ||D]!\} } t || d | ^qSg|D]} t || ^q Sd S( uX Parse a grammar rule, given as a string, and return a list of productions. iuExpected an arrowgiig?u9Production probability %f, should not be greater than 1.0u'"uUnterminated stringu|RDN(t _ARROW_REtmatchRutendR7t_PROBABILITY_RERtgroupt _TERMINAL_RERVt_DISJUNCTION_REtzipRCR1( Rtnonterm_parserRtposR6tmt probabilitiestrhsidestnontermR t probability((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRsB      !  2cCs|dk r|j|}nt|tr?|jd}n|}d}g}d}xGt|D]9\}} || j} | jdsd| dkrqdn| jdr| d j d}qdnd}y| ddkrS| d jdd \} } | d krD|| d\}} | t | krPt d qPqit d n|t | ||7}Wqdt k r} t d |d | | fqdXqdW|st dn|s|dj }n||fS(u7 Return a pair consisting of a starting category and a list of ``Productions``. :param input: a grammar, either in the form of a string or else as a list of strings. :param nonterm_parser: a function for parsing nonterminals. It should take a ``(string, position)`` as argument and return a ``(nonterminal, position)`` as result. :param probabilistic: are the grammar rules probabilistic? :type probabilistic: bool :param encoding: the encoding of the grammar, if it is a binary string :type encoding: str u uu#u\iu iu%iustartuBad argument to start directiveu Bad directiveuUnable to parse line %s: %s %suNo productions found!N(RftdecodeRRR%RR&RtendswithtrstripR7RuRR6(RsRRRotlinesRORPt continue_lineRRt directivetargsRte((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRpsB  u( [\w/][\w/^<>-]* ) \s*cCsNtj||}|s/td||nt|jd|jfS(NuExpected a nonterminal, found: i(t_STANDARD_NONTERM_RERRuRRR(tstringRR((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRqs uH^\s* # leading whitespace ('[^']+')\s* # single-quoted lhs (?:[-=]+>)\s* # arrow (?:( # rhs: "[^"]+" # doubled-quoted terminal | '[^']+' # single-quoted terminal | \| # disjunction ) \s*) # trailing space *$u"('[^']'|[-=]+>|"[^"]+"|'[^']+'|\|)cCstj|stdntj|}gt|D]"\}}|ddkr:|^q:}|djd}gg}xH|dD]<}|dkr|jgq|dj|jdqWg|D]}t||^qS(NuBad production stringiiiu'"u|i( t _READ_DG_RERRut _SPLIT_DG_RER%RR&RVRA(R)tpiecestiRtlhsideRtpiecetrhside((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyRs5  c Cs8ddlm}m}m}|d\}}}}|d\}}} } ||} td||||||| | ||g tdt|jtt|||g|jd} tdt| td t| jtd d d tt| j j d dd dtdS(uG A demonstration showing how ``CFGs`` can be created and used. i(R*R1RFu S, NP, VP, PPu N, V, P, DetuSome nonterminals:u S.symbol() =>u S -> NP VP PP -> P NP NP -> Det N | NP PP VP -> V NP | VP PP Det -> 'a' | 'the' N -> 'dog' | 'cat' V -> 'chased' | 'sat' P -> 'on' | 'in' u A Grammar:u grammar.start() =>u grammar.productions() =>Ru u,u, iN( tnltkR*R1RFtprinttreprRRtRORPtreplace( R*R1RFtStNPtVPtPPtNtVtPtDett VP_slash_NPtgrammar((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pytcfg_demo.s , *u7 S -> NP VP [1.0] NP -> Det N [0.5] | NP PP [0.25] | 'John' [0.1] | 'I' [0.15] Det -> 'the' [0.8] | 'my' [0.2] N -> 'man' [0.5] | 'telescope' [0.5] VP -> VP PP [0.1] | V NP [0.7] | V [0.2] V -> 'ate' [0.35] | 'saw' [0.65] PP -> P NP [1.0] P -> 'with' [0.61] | 'under' [0.39] u S -> NP VP [1.0] VP -> V NP [.59] VP -> V [.40] VP -> VP PP [.01] NP -> Det N [.41] NP -> Name [.28] NP -> NP PP [.31] PP -> P NP [1.0] V -> 'saw' [.21] V -> 'ate' [.51] V -> 'ran' [.28] N -> 'boy' [.11] N -> 'cookie' [.12] N -> 'table' [.13] N -> 'telescope' [.14] N -> 'hill' [.5] Name -> 'Jack' [.52] Name -> 'Bob' [.48] P -> 'with' [.61] P -> 'under' [.39] Det -> 'the' [.41] Det -> 'a' [.31] Det -> 'my' [.28] cCs6ddlm}ddlm}ddlm}ddlm}tj}|d}t dt |t dt |j t d t |j t d t |j t t}t d t |t d t |jt d ddt t |jjddddt t dg}|jd}xK|j|d D]6} | jdt| jdd|| j7}q_Wtd} || |}t |t t d|j|} | jd|j|dj} t | x!| j| D]} t | qWdS(uI A demonstration showing how a ``PCFG`` can be created and used. i(ttreebank(ttreetransforms(R(tpchartiuA PCFG production:u pcfg_prod.lhs() =>u pcfg_prod.rhs() =>u pcfg_prod.prob() =>uA PCFG grammar:u grammar.start() =>u grammar.productions() =>Ru u,u, iu'Induce PCFG grammar from treebank data:iit collapsePOSt horzMarkovuSu%Parse sentence using induced grammar:N(t nltk.corpusRRRRt nltk.parseRt toy_pcfg1RPRRR6R RDt toy_pcfg2RORt_fileidst parsed_sentstcollapse_unaryRtchomsky_normal_formRtInsideChartParserttracetleavestparse(RRRRt pcfg_prodst pcfg_prodRRPR-ttreeRtparsertsentR((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyt pcfg_demoxsF  *       cCs3ddl}|jjd}t|tdS(Niu!grammars/book_grammars/feat0.fcfg(t nltk.datatdatatloadR(Rtg((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pyt fcfg_demos  cCstjd}t|dS(u] A demonstration showing the creation and inspection of a ``DependencyGrammar``. uP 'scratch' -> 'cats' | 'walls' 'walls' -> 'the' 'cats' -> 'the' N(RRtR(R((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pytdg_demos cCs<ddlm}|d}|j}t|jdS(ug A demonstration of how to read a string representation of a CoNLL format dependency tree. i(tDependencyGraphug 1 Ze ze Pron Pron per|3|evofmv|nom 2 su _ _ 2 had heb V V trans|ovt|1of2of3|ev 0 ROOT _ _ 3 met met Prep Prep voor 8 mod _ _ 4 haar haar Pron Pron bez|3|ev|neut|attr 5 det _ _ 5 moeder moeder N N soort|ev|neut 3 obj1 _ _ 6 kunnen kan V V hulp|ott|1of2of3|mv 2 vc _ _ 7 gaan ga V V hulp|inf 6 vc _ _ 8 winkelen winkel V V intrans|inf 11 cnj _ _ 9 , , Punc Punc komma 8 punct _ _ 10 zwemmen zwem V V intrans|inf 11 cnj _ _ 11 of of Conj Conj neven 7 vc _ _ 12 terrassen terras N N soort|mv|neut 11 cnj _ _ 13 . . Punc Punc punt 12 punct _ _ N(RR RRtpprint(R tdgR((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pytsdg_demos   cCs'tttttdS(N(RRR R R(((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pytdemos u__main__u Nonterminalu nonterminalsuCFGu ProductionuPCFGuProbabilisticProductionuDependencyGrammaruDependencyProductionuProbabilisticDependencyGrammaru induce_pcfgu read_grammar(ER$t __future__RRtret nltk.utilRRt nltk.compatRRRRRtnltk.internalsR tnltk.probabilityR tnltk.featstructR R R RRtobjectRR*R+R.R0R1RARCRFRRRRRRRRRtcompiletVERBOSERRRRRRRfRpRRqRRRRRtRRRR R RRR"t__all__(((s^/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/grammar.pytFs ((]  y,(\3F #    ;6     %   9