
    /h6E                     r   S r SSKJr  SSKJrJrJrJrJrJ	r	J
r
JrJrJrJrJrJr  SSKJrJrJrJrJrJrJrJr   " S S\5      r " S S\\5      r " S	 S
\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r  " S S\5      r! " S S\5      r" " S S\5      r# " S S\5      r$\" 5       \" 5       \" 5       \" 5       \" 5       /r%\" 5       \" 5       \" 5       \" 5       /r&\" 5       \
" 5       \" 5       \" 5       /r'\" 5       \
" 5       \" 5       \" 5       /r(\" 5       \" 5       \ " 5       /r) " S S\5      r* " S S\*5      r+ " S S \*5      r, " S! S"\*5      r- " S# S$\*5      r. " S% S&\*5      r/\" 5       \" 5       \"" 5       \#" 5       \$" 5       /r0\" 5       \" 5       \" 5       \!" 5       /r1\" 5       \" 5       \" 5       \!" 5       /r2\" 5       \" 5       \" 5       \!" 5       /r3 " S' S(\*\5      r4 " S) S*\45      r5 " S+ S,\45      r6 " S- S.\45      r7 " S/ S0\45      r8      S4S1 jr9\:S2:X  a  \9" 5         g3g3)5a  
Data classes and parser implementations for *incremental* chart
parsers, which use dynamic programming to efficiently parse a text.
A "chart parser" derives parse trees for a text by iteratively adding
"edges" to a "chart".  Each "edge" represents a hypothesis about the tree
structure for a subsequence of the text.  The "chart" is a
"blackboard" for composing and combining these hypotheses.

A parser is "incremental", if it guarantees that for all i, j where i < j,
all edges ending at i are built before any edges ending at j.
This is appealing for, say, speech recognizer hypothesis filtering.

The main parser class is ``EarleyChartParser``, which is a top-down
algorithm, originally formulated by Jay Earley (1970).
    )perf_counter)BottomUpPredictCombineRuleBottomUpPredictRuleCachedTopDownPredictRuleChartChartParserEdgeIEmptyPredictRule"FilteredBottomUpPredictCombineRule!FilteredSingleEdgeFundamentalRuleLeafEdgeLeafInitRuleSingleEdgeFundamentalRuleTopDownInitRule)!FeatureBottomUpPredictCombineRuleFeatureBottomUpPredictRuleFeatureChartFeatureChartParserFeatureEmptyPredictRule FeatureSingleEdgeFundamentalRuleFeatureTopDownInitRuleFeatureTopDownPredictRulec                   D    \ rS rSrS rS rS rS rS rS r	S r
S	 rS
rg)IncrementalChart=   c                 j    [        S U R                  5        5       5      U l        0 U l        0 U l        g )Nc              3   &   #    U  H  n/ v   M	     g 7fN .0xs     N/var/www/auris/envauris/lib/python3.13/site-packages/nltk/parse/earleychart.py	<genexpr>.IncrementalChart.initialize.<locals>.<genexpr>@   s     >,=q,=   )tuple
_positions
_edgelists_edge_to_cpls_indexesselfs    r#   
initializeIncrementalChart.initialize>   s.    >DOO,=>>       c                 4    [        U R                  5       5      $ r   )list	iteredgesr,   s    r#   edgesIncrementalChart.edgesI   s    DNN$%%r0   c                 (    S U R                    5       $ )Nc              3   6   #    U  H  o  H  o"v   M     M     g 7fr   r   )r!   edgelistedges      r#   r$   -IncrementalChart.iteredges.<locals>.<genexpr>M   s     J__s   )r)   r,   s    r#   r3   IncrementalChart.iteredgesL   s    JT__JJr0   c                 L  ^ U R                   U   nT0 :X  a  [        U5      $ [        TR                  5       5      n[	        U5      nX@R
                  ;  a  U R                  U5        [	        U4S jU 5       5      n[        U R
                  U   U   R                  U/ 5      5      $ )Nc              3   .   >#    U  H
  nTU   v   M     g 7fr   r   )r!   keyrestrictionss     r#   r$   *IncrementalChart.select.<locals>.<genexpr>^   s     =*3\#&*s   r)   itersortedkeysr'   r+   
_add_indexgetr-   endr?   r8   
restr_keysvalss     `   r#   selectIncrementalChart.selectO   s    ??3' 2>! L--/0
:&
 ]]*OOJ'=*==DMM*-c266tR@AAr0   c                 |  ^ U H&  n[        [        U5      (       a  M  [        SU-  5      e   [        S U R	                  5        5       5      =o0R
                  U'   [        U R                  5       HH  u  pEX4   nU H9  m[        U4S jU 5       5      nUR                  U/ 5      R                  T5        M;     MJ     g )NBad restriction: %sc              3   &   #    U  H  n0 v   M	     g 7fr   r   r    s     r#   r$   .IncrementalChart._add_index.<locals>.<genexpr>h        1P>O">Or&   c              3   F   >#    U  H  n[        TU5      " 5       v   M     g 7fr   getattrr!   r>   r9   s     r#   r$   rP   n   s     HZcWT3/11Z   !
hasattrr	   
ValueErrorr'   r(   r+   	enumerater)   
setdefaultappend	r-   rI   r>   indexrH   r8   
this_indexrJ   r9   s	           @r#   rE   IncrementalChart._add_indexa   s    C5#&& !6!<== 
 -21Pdoo>O1P,PPj) 't7MCJ HZHH%%dB/66t< ! 8r0   c                    ^ TR                  5       nU R                  R                  5        H=  u  p4[        U4S jU 5       5      nXB   R	                  U/ 5      R                  T5        M?     g )Nc              3   F   >#    U  H  n[        TU5      " 5       v   M     g 7fr   rS   rU   s     r#   r$   :IncrementalChart._register_with_indexes.<locals>.<genexpr>t   s     D#s+--rV   rH   r+   itemsr'   r[   r\   r-   r9   rH   rI   r^   rJ   s    `    r#   _register_with_indexes'IncrementalChart._register_with_indexesq   sU    hhj!%!4!4!6JDDDDJ!!$+2248 "7r0   c                 \    U R                   UR                  5          R                  U5        g r   )r)   rH   r\   )r-   r9   s     r#   _append_edgeIncrementalChart._append_edgew   s    
#**40r0   c                 :    [        U R                  5       S-   5      $ )N   )range
num_leavesr,   s    r#   r(   IncrementalChart._positionsz   s    T__&*++r0   )r*   r)   r+   N)__name__
__module____qualname____firstlineno__r.   r4   r3   rK   rE   rg   rj   r(   __static_attributes__r   r0   r#   r   r   =   s,    	&KB$= 91,r0   r   c                   &    \ rS rSrS rS rS rSrg)FeatureIncrementalChart~   c                 R  ^ ^ T R                   U   nT0 :X  a  [        U5      $ [        TR                  5       5      n[	        U5      nUT R
                  ;  a  T R                  U5        [	        UU 4S jU 5       5      n[        T R
                  U   U   R                  U/ 5      5      $ )Nc              3   L   >#    U  H  nTR                  TU   5      v   M     g 7fr   )_get_type_if_possible)r!   r>   r?   r-   s     r#   r$   1FeatureIncrementalChart.select.<locals>.<genexpr>   s'      
EOcD&&|C'899Zs   !$rA   rG   s   ` `   r#   rK   FeatureIncrementalChart.select   s    ??3' 2>! L--/0
:&
 T]]*OOJ' 
EO
 
 DMM*-c266tR@AAr0   c                   ^ ^ U H&  n[        [        U5      (       a  M  [        SU-  5      e   [        S T R	                  5        5       5      =nT R
                  U'   [        T R                  5       HI  u  pEX4   nU H:  m[        UU 4S jU 5       5      nUR                  U/ 5      R                  T5        M<     MK     g )NrN   c              3   &   #    U  H  n0 v   M	     g 7fr   r   r    s     r#   r$   5FeatureIncrementalChart._add_index.<locals>.<genexpr>   rQ   r&   c              3   d   >#    U  H%  nTR                  [        TU5      " 5       5      v   M'     g 7fr   r{   rT   r!   r>   r9   r-   s     r#   r$   r      s0      ) ..wtS/A/CDD)   -0rW   r]   s	   `       @r#   rE   "FeatureIncrementalChart._add_index   s    C5#&& !6!<== 
 -21Pdoo>O1P,PPj) 't7MCJ  )  %%dB/66t< ! 8r0   c                    ^ ^ TR                  5       nT R                  R                  5        H>  u  p4[        UU 4S jU 5       5      nXB   R	                  U/ 5      R                  T5        M@     g )Nc              3   d   >#    U  H%  nTR                  [        TU5      " 5       5      v   M'     g 7fr   r   r   s     r#   r$   AFeatureIncrementalChart._register_with_indexes.<locals>.<genexpr>   s,      LVS**74+=+?@@Jr   rd   rf   s   ``    r#   rg   .FeatureIncrementalChart._register_with_indexes   s^    hhj!%!4!4!6J LV D J!!$+2248	 "7r0   r   N)rq   rr   rs   rt   rK   rE   rg   ru   r   r0   r#   rw   rw   ~   s    B(=&9r0   rw   c                       \ rS rSrS rSrg)CompleteFundamentalRule   c              #      #    UR                  5       nUR                  XDSUR                  5       S9 H?  nUR                  UR                  5       5      nUR	                  XcU5      (       d  M;  Uv   MA     g 7fNT)startrH   is_completelhs)rH   rK   nextsymmove_dot_forwardinsert_with_backpointer)r-   chartgrammar	left_edgerH   
right_edgenew_edges          r#   _apply_incomplete)CompleteFundamentalRule._apply_incomplete   sk     mmo  ,,Di6G6G6I ' 
J !11*..2BCH,,X*MM
s   A'A6-	A6r   Nrq   rr   rs   rt   r   ru   r   r0   r#   r   r      s    	r0   r   c                   (    \ rS rSr\" 5       rS rSrg)CompleterRule   c              #      #    [        U[        5      (       d%  U R                  R                  XU5       S h  vN   g g  N7fr   
isinstancer   _fundamental_ruleapplyr-   r   r   r9   s       r#   r   CompleterRule.apply   s6     $))--33EDIII *I   5A >A r   Nrq   rr   rs   rt   r   r   r   ru   r   r0   r#   r   r          /1Jr0   r   c                   (    \ rS rSr\" 5       rS rSrg)ScannerRule   c              #      #    [        U[        5      (       a%  U R                  R                  XU5       S h  vN   g g  N7fr   r   r   s       r#   r   ScannerRule.apply   s6     dH%%--33EDIII &Ir   r   Nr   r   r0   r#   r   r      r   r0   r   c                       \ rS rSrSrg)PredictorRule   r   Nrq   rr   rs   rt   ru   r   r0   r#   r   r          r0   r   c                       \ rS rSrS rSrg)FilteredCompleteFundamentalRule   c              #   p   #    UR                  5       (       a  U R                  XU5       S h  vN   g g  N7fr   )r   _apply_completer   s       r#   r   %FilteredCompleteFundamentalRule.apply   s4      ++EDAAA As   +646r   N)rq   rr   rs   rt   r   ru   r   r0   r#   r   r      s    Br0   r   c                       \ rS rSrS rSrg)FeatureCompleteFundamentalRule   c              #      #    U R                   nUR                  5       nUR                  XUSUR                  5       S9 H  nUR	                  XX65       S h  vN   M     g  N	7fr   )r   rH   rK   r   r   )r-   r   r   r   frrH   r   s          r#   r   0FeatureCompleteFundamentalRule._apply_incomplete   sa     ##mmo  ,,Di6G6G6I ' 
J xx	FFF
 Gs   AA"A 
A"r   Nr   r   r0   r#   r   r      s    Gr0   r   c                   "    \ rS rSr\" 5       rSrg)FeatureCompleterRule   r   Nrq   rr   rs   rt   r   r   ru   r   r0   r#   r   r          68r0   r   c                   "    \ rS rSr\" 5       rSrg)FeatureScannerRule   r   Nr   r   r0   r#   r   r      r   r0   r   c                       \ rS rSrSrg)FeaturePredictorRule   r   Nr   r   r0   r#   r   r      r   r0   r   c                   4    \ rS rSrSr\SS\4S jrS	S jrSr	g)
IncrementalChartParseri  a  
An *incremental* chart parser implementing Jay Earley's
parsing algorithm:

| For each index end in [0, 1, ..., N]:
|   For each edge such that edge.end = end:
|     If edge is incomplete and edge.next is not a part of speech:
|       Apply PredictorRule to edge
|     If edge is incomplete and edge.next is a part of speech:
|       Apply ScannerRule to edge
|     If edge is complete:
|       Apply CompleterRule to edge
| Return any complete parses in the chart
r   2   c                 (   Xl         X0l        X@l        XPl        / U l        / U l        U Hf  nUR                  S:X  a  U R                  R                  U5        M0  UR                  S:X  a  U R
                  R                  U5        M]  [        S5      e   g)av  
Create a new Earley chart parser, that uses ``grammar`` to
parse texts.

:type grammar: CFG
: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.
:type trace_chart_width: int
:param trace_chart_width: The default total width reserved for
    the chart in trace output.  The remainder of each line will
    be used to display edges.
:param chart_class: The class that should be used to create
    the charts used by this parser.
r   rm   z9Incremental inference rules must have NUM_EDGES == 0 or 1N)	_grammar_trace_trace_chart_width_chart_class_axioms_inference_rules	NUM_EDGESr\   rY   )r-   r   strategytracetrace_chart_widthchart_classrules          r#   __init__IncrementalChartParser.__init__/  s    4  "3' "D~~"##D)1$%%,,T2 R  r0   Nc           	      f   Uc  U R                   nU R                  n[        U5      nU R                  R	                  U5        U R                  U5      nU R                  nU R                  UR                  5       S-   -  nU(       a  [        UR                  U5      5        U R                   H'  n[        UR                  XE5      5      nU" XGXU5        M)     U R                  n	[        UR                  5       S-   5       H  n
US:  a  [        SU
S5        [        UR                  U
S95      nU(       d  M7  UR                  5       nU	 HX  n[        UR                  XEU5      5      nU" XMXU5        U H*  nUR!                  5       U
:X  d  M  UR#                  U5        M,     MZ     U(       a  Mw  M     U$ )Nrm   z
* Processing queue:
)rH   )r   _trace_new_edgesr2   r   check_coverager   r   ro   printpretty_format_leavesr   r   r   rn   rK   poprH   r\   )r-   tokensr   trace_new_edgesr   r   trace_edge_widthaxiom	new_edgesinference_rulesrH   agendar9   r   r   s                  r#   chart_parse"IncrementalChartParser.chart_parseZ  sv   =KKE//f$$V,!!&)--  22u7G7G7IA7MN%,,-=>?\\EU[[89IE)<LM " //))+a/0Cqy-sD9%,,3,/0F&zz|+D $TZZ%E FI#ECST$-#<<>S0"MM(3 %. , &	 1 r0   )r   r   r   r   r   r   r   )
rq   rr   rs   rt   __doc__BU_LC_INCREMENTAL_STRATEGYr   r   r   ru   r   r0   r#   r   r     s     $ ,$)V!r0   r   c                       \ rS rSrS rSrg)EarleyChartParseri~  c                 <    [         R                  " X[        40 UD6  g r   )r   r   EARLEY_STRATEGYr-   r   parser_argss      r#   r   EarleyChartParser.__init__  s    ''V+Vr0   r   Nrq   rr   rs   rt   r   ru   r   r0   r#   r   r   ~  s    Wr0   r   c                       \ rS rSrS rSrg)IncrementalTopDownChartParseri  c                 <    [         R                  " X[        40 UD6  g r   )r   r   TD_INCREMENTAL_STRATEGYr   s      r#   r   &IncrementalTopDownChartParser.__init__      ''2	
6A	
r0   r   Nr   r   r0   r#   r   r         
r0   r   c                       \ rS rSrS rSrg)IncrementalBottomUpChartParseri  c                 <    [         R                  " X[        40 UD6  g r   )r   r   BU_INCREMENTAL_STRATEGYr   s      r#   r   'IncrementalBottomUpChartParser.__init__  r   r0   r   Nr   r   r0   r#   r   r     r   r0   r   c                       \ rS rSrS rSrg)(IncrementalBottomUpLeftCornerChartParseri  c                 <    [         R                  " X[        40 UD6  g r   )r   r   r   r   s      r#   r   1IncrementalBottomUpLeftCornerChartParser.__init__  s    ''5	
9D	
r0   r   Nr   r   r0   r#   r  r    r   r0   r  c                       \ rS rSrS rSrg) IncrementalLeftCornerChartParseri  c                 |    UR                  5       (       d  [        S5      e[        R                  " X[        40 UD6  g )NzNIncrementalLeftCornerParser only works for grammars without empty productions.)is_nonemptyrY   r   r   LC_INCREMENTAL_STRATEGYr   s      r#   r   )IncrementalLeftCornerChartParser.__init__  sA    ""$$-  	''2	
6A	
r0   r   Nr   r   r0   r#   r  r    s    
r0   r  c                   $    \ rS rSr\S\4S jrSrg)FeatureIncrementalChartParseri     c                 <    [         R                  " U U4UUUS.UD6  g )N)r   r   r   )r   r   )r-   r   r   r   r   r   s         r#   r   &FeatureIncrementalChartParser.__init__  s2     	''	
 /#	
 	
r0   r   N)rq   rr   rs   rt   "BU_LC_INCREMENTAL_FEATURE_STRATEGYrw   r   ru   r   r0   r#   r  r    s     4+
r0   r  c                       \ rS rSrS rSrg)FeatureEarleyChartParseri  c                 <    [         R                  " X[        40 UD6  g r   )r  r   EARLEY_FEATURE_STRATEGYr   s      r#   r   !FeatureEarleyChartParser.__init__  s    %..2	
6A	
r0   r   Nr   r   r0   r#   r  r    r   r0   r  c                       \ rS rSrS rSrg)$FeatureIncrementalTopDownChartParseri  c                 <    [         R                  " X[        40 UD6  g r   )r  r   TD_INCREMENTAL_FEATURE_STRATEGYr   s      r#   r   -FeatureIncrementalTopDownChartParser.__init__      %..:	
>I	
r0   r   Nr   r   r0   r#   r  r    r   r0   r  c                       \ rS rSrS rSrg)%FeatureIncrementalBottomUpChartParseri  c                 <    [         R                  " X[        40 UD6  g r   )r  r   BU_INCREMENTAL_FEATURE_STRATEGYr   s      r#   r   .FeatureIncrementalBottomUpChartParser.__init__  r  r0   r   Nr   r   r0   r#   r  r    r   r0   r  c                       \ rS rSrS rSrg)/FeatureIncrementalBottomUpLeftCornerChartParseri  c                 <    [         R                  " X[        40 UD6  g r   )r  r   r  r   s      r#   r   8FeatureIncrementalBottomUpLeftCornerChartParser.__init__  s    %..=	
AL	
r0   r   Nr   r   r0   r#   r$  r$    r   r0   r$  c                 T   SSK nSSKnSSKJn  U" 5       n	U(       a  [	        S5        [	        U	5        [	        S5        [	        U5        UR                  5       n
[	        U
5        [	        5         [        XS9n[        5       nUR                  U
5      n[        UR                  U	R                  5       5      5      n[        5       U-
  nU(       a  [        U5      U:X  d   S5       eU(       a  U H  n[	        U5        M     O[	        S[        U5      5        U (       a  [	        S	U5        gg)
z(
A demonstration of the Earley parsers.
r   N)demo_grammarz	* Grammarz* Sentence:)r   zNot all parses foundz	Nr trees:zTime:)systimenltk.parse.chartr(  r   splitr   r   r   r2   parsesr   len)print_timesprint_grammarprint_treesr   sent	numparsesr)  r*  r(  r   r   earleytr   r-  trees                   r#   demor7    s     - nGkg 
-	$KZZ\F	&M	G w4FAv&E%,,w}}/0FA 6{i'?)??'D$K  	k3v;'gq r0   __main__N)TFT   z$I saw John with a dog with my cookie   );r   r*  r   r+  r   r   r   r   r   r	   r
   r   r   r   r   r   r   nltk.parse.featurechartr   r   r   r   r   r   r   r   r   rw   r   r   r   r   r   r   r   r   r   r   r   r  r   r  r   r   r   r   r  r  r  r  r!  r  r  r  r  r  r$  r7  rq   r   r0   r#   <module>r<     s       	 	 	 >,u >,B.9. .9l
7 
J+ JJ) J	, 	B&G B	G%E 	G9= 99 9	4 	 NOMO N	  N	  N 	  N&(#% \[ \~W. W

$: 

%; 

/E 
	
'= 	
" N  N"$	#  N "$	#  N%'"$	& "
$:<N 
&
< 

+H 

,I 

6S 
 
	/-` zF r0   