
    Ng6E                        d Z ddlmZ ddlmZmZmZmZmZm	Z	m
Z
mZmZmZmZmZmZ ddlmZmZmZmZmZmZmZmZ  G d de          Z G d dee          Z G d	 d
e          Z G d de          Z G d de          Z G d de          Z G d de          Z  G d de          Z! G d de          Z" G d de          Z# G d de          Z$ e             e             e             e             e            gZ% e             e             e             e            gZ& e             e
             e             e            gZ' e             e
             e             e            gZ( e             e             e             gZ) G d de          Z* G d de*          Z+ G d d e*          Z, G d! d"e*          Z- G d# d$e*          Z. G d% d&e*          Z/ e             e             e"             e#             e$            gZ0 e             e             e             e!            gZ1 e             e             e             e!            gZ2 e             e             e             e!            gZ3 G d' d(e*e          Z4 G d) d*e4          Z5 G d+ d,e4          Z6 G d- d.e4          Z7 G d/ d0e4          Z8	 	 	 	 	 	 d9d6Z9e:d7k    r e9             d8S d8S ):a  
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                   >    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	S )
IncrementalChartc                     t          d |                                 D                       | _        i | _        i | _        d S )Nc              3      K   | ]}g V  d S N .0xs     R/var/www/html/ai-engine/env/lib/python3.11/site-packages/nltk/parse/earleychart.py	<genexpr>z.IncrementalChart.initialize.<locals>.<genexpr>@   s"      >>q>>>>>>    )tuple
_positions
_edgelists_edge_to_cpls_indexesselfs    r"   
initializezIncrementalChart.initialize>   sA    >>DOO,=,=>>>>>   r$   c                 D    t          |                                           S r   )list	iteredgesr*   s    r"   edgeszIncrementalChart.edgesI   s    DNN$$%%%r$   c                 $    d | j         D             S )Nc              3   $   K   | ]}|D ]}|V  d S r   r   )r    edgelistedges      r"   r#   z-IncrementalChart.iteredges.<locals>.<genexpr>M   s/      JJJJJJJJJJJr$   )r'   r*   s    r"   r/   zIncrementalChart.iteredgesL   s    JJT_JJJJr$   c                    | j         |         }i k    rt          |          S t                                                    }t	          |          }|| j        vr|                     |           t	          fd|D                       }t          | j        |         |                             |g                     S )Nc              3   (   K   | ]}|         V  d S r   r   )r    keyrestrictionss     r"   r#   z*IncrementalChart.select.<locals>.<genexpr>^   s(      ==3\#&======r$   r'   itersortedkeysr%   r)   
_add_indexgetr+   endr8   r3   
restr_keysvalss     `   r"   selectzIncrementalChart.selectO   s    ?3' 2>>! L--//00
:&&
 T]**OOJ'''====*=====DM*-c266tR@@AAAr$   c                    |D ])}t          t          |          st          d|z            *t          d |                                 D                       x}| j        |<   t          | j                  D ]V\  }}||         }|D ]Ft          fd|D                       }|                    |g           	                               GWd S )NBad restriction: %sc              3      K   | ]}i V  d S r   r   r   s     r"   r#   z.IncrementalChart._add_index.<locals>.<genexpr>h   "      1P1P"1P1P1P1P1P1Pr$   c              3   H   K   | ]} t          |                      V  d S r   getattrr    r7   r4   s     r"   r#   z.IncrementalChart._add_index.<locals>.<genexpr>n   s6      HHc/WT3//11HHHHHHr$   
hasattrr	   
ValueErrorr%   r&   r)   	enumerater'   
setdefaultappend	r+   rA   r7   indexr@   r3   
this_indexrB   r4   s	           @r"   r=   zIncrementalChart._add_indexa   s     	> 	>C5#&& > !6!<===> -21P1Pdoo>O>O1P1P1P,P,PPj) 't77 	= 	=MCsJ  = =HHHHZHHHHH%%dB//66t<<<<=	= 	=r$   c                                                     }| j                                        D ]O\  }}t          fd|D                       }||                             |g                                          Pd S )Nc              3   H   K   | ]} t          |                      V  d S r   rI   rK   s     r"   r#   z:IncrementalChart._register_with_indexes.<locals>.<genexpr>t   s6      DD#+s++--DDDDDDr$   r@   r)   itemsr%   rP   rQ   r+   r4   r@   rA   rS   rB   s    `    r"   _register_with_indexesz'IncrementalChart._register_with_indexesq   s    hhjj!%!4!4!6!6 	9 	9JDDDDDDDDDD#J!!$++2248888	9 	9r$   c                 j    | j         |                                                             |           d S r   )r'   r@   rQ   )r+   r4   s     r"   _append_edgezIncrementalChart._append_edgew   s+    

#**400000r$   c                 J    t          |                                 dz             S )N   )range
num_leavesr*   s    r"   r&   zIncrementalChart._positionsz   s    T__&&*+++r$   N)__name__
__module____qualname__r,   r0   r/   rC   r=   rZ   r\   r&   r   r$   r"   r   r   =   s        	 	 	& & &K K KB B B$= = = 9 9 91 1 1, , , , ,r$   r   c                        e Zd Zd Zd Zd ZdS )FeatureIncrementalChartc                      j         |         }i k    rt          |          S t                                                    }t	          |          }| j        vr                     |           t	           fd|D                       }t           j        |         |                             |g                     S )Nc              3   N   K   | ]}                     |                   V   d S r   )_get_type_if_possible)r    r7   r8   r+   s     r"   r#   z1FeatureIncrementalChart.select.<locals>.<genexpr>   sG       
 
>AD&&|C'899
 
 
 
 
 
r$   r9   r?   s   ` `   r"   rC   zFeatureIncrementalChart.select   s    ?3' 2>>! L--//00
:&&
 T]**OOJ''' 
 
 
 
 
EO
 
 
 
 
 DM*-c266tR@@AAAr$   c                     |D ])}t          t          |          st          d|z            *t          d                                  D                       x} j        |<   t           j                  D ]W\  }}||         }|D ]Gt           fd|D                       }|                    |g           	                               HXd S )NrE   c              3      K   | ]}i V  d S r   r   r   s     r"   r#   z5FeatureIncrementalChart._add_index.<locals>.<genexpr>   rG   r$   c              3   n   K   | ]/}                      t          |                                V  0d S r   rh   rJ   r    r7   r4   r+   s     r"   r#   z5FeatureIncrementalChart._add_index.<locals>.<genexpr>   sX         ../AwtS/A/A/C/CDD     r$   rL   rR   s	   `       @r"   r=   z"FeatureIncrementalChart._add_index   s    	> 	>C5#&& > !6!<===> -21P1Pdoo>O>O1P1P1P,P,PPj) 't77 	= 	=MCsJ  = =     )     %%dB//66t<<<<=	= 	=r$   c                                                      } j                                        D ]P\  }}t           fd|D                       }||                             |g                                          Qd S )Nc              3   n   K   | ]/}                      t          |                                V  0d S r   rl   rm   s     r"   r#   zAFeatureIncrementalChart._register_with_indexes.<locals>.<genexpr>   sU        EH**+=74+=+=+?+?@@     r$   rW   rY   s   ``    r"   rZ   z.FeatureIncrementalChart._register_with_indexes   s    hhjj!%!4!4!6!6 	9 	9J     LV    D #J!!$++2248888		9 	9r$   N)ra   rb   rc   rC   r=   rZ   r   r$   r"   re   re   ~   sD        B B B(= = =&9 9 9 9 9r$   re   c                       e Zd Zd ZdS )CompleteFundamentalRulec              #     K   |                                 }|                    ||d|                                          D ]D}|                    |                                           }|                    |||          r|V  Ed S NT)startr@   is_completelhs)r@   rC   nextsymmove_dot_forwardinsert_with_backpointer)r+   chartgrammar	left_edger@   
right_edgenew_edges          r"   _apply_incompletez)CompleteFundamentalRule._apply_incomplete   s      mmoo  ,,3Di6G6G6I6I ' 
 
 	 	J !11*..2B2BCCH,,Xy*MM 	 	r$   Nra   rb   rc   r   r   r$   r"   rq   rq      s#        	 	 	 	 	r$   rq   c                   (    e Zd Z e            Zd ZdS )CompleterRulec              #   |   K   t          |t                    s$| j                            |||          E d {V  d S d S r   
isinstancer   _fundamental_ruleapplyr+   rz   r{   r4   s       r"   r   zCompleterRule.apply   sX      $)) 	J-33E7DIIIIIIIIIII	J 	Jr$   Nra   rb   rc   rq   r   r   r   r$   r"   r   r      7        //11J J J J Jr$   r   c                   (    e Zd Z e            Zd ZdS )ScannerRulec              #   |   K   t          |t                    r$| j                            |||          E d {V  d S d S r   r   r   s       r"   r   zScannerRule.apply   sX      dH%% 	J-33E7DIIIIIIIIIII	J 	Jr$   Nr   r   r$   r"   r   r      r   r$   r   c                       e Zd ZdS )PredictorRuleNra   rb   rc   r   r$   r"   r   r              Dr$   r   c                       e Zd Zd ZdS )FilteredCompleteFundamentalRulec              #   p   K   |                                 r|                     |||          E d {V  d S d S r   )ru   _apply_completer   s       r"   r   z%FilteredCompleteFundamentalRule.apply   sY        	B++E7DAAAAAAAAAAA	B 	Br$   N)ra   rb   rc   r   r   r$   r"   r   r      s(        B B B B Br$   r   c                       e Zd Zd ZdS )FeatureCompleteFundamentalRulec              #      K   | j         }|                                }|                    ||d|                                          D ] }|                    ||||          E d {V  !d S rs   )r   r@   rC   rw   r   )r+   rz   r{   r|   frr@   r}   s          r"   r   z0FeatureCompleteFundamentalRule._apply_incomplete   s      #mmoo  ,,3Di6G6G6I6I ' 
 
 	G 	GJ xxw	:FFFFFFFFFF	G 	Gr$   Nr   r   r$   r"   r   r      s(        G G G G Gr$   r   c                   "    e Zd Z e            ZdS )FeatureCompleterRuleNra   rb   rc   r   r   r   r$   r"   r   r              6688r$   r   c                   "    e Zd Z e            ZdS )FeatureScannerRuleNr   r   r$   r"   r   r      r   r$   r   c                       e Zd ZdS )FeaturePredictorRuleNr   r   r$   r"   r   r      r   r$   r   c                   *    e Zd ZdZeddefdZddZdS )IncrementalChartParsera  
    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                    || _         || _        || _        || _        g | _        g | _        |D ]\}|j        dk    r| j                            |           (|j        dk    r| j                            |           Nt          d          dS )a  
        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   r^   z9Incremental inference rules must have NUM_EDGES == 0 or 1N)	_grammar_trace_trace_chart_width_chart_class_axioms_inference_rules	NUM_EDGESrQ   rN   )r+   r{   strategytracetrace_chart_widthchart_classrules          r"   __init__zIncrementalChartParser.__init__/  s    4  "3' " 	 	D~""##D))))1$$%,,T2222 R  	 	r$   Nc           	         || j         }| j        }t          |          }| j                            |           |                     |          }| j        }| j        |                                dz   z  }|r"t          |	                    |                     | j
        D ]4}t          |                    ||                    } ||||||           5| j        }	t          |                                dz             D ]}
|dk    rt          d|
d           t          |                    |
                    }|r|                                }|	D ]g}t          |                    |||                    } ||||||           |D ]/}|                                |
k    r|                    |           0h||S )Nr^   z
* Processing queue:
)r@   )r   _trace_new_edgesr.   r   check_coverager   r   r`   printpretty_format_leavesr   r   r   r_   rC   popr@   rQ   )r+   tokensr   trace_new_edgesrz   r{   trace_edge_widthaxiom	new_edgesinference_rulesr@   agendar4   r   r~   s                  r"   chart_parsez"IncrementalChartParser.chart_parseZ  s   =KE/f$$V,,,!!&))-  2u7G7G7I7IA7MN 	@%,,-=>>???\ 	N 	NEU[[8899IOE5)U<LMMMM/))++a/00 	4 	4Cqyy-sD999%,,3,//00F 4zz||+ 4 4D $TZZw%E%E F FI#OE4ECSTTT$- 4 4#<<>>S00"MM(3334  4 r$   r   )ra   rb   rc   __doc__BU_LC_INCREMENTAL_STRATEGYr   r   r   r   r$   r"   r   r     sU         $ ,$) ) ) )V! ! ! ! ! !r$   r   c                       e Zd Zd ZdS )EarleyChartParserc                 8    t          j        | |t          fi | d S r   )r   r   EARLEY_STRATEGYr+   r{   parser_argss      r"   r   zEarleyChartParser.__init__  s%    'gVV+VVVVVr$   Nra   rb   rc   r   r   r$   r"   r   r   ~  s(        W W W W Wr$   r   c                       e Zd Zd ZdS )IncrementalTopDownChartParserc                 8    t          j        | |t          fi | d S r   )r   r   TD_INCREMENTAL_STRATEGYr   s      r"   r   z&IncrementalTopDownChartParser.__init__  6    ''2	
 	
6A	
 	
 	
 	
 	
r$   Nr   r   r$   r"   r   r     #        
 
 
 
 
r$   r   c                       e Zd Zd ZdS )IncrementalBottomUpChartParserc                 8    t          j        | |t          fi | d S r   )r   r   BU_INCREMENTAL_STRATEGYr   s      r"   r   z'IncrementalBottomUpChartParser.__init__  r   r$   Nr   r   r$   r"   r   r     r   r$   r   c                       e Zd Zd ZdS )(IncrementalBottomUpLeftCornerChartParserc                 8    t          j        | |t          fi | d S r   )r   r   r   r   s      r"   r   z1IncrementalBottomUpLeftCornerChartParser.__init__  s6    ''5	
 	
9D	
 	
 	
 	
 	
r$   Nr   r   r$   r"   r   r     r   r$   r   c                       e Zd Zd ZdS ) IncrementalLeftCornerChartParserc                 ~    |                                 st          d          t          j        | |t          fi | d S )NzNIncrementalLeftCornerParser only works for grammars without empty productions.)is_nonemptyrN   r   r   LC_INCREMENTAL_STRATEGYr   s      r"   r   z)IncrementalLeftCornerChartParser.__init__  sc    ""$$ 	-   	''2	
 	
6A	
 	
 	
 	
 	
r$   Nr   r   r$   r"   r   r     s#        
 
 
 
 
r$   r   c                       e Zd ZedefdZdS )FeatureIncrementalChartParser   c                 4    t          j        | |f|||d| d S )N)r   r   r   )r   r   )r+   r{   r   r   r   r   s         r"   r   z&FeatureIncrementalChartParser.__init__  sH     	'	
 /#	
 	
 	
 	
 	
 	
 	
r$   N)ra   rb   rc   "BU_LC_INCREMENTAL_FEATURE_STRATEGYre   r   r   r$   r"   r   r     s3         4+
 
 
 
 
 
r$   r   c                       e Zd Zd ZdS )FeatureEarleyChartParserc                 8    t          j        | |t          fi | d S r   )r   r   EARLEY_FEATURE_STRATEGYr   s      r"   r   z!FeatureEarleyChartParser.__init__  s6    %.'2	
 	
6A	
 	
 	
 	
 	
r$   Nr   r   r$   r"   r   r     r   r$   r   c                       e Zd Zd ZdS )$FeatureIncrementalTopDownChartParserc                 8    t          j        | |t          fi | d S r   )r   r   TD_INCREMENTAL_FEATURE_STRATEGYr   s      r"   r   z-FeatureIncrementalTopDownChartParser.__init__  6    %.':	
 	
>I	
 	
 	
 	
 	
r$   Nr   r   r$   r"   r   r     r   r$   r   c                       e Zd Zd ZdS )%FeatureIncrementalBottomUpChartParserc                 8    t          j        | |t          fi | d S r   )r   r   BU_INCREMENTAL_FEATURE_STRATEGYr   s      r"   r   z.FeatureIncrementalBottomUpChartParser.__init__  r   r$   Nr   r   r$   r"   r   r     r   r$   r   c                       e Zd Zd ZdS )/FeatureIncrementalBottomUpLeftCornerChartParserc                 8    t          j        | |t          fi | d S r   )r   r   r   r   s      r"   r   z8FeatureIncrementalBottomUpLeftCornerChartParser.__init__  s6    %.'=	
 	
AL	
 	
 	
 	
 	
r$   Nr   r   r$   r"   r   r     r   r$   r   TF   $I saw John with a dog with my cookie   c                    ddl }ddl}ddlm}  |            }	|rt	          d           t	          |	           t	          d           t	          |           |                                }
t	          |
           t	                       t          |	|          }t                      }|                    |
          }t          |
                    |	                                                    }t                      |z
  }|rt          |          |k    s
J d            |r|D ]}t	          |           nt	          dt          |                     | rt	          d	|           dS dS )
z0
    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   r.   parsesrt   len)print_timesprint_grammarprint_treesr   sent	numparsesr   r   r   r{   r   earleytrz   r   trees                   r"   demor    s    JJJKKK------ lnnG kg 
-	$KKKZZ\\F	&MMM	GGG we444FAv&&E%,,w}}//00FA  @6{{i''')?''' ( 	 	D$KKKK	 	k3v;;''' gq r$   __main__N)TFTr   r   r   );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   re   rq   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  ra   r   r$   r"   <module>r     s?                                       	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 >, >, >, >, >,u >, >, >,B.9 .9 .9 .9 .9. .9 .9 .9l
 
 
 
 
7 
 
 
J J J J J+ J J JJ J J J J) J J J	 	 	 	 	, 	 	 	B B B B B&G B B B	G 	G 	G 	G 	G%E 	G 	G 	G9 9 9 9 9= 9 9 99 9 9 9 9 9 9 9	 	 	 	 	4 	 	 	 LNNOMOOKMMMOO LNNO	  LNN	  LNN  	  LNN&&((##%% \ \ \ \ \[ \ \ \~W W W W W. W W W

 
 
 
 
$: 
 
 

 
 
 
 
%; 
 
 

 
 
 
 
/E 
 
 
	
 	
 	
 	
 	
'= 	
 	
 	
" LNN  LNN""$$	#  LNN  ""$$	#  LNN%%''""$$	& "
 
 
 
 
$:<N 
 
 
&
 
 
 
 
< 
 
 

 
 
 
 
+H 
 
 

 
 
 
 
,I 
 
 

 
 
 
 
6S 
 
 
 
	/- - - -` zDFFFFF r$   