a
     ˜ëj6E  ã                   @   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ƒZ8d:d6d7„Z9e:d8k�rªe9ƒ  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   úU/mnt/bigstor/www/usecases/.venv/lib/python3.9/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   r    z-IncrementalChart.iteredges.<locals>.<genexpr>)r#   r%   r   r   r   r)   L   s    zIncrementalChart.iteredgesc                    sr   | j | }ˆ i krt|ƒS tˆ  ¡ ƒ}t|ƒ}|| jvrB|  |¡ t‡ fdd„|D ƒƒ}t| j| |  |g ¡ƒS )Nc                 3   s   | ]}ˆ | V  qd S r   r   ©r   Úkey©Úrestrictionsr   r   r   ^   r    z*IncrementalChart.select.<locals>.<genexpr>©r#   ÚiterÚsortedÚkeysr!   r$   Ú
_add_indexÚget©r&   Úendr0   r+   Ú
restr_keysÚvalsr   r/   r   ÚselectO   s    


zIncrementalChart.selectc                    s’   |D ]}t t|ƒstd| ƒ‚qtdd„ |  ¡ D ƒƒ }| j|< t| jƒD ]B\}}|| }|D ],‰ t‡ fdd„|D ƒƒ}| |g ¡ 	ˆ ¡ q^qJd S )NúBad restriction: %sc                 s   s   | ]
}i V  qd S r   r   r   r   r   r   r   h   r    z.IncrementalChart._add_index.<locals>.<genexpr>c                 3   s   | ]}t ˆ |ƒƒ V  qd S r   ©Úgetattrr-   ©r,   r   r   r   n   r    ©
Úhasattrr   Ú
ValueErrorr!   r"   r$   Ú	enumerater#   Ú
setdefaultÚappend©r&   r9   r.   Úindexr8   r+   Z
this_indexr:   r   r?   r   r5   a   s    
 zIncrementalChart._add_indexc                    sL   ˆ   ¡ }| j ¡ D ]4\}}t‡ fdd„|D ƒƒ}||  |g ¡ ˆ ¡ qd S )Nc                 3   s   | ]}t ˆ |ƒƒ V  qd S r   r=   r-   r?   r   r   r   t   r    z:IncrementalChart._register_with_indexes.<locals>.<genexpr>©r8   r$   Úitemsr!   rD   rE   ©r&   r,   r8   r9   rG   r:   r   r?   r   Ú_register_with_indexesq   s    z'IncrementalChart._register_with_indexesc                 C   s   | j | ¡   |¡ d S r   )r#   r8   rE   )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   s    zIncrementalChart._positionsN)Ú__name__Ú
__module__Ú__qualname__r'   r*   r)   r;   r5   rK   rL   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rBˆ |¡ t‡ ‡fdd„|D ƒƒ}tˆj| |  |g ¡ƒS )Nc                 3   s   | ]}ˆ  ˆ | ¡V  qd S r   )Ú_get_type_if_possibler-   ©r0   r&   r   r   r   Ž   s   z1FeatureIncrementalChart.select.<locals>.<genexpr>r1   r7   r   rU   r   r;      s    


ÿzFeatureIncrementalChart.selectc                    s”   |D ]}t t|ƒstd| ƒ‚qtdd„ ˆ ¡ D ƒƒ }ˆj|< tˆjƒD ]D\}}|| }|D ].‰ t‡ ‡fdd„|D ƒƒ}| |g ¡ 	ˆ ¡ q^qJd S )Nr<   c                 s   s   | ]
}i V  qd S r   r   r   r   r   r   r   š   r    z5FeatureIncrementalChart._add_index.<locals>.<genexpr>c                 3   s    | ]}ˆ  tˆ |ƒƒ ¡V  qd S r   ©rT   r>   r-   ©r,   r&   r   r   r       s   ÿr@   rF   r   rW   r   r5   “   s    
 þz"FeatureIncrementalChart._add_indexc                    sN   ˆ   ¡ }ˆj ¡ D ]6\}}t‡ ‡fdd„|D ƒƒ}||  |g ¡ ˆ ¡ qd S )Nc                 3   s    | ]}ˆ  tˆ |ƒƒ ¡V  qd S r   rV   r-   rW   r   r   r   ©   s   zAFeatureIncrementalChart._register_with_indexes.<locals>.<genexpr>rH   rJ   r   rW   r   rK   ¦   s    ÿz.FeatureIncrementalChart._register_with_indexesN)rP   rQ   rR   r;   r5   rK   r   r   r   r   rS   ~   s   rS   c                   @   s   e Zd Zdd„ ZdS )ÚCompleteFundamentalRulec                 c   sJ   |  ¡ }|j||d| ¡ d�D ]&}| |  ¡ ¡}| |||¡r|V  qd S ©NT)Ústartr8   Úis_completeÚlhs)r8   r;   ÚnextsymZmove_dot_forwardZinsert_with_backpointer)r&   ÚchartÚgrammarÚ	left_edger8   Ú
right_edgeÚnew_edger   r   r   Ú_apply_incompleteµ   s    ÿ
z)CompleteFundamentalRule._apply_incompleteN©rP   rQ   rR   rc   r   r   r   r   rX   ´   s   rX   c                   @   s   e Zd Zeƒ Zdd„ ZdS )ÚCompleterRulec                 c   s$   t |tƒs | j |||¡E d H  d S r   ©Ú
isinstancer   Ú_fundamental_ruleÚapply©r&   r^   r_   r,   r   r   r   ri   Ä   s    
zCompleterRule.applyN©rP   rQ   rR   rX   rh   ri   r   r   r   r   re   Á   s   re   c                   @   s   e Zd Zeƒ Zdd„ ZdS )ÚScannerRulec                 c   s$   t |tƒr | j |||¡E d H  d S r   rf   rj   r   r   r   ri   Ì   s    
zScannerRule.applyNrk   r   r   r   r   rl   É   s   rl   c                   @   s   e Zd ZdS )ÚPredictorRuleN©rP   rQ   rR   r   r   r   r   rm   Ñ   s   rm   c                   @   s   e Zd Zdd„ ZdS )ÚFilteredCompleteFundamentalRulec                 c   s    |  ¡ r|  |||¡E d H  d S r   )r[   Z_apply_completerj   r   r   r   ri   Ö   s    z%FilteredCompleteFundamentalRule.applyN)rP   rQ   rR   ri   r   r   r   r   ro   Õ   s   ro   c                   @   s   e Zd Zdd„ ZdS )ÚFeatureCompleteFundamentalRulec                 c   sD   | j }| ¡ }|j||d| ¡ d�D ]}| ||||¡E d H  q$d S rY   )rh   r8   r;   r]   ri   )r&   r^   r_   r`   Úfrr8   ra   r   r   r   rc   ã   s    ÿ
z0FeatureCompleteFundamentalRule._apply_incompleteNrd   r   r   r   r   rp   â   s   rp   c                   @   s   e Zd Zeƒ ZdS )ÚFeatureCompleterRuleN©rP   rQ   rR   rp   rh   r   r   r   r   rr   î   s   rr   c                   @   s   e Zd Zeƒ ZdS )ÚFeatureScannerRuleNrs   r   r   r   r   rt   ò   s   rt   c                   @   s   e Zd ZdS )ÚFeaturePredictorRuleNrn   r   r   r   r   ru   ö   s   ru   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   sj   || _ || _|| _|| _g | _g | _|D ]<}|jdkrD| j |¡ q(|jdkr\| j |¡ q(tdƒ‚q(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   rM   z9Incremental inference rules must have NUM_EDGES == 0 or 1N)	Ú_grammarÚ_traceÚ_trace_chart_widthÚ_chart_classÚ_axiomsÚ_inference_rulesZ	NUM_EDGESrE   rB   )r&   r_   Ú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| ||¡ƒ}||||||ƒ qb| j}	t| ¡ d ƒD ]†}
|dkr¶td|
dƒ t|j|
d�ƒ}|rž| ¡ }|	D ]J}t| |||¡ƒ}||||||ƒ |D ]}| ¡ |
k�r | |¡ �q qÖqÆqž|S )NrM   z
* Processing queue:Ú
)r8   )ry   Z_trace_new_edgesr(   rx   Zcheck_coverager{   rz   rO   ÚprintZpretty_format_leavesr|   ri   r}   rN   r;   Úpopr8   rE   )r&   Útokensr   Ztrace_new_edgesr^   r_   Ztrace_edge_widthZaxiomZ	new_edgesZinference_rulesr8   Zagendar,   r‚   rb   r   r   r   Úchart_parseZ  s6    

z"IncrementalChartParser.chart_parse)N)rP   rQ   rR   Ú__doc__ÚBU_LC_INCREMENTAL_STRATEGYr   rƒ   rˆ   r   r   r   r   rv     s   ú
+rv   c                   @   s   e Zd Zdd„ ZdS )ÚEarleyChartParserc                 K   s   t j| |tfi |¤Ž d S r   )rv   rƒ   ÚEARLEY_STRATEGY©r&   r_   Úparser_argsr   r   r   rƒ     s    zEarleyChartParser.__init__N©rP   rQ   rR   rƒ   r   r   r   r   r‹   ~  s   r‹   c                   @   s   e Zd Zdd„ ZdS )ÚIncrementalTopDownChartParserc                 K   s   t j| |tfi |¤Ž d S r   )rv   rƒ   ÚTD_INCREMENTAL_STRATEGYr�   r   r   r   rƒ   „  s
    ÿÿz&IncrementalTopDownChartParser.__init__Nr�   r   r   r   r   r�   ƒ  s   r�   c                   @   s   e Zd Zdd„ ZdS )ÚIncrementalBottomUpChartParserc                 K   s   t j| |tfi |¤Ž d S r   )rv   rƒ   ÚBU_INCREMENTAL_STRATEGYr�   r   r   r   rƒ   ‹  s
    ÿÿz'IncrementalBottomUpChartParser.__init__Nr�   r   r   r   r   r’   Š  s   r’   c                   @   s   e Zd Zdd„ ZdS )Ú(IncrementalBottomUpLeftCornerChartParserc                 K   s   t j| |tfi |¤Ž d S r   )rv   rƒ   rŠ   r�   r   r   r   rƒ   ’  s
    ÿÿz1IncrementalBottomUpLeftCornerChartParser.__init__Nr�   r   r   r   r   r”   ‘  s   r”   c                   @   s   e Zd Zdd„ ZdS )Ú IncrementalLeftCornerChartParserc                 K   s*   |  ¡ stdƒ‚tj| |tfi |¤Ž d S )NzNIncrementalLeftCornerParser only works for grammars without empty productions.)Zis_nonemptyrB   rv   rƒ   ÚLC_INCREMENTAL_STRATEGYr�   r   r   r   rƒ   ™  s    ÿÿÿz)IncrementalLeftCornerChartParser.__init__Nr�   r   r   r   r   r•   ˜  s   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�   )rv   rƒ   )r&   r_   r~   r€   r�   rŽ   r   r   r   rƒ   Ä  s    þûúz&FeatureIncrementalChartParser.__init__N)rP   rQ   rR   Ú"BU_LC_INCREMENTAL_FEATURE_STRATEGYrS   rƒ   r   r   r   r   r—   Ã  s   ûr—   c                   @   s   e Zd Zdd„ ZdS )ÚFeatureEarleyChartParserc                 K   s   t j| |tfi |¤Ž d S r   )r—   rƒ   ÚEARLEY_FEATURE_STRATEGYr�   r   r   r   rƒ   ×  s
    ÿÿz!FeatureEarleyChartParser.__init__Nr�   r   r   r   r   rš   Ö  s   rš   c                   @   s   e Zd Zdd„ ZdS )Ú$FeatureIncrementalTopDownChartParserc                 K   s   t j| |tfi |¤Ž d S r   )r—   rƒ   ÚTD_INCREMENTAL_FEATURE_STRATEGYr�   r   r   r   rƒ   Þ  s
    ÿÿz-FeatureIncrementalTopDownChartParser.__init__Nr�   r   r   r   r   rœ   Ý  s   rœ   c                   @   s   e Zd Zdd„ ZdS )Ú%FeatureIncrementalBottomUpChartParserc                 K   s   t j| |tfi |¤Ž d S r   )r—   rƒ   ÚBU_INCREMENTAL_FEATURE_STRATEGYr�   r   r   r   rƒ   å  s
    ÿÿz.FeatureIncrementalBottomUpChartParser.__init__Nr�   r   r   r   r   rž   ä  s   rž   c                   @   s   e Zd Zdd„ ZdS )Ú/FeatureIncrementalBottomUpLeftCornerChartParserc                 K   s   t j| |tfi |¤Ž d S r   )r—   rƒ   r™   r�   r   r   r   rƒ   ì  s
    ÿÿz8FeatureIncrementalBottomUpLeftCornerChartParser.__init__Nr�   r   r   r   r   r    ë  s   r    TFé   ú$I saw John with a dog with my cookieé   c                 C   sä   ddl }ddl}ddlm} |ƒ }	|r6tdƒ t|	ƒ tdƒ t|ƒ | ¡ }
t|
ƒ tƒ  t|	|d�}tƒ }| |
¡}t	| 
|	 ¡ ¡ƒ}tƒ | }|r¬t|ƒ|ks¬J dƒ‚|rÄ|D ]}t|ƒ q´ntdt|ƒƒ | ràtd	|ƒ 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(   ÚparsesrZ   Úlen)Zprint_timesZprint_grammarZprint_treesr   ÚsentZ	numparsesr¥   r¦   r¤   r_   r‡   ZearleyÚtr^   r©   Útreer   r   r   Údemo÷  s2    

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   rS   rX   re   rl   rm   ro   rp   rr   rt   ru   rŒ   r‘   r“   rŠ   r–   rv   r‹   r�   r’   r”   r•   r›   r�   rŸ   r™   r—   rš   rœ   rž   r    r®   rP   r   r   r   r   Ú<module>   s    <(A6	ûüüüý_ûüüü      ú
0
