Ë
    çÍ:j%{  ã                   óä   — d dl Z d dlZd dlmZ d dlmZ d dlmZ 	 d dlm	Z	 d dl
mZ d dlmZ d dlmZ d d	lmZmZmZ d d
lmZ  G d„ d«      Z G d„ d«      Z G d„ de«      Zd„ Zy# e$ r Y Œ:w xY w)é    N)Údeepcopy)Ú
itemgetter)Úremove)Úarray)Úsparse)Úsvm)Úload_svmlight_file)ÚDependencyEvaluatorÚDependencyGraphÚParserI)Úpickle_loadc                   ó*   — e Zd ZdZd„ Zd„ Zdd„Zd„ Zy)ÚConfigurationa  
    Class for holding configuration which is the partial analysis of the input sentence.
    The transition based parser aims at finding set of operators that transfer the initial
    configuration to the terminal configuration.

    The configuration includes:
        - Stack: for storing partially proceeded words
        - Buffer: for storing remaining input words
        - Set of arcs: for storing partially built dependency tree

    This class also provides a method to represent a configuration as list of features.
    c                 óÒ   — dg| _         t        t        dt        |j                  «      «      «      | _        g | _        |j                  | _        t        | j
                  «      | _        y)z¶
        :param dep_graph: the representation of an input in the form of dependency graph.
        :type dep_graph: DependencyGraph where the dependencies are not specified.
        r   é   N)	ÚstackÚlistÚrangeÚlenÚnodesÚbufferÚarcsÚ_tokensÚ_max_address)ÚselfÚ	dep_graphs     úp/home/mcse/projects/srt_converter/srt-converter-venv/lib/python3.12/site-packages/nltk/parse/transitionparser.pyÚ__init__zConfiguration.__init__)   sL   € ð �SˆŒ
Üœ5 ¤C¨	¯©Ó$8Ó9Ó:ˆŒØˆŒ	Ø —‘ˆŒÜ §¡Ó,ˆÕó    c                 ó–   — dt        | j                  «      z   dz   t        | j                  «      z   dz   t        | j                  «      z   S )NzStack : z  Buffer : z
   Arcs : )Ústrr   r   r   )r   s    r   Ú__str__zConfiguration.__str__5   sQ   € àÜ�$—*‘*‹oñàñô �$—+‘+Óñð ñ	ô
 �$—)‘)‹nñð	
r   c                 ó*   — |€y|dk(  ry|du r|dk(  ryy)zs
        Check whether a feature is informative
        The flag control whether "_" is informative or not
        FÚ Ú_T© )r   ÚfeatÚflags      r   Ú_check_informativez Configuration._check_informative?   s*   € ð
 ˆ<ØØ�2Š:ØØ�5‰=Ø�sŠ{ØØr   c                 óþ	  — g }t        | j                  «      dkD  �r| j                  t        | j                  «      dz
     }| j                  |   }| j                  |d   d«      r|j	                  d|d   z   «       d|v r+| j                  |d   «      r|j	                  d|d   z   «       | j                  |d   «      r|j	                  d	|d   z   «       d
|v rC| j                  |d
   «      r/|d
   j                  d«      }|D ]  }|j	                  d|z   «       Œ t        | j                  «      dkD  r_| j                  t        | j                  «      dz
     }| j                  |   }| j                  |d   «      r|j	                  d|d   z   «       d}d}d}	d}
| j                  D ]*  \  }}}||k(  sŒ||kD  r	||kD  r|}|}
||k  sŒ!||k  sŒ'|}|}	Œ, | j                  |	«      r|j	                  d|	z   «       | j                  |
«      r|j	                  d|
z   «       t        | j                  «      dkD  �rÅ| j                  d   }| j                  |   }| j                  |d   d«      r|j	                  d|d   z   «       d|v r+| j                  |d   «      r|j	                  d|d   z   «       | j                  |d   «      r|j	                  d|d   z   «       d
|v rC| j                  |d
   «      r/|d
   j                  d«      }|D ]  }|j	                  d|z   «       Œ t        | j                  «      dkD  ru| j                  d   }| j                  |   }| j                  |d   d«      r|j	                  d|d   z   «       | j                  |d   «      r|j	                  d|d   z   «       t        | j                  «      dkD  rI| j                  d   }| j                  |   }| j                  |d   «      r|j	                  d|d   z   «       t        | j                  «      dkD  rI| j                  d   }| j                  |   }| j                  |d   «      r|j	                  d|d   z   «       d}d}d}	d}
| j                  D ]*  \  }}}||k(  sŒ||kD  r	||kD  r|}|}
||k  sŒ!||k  sŒ'|}|}	Œ, | j                  |	«      r|j	                  d|	z   «       | j                  |
«      r|j	                  d|
z   «       |S )a/  
        Extract the set of features for the current configuration. Implement standard features as describe in
        Table 3.2 (page 31) in Dependency Parsing book by Sandra Kubler, Ryan McDonal, Joakim Nivre.
        Please note that these features are very basic.
        :return: list(str)
        r   r   ÚwordTÚSTK_0_FORM_ÚlemmaÚSTK_0_LEMMA_ÚtagÚ
STK_0_POS_Úfeatsú|ÚSTK_0_FEATS_é   Ú
STK_1_POS_i@B éÿÿÿÿr$   ÚSTK_0_LDEP_ÚSTK_0_RDEP_ÚBUF_0_FORM_ÚBUF_0_LEMMA_Ú
BUF_0_POS_ÚBUF_0_FEATS_ÚBUF_1_FORM_Ú
BUF_1_POS_Ú
BUF_2_POS_é   Ú
BUF_3_POS_ÚBUF_0_LDEP_ÚBUF_0_RDEP_)r   r   r   r)   ÚappendÚsplitr   r   )r   ÚresultÚ
stack_idx0Útokenr1   r'   Ú
stack_idx1Ú	left_mostÚ
right_mostÚdep_left_mostÚdep_right_mostÚwiÚrÚwjÚbuffer_idx0Úbuffer_idx1Úbuffer_idx2Úbuffer_idx3s                     r   Úextract_featureszConfiguration.extract_featuresM   sÛ  € ð ˆô ˆt�z‰z‹?˜QÓàŸ™¤C¨¯
©
£O°aÑ$7Ñ8ˆJØ—L‘L Ñ,ˆEØ×&Ñ& u¨V¡}°dÔ;Ø—‘˜m¨e°F©mÑ;Ô<Ø˜%Ñ D×$;Ñ$;¸EÀ'¹NÔ$KØ—‘˜n¨u°W©~Ñ=Ô>Ø×&Ñ& u¨U¡|Ô4Ø—‘˜l¨U°5©\Ñ9Ô:Ø˜%Ñ D×$;Ñ$;¸EÀ'¹NÔ$KØ˜g™×,Ñ,¨SÓ1�Ø!ò 9�DØ—M‘M .°4Ñ"7Õ8ð9ô �4—:‘:‹ Ò"Ø!ŸZ™Z¬¨D¯J©J«¸!Ñ(;Ñ<�
ØŸ™ ZÑ0�Ø×*Ñ*¨5°©<Ô8Ø—M‘M ,°°u±Ñ"=Ô>ð  ˆIØˆJØˆMØˆNØ!ŸY™Yò *‘	��A�rØ˜Ó#Ø˜Rš b¨:¢oØ%'˜
Ø)*˜Ø˜R› b¨9£nØ$&˜	Ø()™ð*ð ×&Ñ& }Ô5Ø—‘˜m¨mÑ;Ô<Ø×&Ñ& ~Ô6Ø—‘˜m¨nÑ<Ô=ô ˆt�{‰{Ó˜aÓàŸ+™+ a™.ˆKØ—L‘L Ñ-ˆEØ×&Ñ& u¨V¡}°dÔ;Ø—‘˜m¨e°F©mÑ;Ô<Ø˜%Ñ D×$;Ñ$;¸EÀ'¹NÔ$KØ—‘˜n¨u°W©~Ñ=Ô>Ø×&Ñ& u¨U¡|Ô4Ø—‘˜l¨U°5©\Ñ9Ô:Ø˜%Ñ D×$;Ñ$;¸EÀ'¹NÔ$KØ˜g™×,Ñ,¨SÓ1�Ø!ò 9�DØ—M‘M .°4Ñ"7Õ8ð9ô �4—;‘;Ó !Ò#Ø"Ÿk™k¨!™n�ØŸ™ [Ñ1�Ø×*Ñ*¨5°©=¸$Ô?Ø—M‘M -°%¸±-Ñ"?Ô@Ø×*Ñ*¨5°©<Ô8Ø—M‘M ,°°u±Ñ"=Ô>Ü�4—;‘;Ó !Ò#Ø"Ÿk™k¨!™n�ØŸ™ [Ñ1�Ø×*Ñ*¨5°©<Ô8Ø—M‘M ,°°u±Ñ"=Ô>Ü�4—;‘;Ó !Ò#Ø"Ÿk™k¨!™n�ØŸ™ [Ñ1�Ø×*Ñ*¨5°©<Ô8Ø—M‘M ,°°u±Ñ"=Ô>àˆIØˆJØˆMØˆNØ!ŸY™Yò *‘	��A�rØ˜Ó$Ø˜Rš b¨:¢oØ%'˜
Ø)*˜Ø˜R› b¨9£nØ$&˜	Ø()™ð*ð ×&Ñ& }Ô5Ø—‘˜m¨mÑ;Ô<Ø×&Ñ& ~Ô6Ø—‘˜m¨nÑ<Ô=àˆr   N)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r"   r)   rU   r&   r   r   r   r      s   „ ñò
-ò
óódr   r   c                   ó>   — e Zd ZdZdZdZdZdZd„ Zd„ Z	d„ Z
d	„ Zd
„ Zy)Ú
Transitionz½
    This class defines a set of transition which is applied to a configuration to get another configuration
    Note that for different parsing algorithm, the transition is different.
    ÚLEFTARCÚRIGHTARCÚSHIFTÚREDUCEc                 ó²   — || _         |t        j                  t        j                  fvr.t	        dt        j                  ›dt        j                  ›d�«      ‚y)z¢
        :param alg_option: the algorithm option of this parser. Currently support `arc-standard` and `arc-eager` algorithm
        :type alg_option: str
        ú Currently we only support ú and ú N)Ú_algoÚTransitionParserÚARC_STANDARDÚ	ARC_EAGERÚ
ValueError)r   Ú
alg_options     r   r   zTransition.__init__À   sV   € ð
  ˆŒ
ØÜ×)Ñ)Ü×&Ñ&ð
ñ 
õ ä#×0Ó0Ô2B×2LÓ2LðNóð ð	
r   c                 óâ  — t        |j                  «      dk  st        |j                  «      dk  ry|j                  d   dk(  ry|j                  t        |j                  «      dz
     }d}| j                  t        j
                  k(  r|j                  D ]  \  }}}||k(  sŒd}Œ |rH|j                  j                  «        |j                  d   }|j                  j                  |||f«       yy)a  
        Note that the algorithm for left-arc is quite similar except for precondition for both arc-standard and arc-eager

        :param configuration: is the current configuration
        :return: A new configuration or -1 if the pre-condition is not satisfied
        r   r6   r   TFN)	r   r   r   rd   re   rg   r   ÚpoprD   )	r   ÚconfÚrelationÚidx_wir(   Ú
idx_parentrO   Ú	idx_childÚidx_wjs	            r   Úleft_arczTransition.left_arcÏ   sÐ   € ô �—‘Ó Ò!¤s¨4¯:©:£¸!Ò';ØØ�;‰;�q‰>˜QÒàà—‘œC §
¡
›O¨aÑ/Ñ0ˆàˆØ�:‰:Ô)×3Ñ3Ò3Ø,0¯I©Iò !Ñ(�
˜A˜yØ Ó&Ø ‘Dð!ñ Ø�J‰J�N‰NÔØ—[‘[ ‘^ˆFØ�I‰I×Ñ˜f h°Ð7Õ8àr   c                 ó@  — t        |j                  «      dk  st        |j                  «      dk  ry| j                  t        j
                  k(  rW|j                  j                  «       }|j                  d   }||j                  d<   |j                  j                  |||f«       y|j                  t        |j                  «      dz
     }|j                  j                  d«      }|j                  j                  |«       |j                  j                  |||f«       y)zð
        Note that the algorithm for right-arc is DIFFERENT for arc-standard and arc-eager

        :param configuration: is the current configuration
        :return: A new configuration or -1 if the pre-condition is not satisfied
        r   r6   r   N)	r   r   r   rd   re   rf   rk   r   rD   )r   rl   rm   rn   rq   s        r   Ú	right_arczTransition.right_arcë   sÕ   € ô �—‘Ó Ò!¤s¨4¯:©:£¸!Ò';ØØ�:‰:Ô)×6Ñ6Ò6Ø—Z‘Z—^‘^Ó%ˆFØ—[‘[ ‘^ˆFØ#ˆD�K‰K˜‰NØ�I‰I×Ñ˜f h°Ð7Õ8à—Z‘Z¤ D§J¡J£°!Ñ 3Ñ4ˆFØ—[‘[—_‘_ QÓ'ˆFØ�J‰J×Ñ˜fÔ%Ø�I‰I×Ñ˜f h°Ð7Õ8r   c                 ó4  — | j                   t        j                  k7  ryt        |j                  «      dk  ry|j                  t        |j                  «      dz
     }d}|j
                  D ]  \  }}}||k(  sŒd}Œ |r|j                  j                  «        yy)zá
        Note that the algorithm for reduce is only available for arc-eager

        :param configuration: is the current configuration
        :return: A new configuration or -1 if the pre-condition is not satisfied
        r6   r   r   FTN)rd   re   rg   r   r   r   rk   )r   rl   rn   r(   ro   rO   rp   s          r   ÚreducezTransition.reduceÿ   sŠ   € ð �:‰:Ô)×3Ñ3Ò3ØÜˆt�z‰z‹?˜aÒØà—‘œC §
¡
›O¨aÑ/Ñ0ˆØˆØ(,¯	©	ò 	Ñ$ˆJ˜˜9Ø˜FÓ"Ø‘ð	ñ Ø�J‰J�N‰NÕàr   c                 ó¢   — t        |j                  «      dk  ry|j                  j                  d«      }|j                  j	                  |«       y)zë
        Note that the algorithm for shift is the SAME for arc-standard and arc-eager

        :param configuration: is the current configuration
        :return: A new configuration or -1 if the pre-condition is not satisfied
        r   r6   N)r   r   rk   r   rD   )r   rl   rn   s      r   ÚshiftzTransition.shift  s<   € ô ˆt�{‰{Ó˜qÒ ØØ—‘—‘ Ó#ˆØ�
‰
×Ñ˜&Õ!r   N)rV   rW   rX   rY   ÚLEFT_ARCÚ	RIGHT_ARCr^   r_   r   rr   rt   rv   rx   r&   r   r   r[   r[   ´   s6   „ ñð €HØ€IØ€EØ€Fòòò89ò(ó.
"r   r[   c                   óP   — e Zd ZdZdZdZd„ Zd„ Zd„ Zd„ Z	d„ Z
d	„ Zd
„ Zdd„Zd„ Zy)re   zl
    Class for transition based parser. Implement 2 algorithms which are "arc-standard" and "arc-eager"
    zarc-standardz	arc-eagerc                 ó¼   — || j                   | j                  fvr&t        d| j                   ›d| j                  ›d�«      ‚|| _        i | _        i | _        i | _        y)z 
        :param algorithm: the algorithm option of this parser. Currently support `arc-standard` and `arc-eager` algorithm
        :type algorithm: str
        ra   rb   rc   N)rf   rg   rh   Ú
_algorithmÚ_dictionaryÚ_transitionÚ_match_transition)r   Ú	algorithms     r   r   zTransitionParser.__init__+  s]   € ð
 ˜T×.Ñ.°·±Ð?Ñ?Ýà×$Ó$ d§n£nð6óð ð $ˆŒàˆÔØˆÔØ!#ˆÕr   c                 ól   — |j                   |   }|j                   |   }|d   €y |d   |d   k(  r|d   S y )Nr+   ÚheadÚaddressÚrel)r   )r   ro   rp   ÚdepgraphÚp_nodeÚc_nodes         r   Ú_get_dep_relationz"TransitionParser._get_dep_relation;  sI   € Ø—‘ 
Ñ+ˆØ—‘ 	Ñ*ˆà�&‰>Ð!Øà�&‰>˜V IÑ.Ò.Ø˜%‘=Ð àr   c                 óð   — g }|D ]O  }| j                   j                  |t        | j                   «      «       |j                  | j                   |   «       ŒQ dj	                  d„ t        |«      D «       «      S )zè
        :param features: list of feature string which is needed to convert to binary features
        :type features: list(str)
        :return : string of binary features in libsvm format  which is 'featureID:value' pairs
        rc   c              3   ó8   K  — | ]  }t        |«      d z   –— Œ y­w)z:1.0N)r!   )Ú.0Ú	featureIDs     r   ú	<genexpr>z?TransitionParser._convert_to_binary_features.<locals>.<genexpr>S  s   è ø€ ò 
Ø(1ŒC�	‹N˜VÕ#ñ
ùs   ‚)r~   Ú
setdefaultr   rD   ÚjoinÚsorted)r   ÚfeaturesÚunsorted_resultÚfeatures       r   Ú_convert_to_binary_featuresz,TransitionParser._convert_to_binary_featuresG  sw   € ð ˆØò 	>ˆGØ×Ñ×'Ñ'¨´°T×5EÑ5EÓ1FÔGØ×"Ñ" 4×#3Ñ#3°GÑ#<Õ=ð	>ð
 �x‰xñ 
Ü5;¸OÓ5Lô
ó 
ð 	
r   c                 óh  — g }|j                   D ]6  }|j                   |   }d|v sŒ|d   }|d   }|€Œ$|j                  ||f«       Œ8 |D ]f  \  }}||kD  r|}|}|}t        |dz   |«      D ]D  }t        t        |j                   «      «      D ]!  }	|	|k  s|	|kD  sŒ||	f|v r   y|	|f|v sŒ   y ŒF Œh y)Nrƒ   r„   r   FT)r   rD   r   r   )
r   r†   Úarc_listÚkeyÚnodeÚchildIdxÚ	parentIdxÚtempÚkÚms
             r   Ú_is_projectivezTransitionParser._is_projectiveW  sì   € ØˆØ—>‘>ò 	;ˆCØ—>‘> #Ñ&ˆDà˜Š~Ø 	™?�Ø  ™L�	ØÑ(Ø—O‘O Y°Ð$9Õ:ð	;ð $,ò 	)ÑˆI�xà˜)Ò#Ø�Ø$�Ø �	Ü˜8 a™<¨Ó3ò )�Üœs 8§>¡>Ó2Ó3ò )�AØ˜Hš¨!¨i«-Ø˜q˜6 XÑ-Û#(Ø˜q˜6 XÒ-Û#(ñ)ñ)ð	)ð r   c                 ó"  — | j                   j                  |t        | j                   «      dz   «       || j                  | j                   |   <   t	        | j                   |   «      dz   |z   dz   }|j                  |j                  d«      «       y)z^
        write the binary features to input file and update the transition dictionary
        r   rc   ú
zutf-8N)r   r�   r   r€   r!   ÚwriteÚencode)r   r˜   Úbinary_featuresÚ
input_fileÚ	input_strs        r   Ú_write_to_filezTransitionParser._write_to_fileq  s‚   € ð 	×Ñ×#Ñ# C¬¨T×-=Ñ-=Ó)>ÀÑ)BÔCØ8;ˆ×Ñ˜t×/Ñ/°Ñ4Ñ5ä˜×(Ñ(¨Ñ-Ó.°Ñ4°ÑFÈÑMˆ	Ø×Ñ˜×)Ñ)¨'Ó2Õ3r   c                 óø  — t        | j                  «      }d}g }|D �]#  }| j                  |«      sŒ|dz  }t        |«      }t	        |j
                  «      dkD  sŒ?|j
                  d   }|j                  «       }	| j                  |	«      }
t	        |j                  «      dkD  �r=|j                  t	        |j                  «      dz
     }| j                  |||«      }|�Mt         j                  dz   |z   }| j                  ||
|«       |j                  ||«       |j                  |«       Œé| j                  |||«      }|�¡d}|j                  }t        |dz   «      D ]2  }||k7  sŒ	| j                  |||«      }|€Œ|||f|j                   vsŒ1d}Œ4 |rNt         j"                  dz   |z   }| j                  ||
|«       |j%                  ||«       |j                  |«       �ŒŸt         j&                  }| j                  ||
|«       |j)                  |«       |j                  |«       t	        |j
                  «      dkD  r�Œå�Œ& t+        dt-        t	        |«      «      z   «       t+        dt-        |«      z   «       |S )zÔ
        Create the training example in the libsvm format and write it to the input_file.
        Reference : Page 32, Chapter 3. Dependency Parsing by Sandra Kubler, Ryan McDonal and Joakim Nivre (2009)
        r   r   ú:TFú Number of training examples : ú) Number of valid (projective) examples : )r[   rf   rŸ   r   r   r   rU   r•   r   r‰   ry   r§   rr   rD   r   r   r   rz   rt   r^   rx   Úprintr!   )r   Ú	depgraphsr¥   Ú	operationÚ
count_projÚtraining_seqr†   rl   Úb0r’   r¤   Ús0r…   r˜   ÚpreconditionÚmaxIDÚwÚrelws                     r   Ú!_create_training_examples_arc_stdz2TransitionParser._create_training_examples_arc_std{  s`  € ô
 ˜t×0Ñ0Ó1ˆ	Øˆ
Øˆà!ó /	)ˆHØ×&Ñ& xÔ0Øà˜!‰OˆJÜ  Ó*ˆDÜ�d—k‘kÓ" QÓ&Ø—[‘[ ‘^�Ø×0Ñ0Ó2�Ø"&×"BÑ"BÀ8Ó"L�ä�t—z‘z“? QÓ&ØŸ™¤C¨¯
©
£O°aÑ$7Ñ8�Bà×0Ñ0°°R¸ÓB�CØ�Ü(×1Ñ1°CÑ7¸#Ñ=˜Ø×+Ñ+¨C°À*ÔMØ!×*Ñ*¨4°Ô5Ø$×+Ñ+¨CÔ0Ø ð ×0Ñ0°°R¸ÓB�CØ�Ø'+˜à $× 1Ñ 1˜ä!& u¨q¡yÓ!1ò =˜AØ  B›wØ'+×'=Ñ'=¸bÀ!ÀXÓ'N Ø#'Ñ#3Ø(*¨D°! }¸D¿I¹IÒ'EØ7<©ð=ñ (Ü",×"6Ñ"6¸Ñ"<¸sÑ"B˜CØ ×/Ñ/°°_ÀjÔQØ%×/Ñ/°°cÔ:Ø(×/Ñ/°Ô4Ù$ô !×&Ñ&�Ø×#Ñ# C¨¸*ÔEØ—‘ Ô%Ø×#Ñ# CÔ(ôS �d—k‘kÓ" QÖ&ð/	)ôb 	Ð/´#´c¸)³nÓ2EÑEÔFÜÐ9¼CÀ
»OÑKÔLØÐr   c                 ó^  — t        | j                  «      }d}g }|D �]V  }| j                  |«      sŒ|dz  }t        |«      }t	        |j
                  «      dkD  sŒ?|j
                  d   }|j                  «       }	| j                  |	«      }
t	        |j                  «      dkD  �rp|j                  t	        |j                  «      dz
     }| j                  |||«      }|�Mt         j                  dz   |z   }| j                  ||
|«       |j                  ||«       |j                  |«       Œé| j                  |||«      }|�Nt         j                  dz   |z   }| j                  ||
|«       |j                  ||«       |j                  |«       �ŒLd}t!        |«      D ]-  }| j                  |||«      �d}| j                  |||«      €Œ,d}Œ/ |rGt         j"                  }| j                  ||
|«       |j%                  |«       |j                  |«       �ŒÒt         j&                  }| j                  ||
|«       |j)                  |«       |j                  |«       t	        |j
                  «      dkD  r�Œ�ŒY t+        dt-        t	        |«      «      z   «       t+        dt-        |«      z   «       |S )zÌ
        Create the training example in the libsvm format and write it to the input_file.
        Reference : 'A Dynamic Oracle for Arc-Eager Dependency Parsing' by Joav Goldberg and Joakim Nivre
        r   r   r©   FTrª   r«   )r[   rg   rŸ   r   r   r   rU   r•   r   r‰   ry   r§   rr   rD   rz   rt   r   r_   rv   r^   rx   r¬   r!   )r   r­   r¥   r®   Ú	countProjr°   r†   rl   r±   r’   r¤   r²   r…   r˜   r(   r�   s                   r   Ú#_create_training_examples_arc_eagerz4TransitionParser._create_training_examples_arc_eager¹  sˆ  € ô
 ˜tŸ~™~Ó.ˆ	Øˆ	Øˆà!ó 1	)ˆHØ×&Ñ& xÔ0Øà˜‰NˆIÜ  Ó*ˆDÜ�d—k‘kÓ" QÓ&Ø—[‘[ ‘^�Ø×0Ñ0Ó2�Ø"&×"BÑ"BÀ8Ó"L�ä�t—z‘z“? QÓ&ØŸ™¤C¨¯
©
£O°aÑ$7Ñ8�Bà×0Ñ0°°R¸ÓB�CØ�Ü(×1Ñ1°CÑ7¸#Ñ=˜Ø×+Ñ+¨C°À*ÔMØ!×*Ñ*¨4°Ô5Ø$×+Ñ+¨CÔ0Ø ð ×0Ñ0°°R¸ÓB�CØ�Ü(×2Ñ2°SÑ8¸3Ñ>˜Ø×+Ñ+¨C°À*ÔMØ!×+Ñ+¨D°#Ô6Ø$×+Ñ+¨CÔ0Ù ð !�DÜ" 2›Yò (˜Ø×1Ñ1°!°R¸ÓBÐNØ#'˜DØ×1Ñ1°"°a¸ÓBÑNØ#'™Dð	(ñ
 Ü(×/Ñ/˜Ø×+Ñ+¨C°À*ÔMØ!×(Ñ(¨Ô.Ø$×+Ñ+¨CÔ0Ù ô !×&Ñ&�Ø×#Ñ# C¨¸*ÔEØ—‘ Ô%Ø×#Ñ# CÔ(ôW �d—k‘kÓ" QÖ&ð1	)ôf 	Ð/´#´c¸)³nÓ2EÑEÔFÜÐ9¼CÀ	»NÑJÔKØÐr   c           	      ó   — 	 t        j                  dt        j                  «       d¬«      }| j                  | j                  k(  r| j                  ||«       n| j                  ||«       |j                  «        t        |j                  «      \  }}t        j                  ddddd|d	¬
«      }|j                  ||«       t        j                  |t        |d«      «       t!        |j                  «       y# t!        j                  «       w xY w)zØ
        :param depgraphs : list of DependencyGraph as the training data
        :type depgraphs : DependencyGraph
        :param modelfile : file name to save the trained model
        :type modelfile : str
        ztransition_parse.trainF)ÚprefixÚdirÚdeleteÚpolyr4   r   gš™™™™™É?g      à?T)ÚkernelÚdegreeÚcoef0ÚgammaÚCÚverboseÚprobabilityÚwbN)ÚtempfileÚNamedTemporaryFileÚ
gettempdirr}   rf   r·   rº   Úcloser	   Únamer   ÚSVCÚfitÚpickleÚdumpÚopenr   )r   r­   Ú	modelfilerÅ   r¥   Úx_trainÚy_trainÚmodels           r   ÚtrainzTransitionParser.trainù  sÞ   € ð	$Ü!×4Ñ4Ø/´X×5HÑ5HÓ5JÐSXôˆJð �‰ $×"3Ñ"3Ò3Ø×6Ñ6°yÀ*ÕMà×8Ñ8¸ÀJÔOà×ÑÔä1°*·/±/ÓBÑˆG�Wô
 —G‘GØØØØØØØ ôˆEð �I‰I�g˜wÔ'ä�K‰K˜œt I¨tÓ4Ô5ä�:—?‘?Õ#øŒF�:—?‘?Õ#ús   ‚CC6 Ã6Dc                 ó’  — g }t        |d«      5 }t        |«      }ddd«       t        | j                  «      }|D �]ü  }t	        |«      }t        |j                  «      dkD  �ra|j                  «       }	g }
g }g }|	D ]Q  }|| j                  v sŒ|
j                  | j                  |   «       |j                  d«       |j                  d«       ŒS t        t        |
«      «      }t        |«      }t        |«      }t        j                  |||ffdt        | j                  «      f¬«      }i }j                  |«      d   }t        t        |«      «      D ]
  }||   ||<   Œ t        |j!                  «       t#        d«      d¬«      }|D �]  \  }}|j$                  |   }|| j&                  v ró| j&                  |   }|j)                  d	«      d   }|t        j*                  k(  r*|j-                  ||j)                  d	«      d   «      d
k7  sŒ‚ nŸ|t        j.                  k(  r*|j1                  ||j)                  d	«      d   «      d
k7  sŒ¿ nb|t        j2                  k(  r|j5                  |«      d
k7  sŒé n8|t        j6                  k(  sŒÿ|j9                  |«      d
k7  s�Œ nt;        d«      ‚ t        |j                  «      dkD  r�Œat=        |«      }|j>                  D ]  }|j>                  |   }d|d<   d|d<   Œ |j@                  D ]  \  }}} |j>                  |    }!||!d<   ||!d<   Œ! |j                  |«       �Œÿ |S # 1 sw Y   �Œ#xY w)aZ  
        :param depgraphs: the list of test sentence, each sentence is represented as a dependency graph where the 'head' information is dummy
        :type depgraphs: list(DependencyGraph)
        :param modelfile: the model file
        :type modelfile: str
        :return: list (DependencyGraph) with the 'head' and 'rel' information
        ÚrbNr   g      ð?r   )ÚshapeT)r˜   Úreverser©   r6   z;The predicted transition is not recognized, expected errorsr$   r…   rƒ   )!rÑ   r   r[   r}   r   r   r   rU   r~   rD   r   r‘   r   Ú
csr_matrixÚpredict_probar   Úitemsr   Úclasses_r€   rE   ry   rr   rz   rt   r_   rv   r^   rx   rh   r   r   r   )"r   r­   Ú	modelFilerF   ÚfrÕ   r®   r†   rl   r’   ÚcolÚrowÚdatar”   Únp_colÚnp_rowÚnp_dataÚx_testÚ	prob_dictÚ	pred_probÚiÚsorted_ProbÚ
y_pred_idxÚ
confidenceÚy_predÚstrTransitionÚbaseTransitionÚnew_depgraphr˜   r™   rƒ   r…   Úchildrˆ   s"                                     r   ÚparsezTransitionParser.parse"  sV  € ð ˆä�)˜TÓ"ð 	# aÜ “NˆE÷	#ä˜tŸ™Ó/ˆ	à!ó Y	(ˆHÜ  Ó*ˆDÜ�d—k‘kÓ" QÓ&Ø×0Ñ0Ó2�Ø�Ø�Ø�Ø'ò )�GØ $×"2Ñ"2Ò2ØŸ
™
 4×#3Ñ#3°GÑ#<Ô=ØŸ
™
 1œØŸ™ CÕ(ð	)ô
 œv c›{Ó+�Ü˜s›�Ü ›+�ä×*Ñ*Ø˜v vÐ.Ð/¸¼3¸t×?OÑ?OÓ;PÐ7Qô�ð, �	Ø!×/Ñ/°Ó7¸Ñ:�	Üœs 9›~Ó.ò 0�AØ#,¨Q¡<�I˜a’Lð0ä$ Y§_¡_Ó%6¼JÀq»MÐSWÔX�ð /:ó Ñ*�J 
ð #Ÿ^™^¨JÑ7�Fà ×!7Ñ!7Ñ7Ø(,×(>Ñ(>¸vÑ(F˜Ø)6×)<Ñ)<¸SÓ)AÀ!Ñ)D˜à)¬Z×-@Ñ-@Ò@à )× 2Ñ 2°4¸×9LÑ9LÈSÓ9QÐRSÑ9TÓ UØ#%ó!&ñ !&Ø+¬z×/CÑ/CÒCà )× 3Ñ 3°D¸-×:MÑ:MÈcÓ:RÐSTÑ:UÓ VØ#%ó!&ñ !&Ø+¬z×/@Ñ/@Ò@Ø(×/Ñ/°Ó5¸Ó;Ù %Ø+¬z×/?Ñ/?Ó?Ø(Ÿ™¨tÓ4¸Ô:Ù %ä(ØYóð ð9ôW �d—k‘kÓ" QÔ&ôZ $ HÓ-ˆLØ#×)Ñ)ò !�Ø#×)Ñ)¨#Ñ.�Ø ��U‘à ��V’ð	!ð
 %)§I¡Iò $Ñ ��c˜5Ø%×+Ñ+¨EÑ2�Ø!%��v‘Ø #��u’ð$ð �M‰M˜,Ö'ðsY	(ðv ˆ÷	#ñ 	#ús   �L<Ì<MN)T)rV   rW   rX   rY   rf   rg   r   r‰   r•   rŸ   r§   r·   rº   rÖ   ró   r&   r   r   re   re   #  sD   „ ñð "€LØ€Iò$ò 
ò
ò ò44ò<ò|>ó@'$óRir   re   c                   ó   — y)a6  
    >>> from nltk.parse import DependencyGraph, DependencyEvaluator
    >>> from nltk.parse.transitionparser import TransitionParser, Configuration, Transition
    >>> gold_sent = DependencyGraph("""
    ... Economic  JJ     2      ATT
    ... news  NN     3       SBJ
    ... has       VBD       0       ROOT
    ... little      JJ      5       ATT
    ... effect   NN     3       OBJ
    ... on     IN      5       ATT
    ... financial       JJ       8       ATT
    ... markets    NNS      6       PC
    ... .    .      3       PU
    ... """)

    >>> conf = Configuration(gold_sent)

    ###################### Check the Initial Feature ########################

    >>> print(', '.join(conf.extract_features()))
    STK_0_POS_TOP, BUF_0_FORM_Economic, BUF_0_LEMMA_Economic, BUF_0_POS_JJ, BUF_1_FORM_news, BUF_1_POS_NN, BUF_2_POS_VBD, BUF_3_POS_JJ

    ###################### Check The Transition #######################
    Check the Initialized Configuration
    >>> print(conf)
    Stack : [0]  Buffer : [1, 2, 3, 4, 5, 6, 7, 8, 9]   Arcs : []

    A. Do some transition checks for ARC-STANDARD

    >>> operation = Transition('arc-standard')
    >>> operation.shift(conf)
    >>> operation.left_arc(conf, "ATT")
    >>> operation.shift(conf)
    >>> operation.left_arc(conf,"SBJ")
    >>> operation.shift(conf)
    >>> operation.shift(conf)
    >>> operation.left_arc(conf, "ATT")
    >>> operation.shift(conf)
    >>> operation.shift(conf)
    >>> operation.shift(conf)
    >>> operation.left_arc(conf, "ATT")

    Middle Configuration and Features Check
    >>> print(conf)
    Stack : [0, 3, 5, 6]  Buffer : [8, 9]   Arcs : [(2, 'ATT', 1), (3, 'SBJ', 2), (5, 'ATT', 4), (8, 'ATT', 7)]

    >>> print(', '.join(conf.extract_features()))
    STK_0_FORM_on, STK_0_LEMMA_on, STK_0_POS_IN, STK_1_POS_NN, BUF_0_FORM_markets, BUF_0_LEMMA_markets, BUF_0_POS_NNS, BUF_1_FORM_., BUF_1_POS_., BUF_0_LDEP_ATT

    >>> operation.right_arc(conf, "PC")
    >>> operation.right_arc(conf, "ATT")
    >>> operation.right_arc(conf, "OBJ")
    >>> operation.shift(conf)
    >>> operation.right_arc(conf, "PU")
    >>> operation.right_arc(conf, "ROOT")
    >>> operation.shift(conf)

    Terminated Configuration Check
    >>> print(conf)
    Stack : [0]  Buffer : []   Arcs : [(2, 'ATT', 1), (3, 'SBJ', 2), (5, 'ATT', 4), (8, 'ATT', 7), (6, 'PC', 8), (5, 'ATT', 6), (3, 'OBJ', 5), (3, 'PU', 9), (0, 'ROOT', 3)]


    B. Do some transition checks for ARC-EAGER

    >>> conf = Configuration(gold_sent)
    >>> operation = Transition('arc-eager')
    >>> operation.shift(conf)
    >>> operation.left_arc(conf,'ATT')
    >>> operation.shift(conf)
    >>> operation.left_arc(conf,'SBJ')
    >>> operation.right_arc(conf,'ROOT')
    >>> operation.shift(conf)
    >>> operation.left_arc(conf,'ATT')
    >>> operation.right_arc(conf,'OBJ')
    >>> operation.right_arc(conf,'ATT')
    >>> operation.shift(conf)
    >>> operation.left_arc(conf,'ATT')
    >>> operation.right_arc(conf,'PC')
    >>> operation.reduce(conf)
    >>> operation.reduce(conf)
    >>> operation.reduce(conf)
    >>> operation.right_arc(conf,'PU')
    >>> print(conf)
    Stack : [0, 3, 9]  Buffer : []   Arcs : [(2, 'ATT', 1), (3, 'SBJ', 2), (0, 'ROOT', 3), (5, 'ATT', 4), (3, 'OBJ', 5), (5, 'ATT', 6), (8, 'ATT', 7), (6, 'PC', 8), (3, 'PU', 9)]

    ###################### Check The Training Function #######################

    A. Check the ARC-STANDARD training
    >>> import tempfile
    >>> import os
    >>> input_file = tempfile.NamedTemporaryFile(prefix='transition_parse.train', dir=tempfile.gettempdir(), delete=False)

    >>> parser_std = TransitionParser('arc-standard')
    >>> print(', '.join(parser_std._create_training_examples_arc_std([gold_sent], input_file)))
     Number of training examples : 1
     Number of valid (projective) examples : 1
    SHIFT, LEFTARC:ATT, SHIFT, LEFTARC:SBJ, SHIFT, SHIFT, LEFTARC:ATT, SHIFT, SHIFT, SHIFT, LEFTARC:ATT, RIGHTARC:PC, RIGHTARC:ATT, RIGHTARC:OBJ, SHIFT, RIGHTARC:PU, RIGHTARC:ROOT, SHIFT

    >>> parser_std.train([gold_sent],'temp.arcstd.model', verbose=False)
     Number of training examples : 1
     Number of valid (projective) examples : 1
    >>> input_file.close()
    >>> remove(input_file.name)

    B. Check the ARC-EAGER training

    >>> input_file = tempfile.NamedTemporaryFile(prefix='transition_parse.train', dir=tempfile.gettempdir(),delete=False)
    >>> parser_eager = TransitionParser('arc-eager')
    >>> print(', '.join(parser_eager._create_training_examples_arc_eager([gold_sent], input_file)))
     Number of training examples : 1
     Number of valid (projective) examples : 1
    SHIFT, LEFTARC:ATT, SHIFT, LEFTARC:SBJ, RIGHTARC:ROOT, SHIFT, LEFTARC:ATT, RIGHTARC:OBJ, RIGHTARC:ATT, SHIFT, LEFTARC:ATT, RIGHTARC:PC, REDUCE, REDUCE, REDUCE, RIGHTARC:PU

    >>> parser_eager.train([gold_sent],'temp.arceager.model', verbose=False)
     Number of training examples : 1
     Number of valid (projective) examples : 1

    >>> input_file.close()
    >>> remove(input_file.name)

    ###################### Check The Parsing Function ########################

    A. Check the ARC-STANDARD parser

    >>> result = parser_std.parse([gold_sent], 'temp.arcstd.model')
    >>> de = DependencyEvaluator(result, [gold_sent])
    >>> de.eval() >= (0, 0)
    True

    B. Check the ARC-EAGER parser
    >>> result = parser_eager.parse([gold_sent], 'temp.arceager.model')
    >>> de = DependencyEvaluator(result, [gold_sent])
    >>> de.eval() >= (0, 0)
    True

    Remove test temporary files
    >>> remove('temp.arceager.model')
    >>> remove('temp.arcstd.model')

    Note that result is very poor because of only one training example.
    Nr&   r&   r   r   Údemorõ   Ž  s   � r   )rÏ   rÈ   Úcopyr   Úoperatorr   Úosr   Únumpyr   Úscipyr   Úsklearnr   Úsklearn.datasetsr	   ÚImportErrorÚ
nltk.parser
   r   r   Únltk.picklesecr   r   r[   re   rõ   r&   r   r   ú<module>r      sw   ðó Û Ý Ý Ý ð	ÝÝÝÝ3÷ EÑ DÝ &÷Vñ V÷rl"ñ l"ô^h�wô hóVMøðu ò 	Ùð	ús   œA' Á'A/Á.A/