o
    i6E                     @   s  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 deZG dd deeZG d	d
 d
eZG dd deZG dd deZG dd deZG dd deZ G dd deZ!G dd deZ"G dd deZ#G d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 deZ*G dd de*Z+G dd  d e*Z,G d!d" d"e*Z-G d#d$ d$e*Z.G d%d& d&e*Z/e e e" e# e$ gZ0e e e e! gZ1e e e e! gZ2e e e e! gZ3G d'd( d(e*eZ4G d)d* d*e4Z5G d+d, d,e4Z6G d-d. d.e4Z7G d/d0 d0e4Z8	1	2	1	3	4	5d:d6d7Z9e:d8kr]e9  d9S d9S );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                   @   sL   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dS )IncrementalChartc                 C   s(   t dd |  D | _i | _i | _d S )Nc                 s   s    | ]}g V  qd S N .0xr   r   Q/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/nltk/parse/earleychart.py	<genexpr>@       z.IncrementalChart.initialize.<locals>.<genexpr>)tuple
_positions
_edgelists_edge_to_cpls_indexesselfr   r   r   
initialize>   s   
zIncrementalChart.initializec                 C   s   t |  S r   )list	iteredgesr&   r   r   r   edgesI   s   zIncrementalChart.edgesc                 C   s   dd | j D S )Nc                 s   s    | ]
}|D ]}|V  qqd S r   r   )r   edgelistedger   r   r   r   M   s    z-IncrementalChart.iteredges.<locals>.<genexpr>)r#   r&   r   r   r   r*   L      zIncrementalChart.iteredgesc                    sr   | j | } i krt|S t  }t|}|| jvr!| | t fdd|D }t| j| | |g S )Nc                 3   s    | ]} | V  qd S r   r   r   keyrestrictionsr   r   r   ^   s    z*IncrementalChart.select.<locals>.<genexpr>r#   itersortedkeysr!   r%   
_add_indexgetr'   endr2   r,   
restr_keysvalsr   r1   r   selectO   s   


zIncrementalChart.selectc                    s   |D ]}t t|std| qtdd |  D  }| j|< t| jD ]!\}}|| }|D ] t fdd|D }||g 	  q/q%d S )NBad restriction: %sc                 s       | ]}i V  qd S r   r   r   r   r   r   r   h   r    z.IncrementalChart._add_index.<locals>.<genexpr>c                 3       | ]	}t  | V  qd S r   getattrr/   r-   r   r   r   n       
hasattrr   
ValueErrorr!   r"   r%   	enumerater#   
setdefaultappendr'   r;   r0   indexr:   r,   
this_indexr<   r   rC   r   r7   a   s   
 zIncrementalChart._add_indexc                    sL      }| j D ]\}}t fdd|D }|| |g   q	d S )Nc                 3   r@   r   rA   r/   rC   r   r   r   t   rD   z:IncrementalChart._register_with_indexes.<locals>.<genexpr>r:   r%   itemsr!   rI   rJ   r'   r-   r:   r;   rL   r<   r   rC   r   _register_with_indexesq   s
   z'IncrementalChart._register_with_indexesc                 C   s   | j |  | d S r   )r#   r:   rJ   )r'   r-   r   r   r   _append_edgew   s   zIncrementalChart._append_edgec                 C   s   t |  d S )N   )range
num_leavesr&   r   r   r   r"   z   r.   zIncrementalChart._positionsN)__name__
__module____qualname__r(   r+   r*   r=   r7   rQ   rR   r"   r   r   r   r   r   =   s    r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )FeatureIncrementalChartc                    st   j | } i krt|S t  }t|}|jvr!| t fdd|D }tj| | |g S )Nc                 3   s    | ]
}  | V  qd S r   )_get_type_if_possibler/   r2   r'   r   r   r      s    
z1FeatureIncrementalChart.select.<locals>.<genexpr>r3   r9   r   r[   r   r=      s   


zFeatureIncrementalChart.selectc                    s   |D ]}t t|std| qtdd  D  }j|< tjD ]"\}}|| }|D ] t fdd|D }||g 	  q/q%d S )Nr>   c                 s   r?   r   r   r   r   r   r   r      r    z5FeatureIncrementalChart._add_index.<locals>.<genexpr>c                 3   "    | ]} t | V  qd S r   rZ   rB   r/   r-   r'   r   r   r      s
    
rE   rK   r   r^   r   r7      s   
 z"FeatureIncrementalChart._add_indexc                    sN      }j D ]\}}t fdd|D }|| |g   q	d S )Nc                 3   r\   r   r]   r/   r^   r   r   r      s    
zAFeatureIncrementalChart._register_with_indexes.<locals>.<genexpr>rN   rP   r   r^   r   rQ      s   z.FeatureIncrementalChart._register_with_indexesN)rV   rW   rX   r=   r7   rQ   r   r   r   r   rY   ~   s    rY   c                   @      e Zd Zdd ZdS )CompleteFundamentalRulec                 c   sL    |  }|j||d| dD ]}||  }||||r#|V  qd S NT)startr:   is_completelhs)r:   r=   nextsymmove_dot_forwardinsert_with_backpointer)r'   chartgrammar	left_edger:   
right_edgenew_edger   r   r   _apply_incomplete   s   
z)CompleteFundamentalRule._apply_incompleteNrV   rW   rX   rm   r   r   r   r   r`          r`   c                   @      e Zd Ze Zdd ZdS )CompleterRulec                 c   s*    t |ts| j|||E d H  d S d S r   
isinstancer   _fundamental_ruleapplyr'   rh   ri   r-   r   r   r   ru         
zCompleterRule.applyNrV   rW   rX   r`   rt   ru   r   r   r   r   rq          rq   c                   @   rp   )ScannerRulec                 c   s*    t |tr| j|||E d H  d S d S r   rr   rv   r   r   r   ru      rw   zScannerRule.applyNrx   r   r   r   r   rz      ry   rz   c                   @      e Zd ZdS )PredictorRuleNrV   rW   rX   r   r   r   r   r|          r|   c                   @   r_   )FilteredCompleteFundamentalRulec                 c   s&    |  r| |||E d H  d S d S r   )rc   _apply_completerv   r   r   r   ru      s   z%FilteredCompleteFundamentalRule.applyN)rV   rW   rX   ru   r   r   r   r   r      ro   r   c                   @   r_   )FeatureCompleteFundamentalRulec                 c   sF    | j }| }|j||d| dD ]}|||||E d H  qd S ra   )rt   r:   r=   re   ru   )r'   rh   ri   rj   frr:   rk   r   r   r   rm      s   
z0FeatureCompleteFundamentalRule._apply_incompleteNrn   r   r   r   r   r      ro   r   c                   @      e Zd Ze ZdS )FeatureCompleterRuleNrV   rW   rX   r   rt   r   r   r   r   r          
r   c                   @   r   )FeatureScannerRuleNr   r   r   r   r   r      r   r   c                   @   r{   )FeaturePredictorRuleNr}   r   r   r   r   r      r~   r   c                   @   s,   e Zd ZdZeddefddZd	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                 C   sh   || _ || _|| _|| _g | _g | _|D ]}|jdkr"| j| q|jdkr.| j| qt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   rS   z9Incremental inference rules must have NUM_EDGES == 0 or 1N)	_grammar_trace_trace_chart_width_chart_class_axioms_inference_rules	NUM_EDGESrJ   rG   )r'   ri   strategytracetrace_chart_widthchart_classruler   r   r   __init__/  s   

zIncrementalChartParser.__init__Nc                 C   s(  |d u r| j }| j}t|}| j| | |}| j}| j| d  }|r.t|	| | j
D ]}t|||}|||||| q1| j}	t| d D ]B}
|dkr[td|
d t|j|
d}|r| }|	D ]#}t||||}|||||| |D ]}| |
kr|| qqk|seqO|S )NrS   z
* Processing queue:
)r:   )r   _trace_new_edgesr)   r   check_coverager   r   rU   printpretty_format_leavesr   ru   r   rT   r=   popr:   rJ   )r'   tokensr   trace_new_edgesrh   ri   trace_edge_widthaxiom	new_edgesinference_rulesr:   agendar-   r   rl   r   r   r   chart_parseZ  s>   


	z"IncrementalChartParser.chart_parser   )rV   rW   rX   __doc__BU_LC_INCREMENTAL_STRATEGYr   r   r   r   r   r   r   r     s    
+r   c                   @   r_   )EarleyChartParserc                 K      t j| |tfi | d S r   )r   r   EARLEY_STRATEGYr'   ri   parser_argsr   r   r   r     s   zEarleyChartParser.__init__NrV   rW   rX   r   r   r   r   r   r   ~  ro   r   c                   @   r_   )IncrementalTopDownChartParserc                 K   r   r   )r   r   TD_INCREMENTAL_STRATEGYr   r   r   r   r     
   
z&IncrementalTopDownChartParser.__init__Nr   r   r   r   r   r     ro   r   c                   @   r_   )IncrementalBottomUpChartParserc                 K   r   r   )r   r   BU_INCREMENTAL_STRATEGYr   r   r   r   r     r   z'IncrementalBottomUpChartParser.__init__Nr   r   r   r   r   r     ro   r   c                   @   r_   )(IncrementalBottomUpLeftCornerChartParserc                 K   r   r   )r   r   r   r   r   r   r   r     r   z1IncrementalBottomUpLeftCornerChartParser.__init__Nr   r   r   r   r   r     ro   r   c                   @   r_   ) IncrementalLeftCornerChartParserc                 K   s*   |  stdtj| |tfi | d S )NzNIncrementalLeftCornerParser only works for grammars without empty productions.)is_nonemptyrG   r   r   LC_INCREMENTAL_STRATEGYr   r   r   r   r     s   
z)IncrementalLeftCornerChartParser.__init__Nr   r   r   r   r   r     ro   r   c                   @   s   e Zd ZedefddZdS )FeatureIncrementalChartParser   c                 K   s    t j| |f|||d| d S )N)r   r   r   )r   r   )r'   ri   r   r   r   r   r   r   r   r     s   
z&FeatureIncrementalChartParser.__init__N)rV   rW   rX   "BU_LC_INCREMENTAL_FEATURE_STRATEGYrY   r   r   r   r   r   r     s
    r   c                   @   r_   )FeatureEarleyChartParserc                 K   r   r   )r   r   EARLEY_FEATURE_STRATEGYr   r   r   r   r     r   z!FeatureEarleyChartParser.__init__Nr   r   r   r   r   r     ro   r   c                   @   r_   )$FeatureIncrementalTopDownChartParserc                 K   r   r   )r   r   TD_INCREMENTAL_FEATURE_STRATEGYr   r   r   r   r     r   z-FeatureIncrementalTopDownChartParser.__init__Nr   r   r   r   r   r     ro   r   c                   @   r_   )%FeatureIncrementalBottomUpChartParserc                 K   r   r   )r   r   BU_INCREMENTAL_FEATURE_STRATEGYr   r   r   r   r     r   z.FeatureIncrementalBottomUpChartParser.__init__Nr   r   r   r   r   r     ro   r   c                   @   r_   )/FeatureIncrementalBottomUpLeftCornerChartParserc                 K   r   r   )r   r   r   r   r   r   r   r     r   z8FeatureIncrementalBottomUpLeftCornerChartParser.__init__Nr   r   r   r   r   r     ro   r   TF   $I saw John with a dog with my cookie   c                 C   s   ddl }ddl}ddlm} | }	|rtd t|	 td t| | }
t|
 t  t|	|d}t }||
}t	|
|	 }t | }|rVt||ksVJ d|rb|D ]}t| qZntdt| | rrt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)   parsesrb   len)print_timesprint_grammarprint_treesr   sent	numparsesr   r   r   ri   r   earleytrh   r   treer   r   r   demo  s6   


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   rY   r`   rq   rz   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   rV   r   r   r   r   <module>   s   <(A6	_

0
