
    /h`U                     L   S r SSKJr  SSKJrJrJrJr  SSK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JrJrJrJr  SSKJr  SSK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(\" 5       \$" 5       \%" 5       \#" 5       /r)\" 5       \(" 5       \&" 5       \#" 5       /r*\" 5       \(" 5       \'" 5       \#" 5       /r+ " S S\5      r, " S S\,5      r- " S S\,5      r. " S  S!\,5      r/ " S" S#\!5      r0S$ r1S%S%S%S%S&\,S'4S( jr2S) r3\4S*:X  aX  SS+K5J6r6  \2" 5         \7" 5         \6" S,5      r8\," \8S-S.9r9S/r:\:Rw                  5       r<\9R{                  \<5      r>\> H  r?\7" \?5        M     g0g0)1z`
Extension of chart parsing implementation to handle grammars with
feature structures as nodes.
    )perf_counter)TYPE
FeatStructfind_variablesunify)CFGFeatStructNonterminalNonterminal
Productionis_nonterminalis_terminal)BottomUpPredictCombineRuleBottomUpPredictRuleCachedTopDownPredictRuleChartChartParserEdgeIEmptyPredictRuleFundamentalRuleLeafInitRuleSingleEdgeFundamentalRuleTopDownInitRuleTreeEdge)logic)Treec                   h   ^  \ rS rSrSrSS jr\S 5       rSS jrS r	S r
S rS	 rU 4S
 jrSrU =r$ )FeatureTreeEdge.   a  
A specialized tree edge that allows shared variable bindings
between nonterminals on the left-hand side and right-hand side.

Each ``FeatureTreeEdge`` contains a set of ``bindings``, i.e., a
dictionary mapping from variables to values.  If the edge is not
complete, then these bindings are simply stored.  However, if the
edge is complete, then the constructor applies these bindings to
every nonterminal in the edge whose symbol implements the
interface ``SubstituteBindingsI``.
c                 L   Uc  0 nU[        U5      :X  a:  U(       a3  U R                  X%5      nU Vs/ s H  o`R                  Xe5      PM     nn0 n[        R                  " XX#U5        XPl        U R
                  [        [        UR                  5       5      5      4U l        gs  snf )aB  
Construct a new edge.  If the edge is incomplete (i.e., if
``dot<len(rhs)``), then store the bindings as-is.  If the edge
is complete (i.e., if ``dot==len(rhs)``), then apply the
bindings to all nonterminals in ``lhs`` and ``rhs``, and then
clear the bindings.  See ``TreeEdge`` for a description of
the other arguments.
N)	len_bindr   __init__	_bindings_comparison_keytuplesorteditems)selfspanlhsrhsdotbindingselts          O/var/www/auris/envauris/lib/python3.13/site-packages/nltk/parse/featurechart.pyr"   FeatureTreeEdge.__init__;   s     H #c(?x**S+C8;<::c,C<H 	$c4! $ 4 4eF8>>CS<T6UV =s   B!c                 T    [        X4U R                  5       U R                  5       SS9$ )z
:return: A new ``TreeEdge`` formed from the given production.
    The new edge's left-hand side and right-hand side will
    be taken from ``production``; its span will be
    ``(index,index)``; and its dot position will be ``0``.
:rtype: TreeEdge
r   )r)   r*   r+   r,   )r   r*   r+   )
productionindexs     r/   from_productionFeatureTreeEdge.from_productionV   s+     Z^^%5:>>;KQR
 	
    c                 |    [        U R                  S   U4U R                  U R                  U R                  S-   US9$ )a8  
:return: A new ``FeatureTreeEdge`` formed from this edge.
    The new edge's dot position is increased by ``1``,
    and its end index will be replaced by ``new_end``.
:rtype: FeatureTreeEdge
:param new_end: The new end index.
:type new_end: int
:param bindings: Bindings for the new edge.
:type bindings: dict
r      )r)   r*   r+   r,   r-   )r   _span_lhs_rhs_dot)r(   new_endr-   s      r/   move_dot_forward FeatureTreeEdge.move_dot_forwardc   s=     **Q-)						A
 	
r6   c                 R    [        U[        5      (       d  U$ UR                  U5      $ N)
isinstancer	   substitute_bindings)r(   ntr-   s      r/   r!   FeatureTreeEdge._bindv   s&    "344I%%h//r6   c                 V    U R                  U R                  5       U R                  5      $ rA   )r!   nextsymr#   r(   s    r/   next_with_bindings"FeatureTreeEdge.next_with_bindings{   s    zz$,,.$..99r6   c                 6    U R                   R                  5       $ )z3
Return a copy of this edge's bindings dictionary.
)r#   copyrH   s    r/   r-   FeatureTreeEdge.bindings~   s     ~~""$$r6   c                     [        U R                  /[        U R                  5      -   [        U R                  R                  5       5      -   [        U R                  R                  5       5      -   [        S9$ )zH
:return: The set of variables used by this edge.
:rtype: set(Variable)
fs_class)r   r:   listr;   r#   keysvaluesr   rH   s    r/   	variablesFeatureTreeEdge.variables   se    
 YYK499o4>>&&()* 4>>((*+,  
 	
r6   c                    > U R                  5       (       a  [        TU ]	  5       $ SSR                  S [	        U R
                  R                  5       5       5       5      -  n[        TU ]	  5        SU 3$ )Nz{%s}z, c              3   ,   #    U  H
  nS U-  v   M     g7f)z%s: %rN ).0items     r/   	<genexpr>*FeatureTreeEdge.__str__.<locals>.<genexpr>   s      *,JD4,Js    )is_completesuper__str__joinr&   r#   r'   )r(   r-   	__class__s     r/   r`   FeatureTreeEdge.__str__   sn    7?$$		 *,24>>3G3G3I,J* ! H go'((44r6   )r#   r$   )r   NrA   )__name__
__module____qualname____firstlineno____doc__r"   staticmethodr4   r>   r!   rI   r-   rT   r`   __static_attributes____classcell__)rb   s   @r/   r   r   .   sE    
W6 

 


&0
:%
5 5r6   r   c                   <    \ rS rSrSrS rS rS rS r\	4S jr
Srg	)
FeatureChart   zE
A Chart for feature grammars.
:see: ``Chart`` for more information.
c                 B  ^ ^ T0 :X  a  [        T R                  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   R                  U/ 5      5      $ )z
Returns an iterator over the edges in this chart.
See ``Chart.select`` for more information about the
``restrictions`` on the edges.
c              3   L   >#    U  H  nTR                  TU   5      v   M     g 7frA   )_get_type_if_possible)rY   keyrestrictionsr(   s     r/   r[   &FeatureChart.select.<locals>.<genexpr>   s'      
EOcD&&|C'899Zs   !$)iter_edgesr&   rR   r%   _indexes
_add_indexget)r(   rs   
restr_keysvalss   ``  r/   selectFeatureChart.select   s     2$$ L--/0
:&
 T]]*OOJ' 
EO
 
 DMM*-11$;<<r6   c                   ^ ^ U H&  n[        [        U5      (       a  M  [        SU-  5      e   0 =nT R                  U'   T R                   H:  m[        UU 4S jU 5       5      nUR                  U/ 5      R                  T5        M<     g)zs
A helper function for ``select``, which creates a new index for
a given set of attributes (aka restriction keys).
zBad restriction: %sc              3   d   >#    U  H%  nTR                  [        TU5      " 5       5      v   M'     g 7frA   rq   getattrrY   rr   edger(   s     r/   r[   *FeatureChart._add_index.<locals>.<genexpr>   ,      LVS**74+=+?@@J   -0N)hasattrr   
ValueErrorrw   rv   r%   
setdefaultappend)r(   rz   rr   r3   r{   r   s   `    @r/   rx   FeatureChart._add_index   s     C5#&& !6!<== 
 -/.j) KKD LV D T2&--d3	  r6   c                    ^ ^ T R                   R                  5        H<  u  p#[        UU 4S jU 5       5      nUR                  U/ 5      R	                  T5        M>     g)z[
A helper function for ``insert``, which registers the new
edge with all existing indexes.
c              3   d   >#    U  H%  nTR                  [        TU5      " 5       5      v   M'     g 7frA   r   r   s     r/   r[   6FeatureChart._register_with_indexes.<locals>.<genexpr>   r   r   N)rw   r'   r%   r   r   )r(   r   rz   r3   r{   s   ``   r/   _register_with_indexes#FeatureChart._register_with_indexes   sS    
 "&!4!4!6J LV D T2&--d3	 "7r6   c                 V    [        U[        5      (       a  [        U;   a	  U[           $ U$ )z|
Helper function which returns the ``TYPE`` feature of the ``item``,
if it exists, otherwise it returns the ``item`` itself
)rB   dictr   )r(   rZ   s     r/   rq   "FeatureChart._get_type_if_possible   s&    
 dD!!ddl:Kr6   c              #   :  #    U R                  SU R                  S9 Hw  n[        U[        5      (       d  M  UR	                  5       [
           U[
           :X  d  M>  [        UR	                  5       USS9(       d  M^  U R                  USUS9 S h  vN   My     g  N	7f)Nr   )startendTrename_vars)complete
tree_class)r|   _num_leavesrB   r   r*   r   r   trees)r(   r   r   r   s       r/   parsesFeatureChart.parses   sy     KKaT-=-=K>DD/22XXZ%t4488:u$??::dTj:QQQ ? Rs"   1B BB;BB
BrX   N)rd   re   rf   rg   rh   r|   rx   r   rq   r   r   rj   rX   r6   r/   rm   rm      s&    
=.4(	4 (, Rr6   rm   c                       \ rS rSrSrS rSrg)FeatureFundamentalRule   ak  
A specialized version of the fundamental rule that operates on
nonterminals whose symbols are ``FeatStructNonterminal``s.  Rather
than simply comparing the nonterminals for equality, they are
unified.  Variable bindings from these unifications are collected
and stored in the chart using a ``FeatureTreeEdge``.  When a
complete edge is generated, these bindings are applied to all
nonterminals in the edge.

The fundamental rule states that:

- ``[A -> alpha \* B1 beta][i:j]``
- ``[B2 -> gamma \*][j:k]``

licenses the edge:

- ``[A -> alpha B3 \* beta][i:j]``

assuming that B1 and B2 can be unified to generate B3.
c              #     #    UR                  5       UR                  5       :X  a?  UR                  5       (       a*  UR                  5       (       a  [	        U[
        5      (       d  g UR                  5       nUR                  5       n[	        U[
        5      (       a  [        U5      (       d  g UR                  5       [           UR                  5       [           :w  a  g UR                  5       nUR                  UR                  5       S9n[        XeUSS9nUc  g OXe:w  a  g UR                  5       nUR                  UR                  5       U5      n	UR                  XU5      (       a  U	v   g g 7f)N	used_varsFr   )r   r   is_incompleter^   rB   r   r*   rG   r   r   r-   rename_variablesrT   r   r>   insert_with_backpointer)
r(   chartgrammar	left_edge
right_edgefoundrG   r-   resultnew_edges
             r/   applyFeatureFundamentalRule.apply  sC     MMOz//11''))&&((9o66 ##%j/22!'**  "4(JNN,<T,BB ))+H **Y5H5H5J*KE 78GF~   ))+H --jnn.>I ((jIIN Js   E*E,rX   Nrd   re   rf   rg   rh   r   rj   rX   r6   r/   r   r      s    *%r6   r   c                   2    \ rS rSrSr\" 5       rS rS rSr	g) FeatureSingleEdgeFundamentalRulei5  z
A specialized version of the completer / single edge fundamental rule
that operates on nonterminals whose symbols are ``FeatStructNonterminal``.
Rather than simply comparing the nonterminals for equality, they are
unified.
c              #      #    U R                   nUR                  UR                  5       SUR                  5       S9 H  nUR	                  XXS5       S h  vN   M     g  N	7f)NF)r   r^   rG   )_fundamental_ruler|   r   r*   r   )r(   r   r   r   frr   s         r/   _apply_complete0FeatureSingleEdgeFundamentalRule._apply_complete?  s[     ##  "z~~?O & 
I xx	FFF
 G   AA A
A c              #      #    U R                   nUR                  UR                  5       SUR                  5       S9 H  nUR	                  XX55       S h  vN   M     g  N	7f)NT)r   r^   r*   )r   r|   r   rG   r   )r(   r   r   r   r   r   s         r/   _apply_incomplete2FeatureSingleEdgeFundamentalRule._apply_incompleteF  sZ     ##,,--/t9J9J9L ' 
J xx	FFF
 Gr   rX   N)
rd   re   rf   rg   rh   r   r   r   r   rj   rX   r6   r/   r   r   5  s     /0GGr6   r   c                       \ rS rSrS rSrg)FeatureTopDownInitRuleiS  c              #      #    UR                  UR                  5       S9 H6  n[        R                  US5      nUR	                  US5      (       d  M2  Uv   M8     g 7f)Nr*   r   rX   )productionsr   r   r4   insert)r(   r   r   prodr   s        r/   r   FeatureTopDownInitRule.applyT  sK     ''GMMO'<D&66tQ?H||Hb)) =s   AA	ArX   Nrd   re   rf   rg   r   rj   rX   r6   r/   r   r   S  s    r6   r   c                       \ rS rSrSrS rSrg)FeatureTopDownPredictRulei[  a  
A specialized version of the (cached) top down predict rule that operates
on nonterminals whose symbols are ``FeatStructNonterminal``.  Rather
than simply comparing the nonterminals for equality, they are
unified.

The top down expand rule states that:

- ``[A -> alpha \* B1 beta][i:j]``

licenses the edge:

- ``[B2 -> \* gamma][j:j]``

for each grammar production ``B2 -> gamma``, assuming that B1
and B2 can be unified.
c              #     #    UR                  5       (       a  g UR                  5       UR                  5       pT[        U5      (       d  g UR	                  5       nU R
                  R                  Xe4S5      nUS   UL a	  US   UL a  g UR                  US9 H  nUR                  5       (       aN  UR                  5       S   n	[        U	5      (       a+  XQR                  5       :  a  MP  XR                  U5      :w  a  Mf  [        UR                  5       USS9(       d  M  [        R                  XR                  5       5      n
UR!                  U
S5      (       d  M  U
v   M     X4U R
                  Xe4'   g 7f)N)NNr   r8   r   Tr   rX   )r^   rG   r   r   rI   _donery   r   r+   r   
num_leavesleafr   r*   r   r4   r   )r(   r   r   r   rG   r3   nextsym_with_bindingsdoner   firstr   s              r/   r   FeatureTopDownPredictRule.applyn  s9    g&&
 !% 7 7 9zz~~4<lK7eQ7 2''G'4D xxzz
1u%% 0 0 22 

5 11  TXXZ!6DII*::4L<<"--"N! 5& 5:3C

(/0s   DE28E2E2rX   Nr   rX   r6   r/   r   r   [  s    $"Dr6   r   c                       \ rS rSrS rSrg)FeatureBottomUpPredictRulei  c              #   t  #    UR                  5       (       a  g UR                  UR                  5       S9 H}  n[        U[        5      (       a%  UR                  5       S   n[        U5      (       d  M=  [        R                  XCR                  5       5      nUR                  US5      (       d  My  Uv   M     g 7f)Nr+   r   rX   )
r   r   r*   rB   r   r+   r   r4   r   r   )r(   r   r   r   r   _nextr   s          r/   r    FeatureBottomUpPredictRule.apply  s     ''DHHJ'7D$00
1%e,,&66tZZ\JH||Hb)) 8s   B)B8/	B8rX   Nr   rX   r6   r/   r   r     s    r6   r   c                       \ rS rSrS rSrg)!FeatureBottomUpPredictCombineRulei  c              #   R  #    UR                  5       (       a  g UR                  5       nUR                  US9 H  n0 n[        U[        5      (       ar  UR                  5       S   n[        U5      (       d  M?  [        UR                  5       4UR                  5       -   [        S9nUR                  US9n[        XtUSS9n	U	c  M  [        R                  XSR                  5       5      R                  UR                  5       U5      n
UR                  X45      (       d  M  U
v   M     g 7f)Nr   r   rO   r   Fr   )r   r*   r   rB   r   r+   r   r   r   r   r   r4   r   r>   r   r   )r(   r   r   r   r   r   r-   r   r   r   r   s              r/   r   'FeatureBottomUpPredictCombineRule.apply  s     
''E'2DH$00
1%e,, +XXZMDHHJ.	 ...CuX5I>&66jjltxxz84  ||Hg..- 3s   DD'	D'rX   Nr   rX   r6   r/   r   r     s    r6   r   c                       \ rS rSrS rSrg)FeatureEmptyPredictRulei  c              #      #    UR                  SS9 HX  n[        UR                  5       S-   5       H5  n[        R	                  X45      nUR                  US5      (       d  M1  Uv   M7     MZ     g 7f)NT)emptyr8   rX   )r   ranger   r   r4   r   )r(   r   r   r   r3   r   s         r/   r   FeatureEmptyPredictRule.apply  s`     ''d'3Du//1A56*::4G<<"--"N 7 4s   AA/"A/rX   Nr   rX   r6   r/   r   r     s    #r6   r   c                   $    \ rS rSr\S\4S jrSrg)FeatureChartParseri     c                 <    [         R                  " U U4UUUS.UD6  g )N)strategytrace_chart_widthchart_class)r   r"   )r(   r   r   r   r   parser_argss         r/   r"   FeatureChartParser.__init__  s2     		
 /#	
 	
r6   rX   N)rd   re   rf   rg   BU_LC_FEATURE_STRATEGYrm   r"   rj   rX   r6   r/   r   r     s     ( 
r6   r   c                       \ rS rSrS rSrg)FeatureTopDownChartParseri  c                 <    [         R                  " X[        40 UD6  g rA   )r   r"   TD_FEATURE_STRATEGYr(   r   r   s      r/   r"   "FeatureTopDownChartParser.__init__      ##D3FV+Vr6   rX   Nrd   re   rf   rg   r"   rj   rX   r6   r/   r   r         Wr6   r   c                       \ rS rSrS rSrg)FeatureBottomUpChartParseri  c                 <    [         R                  " X[        40 UD6  g rA   )r   r"   BU_FEATURE_STRATEGYr   s      r/   r"   #FeatureBottomUpChartParser.__init__  r   r6   rX   Nr   rX   r6   r/   r   r     r   r6   r   c                       \ rS rSrS rSrg)$FeatureBottomUpLeftCornerChartParseri  c                 <    [         R                  " X[        40 UD6  g rA   )r   r"   r   r   s      r/   r"   -FeatureBottomUpLeftCornerChartParser.__init__  s    ##1	
5@	
r6   rX   Nr   rX   r6   r/   r   r     s    
r6   r   c                   6    \ rS rSrSrS rS rS rS rS r	Sr
g	)
InstantiateVarsCharti  a'  
A specialized chart that 'instantiates' variables whose names
start with '@', by replacing them with unique new variables.
In particular, whenever a complete edge is added to the chart, any
variables in the edge's ``lhs`` whose names start with '@' will be
replaced by unique new ``Variable``.
c                 .    [         R                  X5        g rA   )rm   r"   )r(   tokenss     r/   r"   InstantiateVarsChart.__init__  s    d+r6   c                 L    [        5       U l        [        R                  U 5        g rA   )set_instantiatedrm   
initializerH   s    r/   r  InstantiateVarsChart.initialize  s     U%r6   c                 p    XR                   ;   a  gU R                  U5        [        R                  XU5      $ )NF)r  instantiate_edgerm   r   )r(   r   child_pointer_lists      r/   r   InstantiateVarsChart.insert  s3    %%%d#""4/ABBr6   c                 ,   [        U[        5      (       d  gUR                  5       (       d  gXR                  ;   a  gU R	                  U5      nU(       d  gU R
                  R                  U5        UR                  5       R                  U5      Ul	        g)a&  
If the edge is a ``FeatureTreeEdge``, and it is complete,
then instantiate all variables whose names start with '@',
by replacing them with unique new variables.

Note that instantiation is done in-place, since the
parsing algorithms might already hold a reference to
the edge for future use.
N)
rB   r   r^   _edge_to_cpls	inst_varsr  addr*   rC   r:   )r(   r   r  s      r/   r  %InstantiateVarsChart.instantiate_edge%  sy     $00!!%%% NN4(	 	t$HHJ229=	r6   c                     UR                  5       R                  5        Vs0 s H;  nUR                  R                  S5      (       d  M%  U[        R
                  " 5       _M=     sn$ s  snf )N@)r*   rT   name
startswithr   unique_variable)r(   r   vars      r/   r  InstantiateVarsChart.inst_varsB  sZ     xxz++-
-xx""3' )C&&((-
 	
 
s   $A&	A&)r  N)rd   re   rf   rg   rh   r"   r  r   r  r  rj   rX   r6   r/   r   r     s!    ,&C>:
r6   r   c                  0    SSK Jn   U R                  S5      $ )Nr   FeatureGrammara  
S  -> NP VP
PP -> Prep NP
NP -> NP PP
VP -> VP PP
VP -> Verb NP
VP -> Verb
NP -> Det[pl=?x] Noun[pl=?x]
NP -> "John"
NP -> "I"
Det -> "the"
Det -> "my"
Det[-pl] -> "a"
Noun[-pl] -> "dog"
Noun[-pl] -> "cookie"
Verb -> "ate"
Verb -> "saw"
Prep -> "with"
Prep -> "under"
)nltk.grammarr  
fromstringr  s    r/   demo_grammarr  O  s    +$$	 r6   Tr8   z$I saw John with a dog with my cookiec                    SS K nSS Kn[        5         [        5       n	U(       a  [        U	5        [        5         [        SUR                  5        U(       a  [        SU5        UR                  5       n
[        5       nU" XS9nUR                  U
5      n[        UR                  U	R                  5       5      5      nU (       a  [        S[        5       U-
  -  5        U(       a  U H  n[        U5        M     g [        S[        U5      5        g )Nr   *z	Sentence:tracezTime: %sz	Nr trees:)systimeprintr  rd   splitr   chart_parserQ   r   r   r    )print_timesprint_grammarprint_treesprint_sentencer  parsersentr   r!  r   r   tcpr   r   trees                   r/   demor.  j  s     	GnGg	#vk4 ZZ\FA		%BNN6"Egmmo./EjLNQ./0D$K  	k3u:&r6   c                     SS K n U R                  SS5        SS KnUR                  S5      nUR	                  5       R                  SS5      R                  S5        UR	                  5       R                  SS5      R                  S5        g )Nr   zfor i in range(1): demo()z/tmp/profile.outr!  cum<   )profilerunpstatsStats
strip_dirs
sort_statsprint_stats)r2  r4  ps      r/   run_profiler:    si    KK+-?@'(ALLNfe,88<LLNeV,88<r6   __main__)loadz!grammars/book_grammars/feat0.fcfg   r  zKim likes childrenN)@rh   r!  r   nltk.featstructr   r   r   r   r  r   r	   r
   r   r   r   nltk.parse.chartr   r   r   r   r   r   r   r   r   r   r   r   nltk.semr   	nltk.treer   r   rm   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r.  r:  rd   	nltk.datar<  r"  r   r,  r*  r#  r   parser   r-  rX   r6   r/   <module>rD     s    C C      j5h j5hMR5 MRj;_ ;|G'@ G<_ 5D 8 5Dz!4 (B <#. # N$&	  N $&	  N%'$&	 
 
&W 2 W
W!3 W

+= 
8
< 8
@8 
	/'D= zF	G67G	G1	-BDZZ\FHHVEd  r6   