
    gz                         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  G d
 d      Z G d d      Z G d de      Zd Zy# e$ r Y 4w xY w)    N)deepcopy)
itemgetter)remove)array)sparse)svm)load_svmlight_file)DependencyEvaluatorDependencyGraphParserIc                   *    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/var/www/openai/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__4   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tokenr0   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_featuresL   s     tzz?QC

Oa$78JLL,E&&uV}d;meFm;<%D$;$;E'N$KnuW~=>&&uU|4lU5\9:%D$;$;E'N$Kg,,S1!DMM.4"78 " 4::"!ZZDJJ!(;<
Z0**5<8MM,u"=>  IJMN!YY	Ar#Rb:o%'
)*Rb9n$&	() ' &&}5mm;<&&~6mn<= t{{a++a.KLL-E&&uV}d;meFm;<%D$;$;E'N$KnuW~=>&&uU|4lU5\9:%D$;$;E'N$Kg,,S1!DMM.4"78 " 4;;!#"kk!n[1**5=$?MM-%-"?@**5<8MM,u"=>4;;!#"kk!n[1**5<8MM,u"=>4;;!#"kk!n[1**5<8MM,u"=>IJMN!YY	Ar$Rb:o%'
)*Rb9n$&	() ' &&}5mm;<&&~6mn<=r   N)F)__name__
__module____qualname____doc__r   r!   r(   rT   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02B2L2L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   r5   r   TFN)	r   r   r   rc   rd   rf   r   poprC   )	r   confrelationidx_wir'   
idx_parentrN   	idx_childidx_wjs	            r   left_arczTransition.left_arc   s     !s4::!';;;q>QC

Oa/0::)333,0II(
Ay& D -6 JJNN[[^FIIfh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   r5   r   N)	r   r   r   rc   rd   re   rj   r   rC   )r   rk   rl   rm   rp   s        r   	right_arczTransition.right_arc   s     !s4::!';::)666ZZ^^%F[[^F#DKKNIIfh78ZZDJJ! 34F[[__Q'FJJf%IIfh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
        r5   r   r   FTN)rc   rd   rf   r   r   r   rj   )r   rk   rm   r'   rn   rN   ro   s          r   reducezTransition.reduce   s     ::)333tzz?aC

Oa/0(,		$J9F" )2 JJNN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   r5   N)r   r   rj   r   rC   )r   rk   rm   s      r   shiftzTransition.shift  s<     t{{q #

&!r   N)rU   rV   rW   rX   LEFT_ARC	RIGHT_ARCr]   r^   r   rq   rs   ru   rw   r%   r   r   rZ   rZ      s6     HIEF89(.
"r   rZ   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)rd   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
        r`   ra   rb   N)re   rf   rg   
_algorithm_dictionary_transition_match_transition)r   	algorithms     r   r   zTransitionParser.__init__*  s]    
 d//@@$$dnn6  $!#r   c                 l    |j                   |   }|j                   |   }|d   y |d   |d   k(  r|d   S y )Nr*   headaddressrel)r   )r   rn   ro   depgraphp_nodec_nodes         r   _get_dep_relationz"TransitionParser._get_dep_relation:  sI    
+	*&>!&>VI..%= 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
        rb   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>R  s      
5L	C	NV#5Ls   )r}   
setdefaultr   rC   joinsorted)r   featuresunsorted_resultfeatures       r   _convert_to_binary_featuresz,TransitionParser._convert_to_binary_featuresF  sr     G''T5E5E1FG""4#3#3G#<=  
 xx 
5;O5L
 
 	
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   rC   r   r   )
r   r   arc_listkeynodechildIdx	parentIdxtempkms
             r   _is_projectivezTransitionParser._is_projectiveV  s    >>C>>#&D~	? L	(OOY$9: " $,Ix)#$ 	8a<3s8>>23AH!i-q6X-#(q6X-#( 4 4 $, 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   rb   
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_filep  s     	##CT-=-=)>)BC8;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 : )rZ   re   r   r   r   r   rT   r   r   r   rx   r   rq   rC   r   r   r   ry   rs   r]   rw   printr    )r   	depgraphsr   	operation
count_projtraining_seqr   rk   b0r   r   s0r   r   preconditionmaxIDwrelws                     r   !_create_training_examples_arc_stdz2TransitionParser._create_training_examples_arc_stdz  sV   
 t001	
!H&&x0!OJ *Ddkk"Q&[[^002"&"B"B8"Ltzz?Q&C

Oa$78B00RBC(11C7#=++C*M!**45$++C0  00RBC'+ $ 1 1!&uqy!1A Bw'+'='=b!X'N#'#3(*D!}DII'E7< "2 (","6"6"<s"BC //_jQ%//c:(//4$ !&&##C*E%##C(S dkk"Q& "b 	/#c)n2EEF9C
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   )rZ   rf   r   r   r   r   rT   r   r   r   rx   r   rq   rC   ry   rs   r   r^   ru   r]   rw   r   r    )r   r   r   r   	countProjr   r   rk   r   r   r   r   r   r   r'   r   s                   r   #_create_training_examples_arc_eagerz4TransitionParser._create_training_examples_arc_eager  s~   
 t~~.		!H&&x0NI *Ddkk"Q&[[^002"&"B"B8"Ltzz?Q&C

Oa$78B00RBC(11C7#=++C*M!**45$++C0  00RBC(22S83>++C*M!++D#6$++C0  !D"2Y11!RBN#'D11"aBN#'D	 '
 (//++C*M!((.$++C0  !&&##C*E%##C(W dkk"Q& "f 	/#c)n2EEF9C	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polyr3   r   g?g      ?T)kerneldegreecoef0gammaCverboseprobabilitywbN)tempfileNamedTemporaryFile
gettempdirr|   re   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/X5H5H5JSXJ $"3"3366y*M88JO1*//BGW
 GG E IIgw'KKtIt45:??#F:??#s   CC6 6Dc                 v   g }t        j                  t        |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        j0                  k(  r*|j3                  ||j+                  d      d         d	k7  s nb|t        j4                  k(  r|j7                  |      d	k7  s n8|t        j8                  k(  s|j;                  |      d	k7  s nt=        d
       t        |j                        dkD  rat?        |      }|j@                  D ]  }|j@                  |   }d|d<   d|d<    |jB                  D ]  \  }}}|j@                  |   } || d<   || d<   ! |j                  |        |S )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
        rbr   g      ?r   )shapeT)r   reverser   r5   z;The predicted transition is not recognized, expected errorsr#   r   r   )"r   loadr   rZ   r|   r   r   r   rT   r}   rC   r   r   r   
csr_matrixpredict_probar   itemsr   classes_r   rD   rx   rq   ry   rs   r^   ru   r]   rw   rg   r   r   r   )!r   r   	modelFilerE   r   r   r   rk   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!  s     DD12t/	!H *Ddkk"Q&002'G$"2"22

4#3#3G#<=

1C(	  (
 vc{+s+**vv./3t?O?O;P7Q, 	!//7:	s9~.A#,Q<IaL /$Y__%6JqMSWX /:*J
 #^^J7F!7!77(,(>(>v(F)6)<)<S)A!)D)Z-@-@@ ) 2 249L9LS9QRS9T U#%!& !&+z/C/CC ) 3 3D-:M:Mc:RST:U V#%!& !&+z/@/@@(//5; %+z/?/??(t4: %(Y 9 /:W dkk"Q&Z $H-L#))#))#. U V	 *
 %)II c5%++E2!%v #u %. MM,'s "v r   N)T)rU   rV   rW   rX   re   rf   r   r   r   r   r   r   r   r   r   r%   r   r   rd   rd   "  sD     "LI$ 

 44<|>@'$Rhr   rd   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   r   rZ   rd   r   r%   r   r   <module>r      st        	3 E DV Vrl" l"^gw gTMq  		s   A! !A)(A)