ó <¿CVc@s›ddlmZmZddlmZddlmZmZddlm Z ddl m Z e de fd„ƒYƒZ d„Z ed kr—e ƒnd S( iÿÿÿÿ(tprint_functiontunicode_literals(treduce(tTreetProbabilisticTree(tpython_2_unicode_compatible(tParserIt ViterbiParsercBsneZdZdd„Zd„Zdd„Zd„Zd„Zd„Zd „Z d „Z d „Z d „Z RS( u· A bottom-up ``PCFG`` parser that uses dynamic programming to find the single most likely parse for a text. The ``ViterbiParser`` parser parses texts by filling in a "most likely constituent table". This table records the most probable tree representation for any given span and node value. In particular, it has an entry for every start index, end index, and node value, recording the most likely subtree that spans from the start index to the end index, and has the given node value. The ``ViterbiParser`` parser fills in this table incrementally. It starts by filling in all entries for constituents that span one element of text (i.e., entries where the end index is one greater than the start index). After it has filled in all table entries for constituents that span one element of text, it fills in the entries for constitutants that span two elements of text. It continues filling in the entries for constituents spanning larger and larger portions of the text, until the entire table has been filled. Finally, it returns the table entry for a constituent spanning the entire text, whose node value is the grammar's start symbol. In order to find the most likely constituent with a given span and node value, the ``ViterbiParser`` parser considers all productions that could produce that node value. For each production, it finds all children that collectively cover the span and have the node values specified by the production's right hand side. If the probability of the tree formed by applying the production to the children is greater than the probability of the current entry in the table, then the table is updated with this new tree. A pseudo-code description of the algorithm used by ``ViterbiParser`` is: | Create an empty most likely constituent table, *MLC*. | For width in 1...len(text): | For start in 1...len(text)-width: | For prod in grammar.productions: | For each sequence of subtrees [t[1], t[2], ..., t[n]] in MLC, | where t[i].label()==prod.rhs[i], | and the sequence covers [start:start+width]: | old_p = MLC[start, start+width, prod.lhs] | new_p = P(t[1])P(t[1])...P(t[n])P(prod) | if new_p > old_p: | new_tree = Tree(prod.lhs, t[1], t[2], ..., t[n]) | MLC[start, start+width, prod.lhs] = new_tree | Return MLC[0, len(text), start_symbol] :type _grammar: PCFG :ivar _grammar: The grammar used to parse sentences. :type _trace: int :ivar _trace: The level of tracing output that should be generated when parsing a text. icCs||_||_dS(u² Create a new ``ViterbiParser`` parser, that uses ``grammar`` to parse texts. :type grammar: PCFG :param grammar: The grammar used to parse texts. :type trace: int :param trace: The level of tracing that should be used when parsing a text. ``0`` will generate no tracing output; and higher numbers will produce more verbose tracing output. N(t_grammart_trace(tselftgrammarttrace((sd/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/viterbi.pyt__init__Ls cCs|jS(N(R(R ((sd/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/viterbi.pyR \sicCs ||_dS(uP Set the level of tracing output that should be generated when parsing a text. :type trace: int :param trace: The trace level. A trace level of ``0`` will generate no tracing output; and higher trace levels will produce more verbose tracing output. :rtype: None N(R (R R ((sd/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/viterbi.pyR _s c csnt|ƒ}|jj|ƒi}|jr<tddƒnxftt|ƒƒD]R}||}||||d|f<|jdkrO|j||t|ƒƒqOqOWx‡tdt|ƒdƒD]l}|jrãtdd|ƒnxEtt|ƒ|dƒD])}|||f}|j|||ƒqþWq¿W|j dt|ƒ|jj ƒfƒ}|dk rj|VndS(Nu%Inserting tokens into the most likelyu constituents table...iu$Finding the most likely constituentsu spanning %d text elements...i( tlistRtcheck_coverageR tprinttrangetlent_trace_lexical_insertiont_add_constituents_spanningtgettstarttNone( R ttokenst constituentstindexttokentlengthRtspanttree((sd/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/viterbi.pytparsels,       ! ' c Cs´t}x§|r¯t}|j||ƒ}x‚|D]z\}}g|D]}t|tƒrA|^qA} td„| |jƒƒ} |jƒjƒ} t | |d| ƒ} |j |d|d|jƒfƒ}|j dkrZ|d ksñ|| krZ|d ks|jƒ| jƒkr(t dddƒnt dddƒ|j|| |t|ƒƒqZn|d ks~|jƒ| jƒkr.| ||d|d|jƒf ProbabilisticToken or ProbabilisticTree) :param constituents: The most likely constituents table. This table records the most probable tree representation for any given span and node value. In particular, ``constituents(s,e,nv)`` is the most likely ``ProbabilisticTree`` that covers ``text[s:e]`` and has a node value ``nv.symbol()``, where ``text`` is the text that we are parsing. When ``_add_constituents_spanning`` is called, ``constituents`` should contain all possible constituents that are shorter than ``span``. :type tokens: list of tokens :param tokens: The text we are parsing. This is only used for trace output. cSs||jƒS(N(tprob(tprtt((sd/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/viterbi.pytÆsR iiu Insert:tendu u Discard:N(tTruetFalset_find_instantiationst isinstanceRRR tlhstsymbolRRR RRt_trace_productionR( R RRRtchangedtinstantiationst productiontchildrentctsubtreestptnodeR((sd/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/viterbi.pyR”s($ ( &$"$!cCscg}xV|jjƒD]E}|j|jƒ||ƒ}x!|D]}|j||fƒq>WqW|S(u  :return: a list of the production instantiations that cover a given span of the text. A "production instantiation" is a tuple containing a production and a list of children, where the production's right hand side matches the list of children; and the children cover ``span``. :rtype: list of ``pair`` of ``Production``, (list of (``ProbabilisticTree`` or token. :type span: tuple(int, int) :param span: The section of the text for which we are trying to find production instantiations. The span is specified as a pair of integers, where the first integer is the index of the first token that should be covered by the production instantiation; and the second integer is the index of the first token that should not be covered by the production instantiation. :type constituents: dict(tuple(int,int,Nonterminal) -> ProbabilisticToken or ProbabilisticTree) :param constituents: The most likely constituents table. This table records the most probable tree representation for any given span and node value. See the module documentation for more information. (Rt productionst _match_rhstrhstappend(R RRtrvR.t childlistst childlist((sd/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/viterbi.pyR'Ùs  c CsÝ|\}}||kr+|dkr+ggS||ksC|dkrGgSg}x‰t||dƒD]t}|j|||dfƒ}|dk ra|j|d||f|ƒ} |g| D]} |g| ^q¸7}qaqaW|S(ul :return: a set of all the lists of children that cover ``span`` and that match ``rhs``. :rtype: list(list(ProbabilisticTree or token) :type rhs: list(Nonterminal or any) :param rhs: The list specifying what kinds of children need to cover ``span``. Each nonterminal in ``rhs`` specifies that the corresponding child should be a tree whose node value is that nonterminal's symbol. Each terminal in ``rhs`` specifies that the corresponding child should be a token whose type is that terminal. :type span: tuple(int, int) :param span: The section of the text for which we are trying to find child lists. The span is specified as a pair of integers, where the first integer is the index of the first token that should be covered by the child list; and the second integer is the index of the first token that should not be covered by the child list. :type constituents: dict(tuple(int,int,Nonterminal) -> ProbabilisticToken or ProbabilisticTree) :param constituents: The most likely constituents table. This table records the most probable tree representation for any given span and node value. See the module documentation for more information. ii((N(RRRR5( R R6RRRR$R9tsplittltrightstr((sd/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/viterbi.pyR5ùs  +cCs„dd|d}|d|d|d7}|d||dd7}|d|7}|jdkrvd ||f}nt|ƒd S( u› Print trace output indicating that a given production has been applied at a given location. :param production: The production that has been applied :type production: Production :param p: The probability of the tree produced by the production. :type p: float :param span: The span of the production :type span: tuple :rtype: None u|u.iu=iu| u%siu%-40s %12.10f N(R R(R R.R2Rtwidthtstr((sd/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/viterbi.pyR+#scCsEdd|dd||dd}|d|f7}t|ƒdS(Nu Insert: |u.u=iu| u%s(R(R RRR?R@((sd/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/viterbi.pyR9s&cCs d|jS(Nu(R(R ((sd/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/viterbi.pyt__repr__>s( t__name__t __module__t__doc__R R R RRR'R5R+RRA(((sd/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/viterbi.pyRs7   ( E *  cCsFddl}ddl}ddlm}ddlm}ddlm}m}d|fd|fg}t ƒxWt t |ƒƒD]C}t d|d ||d fƒt d ||d ƒt ƒq€Wt d d t |ƒfd dƒy3t |j jƒjƒƒd }||\} } Wnt dƒdSX| jƒ} || ƒ} i} t d| | | fƒ| jdƒ|jƒ}| j| ƒ}|jƒ|}|rÁtd„|d ƒt |ƒnd }t |ƒ}x|D]}d | |jƒsÿ2 J