³ò
’B_Kc           @   sn   d  Z  d d k Td d k l Z d d k Td e f d „  ƒ  YZ d „  Z d „  Z e	 d j o e ƒ  n d	 S(
   sË   
A classifier model that decides which label to assign to a token on
the basis of a tree structure, where branches correspond to conditions
on feature values, and leaves correspond to label assignments.
iÿÿÿÿ(   t   *(   t   defaultdictt   DecisionTreeClassifierc           B   sã   e  Z e e e d  „ Z d „  Z d „  Z d „  Z d d d d „ Z d d d „ Z d	 „  Z	 e
 d
 d d e e e d „ ƒ Z e
 d „  ƒ Z e
 d „  ƒ Z e e e d „ Z e
 e d „ ƒ Z e
 d „  ƒ Z e
 e d „ ƒ Z RS(   c         C   s(   | |  _  | |  _ | |  _ | |  _ d S(   sõ  
        @param label: The most likely label for tokens that reach
            this node in the decision tree.  If this decision tree
            has no children, then this label will be assigned to
            any token that reaches this decision tree.
        @param feature_name: The name of the feature that this
            decision tree selects for.
        @param decisions: A dictionary mapping from feature values
            for the feature identified by C{feature_name} to
            child decision trees.
        @param default: The child that will be used if the value of
            feature C{feature_name} does not match any of the keys in
            C{decisions}.  This is used when constructing binary
            decision trees.
        N(   t   _labelt   _fnamet
   _decisionst   _default(   t   selft   labelt   feature_namet	   decisionst   default(    (    s0   /p/zhu/06/nlp/nltk/nltk/classify/decisiontree.pyt   __init__   s    			c         C   s‡   |  i  g } |  i d  j	 o1 x. |  i i ƒ  D] } | i | i ƒ  ƒ q, Wn |  i d  j	 o | i |  i i ƒ  ƒ n t t | ƒ ƒ S(   N(	   R   R   t   Nonet   valuest   extendt   labelsR   t   listt   set(   R   R   t   dt(    (    s0   /p/zhu/06/nlp/nltk/nltk/classify/decisiontree.pyR   +   s     c         C   s   |  i  d  j o |  i Sn | |  i  } | |  i j o |  i | i | ƒ Sn, |  i d  j	 o |  i i | ƒ Sn |  i Sd  S(   N(   R   R   R   R   t   classifyR   (   R   t
   featuresett   fval(    (    s0   /p/zhu/06/nlp/nltk/nltk/classify/decisiontree.pyR   4   s    c         C   sU   d } x8 | D]0 \ } } |  i  | ƒ | j o | d 7} q q Wt | ƒ t | ƒ S(   Ni    i   (   R   t   floatt   len(   R   t   labeled_featuresetst   errorsR   R   (    (    s0   /p/zhu/06/nlp/nltk/nltk/classify/decisiontree.pyt   errorB   s     iF   t    i   c   
      C   s—  |  i  t j o0 | t | ƒ d } d | d | |  i f Sn d } x¹ t t |  i i ƒ  ƒ ƒ D]œ \ } \ } } d | |  i  | f }	 | d t |	 ƒ } | d |	 d | | i f 7} | i  t j	 o2 | d j o% | | i | | d | d ƒ 7} qb qb W|  i	 t j	 o | t | ƒ d } | d	 | d | |  i	 i f 7} |  i	 i  t j	 o5 | d j o( | |  i	 i | | d | d ƒ 7} q“n | S(
   s	  
        Return a string containing a pretty-printed version of this
        decision tree.  Each line in this string corresponds to a
        single decision tree node or leaf, and indentation is used to
        display the structure of the decision tree.
        i   s   %s%s %s
t   .R   s	   %s%s=%s? i   s     i   s   %selse: %s %s
(
   R   R   R   R   t	   enumeratet   sortedR   t   itemst   ppR   (
   R   t   widtht   prefixt   deptht   nt   st   iR   t   resultt   hdr(    (    s0   /p/zhu/06/nlp/nltk/nltk/classify/decisiontree.pyR!   I   s"     )! ,c         C   s†  |  i  t j o d | |  i f Sn d } x‘ t |  i i ƒ  ƒ D]z \ } } | d | |  i  | f 7} | i  t j	 o3 | d j o& | d | i | d | d ƒ 7} qA | d | i 7} qA W|  i t j	 o³ t |  i ƒ d j o+ | d | |  i  |  i i	 ƒ  d	 f 7} n | d
 | f 7} |  i i  t j	 o6 | d j o) | d |  i i | d | d ƒ 7} q‚| d |  i i 7} n | S(   s©   
        Return a string representation of this decision tree that
        expresses the decisions it makes as a nested set of pseudocode
        if statements.
        s   %sreturn %r
R   s   %sif %s == %r: i   s   
s     s
   return %r
s   %sif %s != %r: i    s   %selse: (
   R   R   R   R   R   R    t
   pseudocodeR   R   t   keys(   R   R#   R$   R&   R   R(   (    (    s0   /p/zhu/06/nlp/nltk/nltk/classify/decisiontree.pyR*   b   s$     & )c         C   s
   |  i  ƒ  S(   N(   R!   (   R   (    (    s0   /p/zhu/06/nlp/nltk/nltk/classify/decisiontree.pyt   __str__}   s    gš™™™™™©?id   i
   c         C   s  t  ƒ  } x2 |  D]* \ } }	 x | D] }
 | i |
 ƒ q# Wq W| t j o\ | oU t t  ƒ } xF |  D]: \ } }	 x+ | i ƒ  D] \ }
 } | |
 i | ƒ q~ Wqe Wn | p t i | |  | ƒ } n t i | |  | | ƒ } | i |  | | d | | | | ƒ | S(   s´   
        @param binary: If true, then treat all feature/value pairs a
        individual binary features, rather than using a single n-way
        branch for each feature.
        i   (	   R   t   addR   R   R    R   t
   best_stumpt   best_binary_stumpt   refine(   R   t   entropy_cutofft   depth_cutofft   support_cutofft   binaryt   feature_valuest   verboset   feature_namesR   R   t   fnameR   t   tree(    (    s0   /p/zhu/06/nlp/nltk/nltk/classify/decisiontree.pyt   train€   s*    
	    c         C   s=   t  g  } |  D] \ } } | | q ~ ƒ i ƒ  } t | ƒ S(   N(   t   FreqDistt   maxR   (   R   t   _[1]R   R   (    (    s0   /p/zhu/06/nlp/nltk/nltk/classify/decisiontree.pyt   leaf¥   s    
)c   
      C   s¾   t  g  } | D] \ } } | | q ~ ƒ i ƒ  } t t  ƒ } x/ | D]' \ } } | |  } | | i | ƒ qF Wt g  } | D]# } | | t | | i ƒ  ƒ f q ~ ƒ }	 t | |  |	 ƒ S(   N(   R;   R<   R   t   inct   dictR   (
   R	   R   R=   R   R   t   freqst   feature_valuet   _[2]t   valR
   (    (    s0   /p/zhu/06/nlp/nltk/nltk/classify/decisiontree.pyt   stump«   s    
) 

3c         C   sç  t  | ƒ | j o d  Sn |  i d  j o d  Sn | d j o d  Sn xÈ |  i D]½ } g  }	 | D]1 \ }
 } |
 |  i | j o |	 |
 | f qc qc ~	 } t g  } | D] \ }
 } | | q« ~ ƒ } t t | ƒ ƒ | j o, t i | | | | | | | ƒ |  i | <qR qR W|  i	 d  j	 oÀ g  } | D]: \ }
 } |
 |  i |  i i
 ƒ  j o | |
 | f q.q.~ } t g  } | D] \ }
 } | | q~ ƒ } t t | ƒ ƒ | j o( t i | | | | | | | ƒ |  _	 qãn d  S(   Ni    (   R   R   R   R   R;   t   entropyt   MLEProbDistR   R:   R   R+   (   R   R   R1   R2   R3   R4   R5   R6   R   R=   R   R   t   fval_featuresetsRC   t   label_freqst   _[3]t   default_featuresetst   _[4](    (    s0   /p/zhu/06/nlp/nltk/nltk/classify/decisiontree.pyR0   º   s2    
 .
#	!
-
#	c         C   s—   t  i | ƒ } | i | ƒ } xL |  D]D } t  i | | ƒ } | i | ƒ } | | j  o | } | } q% q% W| o d t | ƒ | i | f GHn | S(   Ns,   best stump for %6d toks uses %-20s err=%6.4f(   R   R>   R   RE   R   R   (   R7   R   R6   R.   t
   best_errorR8   RE   t   stump_error(    (    s0   /p/zhu/06/nlp/nltk/nltk/classify/decisiontree.pyR.   ×   s     c   
   	   C   sË   t  g  } | D] \ } } | | q ~ ƒ i ƒ  } t  ƒ  } t  ƒ  } xC | D]; \ } } | |  | j o | i | ƒ qL | i | ƒ qL Wh  t | i ƒ  ƒ | <} t | i ƒ  ƒ }	 t | |  | |	 ƒ S(   N(   R;   R<   R?   R   (
   R	   RB   R   R=   R   R   t	   pos_fdistt	   neg_fdistR
   R   (    (    s0   /p/zhu/06/nlp/nltk/nltk/classify/decisiontree.pyt   binary_stumpæ   s    
)		 c         C   sà   t  i | ƒ } | i | ƒ } xd |  D]\ } xS | | D]G } t  i | | | ƒ } | i | ƒ }	 |	 | j  o |	 } | } q6 q6 Wq% W| i o$ d | i | i i ƒ  d f }
 n d }
 | o d t | ƒ |
 | f GHn | S(   Ns   %s=%si    s	   (default)s,   best stump for %6d toks uses %-20s err=%6.4f(   R   R>   R   RQ   R   R   R+   R   (   R7   R   R5   R6   R.   RM   R8   R   RE   RN   t   descr(    (    s0   /p/zhu/06/nlp/nltk/nltk/classify/decisiontree.pyR/   ø   s(      
	(   t   __name__t
   __module__R   R   R   R   R   R!   R*   R,   t   staticmethodt   FalseR:   R>   RE   R0   R.   RQ   R/   (    (    (    s0   /p/zhu/06/nlp/nltk/nltk/classify/decisiontree.pyR      s(   						"c         C   s   t  i |  d t d t ƒS(   NR4   R6   (   R   R:   t   True(   t   x(    (    s0   /p/zhu/06/nlp/nltk/nltk/classify/decisiontree.pyt   f  s    c          C   sK   d d k  l }  l } |  t | ƒ } | i d d ƒ GH| i d d ƒ GHd  S(   Niÿÿÿÿ(   t
   names_demot   binary_names_demo_featuresR$   i   (   t   nltk.classify.utilRZ   R[   RY   R!   R*   (   RZ   R[   t
   classifier(    (    s0   /p/zhu/06/nlp/nltk/nltk/classify/decisiontree.pyt   demo  s
    	t   __main__N(
   t   __doc__t   nltk.probabilityt   nltk.compatR   t   apit   ClassifierIR   RY   R^   RS   (    (    (    s0   /p/zhu/06/nlp/nltk/nltk/classify/decisiontree.pys   <module>   s   

þ		