ó <¿CVc@sÊdZddlmZmZddlmZddlmZmZddl m Z m Z ddl m Z ddlmZmZmZmZddlmZd efd „ƒYZd efd „ƒYZd efd„ƒYZdefd„ƒYZdefd„ƒYZedefd„ƒYƒZde fd„ƒYZdefd„ƒYZddlZdefd„ƒYZdefd„ƒYZdefd„ƒYZ dddd „Z"e#d!krÆe"ƒndS("u” Classes and interfaces for associating probabilities with tree structures that represent the internal organization of a text. The probabilistic parser module defines ``BottomUpProbabilisticChartParser``. ``BottomUpProbabilisticChartParser`` is an abstract class that implements a bottom-up chart parser for ``PCFG`` grammars. It maintains a queue of edges, and adds them to the chart one at a time. The ordering of this queue is based on the probabilities associated with the edges, allowing the parser to expand more likely edges before less likely ones. Each subclass implements a different queue ordering, producing different search strategies. Currently the following subclasses are defined: - ``InsideChartParser`` searches edges in decreasing order of their trees' inside probabilities. - ``RandomChartParser`` searches edges in random order. - ``LongestChartParser`` searches edges in decreasing order of their location's length. The ``BottomUpProbabilisticChartParser`` constructor has an optional argument beam_size. If non-zero, this controls the size of the beam (aka the edge queue). This option is most useful with InsideChartParser. iÿÿÿÿ(tprint_functiontunicode_literals(treduce(tTreetProbabilisticTree(t NonterminaltPCFG(tParserI(tCharttLeafEdgetTreeEdgetAbstractChartRule(tpython_2_unicode_compatibletProbabilisticLeafEdgecBseZd„ZRS(cCsdS(Ngð?((tself((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/pchart.pytprob3s(t__name__t __module__R(((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/pchart.pyR 2stProbabilisticTreeEdgecBs)eZd„Zd„Zed„ƒZRS(cOs2tj|||Ž||_|j|f|_dS(N(R t__init__t_probt_comparison_key(RRtargstkwargs((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/pchart.pyR6s cCs|jS(N(R(R((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/pchart.pyR<scCs(t|||f|jƒ|jƒdƒS(Ni(Rtlhstrhs(t productiontindextp((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/pchart.pytfrom_production>s(RRRRt staticmethodR(((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/pchart.pyR5s  tProbabilisticBottomUpInitRulecBseZdZd„ZRS(iccsSxLt|jƒƒD]8}t|j|ƒ|ƒ}|j|dƒr|VqqWdS(N((tranget num_leavesR tleaftinsert(RtcharttgrammarRtnew_edge((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/pchart.pytapplyFs(RRt NUM_EDGESR'(((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/pchart.pyRDst ProbabilisticBottomUpPredictRulecBseZdZd„ZRS(iccs…|jƒrdSxn|jƒD]`}|jƒ|jƒdkrtj||jƒ|jƒƒ}|j|dƒr}|Vq}qqWdS(Ni(( t is_incompletet productionsRRRRtstartRR#(RR$R%tedgetprodR&((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/pchart.pyR'Ns !(RRR(R'(((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/pchart.pyR)LstProbabilisticFundamentalRulecBseZdZd„ZRS(ic cs|jƒ|jƒkoE|jƒ|jƒkoE|jƒoE|jƒsLdS|jƒ|jƒ}t|d|jƒ|jƒfd|jƒd|jƒd|j ƒdƒ}t }x9|j |ƒD](}|j |||fƒrÄt }qÄqÄW|rþ|VndS(NtspanRRtdoti(tendR,tnextsymRR*t is_completeRRRR1tFalsetchild_pointer_listsR#tTrue( RR$R%t left_edget right_edgeRR&t changed_charttcpl1((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/pchart.pyR'Xs  (RRR(R'(((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/pchart.pyR/Vst&SingleEdgeProbabilisticFundamentalRulecBs)eZdZeƒZd„Zd„ZRS(iccsÖ|j}|jƒruxº|jd|jƒdtd|jƒƒD].}x%|j||||ƒD] }|Vq_Wq@Wn]xZ|jd|jƒdtd|j ƒƒD].}x%|j||||ƒD] }|Vq¿Wq WdS(NR,R4RR2R3( t_fundamental_ruleR*tselectR2R7R3R'R,R5R(RR$R%tedge1tfrtedge2R&((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/pchart.pyR'us  cCsdS(NuFundamental Rule((R((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/pchart.pyt__str__„s(RRR(R/R=R'RB(((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/pchart.pyR<os  t BottomUpProbabilisticChartParsercBsVeZdZddd„Zd„Zdd„Zd„Zd„Zd„Zd „Z RS( uî An abstract bottom-up parser for ``PCFG`` grammars that uses a ``Chart`` to record partial results. ``BottomUpProbabilisticChartParser`` maintains a queue of edges that can be added to the chart. This queue is initialized with edges for each token in the text that is being parsed. ``BottomUpProbabilisticChartParser`` inserts these edges into the chart one at a time, starting with the most likely edges, and proceeding to less likely edges. For each edge that is added to the chart, it may become possible to insert additional edges into the chart; these are added to the queue. This process continues until enough complete parses have been generated, or until the queue is empty. The sorting order for the queue is not specified by ``BottomUpProbabilisticChartParser``. Different sorting orders will result in different search strategies. The sorting order for the queue is defined by the method ``sort_queue``; subclasses are required to provide a definition for this method. :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=t|tƒstdƒ‚n||_||_||_dS(u% Create a new ``BottomUpProbabilisticChartParser``, that uses ``grammar`` to parse texts. :type grammar: PCFG :param grammar: The grammar used to parse texts. :type beam_size: int :param beam_size: The maximum length for the parser's edge queue. :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. u&The grammar must be probabilistic PCFGN(t isinstanceRt ValueErrort_grammart beam_sizet_trace(RR%RGttrace((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/pchart.pyR¡s   cCs|jS(N(RF(R((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/pchart.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(RH(RRI((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/pchart.pyRI¹s c Cs|jj|ƒtt|ƒƒ}|j}tƒ}tƒ}tƒ}g}xb|j||ƒD]N}|jdkr t d|j |ddƒ|j ƒfƒn|j |ƒq_WxÁt |ƒdkrt|j||ƒ|jrò|j||ƒn|jƒ}|jdkr9t d|j |ddƒ|j ƒfƒn|j|j|||ƒƒ|j|j|||ƒƒq´Wt|j|jƒtƒƒ} i} x6|jƒD](} | j ƒ| | jƒ| jƒføs(RFtcheck_coverageRtlistRR)R<R'RHtprinttpretty_format_edgeRtappendtlent sort_queueRGt_prunetpoptextendtparsesR,RR+RRt_setprobtsortR7titer( RttokensR$R%tbu_inittbuR@tqueueR-RYt prod_probsR.tparse((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/pchart.pyRbÇs<       & cCsá|jƒdk rdSt|jƒƒ}g}xF|D]>}t|tƒrf|jt|jƒƒƒq5|j|ƒq5W||t|ƒf}x@|D]8}t|tƒr”|j||ƒ||jƒ9}q”q”W|j |ƒdS(N( RtNoneRtlabelRDRRSttupleRZtset_prob(RRMRaRRtchildR((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/pchart.pyRZüs  cCs tƒ‚dS(u¤ Sort the given queue of ``Edge`` objects, placing the edge that should be tried first at the beginning of the queue. This method will be called after each ``Edge`` is added to the queue. :param queue: The queue of ``Edge`` objects to sort. Each edge in this queue is an edge that could be added to the chart by the fundamental rule; but that has not yet been added. :type queue: list(Edge) :param chart: The chart being used to parse the text. This chart can be used to provide extra information for sorting the queue. :type chart: Chart :rtype: None N(tNotImplementedError(RR`R$((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/pchart.pyRUscCswt|ƒ|jkrst|ƒ|j}|jdkrix/|| D] }td|j|dƒƒqBWn||4ndS(uA Discard items in the queue if the queue is longer than the beam.iu %-50s [DISCARDED]N(RTRGRHRQRR(RR`R$tsplitR-((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/pchart.pyRV#s !( RRt__doc__RR%RIRbRZRURV(((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/pchart.pyRC‡s   5  tInsideChartParsercBseZdZd„ZRS(uU A bottom-up parser for ``PCFG`` grammars that tries edges in descending order of the inside probabilities of their trees. The "inside probability" of a tree is simply the probability of the entire tree, ignoring its context. In particular, the inside probability of a tree generated by production *p* with children *c[1], c[2], ..., c[n]* is *P(p)P(c[1])P(c[2])...P(c[n])*; and the inside probability of a token is 1 if it is present in the text, and 0 if it is absent. This sorting order results in a type of lowest-cost-first search strategy. cCs|jdd„ƒdS(uD Sort the given queue of edges, in descending order of the inside probabilities of the edges' trees. :param queue: The queue of ``Edge`` objects to sort. Each edge in this queue is an edge that could be added to the chart by the fundamental rule; but that has not yet been added. :type queue: list(Edge) :param chart: The chart being used to parse the text. This chart can be used to provide extra information for sorting the queue. :type chart: Chart :rtype: None RLcSs |jƒS(N(R(R-((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/pchart.pyRNKsN(R[(RR`R$((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/pchart.pyRU<s(RRRjRU(((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/pchart.pyRk,sNtRandomChartParsercBseZdZd„ZRS(u A bottom-up parser for ``PCFG`` grammars that tries edges in random order. This sorting order results in a random search strategy. cCs=tjdt|ƒdƒ}|||d|d<|| 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] uI saw John with my telescopeu:the boy saw Jack with Bob under the table with a telescopeu%3s: %siiu %ruWhich demo (%d-%d)? R2u uBad sentence numberRGu s: %s parser: %s grammar: %sicSs||jƒS(N(R(tatb((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/pchart.pyRNãsuC Parser Beam | Time (secs) # Parses Average P(parse)uC------------------------+------------------------------------------u%18s %4d |%11.4f%11d%19.14fcSs||jƒS(N(R(RuRv((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/pchart.pyRNñsu%18s |%11s%11d%19.14fu (All Parses)un/auDraw parses (y/n)? uy(t draw_treesu please wait...uPrint parses (y/n)? (#tsysttimetnltkRst nltk.parseRtRt fromstringRcRQR RTtinttstdintreadlinetstripRiRkRlRpRqRIRPRbRSRtfreezet __class__RRGtkeystlowert startswithtnltk.draw.treeRw(tchoicet draw_parsest print_parsesRxRyRsRtt toy_pcfg1t toy_pcfg2tdemosRotsentR%R]tparsersttimest average_pt num_parsest all_parsestparserttRYRRwRb((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/pchart.pytdemoˆs        "      "   +     ! "   '   ' u__main__($Rjt __future__RRt functoolsRt nltk.treeRRt nltk.grammarRRtnltk.parse.apiRtnltk.parse.chartRR R R t nltk.compatR R RRR)R/R<RCRkRmRlRpRqRcR•R(((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/parse/pchart.pyts. " ¥<