o
    rZh6E                     @   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   E/var/www/auris/lib/python3.10/site-packages/nltk/parse/earleychart.py	<genexpr>@       z.IncrementalChart.initialize.<locals>.<genexpr>)tuple
_positions
_edgelistsZ_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&   endr1   r+   
restr_keysvalsr   r0   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:   r/   indexr9   r+   
this_indexr;   r   rB   r   r6   a   s   
 zIncrementalChart._add_indexc                    sL      }| j D ]\}}t fdd|D }|| |g   q	d S )Nc                 3   r?   r   r@   r.   rB   r   r   r   t   rC   z:IncrementalChart._register_with_indexes.<locals>.<genexpr>r9   r$   itemsr!   rH   rI   r&   r,   r9   r:   rK   r;   r   rB   r   _register_with_indexesq   s
   z'IncrementalChart._register_with_indexesc                 C   s   | j |  | d S r   )r#   r9   rI   )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<   r6   rP   rQ   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.   r1   r&   r   r   r      s    
z1FeatureIncrementalChart.select.<locals>.<genexpr>r2   r8   r   rZ   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   rY   rA   r.   r,   r&   r   r   r      s
    
rD   rJ   r   r]   r   r6      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>rM   rO   r   r]   r   rP      s   z.FeatureIncrementalChart._register_with_indexesN)rU   rV   rW   r<   r6   rP   r   r   r   r   rX   ~   s    rX   c                   @      e Zd Zdd ZdS )CompleteFundamentalRulec                 c   sL    |  }|j||d| dD ]}||  }||||r#|V  qd S NT)startr9   is_completelhs)r9   r<   nextsymZmove_dot_forwardZinsert_with_backpointer)r&   chartgrammar	left_edger9   
right_edgenew_edger   r   r   _apply_incomplete   s   
z)CompleteFundamentalRule._apply_incompleteNrU   rV   rW   rj   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&   re   rf   r,   r   r   r   rr         
zCompleterRule.applyNrU   rV   rW   r_   rq   rr   r   r   r   r   rn          rn   c                   @   rm   )ScannerRulec                 c   s*    t |tr| j|||E d H  d S d S r   ro   rs   r   r   r   rr      rt   zScannerRule.applyNru   r   r   r   r   rw      rv   rw   c                   @      e Zd ZdS )PredictorRuleNrU   rV   rW   r   r   r   r   ry          ry   c                   @   r^   )FilteredCompleteFundamentalRulec                 c   s&    |  r| |||E d H  d S d S r   )rb   Z_apply_completers   r   r   r   rr      s   z%FilteredCompleteFundamentalRule.applyN)rU   rV   rW   rr   r   r   r   r   r|      rl   r|   c                   @   r^   )FeatureCompleteFundamentalRulec                 c   sF    | j }| }|j||d| dD ]}|||||E d H  qd S r`   )rq   r9   r<   rd   rr   )r&   re   rf   rg   frr9   rh   r   r   r   rj      s   
z0FeatureCompleteFundamentalRule._apply_incompleteNrk   r   r   r   r   r}      rl   r}   c                   @      e Zd Ze ZdS )FeatureCompleterRuleNrU   rV   rW   r}   rq   r   r   r   r   r          
r   c                   @   r   )FeatureScannerRuleNr   r   r   r   r   r      r   r   c                   @   rx   )FeaturePredictorRuleNrz   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   rR   z9Incremental inference rules must have NUM_EDGES == 0 or 1N)	_grammar_trace_trace_chart_width_chart_class_axioms_inference_rulesZ	NUM_EDGESrI   rF   )r&   rf   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 )NrR   z
* Processing queue:
)r9   )r   Z_trace_new_edgesr(   r   Zcheck_coverager   r   rT   printZpretty_format_leavesr   rr   r   rS   r<   popr9   rI   )r&   tokensr   Ztrace_new_edgesre   rf   Ztrace_edge_widthZaxiomZ	new_edgesZinference_rulesr9   Zagendar,   r   ri   r   r   r   chart_parseZ  s>   


	z"IncrementalChartParser.chart_parser   )rU   rV   rW   __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&   rf   parser_argsr   r   r   r     s   zEarleyChartParser.__init__NrU   rV   rW   r   r   r   r   r   r   ~  rl   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     rl   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     rl   r   c                   @   r^   )(IncrementalBottomUpLeftCornerChartParserc                 K   r   r   )r   r   r   r   r   r   r   r     r   z1IncrementalBottomUpLeftCornerChartParser.__init__Nr   r   r   r   r   r     rl   r   c                   @   r^   ) IncrementalLeftCornerChartParserc                 K   s*   |  stdtj| |tfi | d S )NzNIncrementalLeftCornerParser only works for grammars without empty productions.)Zis_nonemptyrF   r   r   LC_INCREMENTAL_STRATEGYr   r   r   r   r     s   
z)IncrementalLeftCornerChartParser.__init__Nr   r   r   r   r   r     rl   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&   rf   r   r   r   r   r   r   r   r     s   
z&FeatureIncrementalChartParser.__init__N)rU   rV   rW   "BU_LC_INCREMENTAL_FEATURE_STRATEGYrX   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     rl   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     rl   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     rl   r   c                   @   r^   )/FeatureIncrementalBottomUpLeftCornerChartParserc                 K   r   r   )r   r   r   r   r   r   r   r     r   z8FeatureIncrementalBottomUpLeftCornerChartParser.__init__Nr   r   r   r   r   r     rl   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(   parsesra   len)Zprint_timesZprint_grammarZprint_treesr   sentZ	numparsesr   r   r   rf   r   Zearleytre   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   Znltk.parse.featurechartr   r   r   r   r   r   r   r   r   rX   r_   rn   rw   ry   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   rU   r   r   r   r   <module>   s   <(A6	_

0
