³ò
3ÒÇIc           @   sÌ  e  d  ƒ Z d d k Z d d k Z d d k Z d d k l Z d d k l Z d e	 f d „  ƒ  YZ
 d e f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d „  Z d  „  Z d! e f d" „  ƒ  YZ d# e f d$ „  ƒ  YZ d% e f d& „  ƒ  YZ d' e f d( „  ƒ  YZ e i d) d* ƒ Z d+ „  Z  d, „  Z! d- e f d. „  ƒ  YZ" d/ e" f d0 „  ƒ  YZ# d1 „  Z$ d2 „  Z% d3 „  Z& d4 d5 d6 „ Z' e( d7 j o e' d4 d8 ƒ e' d9 d: ƒ n d! d% d# d d' d d d d d d/ d d d d d d- d	 d d; d< d= d> g Z) d S(?   s   -1e300iÿÿÿÿN(   t
   itemgetter(   t   islicet   FreqDistc           B   sC  e  Z d  Z e d „ Z d d „ Z d „  Z d „  Z d „  Z d „  Z	 e d „ Z
 d	 „  Z d
 „  Z e d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z  d „  Z! d  „  Z" d! „  Z# d" „  Z$ RS(#   s  
    A frequency distribution for the outcomes of an experiment.  A
    frequency distribution records the number of times each outcome of
    an experiment has occurred.  For example, a frequency distribution
    could be used to record the frequency of each word type in a
    document.  Formally, a frequency distribution can be defined as a
    function mapping from each sample to the number of times that
    sample occurred as an outcome.

    Frequency distributions are generally constructed by running a
    number of experiments, and incrementing the count for a sample
    every time it is an outcome of an experiment.  For example, the
    following code will produce a frequency distribution that encodes
    how often each word occurs in a text:

        >>> fdist = FreqDist()
        >>> for word in tokenize.whitespace(sent):
        ...    fdist.inc(word.lower())

    An equivalent way to do this is with the initializer:

        >>> fdist = FreqDist(word.lower() for word in tokenize.whitespace(sent))

    c         C   s^   t  i |  ƒ d |  _ d |  _ d |  _ d |  _ | o" x | D] } |  i | ƒ q? Wn d S(   sd  
        Construct a new frequency distribution.  If C{samples} is
        given, then the frequency distribution will be initialized
        with the count of each object in C{samples}; otherwise, it
        will be initialized to be empty.

        In particular, C{FreqDist()} returns an empty frequency
        distribution; and C{FreqDist(samples)} first creates an empty
        frequency distribution, and then calls C{inc} for each element
        in the list C{samples}.

        @param samples: The samples to initialize the frequency
        distribution with.
        @type samples: Sequence
        i    N(   t   dictt   __init__t   _Nt   Nonet	   _Nr_cachet
   _max_cachet   _item_cachet   inc(   t   selft   samplest   sample(    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR   L   s    				 i   c         C   s]   | d j o d Sn |  i  | 7_  |  i | d ƒ | |  | <d |  _ d |  _ d |  _ d S(   sˆ  
        Increment this C{FreqDist}'s count for the given
        sample.

        @param sample: The sample whose count should be incremented.
        @type sample: any
        @param count: The amount to increment the sample's count by.
        @type count: C{int}
        @rtype: None
        @raise NotImplementedError: If C{sample} is not a
               supported sample type.
        i    N(   R   t   getR   R   R   R	   (   R   R   t   count(    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR
   e   s    		c         C   s   |  i  S(   s  
        @return: The total number of sample outcomes that have been
          recorded by this C{FreqDist}.  For the number of unique 
          sample values (or bins) with counts greater than zero, use
          C{FreqDist.B()}.
        @rtype: C{int}
        (   R   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyt   N|   s    c         C   s
   t  |  ƒ S(   s  
        @return: The total number of sample values (or X{bins}) that
            have counts greater than zero.  For the total
            number of sample outcomes recorded, use C{FreqDist.N()}.
            (FreqDist.B() is the same as len(FreqDist).)
        @rtype: C{int}
        (   t   len(   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyt   B†   s    c         C   s
   |  i  ƒ  S(   sÜ   
        @return: A list of all samples that have been recorded as
            outcomes by this frequency distribution.  Use C{count()}
            to determine the count for each sample.
        @rtype: C{list}
        (   t   keys(   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR   ‘   s    c         C   s4   g  } |  D]" } |  | d j o | | q q ~ S(   si   
        @return: A list of all samples that occur once (hapax legomena)
        @rtype: C{list}
        i   (    (   R   t   _[1]t   item(    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyt   hapaxesš   s    c         C   s•   | d j  o t  d ‚ n | d j o' | t j o d SqN | |  i ƒ  Sn |  i t j o |  i ƒ  n | t |  i ƒ j o d Sn |  i | S(   sª  
        @return: The number of samples with count r.
        @rtype: C{int}
        @type r: C{int}
        @param r: A sample count.
        @type bins: C{int}
        @param bins: The number of possible sample outcomes.  C{bins}
            is used to calculate Nr(0).  In particular, Nr(0) is
            C{bins-self.B()}.  If C{bins} is not specified, it
            defaults to C{self.B()} (so Nr(0) will be 0).
        i    s%   FreqDist.Nr(): r must be non-negative(   t
   IndexErrorR   R   R   t   _cache_Nr_valuesR   (   R   t   rt   bins(    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyt   Nr¡   s    c         C   s   d g } xf |  D]^ } |  i  | d ƒ } | t | ƒ j o# | d g | d t | ƒ 7} n | | c d 7<q W| |  _ d  S(   Ni    i   (   R   R   R   (   R   R   R   t   c(    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR   ½   s    	 #c         C   s   t  d ‚ d S(   s  
        Return the count of a given sample.  The count of a sample is
        defined as the number of times that sample outcome was
        recorded by this C{FreqDist}.  Counts are non-negative
        integers.  This method has been replaced by conventional
        dictionary indexing; use fd[item] instead of fd.count(item).

        @return: The count of a given sample.
        @rtype: C{int}
        @param sample: the sample whose count
               should be returned.
        @type sample: any.
        s=   Use indexing to look up an entry in a FreqDist, e.g. fd[item]N(   t   AttributeError(   R   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR   Æ   s    c         c   s.   d } x! | D] } | |  | 7} | Vq Wd S(   sz  
        Return the cumulative frequencies of the specified samples.
        If no samples are specified, all counts are returned, starting
        with the largest.

        @return: The cumulative frequencies of the given samples.
        @rtype: C{list} of C{float}
        @param samples: the samples whose frequencies should be returned.
        @type sample: any.
        g        N(    (   R   R   t   cfR   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyt   _cumulative_frequenciesÖ   s
     c         C   s-   |  i  d j o d Sn t |  | ƒ |  i  S(   sh  
        Return the frequency of a given sample.  The frequency of a
        sample is defined as the count of that sample divided by the
        total number of sample outcomes that have been recorded by
        this C{FreqDist}.  The count of a sample is defined as the
        number of times that sample outcome was recorded by this
        C{FreqDist}.  Frequencies are always real numbers in the range
        [0, 1].

        @return: The frequency of a given sample.
        @rtype: float
        @param sample: the sample whose frequency
               should be returned.
        @type sample: any
        i    (   R   t   float(   R   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyt   freqè   s    c         C   sf   |  i  t j oO t } d } x3 |  D]+ } |  | | j o | } |  | } q# q# W| |  _  n |  i  S(   sÛ  
        Return the sample with the greatest number of outcomes in this
        frequency distribution.  If two or more samples have the same
        number of outcomes, return one of them; which sample is
        returned is undefined.  If no outcomes have occurred in this
        frequency distribution, return C{None}.

        @return: The sample with the maximum number of outcomes in this
                frequency distribution.
        @rtype: any or C{None}
        iÿÿÿÿ(   R   R   (   R   t   best_samplet
   best_countR   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyt   maxü   s     c         O   s›  y d d k  } Wn t j
 o t d ƒ ‚ n Xt | ƒ d j o t |  ƒ g } n t t |  | Œ ƒ } t | d t ƒ } | o t |  i | ƒ ƒ } d } n, g  } | D] }	 | |  |	 q° ~ } d } | i	 t
 d d	 ƒd
 | j o d | d
 <n | i | |  | i t t | ƒ ƒ g  }
 | D] } |
 t | ƒ q.~
 d d ƒd | j o | i | d ƒ n | i d ƒ | i | ƒ | i ƒ  d S(   s  
        Plot samples from the frequency distribution
        displaying the most frequent sample first.  If an integer
        parameter is supplied, stop after this many samples have been
        plotted.  If two integer parameters m, n are supplied, plot a
        subset of the samples, beginning with m and stopping at n-1.
        For a cumulative plot, specify cumulative=True.
        (Requires Matplotlib to be installed.)

        @param title: The title for the graph
        @type title: C{str}
        @param cumulative: A flag to specify whether the plot is cumulative (default = False)
        @type title: C{bool}
        @param num: The maximum number of samples to plot (default=50).  Specify num=0 to get all samples (slow).
        @type num: C{int} 
        iÿÿÿÿNsX   The plot function requires the matplotlib package.See http://matplotlib.sourceforge.net/i    t
   cumulatives   Cumulative Countst   Countst   colort   silvert	   linewidthi   t   rotationiZ   t   titlet   Samples(   t   pylabt   ImportErrort
   ValueErrorR   t   listR   t
   _get_kwargt   FalseR   t   gridt   Truet   plott   xtickst   ranget   strR+   t   xlabelt   ylabelt   show(   R   t   argst   kwargsR-   R   R%   t   freqsR:   R   R   t   _[2]t   s(    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR5     s,    
%C"c   	      O   sð   t  | ƒ d j o t  |  ƒ g } n t t |  | Œ ƒ } t | d t ƒ } | o t |  i | ƒ ƒ } n& g  } | D] } | |  | qx ~ } x, t t  | ƒ ƒ D] } d t | | ƒ Gq¥ WHx& t t  | ƒ ƒ D] } d | | GqÕ WHd S(   s   
        Tabulate the given samples from the frequency distribution (cumulative),
        displaying the most frequent sample first.
        (Requires Matplotlib to be installed.)
        
        @param samples: The samples to plot (default is all samples)
        @type samples: C{list}
        @param title: The title for the graph
        @type title: C{str}
        @param num: The maximum number of samples to plot (default=50).  Specify num=0 to get all samples (slow).
        @type num: C{int} 
        i    R%   s   %4ss   %4dN(   R   R0   R   R1   R2   R   R7   R8   (	   R   R<   R=   R   R%   R>   R   R   t   i(    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyt   tabulate@  s    %  c         C   s   t  d ‚ d  S(   Nsj   Use FreqDist.keys(), or iterate over the FreqDist to get its samples in sorted order (most frequent first)(   R   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyt   sorted_samples_  s    c         C   s   t  d ‚ d  S(   Nsj   Use FreqDist.keys(), or iterate over the FreqDist to get its samples in sorted order (most frequent first)(   R   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyt   sortedb  s    c         C   s<   |  i  p. t t i |  ƒ d t d ƒ d t ƒ|  _  n d  S(   Nt   keyi   t   reverse(   R	   RD   R   t   itemsR    R4   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyt   _sort_keys_by_valuee  s    
c         C   s    |  i  ƒ  t t d ƒ |  i ƒ S(   s¡   
        Return the samples sorted in decreasing order of frequency.

        @return: A list of samples, in sorted order
        @rtype: C{list} of any
        i    (   RH   t   mapR    R	   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR   j  s    
c         C   s    |  i  ƒ  t t d ƒ |  i ƒ S(   s¡   
        Return the samples sorted in decreasing order of frequency.

        @return: A list of samples, in sorted order
        @rtype: C{list} of any
        i   (   RH   RI   R    R	   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyt   valuest  s    
c         C   s   |  i  ƒ  |  i S(   s¢   
        Return the items sorted in decreasing order of frequency.

        @return: A list of items, in sorted order
        @rtype: C{list} of C{tuple}
        (   RH   R	   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRG   ~  s    
c         C   s   t  |  i ƒ  ƒ S(   s¥   
        Return the samples sorted in decreasing order of frequency.

        @return: An iterator over the samples, in sorted order
        @rtype: C{iter}
        (   t   iterR   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyt   __iter__ˆ  s    c         C   s   t  |  i ƒ  ƒ S(   s¥   
        Return the samples sorted in decreasing order of frequency.

        @return: An iterator over the samples, in sorted order
        @rtype: C{iter}
        (   RK   R   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyt   iterkeys‘  s    c         C   s   t  |  i ƒ  ƒ S(   s–   
        Return the values sorted in decreasing order.

        @return: An iterator over the values, in sorted order
        @rtype: C{iter}
        (   RK   RJ   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyt
   itervaluesš  s    c         C   s   |  i  ƒ  t |  i ƒ S(   s¨   
        Return the items sorted in decreasing order of frequency.

        @return: An iterator over the items, in sorted order
        @rtype: C{iter} of any
        (   RH   RK   R	   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyt	   iteritems£  s    
c         C   s.   t  | t ƒ p t Sn |  i ƒ  | i ƒ  j S(   N(   t
   isinstanceR   R2   RG   (   R   t   other(    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyt   __eq__±  s    c         C   s   |  | j S(   N(    (   R   RQ   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyt   __ne__´  s    c            sK   t  ˆ t ƒ p t Sn t ˆ  ƒ i ˆ ƒ o t ‡  ‡ f d †  ˆ  Dƒ ƒ S(   Nc         3   s'   x  |  ] } ˆ  | ˆ | j Vq Wd  S(   N(    (   t   .0RE   (   R   RQ   (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pys	   <genexpr>¸  s    (   RP   R   R2   t   sett   issubsett   all(   R   RQ   (    (   R   RQ   s&   /p/zhu/06/nlp/nltk/nltk/probability.pyt   __le__¶  s    c         C   s/   t  | t ƒ p t Sn |  | j o
 |  | j S(   N(   RP   R   R2   (   R   RQ   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyt   __lt__¹  s    c         C   s"   t  | t ƒ p t Sn | |  j S(   N(   RP   R   R2   (   R   RQ   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyt   __ge__¼  s    c         C   s"   t  | t ƒ p t Sn | |  j  S(   N(   RP   R   R2   (   R   RQ   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyt   __gt__¿  s    c         C   s   d |  i  ƒ  S(   s^   
        @return: A string representation of this C{FreqDist}.
        @rtype: string
        s   <FreqDist with %d outcomes>(   R   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyt   __repr__Ã  s    c         C   s@   g  } |  D] } | d | |  | f q ~ } d d i  | ƒ S(   s^   
        @return: A string representation of this C{FreqDist}.
        @rtype: string
        s   %r: %rs   <FreqDist: %s>s   , (   t   join(   R   R   R@   RG   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyt   __str__Ê  s    /c         C   s   |  i  | d ƒ S(   Ni    (   R   (   R   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyt   __getitem__Ò  s    (%   t   __name__t
   __module__t   __doc__R   R   R
   R   R   R   R   R   R   R   R   R!   R$   R5   RB   RC   RD   RH   R   RJ   RG   RL   RM   RN   RO   RR   RS   RX   RY   RZ   R[   R\   R^   R_   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR   3   sD   	
										.					
	
	
															t	   ProbDistIc           B   sS   e  Z d  Z e Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z RS(   sŽ  
    A probability distribution for the outcomes of an experiment.  A
    probability distribution specifies how likely it is that an
    experiment will have any given outcome.  For example, a
    probability distribution could be used to predict the probability
    that a token in a document will have a given type.  Formally, a
    probability distribution can be defined as a function mapping from
    samples to nonnegative real numbers, such that the sum of every
    number in the function's range is 1.0.  C{ProbDist}s are often
    used to model the probability distribution of the experiment used
    to generate a frequency distribution.
    c         C   s!   |  i  t j o t d ‚ n d  S(   Ns    Interfaces can't be instantiated(   t	   __class__Rc   t   AssertionError(   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR   ê  s    c         C   s   t  ƒ  ‚ d S(   s  
        @return: the probability for a given sample.  Probabilities
            are always real numbers in the range [0, 1].
        @rtype: float
        @param sample: The sample whose probability
               should be returned.
        @type sample: any
        N(   Re   (   R   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyt   probî  s    	c         C   s8   |  i  | ƒ } | d j o t Sn t i | d ƒ Sd S(   s.  
        @return: the base 2 logarithm of the probability for a given
            sample.  Log probabilities range from negitive infinity to
            zero.
        @rtype: float
        @param sample: The sample whose probability
               should be returned.
        @type sample: any
        i    i   N(   Rf   t   _NINFt   matht   log(   R   R   t   p(    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyt   logprobù  s    c         C   s   t  ƒ  ‚ d S(   sÞ   
        @return: the sample with the greatest probability.  If two or
            more samples have the same probability, return one of them;
            which sample is returned is undefined.
        @rtype: any
        N(   Re   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR$     s    c         C   s   t  ƒ  ‚ d S(   s¶   
        @return: A list of all samples that have nonzero
            probabilities.  Use C{prob} to find the probability of
            each sample.
        @rtype: C{list}
        N(   Re   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR     s    c         C   s   d S(   sm   
        @return: The ratio by which counts are discounted on average: c*/c
        @rtype: C{float}
        g        (    (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyt   discount   s    c         C   s¢   t  i  ƒ  } x< |  i ƒ  D]. } | |  i | ƒ 8} | d j o | Sq q W| d j  o | Sn |  i o t i d |  d | f ƒ n t  i t |  i ƒ  ƒ ƒ S(   sÄ   
        @return: A randomly selected sample from this probabilitiy
            distribution.  The probability of returning each sample
            C{samp} is equal to C{self.prob(samp)}.
        i    g-Cëâ6?sT   Probability distribution %r sums to %r; generate() is returning an arbitrary sample.i   (   t   randomR   Rf   t
   SUM_TO_ONEt   warningst   warnt   choiceR0   (   R   Rj   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyt   generate)  s     
	(   R`   Ra   Rb   R4   Rn   R   Rf   Rk   R$   R   Rl   Rr   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRc   Ù  s   				
	
		t   UniformProbDistc           B   s;   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s—   
    A probability distribution that assigns equal probability to each
    sample in a given set; and a zero probability to all other
    samples.
    c         C   sb   t  | ƒ d j o t d d ƒ ‚ n t | ƒ |  _ d t  |  i ƒ |  _ t |  i ƒ |  _ d S(   s5  
        Construct a new uniform probability distribution, that assigns
        equal probability to each sample in C{samples}.

        @param samples: The samples that should be given uniform
            probability.
        @type samples: C{list}
        @raise ValueError: If C{samples} is empty.
        i    s(   A Uniform probability distribution must s   have at least one sample.g      ð?N(   R   R/   RU   t
   _samplesett   _probR0   t   _samples(   R   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR   B  s    
c         C   s#   | |  i  j o |  i Sn d Sd  S(   Ni    (   Rt   Ru   (   R   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRf   S  s    c         C   s   |  i  d S(   Ni    (   Rv   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR$   V  s    c         C   s   |  i  S(   N(   Rv   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR   W  s    c         C   s   d t  |  i ƒ S(   Ns!   <UniformProbDist with %d samples>(   R   Rt   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR\   X  s    (   R`   Ra   Rb   R   Rf   R$   R   R\   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRs   <  s   				t   DictionaryProbDistc           B   sM   e  Z d  Z e e e d „ Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(   s£   
    A probability distribution whose probabilities are directly
    specified by a given dictionary.  The given dictionary maps
    samples to probabilities.
    c   	      C   s\  | i  ƒ  |  _ | |  _ | o9| o t |  i i ƒ  ƒ } | t j oD t i d t | ƒ d ƒ } xX | i	 ƒ  D] } | |  i | <qq WqTxÅ |  i i
 ƒ  D] \ } } |  i | c | 8<qœ WqXt |  i i ƒ  ƒ } | d j o2 d t | ƒ } x\ | D] } | |  i | <qü WqXd | } x4 |  i i
 ƒ  D] \ } } |  i | c | 9<q1Wn d S(   s5  
        Construct a new probability distribution from the given
        dictionary, which maps values to probabilities (or to log
        probabilities, if C{log} is true).  If C{normalize} is
        true, then the probability values are scaled by a constant
        factor such that they sum to 1.
        g      ð?i   i    N(   t   copyt
   _prob_dictt   _logt   sum_logsRJ   Rg   Rh   Ri   R   R   RG   t   sum(	   R   t	   prob_dictRi   t	   normalizet	   value_sumt   logpt   xRj   t   norm_factor(    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR   a  s.    	   
 c         C   sL   |  i  o+ | |  i j o d SqH d |  i | Sn |  i i | d ƒ Sd  S(   Ni    i   (   Rz   Ry   R   (   R   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRf   ‚  s    
c         C   sp   |  i  o |  i i | t ƒ SnL | |  i j o t Sn4 |  i | d j o t Sn t i |  i | d ƒ Sd  S(   Ni    i   (   Rz   Ry   R   Rg   Rh   Ri   (   R   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRk   ‰  s
    
c         C   sA   t  |  d ƒ p* t d „  |  i i ƒ  Dƒ ƒ d |  _ n |  i S(   Nt   _maxc         s   s%   x |  ] \ } } | | f Vq Wd  S(   N(    (   RT   t   vRj   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pys	   <genexpr>“  s    i   (   t   hasattrR$   Ry   RG   Rƒ   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR$   ‘  s    *c         C   s   |  i  i ƒ  S(   N(   Ry   R   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR   •  s    c         C   s   d t  |  i ƒ S(   Ns   <ProbDist with %d samples>(   R   Ry   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR\   —  s    (   R`   Ra   Rb   R   R2   R   Rf   Rk   R$   R   R\   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRw   [  s   !				t   MLEProbDistc           B   sD   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s&  
    The maximum likelihood estimate for the probability distribution
    of the experiment used to generate a frequency distribution.  The
    X{maximum likelihood estimate} approximates the probability of
    each sample as the frequency of that sample in the frequency
    distribution.
    c         C   s   | |  _  d S(   s+  
        Use the maximum likelihood estimate to create a probability
        distribution for the experiment used to generate C{freqdist}.

        @type freqdist: C{FreqDist}
        @param freqdist: The frequency distribution that the
            probability estimates should be based on.
        N(   t	   _freqdist(   R   t   freqdist(    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR   ¢  s    	c         C   s   |  i  S(   s   
        @return: The frequency distribution that this probability
            distribution is based on.
        @rtype: C{FreqDist}
        (   R‡   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRˆ   ­  s    c         C   s   |  i  i | ƒ S(   N(   R‡   R!   (   R   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRf   µ  s    c         C   s   |  i  i ƒ  S(   N(   R‡   R$   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR$   ¸  s    c         C   s   |  i  i ƒ  S(   N(   R‡   R   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR   »  s    c         C   s   d |  i  i ƒ  S(   sa   
        @rtype: C{string}
        @return: A string representation of this C{ProbDist}.
        s!   <MLEProbDist based on %d samples>(   R‡   R   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR\   ¾  s    (	   R`   Ra   Rb   R   Rˆ   Rf   R$   R   R\   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR†   š  s   					t   LidstoneProbDistc           B   sV   e  Z d  Z e Z e d „ Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z RS(   sA  
    The Lidstone estimate for the probability distribution of the
    experiment used to generate a frequency distribution.  The
    C{Lidstone estimate} is paramaterized by a real number M{gamma},
    which typically ranges from 0 to 1.  The X{Lidstone estimate}
    approximates the probability of a sample with count M{c} from an
    experiment with M{N} outcomes and M{B} bins as
    M{(c+gamma)/(N+B*gamma)}.  This is equivalant to adding
    M{gamma} to the count for each bin, and taking the maximum
    likelihood estimate of the resulting frequency distribution.
    c         C   sA  | d j p  | d j o; | i ƒ  d j o( |  i i d  } t d | d ƒ ‚ n | d j	 oQ | | i ƒ  j  o> |  i i d  } t d | d | d d | i ƒ  ƒ ‚ n | |  _ t | ƒ |  _ |  i i ƒ  |  _	 | d j o | i ƒ  } n | |  _
 |  i	 | | |  _ |  i d	 j o d |  _ d
 |  _ n d S(   sÎ  
        Use the Lidstone estimate to create a probability distribution
        for the experiment used to generate C{freqdist}.

        @type freqdist: C{FreqDist}
        @param freqdist: The frequency distribution that the
            probability estimates should be based on.
        @type gamma: C{float}
        @param gamma: A real number used to paramaterize the
            estimate.  The Lidstone estimate is equivalant to adding
            M{gamma} to the count for each bin, and taking the
            maximum likelihood estimate of the resulting frequency
            distribution.
        @type bins: C{int}
        @param bins: The number of sample values that can be generated
            by the experiment that is described by the probability
            distribution.  This value must be correctly set for the
            probabilities of the sample values to sum to one.  If
            C{bins} is not specified, it defaults to C{freqdist.B()}.
        i    iøÿÿÿs   A %s probability distribution s   must have at least one bin.s)   
The number of bins in a %s distribution s&   (%d) must be greater than or equal to
s(   the number of bins in the FreqDist used s   to create it (%d).g        i   N(   R   R   Rd   R`   R/   R   R‡   R    t   _gammaR   t   _binst   _divisor(   R   Rˆ   t   gammaR   t   name(    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR   Ò  s$    -
 			c         C   s   |  i  S(   s   
        @return: The frequency distribution that this probability
            distribution is based on.
        @rtype: C{FreqDist}
        (   R‡   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRˆ      s    c         C   s   |  i  | } | |  i |  i S(   N(   R‡   RŠ   RŒ   (   R   R   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRf     s    c         C   s   |  i  i ƒ  S(   N(   R‡   R$   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR$     s    c         C   s   |  i  i ƒ  S(   N(   R‡   R   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR     s    c         C   s   |  i  |  i } | |  i | S(   N(   RŠ   R‹   R   (   R   t   gb(    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRl     s    c         C   s   d |  i  i ƒ  S(   sa   
        @rtype: C{string}
        @return: A string representation of this C{ProbDist}.
        s&   <LidstoneProbDist based on %d samples>(   R‡   R   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR\     s    (   R`   Ra   Rb   R2   Rn   R   R   Rˆ   Rf   R$   R   Rl   R\   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR‰   Å  s   .					t   LaplaceProbDistc           B   s#   e  Z d  Z e d „ Z d „  Z RS(   s¿  
    The Laplace estimate for the probability distribution of the
    experiment used to generate a frequency distribution.  The
    X{Lidstone estimate} approximates the probability of a sample with
    count M{c} from an experiment with M{N} outcomes and M{B} bins as
    M{(c+1)/(N+B)}.  This is equivalant to adding one to the count for
    each bin, and taking the maximum likelihood estimate of the
    resulting frequency distribution.
    c         C   s   t  i |  | d | ƒ d S(   s‘  
        Use the Laplace estimate to create a probability distribution
        for the experiment used to generate C{freqdist}.

        @type freqdist: C{FreqDist}
        @param freqdist: The frequency distribution that the
            probability estimates should be based on.
        @type bins: C{int}
        @param bins: The number of sample values that can be generated
            by the experiment that is described by the probability
            distribution.  This value must be correctly set for the
            probabilities of the sample values to sum to one.  If
            C{bins} is not specified, it defaults to C{freqdist.B()}.
        i   N(   R‰   R   (   R   Rˆ   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR   +  s    c         C   s   d |  i  i ƒ  S(   sa   
        @rtype: C{string}
        @return: A string representation of this C{ProbDist}.
        s%   <LaplaceProbDist based on %d samples>(   R‡   R   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR\   <  s    (   R`   Ra   Rb   R   R   R\   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR   !  s   	t   ELEProbDistc           B   s#   e  Z d  Z e d „ Z d „  Z RS(   sÚ  
    The expected likelihood estimate for the probability distribution
    of the experiment used to generate a frequency distribution.  The
    X{expected likelihood estimate} approximates the probability of a
    sample with count M{c} from an experiment with M{N} outcomes and
    M{B} bins as M{(c+0.5)/(N+B/2)}.  This is equivalant to adding 0.5
    to the count for each bin, and taking the maximum likelihood
    estimate of the resulting frequency distribution.
    c         C   s   t  i |  | d | ƒ d S(   s  
        Use the expected likelihood estimate to create a probability
        distribution for the experiment used to generate C{freqdist}.

        @type freqdist: C{FreqDist}
        @param freqdist: The frequency distribution that the
            probability estimates should be based on.
        @type bins: C{int}
        @param bins: The number of sample values that can be generated
            by the experiment that is described by the probability
            distribution.  This value must be correctly set for the
            probabilities of the sample values to sum to one.  If
            C{bins} is not specified, it defaults to C{freqdist.B()}.
        g      à?N(   R‰   R   (   R   Rˆ   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR   M  s    c         C   s   d |  i  i ƒ  S(   sa   
        @rtype: C{string}
        @return: A string representation of this C{ProbDist}.
        s!   <ELEProbDist based on %d samples>(   R‡   R   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR\   ^  s    (   R`   Ra   Rb   R   R   R\   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR‘   C  s   	t   HeldoutProbDistc           B   sq   e  Z d  Z e Z e d „ Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z d „  Z d	 „  Z d
 „  Z RS(   s£  
    The heldout estimate for the probability distribution of the
    experiment used to generate two frequency distributions.  These
    two frequency distributions are called the "heldout frequency
    distribution" and the "base frequency distribution."  The
    X{heldout estimate} uses uses the X{heldout frequency
    distribution} to predict the probability of each sample, given its
    frequency in the X{base frequency distribution}.

    In particular, the heldout estimate approximates the probability
    for a sample that occurs M{r} times in the base distribution as
    the average frequency in the heldout distribution of all samples
    that occur M{r} times in the base distribution.

    This average frequency is M{Tr[r]/(Nr[r]*N)}, where:
        - M{Tr[r]} is the total count in the heldout distribution for
          all samples that occur M{r} times in the base
          distribution. 
        - M{Nr[r]} is the number of samples that occur M{r} times in
          the base distribution.
        - M{N} is the number of outcomes recorded by the heldout
          frequency distribution. 

    In order to increase the efficiency of the C{prob} member
    function, M{Tr[r]/(Nr[r]*N)} is precomputed for each value of M{r}
    when the C{HeldoutProbDist} is created.

    @type _estimate: C{list} of C{float}
    @ivar _estimate: A list mapping from M{r}, the number of
        times that a sample occurs in the base distribution, to the
        probability estimate for that sample.  C{_estimate[M{r}]} is
        calculated by finding the average frequency in the heldout
        distribution of all samples that occur M{r} times in the base
        distribution.  In particular, C{_estimate[M{r}]} =
        M{Tr[r]/(Nr[r]*N)}.
    @type _max_r: C{int}
    @ivar _max_r: The maximum number of times that any sample occurs
       in the base distribution.  C{_max_r} is used to decide how
       large C{_estimate} must be.
    c   	      C   s“   | |  _  | |  _ | | i ƒ  |  _ |  i ƒ  } g  } t |  i d ƒ D] } | | i | | ƒ qI ~ } | i ƒ  } |  i | | | ƒ |  _	 d S(   sæ  
        Use the heldout estimate to create a probability distribution
        for the experiment used to generate C{base_fdist} and
        C{heldout_fdist}.

        @type base_fdist: C{FreqDist}
        @param base_fdist: The base frequency distribution.
        @type heldout_fdist: C{FreqDist}
        @param heldout_fdist: The heldout frequency distribution.
        @type bins: C{int}
        @param bins: The number of sample values that can be generated
            by the experiment that is described by the probability
            distribution.  This value must be correctly set for the
            probabilities of the sample values to sum to one.  If
            C{bins} is not specified, it defaults to C{freqdist.B()}.
        i   N(
   t   _base_fdistt   _heldout_fdistR$   t   _max_rt   _calculate_TrR7   R   R   t   _calculate_estimatet	   _estimate(	   R   t
   base_fdistt   heldout_fdistR   t   TrR   R   R   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR     s    		:c         C   sP   d g |  i  d } x5 |  i D]* } |  i | } | | c |  i | 7<q W| S(   sÔ   
        @return: the list M{Tr}, where M{Tr[r]} is the total count in
            C{heldout_fdist} for all samples that occur M{r}
            times in C{base_fdist}.
        @rtype: C{list} of C{float}
        g        i   (   R•   R”   R“   (   R   R›   R   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR–   °  s    
 c         C   sg   g  } xZ t  |  i d ƒ D]E } | | d j o | i t ƒ q | i | | | | | ƒ q W| S(   sÆ  
        @return: the list M{estimate}, where M{estimate[r]} is the
            probability estimate for any sample that occurs M{r} times
            in the base frequency distribution.  In particular,
            M{estimate[r]} is M{Tr[r]/(N[r]*N)}.  In the special case
            that M{N[r]=0}, M{estimate[r]} will never be used; so we
            define M{estimate[r]=None} for those cases.
        @rtype: C{list} of C{float}
        @type Tr: C{list} of C{float}
        @param Tr: the list M{Tr}, where M{Tr[r]} is the total count in
            the heldout distribution for all samples that occur M{r}
            times in base distribution.
        @type Nr: C{list} of C{float}
        @param Nr: The list M{Nr}, where M{Nr[r]} is the number of
            samples that occur M{r} times in the base distribution.
        @type N: C{int}
        @param N: The total number of outcomes recorded by the heldout
            frequency distribution. 
        i   i    (   R7   R•   t   appendR   (   R   R›   R   R   t   estimateR   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR—   ½  s     "!c         C   s   |  i  S(   s’   
        @return: The base frequency distribution that this probability
            distribution is based on.
        @rtype: C{FreqDist}
        (   R“   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR™   ×  s    c         C   s   |  i  S(   s•   
        @return: The heldout frequency distribution that this
            probability distribution is based on.
        @rtype: C{FreqDist}
        (   R”   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRš   ß  s    c         C   s   |  i  i ƒ  S(   N(   R“   R   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR   ç  s    c         C   s   |  i  | } |  i | S(   N(   R“   R˜   (   R   R   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRf   ê  s    c         C   s   |  i  i ƒ  S(   N(   R“   R$   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR$   ï  s    c         C   s   t  ƒ  ‚ d  S(   N(   t   NotImplementedError(   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRl   õ  s    c         C   s&   d } | |  i  i ƒ  |  i i ƒ  f S(   sa   
        @rtype: C{string}
        @return: A string representation of this C{ProbDist}.
        s6   <HeldoutProbDist: %d base samples; %d heldout samples>(   R“   R   R”   (   R   R@   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR\   ø  s    (   R`   Ra   Rb   R2   Rn   R   R   R–   R—   R™   Rš   R   Rf   R$   Rl   R\   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR’   e  s   (!								t   CrossValidationProbDistc           B   sJ   e  Z d  Z e Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(   sB  
    The cross-validation estimate for the probability distribution of
    the experiment used to generate a set of frequency distribution.
    The X{cross-validation estimate} for the probability of a sample
    is found by averaging the held-out estimates for the sample in
    each pair of frequency distributions.
    c         C   sk   | |  _  g  |  _ xR | D]J } xA | D]9 } | | j	 o& t | | | ƒ } |  i i | ƒ q& q& Wq Wd S(   s¥  
        Use the cross-validation estimate to create a probability
        distribution for the experiment used to generate
        C{freqdists}.

        @type freqdists: C{list} of C{FreqDist}
        @param freqdists: A list of the frequency distributions
            generated by the experiment.
        @type bins: C{int}
        @param bins: The number of sample values that can be generated
            by the experiment that is described by the probability
            distribution.  This value must be correctly set for the
            probabilities of the sample values to sum to one.  If
            C{bins} is not specified, it defaults to C{freqdist.B()}.
        N(   t
   _freqdistst   _heldout_probdistsR’   Rœ   (   R   t	   freqdistsR   t   fdist1t   fdist2t   probdist(    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR   	  s    		  c         C   s   |  i  S(   s”   
        @rtype: C{list} of C{FreqDist}
        @return: The list of frequency distributions that this
            C{ProbDist} is based on.
        (   R    (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR¢   $  s    c         C   s7   t  t g  } |  i D] } | | i ƒ  q ~ g  ƒ ƒ S(   N(   RU   R|   R    R   (   R   R   t   fd(    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR   ,  s    c         C   s>   d } x$ |  i  D] } | | i | ƒ 7} q W| t |  i  ƒ S(   Ng        (   R¡   Rf   R   (   R   R   Rf   t   heldout_probdist(    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRf   0  s
    
 c         C   s   t  ƒ  ‚ d  S(   N(   Rž   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRl   8  s    c         C   s   d t  |  i ƒ S(   sa   
        @rtype: C{string}
        @return: A string representation of this C{ProbDist}.
        s!   <CrossValidationProbDist: %d-way>(   R   R    (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR\   ;  s    (   R`   Ra   Rb   R2   Rn   R   R¢   R   Rf   Rl   R\   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRŸ      s   					t   WittenBellProbDistc           B   sP   e  Z d  Z e d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(   s¬  
    The Witten-Bell estimate of a probability distribution. This distribution
    allocates uniform probability mass to as yet unseen events by using the
    number of events that have only been seen once. The probability mass
    reserved for unseen events is equal to:

        - M{T / (N + T)}

    where M{T} is the number of observed event types and M{N} is the total
    number of observed events. This equates to the maximum likelihood estimate
    of a new type event occuring. The remaining probability mass is discounted
    such that all probability estimates sum to one, yielding:

        - M{p = T / Z (N + T)}, if count = 0
        - M{p = c / (N + T)}, otherwise
    c         C   sÙ   | d j p | | i ƒ  j p
 t d ‚ | d j o | i ƒ  } n | |  _ |  i i ƒ  |  _ | |  i i ƒ  |  _ |  i i ƒ  |  _ |  i d j o d |  i |  _ n( |  i t	 |  i |  i |  i ƒ |  _ d S(   s¦  
        Creates a distribution of Witten-Bell probability estimates.  This
        distribution allocates uniform probability mass to as yet unseen
        events by using the number of events that have only been seen once.
        The probability mass reserved for unseen events is equal to:

            - M{T / (N + T)}

        where M{T} is the number of observed event types and M{N} is the total
        number of observed events. This equates to the maximum likelihood
        estimate of a new type event occuring. The remaining probability mass
        is discounted such that all probability estimates sum to one,
        yielding:

            - M{p = T / Z (N + T)}, if count = 0
            - M{p = c / (N + T)}, otherwise

        The parameters M{T} and M{N} are taken from the C{freqdist} parameter
        (the C{B()} and C{N()} values). The normalising factor M{Z} is
        calculated using these values along with the C{bins} parameter.

        @param freqdist:    The frequency counts upon which to base the
                            estimation.
        @type  freqdist:    C{FreqDist}
        @param bins:        The number of possible event types. This must be
                            at least as large as the number of bins in the
                            C{freqdist}. If C{None}, then it's assumed to be
                            equal to that of the C{freqdist}
        @type  bins:        C{Int}
        s1   Bins parameter must not be less than freqdist.B()i    g      ð?N(
   R   R   Re   R‡   t   _Tt   _ZR   R   t   _P0R    (   R   Rˆ   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR   T  s    #	c         C   sA   |  i  | } | d j o |  i Sn | t |  i |  i ƒ Sd  S(   Ni    (   R‡   R«   R    R   R©   (   R   R   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRf   ‚  s    c         C   s   |  i  i ƒ  S(   N(   R‡   R$   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR$   Š  s    c         C   s   |  i  i ƒ  S(   N(   R‡   R   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR     s    c         C   s   |  i  S(   N(   R‡   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRˆ     s    c         C   s   t  ƒ  ‚ d  S(   N(   Rž   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRl   “  s    c         C   s   d |  i  i ƒ  S(   sa   
        @rtype: C{string}
        @return: A string representation of this C{ProbDist}.
        s(   <WittenBellProbDist based on %d samples>(   R‡   R   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR\   –  s    (   R`   Ra   Rb   R   R   Rf   R$   R   Rˆ   Rl   R\   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR¨   B  s   .					t   GoodTuringProbDistc           B   sP   e  Z d  Z e d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(   sí  
    The Good-Turing estimate of a probability distribution. This method
    calculates the probability mass to assign to events with zero or low
    counts based on the number of events with higher counts. It does so by
    using the smoothed count M{c*}:

        - M{c* = (c + 1) N(c + 1) / N(c)}

    where M{c} is the original count, M{N(i)} is the number of event types
    observed with count M{i}. These smoothed counts are then normalised to
    yield a probability distribution.
    c         C   s]   | d j p | | i ƒ  j p
 t d ‚ | d j o | i ƒ  } n | |  _ | |  _ d S(   s'  
        Creates a Good-Turing probability distribution estimate.  This method
        calculates the probability mass to assign to events with zero or low
        counts based on the number of events with higher counts. It does so by
        using the smoothed count M{c*}:

            - M{c* = (c + 1) N(c + 1) / N(c)}

        where M{c} is the original count, M{N(i)} is the number of event types
        observed with count M{i}. These smoothed counts are then normalised to
        yield a probability distribution.

        The C{bins} parameter allows C{N(0)} to be estimated.

        @param freqdist:    The frequency counts upon which to base the
                            estimation.
        @type  freqdist:    C{FreqDist}
        @param bins:        The number of possible event types. This must be
                            at least as large as the number of bins in the
                            C{freqdist}. If C{None}, then it's taken to be
                            equal to C{freqdist.B()}.
        @type  bins:        C{Int}
        s1   Bins parameter must not be less than freqdist.B()N(   R   R   Re   R‡   R‹   (   R   Rˆ   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR   ­  s    #	c         C   s   |  i  | } |  i  i | |  i ƒ } |  i  i | d |  i ƒ } | d j p |  i  i ƒ  d j o d Sn t | d ƒ | | |  i  i ƒ  S(   Ni   i    g        (   R‡   R   R‹   R   R    (   R   R   R   t   nct   ncn(    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRf   Ì  s    #c         C   s   |  i  i ƒ  S(   N(   R‡   R$   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR$   Ø  s    c         C   s   |  i  i ƒ  S(   N(   R‡   R   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR   Û  s    c         C   s   t  ƒ  ‚ d  S(   N(   Rž   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRl   Þ  s    c         C   s   |  i  S(   N(   R‡   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRˆ   á  s    c         C   s   d |  i  i ƒ  S(   sa   
        @rtype: C{string}
        @return: A string representation of this C{ProbDist}.
        s(   <GoodTuringProbDist based on %d samples>(   R‡   R   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR\   ä  s    (   R`   Ra   Rb   R   R   Rf   R$   R   Rl   Rˆ   R\   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR¬     s   					t   MutableProbDistc           B   sA   e  Z d  Z e d „ Z d „  Z d „  Z d „  Z e d „ Z RS(   sÕ   
    An mutable probdist where the probabilities may be easily modified. This
    simply copies an existing probdist, storing the probability values in a
    mutable dictionary and providing an update method.
    c      
      sí   y d d k  } Wn t j
 o d GHt ƒ  n Xˆ  |  _ t ‡  f d †  t t ˆ  ƒ ƒ Dƒ ƒ |  _ | i t ˆ  ƒ | i	 ƒ |  _
 xY t t ˆ  ƒ ƒ D]E } | o | i ˆ  | ƒ |  i
 | <q— | i ˆ  | ƒ |  i
 | <q— W| |  _ d S(   s  
        Creates the mutable probdist based on the given prob_dist and using
        the list of samples given. These values are stored as log
        probabilities if the store_logs flag is set.

        @param prob_dist: the distribution from which to garner the
            probabilities
        @type prob_dist: ProbDist
        @param samples: the complete set of samples
        @type samples: sequence of any
        @param store_logs: whether to store the probabilities as logarithms
        @type store_logs: bool
        iÿÿÿÿNsF   Error: Please install numpy; for instructions see http://www.nltk.org/c         3   s#   x |  ] } ˆ  | | f Vq Wd  S(   N(    (   RT   RA   (   R   (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pys	   <genexpr>  s    (   t   numpyR.   t   exitRv   R   R7   R   t   _sample_dictt   zerost   float64t   _dataRk   Rf   t   _logs(   R   t	   prob_distR   t
   store_logsR°   RA   (    (   R   s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR   ò  s    	+ c         C   s   |  i  S(   N(   Rv   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR     s    c         C   sS   |  i  i | ƒ } | d  j o, |  i o d |  i | SqO |  i | Sn d Sd  S(   Ni   g        (   R²   R   R   R¶   Rµ   (   R   R   RA   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRf     s    
c         C   sa   |  i  i | ƒ } | d  j o4 |  i o |  i | Sq] t i |  i | d ƒ Sn t d ƒ Sd  S(   Ni   s   -inf(   R²   R   R   R¶   Rµ   Rh   Ri   R    (   R   R   RA   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRk     s    
c         C   s’   |  i  i | ƒ } | d j p t ‚ |  i o5 | o | |  i | <qŽ t i | d ƒ |  i | <n* | o d | |  i | <n | |  i | <d S(   sE  
        Update the probability for the given sample. This may cause the object
        to stop being the valid probability distribution - the user must
        ensure that they update the sample probabilities such that all samples
        have probabilities between 0 and 1 and that all probabilities sum to
        one.

        @param sample: the sample for which to update the probability
        @type sample: C{any}
        @param prob: the new probability
        @type prob: C{float}
        @param log: is the probability already logged
        @type log: C{bool}
        i   N(   R²   R   R   Re   R¶   Rµ   Rh   Ri   (   R   R   Rf   Ri   RA   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyt   update)  s    
(	   R`   Ra   Rb   R4   R   R   Rf   Rk   R¹   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR¯   ë  s   			c            sU   t  ˆ  t ƒ p t  ˆ t ƒ o t d ƒ ‚ n t ‡ ‡  f d †  ˆ i ƒ  Dƒ ƒ S(   Ns   expected a ProbDist.c         3   s;   x4 |  ]- } ˆ  i  | ƒ t i ˆ i  | ƒ d  ƒ Vq Wd S(   i   N(   Rf   Rh   Ri   (   RT   R@   (   t   actual_pdistt
   test_pdist(    s&   /p/zhu/06/nlp/nltk/nltk/probability.pys	   <genexpr>J  s   	(   RP   Rc   R/   R|   R   (   R»   Rº   (    (   R»   Rº   s&   /p/zhu/06/nlp/nltk/nltk/probability.pyt   log_likelihoodE  s
    c      
   C   sf   g  } |  i  ƒ  D] } | |  i | ƒ q ~ } t g  } | D] } | | t i | d ƒ q> ~ ƒ S(   Ni   (   R   Rf   R|   Rh   Ri   (   t   pdistR   R@   t   probsR?   Rj   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyt   entropyM  s    0t   ConditionalFreqDistc           B   s˜   e  Z d  Z e d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   sø  
    A collection of frequency distributions for a single experiment
    run under different conditions.  Conditional frequency
    distributions are used to record the number of times each sample
    occurred, given the condition under which the experiment was run.
    For example, a conditional frequency distribution could be used to
    record the frequency of each word (type) in a document, given its
    length.  Formally, a conditional frequency distribution can be
    defined as a function that maps from each condition to the
    C{FreqDist} for the experiment under that condition.

    The frequency distribution for each condition is accessed using
    the indexing operator:

        >>> cfdist[3]
        <FreqDist with 73 outcomes>
        >>> cfdist[3].freq('the')
        0.4
        >>> cfdist[3]['dog']
        2

    When the indexing operator is used to access the frequency
    distribution for a condition that has not been accessed before,
    C{ConditionalFreqDist} creates a new empty C{FreqDist} for that
    condition.

    Conditional frequency distributions are typically constructed by
    repeatedly running an experiment under a variety of conditions,
    and incrementing the sample outcome counts for the appropriate
    conditions.  For example, the following code will produce a
    conditional frequency distribution that encodes how often each
    word type occurs, given the length of that word type:

        >>> cfdist = ConditionalFreqDist()
        >>> for word in tokenize.whitespace(sent):
        ...     condition = len(word)
        ...     cfdist[condition].inc(word)

    An equivalent way to do this is with the initializer:

        >>> cfdist = ConditionalFreqDist((len(word), word) for word in tokenize.whitespace(sent))

    c         C   s@   h  |  _  | o, x) | D] \ } } |  | i | ƒ q Wn d S(   sN  
        Construct a new empty conditional frequency distribution.  In
        particular, the count for every sample, under every condition,
        is zero.

        @param cond_samples: The samples to initialize the conditional frequency distribution with
        @type cond_samples: Sequence of (condition, sample) tuples
        N(   t   _fdistsR
   (   R   t   cond_samplest   condR   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR     s
    		 c         C   s/   | |  i  j o t ƒ  |  i  | <n |  i  | S(   s¡  
        Return the frequency distribution that encodes the frequency
        of each sample outcome, given that the experiment was run
        under the given condition.  If the frequency distribution for
        the given condition has not been accessed before, then this
        will create a new empty C{FreqDist} for that condition.

        @return: The frequency distribution that encodes the frequency
            of each sample outcome, given that the experiment was run
            under the given condition.
        @rtype: C{FreqDist}

        @param condition: The condition under which the experiment was
            run.
        @type condition: any
        (   RÁ   R   (   R   t	   condition(    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR_     s    c         C   s   t  |  i i ƒ  ƒ S(   sg  
        @return: A list of the conditions that have been accessed for
            this C{ConditionalFreqDist}.  Use the indexing operator to
            access the frequency distribution for a given condition.
            Note that the frequency distributions for some conditions
            may contain zero sample outcomes.
        @rtype: C{list}
        (   RD   RÁ   R   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyt
   conditions¥  s    	c         C   s   t  |  i ƒ S(   s   
        @return: The number of conditions that have been accessed
            for this C{ConditionalFreqDist}.
        @rtype: C{int}
        (   R   RÁ   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyt   __len__°  s    c         C   s   t  d „  |  i i ƒ  Dƒ ƒ S(   s—   
        @return: The total number of sample outcomes that have been
          recorded by this C{ConditionalFreqDist}.
        @rtype: C{int}
        c         s   s   x |  ] } | i  ƒ  Vq Wd  S(   N(   R   (   RT   t   fdist(    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pys	   <genexpr>¾  s    (   R|   RÁ   RJ   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR   ¸  s    c            sð  y d d k  } Wn t j
 o t d ƒ ‚ n Xt | d t ƒ } t | d ˆ  i ƒ  ƒ } t | d d ƒ } t | d t t ‡  f d	 †  | Dƒ ƒ ƒ ƒ } d
 | j o d | d
 <n x | D]ˆ } | o) t ˆ  | i	 | ƒ ƒ }	 d }
 d } n6 g  } | D] } | ˆ  | | qÿ ~ }	 d }
 d } | | d <| i
 |	 | | Ž q¾ W| i d | ƒ | i t d d ƒ| i t t | ƒ ƒ g  } | D] } | t | ƒ q~ d d ƒ| o | i | ƒ n | i d ƒ | i |
 ƒ | i ƒ  d S(   s¿  
        Plot the given samples from the conditional frequency distribution.
        For a cumulative plot, specify cumulative=True.
        (Requires Matplotlib to be installed.)
        
        @param samples: The samples to plot
        @type samples: C{list}
        @param title: The title for the graph
        @type title: C{str}
        @param conditions: The conditions to plot (default is all)
        @type conditions: C{list}
        iÿÿÿÿNsX   The plot function requires the matplotlib package.See http://matplotlib.sourceforge.net/R%   RÅ   R+   t    R   c         3   s.   x' |  ]  } x ˆ  | D] } | Vq Wq Wd  S(   N(    (   RT   R   R„   (   R   (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pys	   <genexpr>×  s    R)   i   s   Cumulative Countss   lower rightR&   s   upper rightt   labelt   locR'   R(   R*   iZ   R,   (   R-   R.   R/   R1   R2   RÅ   RD   RU   R0   R   R5   t   legendR3   R4   R6   R7   R   R8   R+   R9   R:   R;   (   R   R<   R=   R-   R%   RÅ   R+   R   RÄ   R>   R:   t
   legend_locR   R   R?   R@   (    (   R   s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR5   À  s<    	% 
)
Cc      	      s&  t  | d t ƒ } t  | d ˆ  i ƒ  ƒ } t  | d t t ‡  f d †  | Dƒ ƒ ƒ ƒ } t d „  | Dƒ ƒ } d | Gx | D] } d t | ƒ Gq} WHx‰ | D] } d | t | ƒ f G| o t ˆ  | i | ƒ ƒ }	 n* g  }
 | D] } |
 ˆ  | | qæ ~
 }	 x |	 D] } d	 | GqWHq Wd
 S(   s\  
        Tabulate the given samples from the conditional frequency distribution.
        
        @param samples: The samples to plot
        @type samples: C{list}
        @param title: The title for the graph
        @type title: C{str}
        @param conditions: The conditions to plot (default is all)
        @type conditions: C{list}
        R%   RÅ   R   c         3   s.   x' |  ]  } x ˆ  | D] } | Vq Wq Wd  S(   N(    (   RT   R   R„   (   R   (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pys	   <genexpr>   s    c         s   s%   x |  ] } t  t | ƒ ƒ Vq Wd  S(   N(   R   R8   (   RT   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pys	   <genexpr>  s    t    s   %4ss   %*ss   %4dN(	   R1   R2   RÅ   RD   RU   R$   R8   R0   R   (   R   R<   R=   R%   RÅ   R   t   condition_sizeR@   R   R>   R   R   t   f(    (   R   s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRB   ñ  s(    	%  ) c            sT   t  ˆ t ƒ p t Sn ˆ  i ƒ  ˆ i ƒ  j o# t ‡ ‡  f d †  ˆ  i ƒ  Dƒ ƒ S(   Nc         3   s'   x  |  ] } ˆ | ˆ  | j Vq Wd  S(   N(    (   RT   R   (   RQ   R   (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pys	   <genexpr>  s    (   RP   RÀ   R2   RÅ   RW   (   R   RQ   (    (   R   RQ   s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRR     s    c         C   s   |  | j S(   N(    (   R   RQ   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRS     s    c            s]   t  ˆ t ƒ p t Sn t ˆ  i ƒ  ƒ i ˆ i ƒ  ƒ o# t ‡ ‡  f d †  ˆ  i ƒ  Dƒ ƒ S(   Nc         3   s'   x  |  ] } ˆ | ˆ  | j Vq Wd  S(   N(    (   RT   R   (   RQ   R   (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pys	   <genexpr>  s    (   RP   RÀ   R2   RU   RÅ   RV   RW   (   R   RQ   (    (   R   RQ   s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRX     s    "c         C   s/   t  | t ƒ p t Sn |  | j o
 |  | j S(   N(   RP   RÀ   R2   (   R   RQ   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRY     s    c         C   s"   t  | t ƒ p t Sn | |  j S(   N(   RP   RÀ   R2   (   R   RQ   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRZ     s    c         C   s"   t  | t ƒ p t Sn | |  j  S(   N(   RP   RÀ   R2   (   R   RQ   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR[   "  s    c         C   s   d |  i  ƒ  S(   sx   
        @return: A string representation of this
            C{ConditionalProbDist}.
        @rtype: C{string}
        s(   <ConditionalProbDist with %d conditions>(   RÆ   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR\   &  s    c         C   s   t  |  i ƒ } d | S(   sx   
        @return: A string representation of this
            C{ConditionalFreqDist}.
        @rtype: C{string}
        s(   <ConditionalFreqDist with %d conditions>(   R   RÁ   (   R   t   n(    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR\   /  s    (   R`   Ra   Rb   R   R   R_   RÅ   RÆ   R   R5   RB   RR   RS   RX   RY   RZ   R[   R\   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRÀ   U  s    +					1	!								t   ConditionalProbDistIc           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   sn  
    A collection of probability distributions for a single experiment
    run under different conditions.  Conditional probability
    distributions are used to estimate the likelihood of each sample,
    given the condition under which the experiment was run.  For
    example, a conditional probability distribution could be used to
    estimate the probability of each word type in a document, given
    the length of the word type.  Formally, a conditional probability
    distribution can be defined as a function that maps from each
    condition to the C{ProbDist} for the experiment under that
    condition.
    c         C   s   t  d ‚ d  S(   Ns$   ConditionalProbDistI is an interface(   Re   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR   E  s    c         C   s
   t  ‚ d S(   s  
        @return: The probability distribution for the experiment run
            under the given condition.
        @rtype: C{ProbDistI}
        @param condition: The condition whose probability distribution
            should be returned.
        @type condition: any
        N(   Re   (   R   RÄ   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR_   H  s    	c         C   s
   t  ‚ d S(   s‹   
        @return: The number of conditions that are represented by
            this C{ConditionalProbDist}.
        @rtype: C{int}
        N(   Re   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRÆ   S  s    c         C   s
   t  ‚ d S(   sñ   
        @return: A list of the conditions that are represented by
            this C{ConditionalProbDist}.  Use the indexing operator to
            access the probability distribution for a given condition.
        @rtype: C{list}
        N(   Re   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRÅ   [  s    (   R`   Ra   Rb   R   R_   RÆ   RÅ   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRÑ   8  s
   			t   ConditionalProbDistc           B   s>   e  Z d  Z e d „ Z d „  Z d „  Z d „  Z d „  Z RS(   sË  
    A conditional probability distribution modelling the experiments
    that were used to generate a conditional frequency distribution.
    A C{ConditoinalProbDist} is constructed from a
    C{ConditionalFreqDist} and a X{C{ProbDist} factory}:

      - The B{C{ConditionalFreqDist}} specifies the frequency
        distribution for each condition.
      - The B{C{ProbDist} factory} is a function that takes a
        condition's frequency distribution, and returns its
        probability distribution.  A C{ProbDist} class's name (such as
        C{MLEProbDist} or C{HeldoutProbDist}) can be used to specify
        that class's constructor.

    The first argument to the C{ProbDist} factory is the frequency
    distribution that it should model; and the remaining arguments are
    specified by the C{factory_args} parameter to the
    C{ConditionalProbDist} constructor.  For example, the following
    code constructs a C{ConditionalProbDist}, where the probability
    distribution for each condition is an C{ELEProbDist} with 10 bins:

        >>> cpdist = ConditionalProbDist(cfdist, ELEProbDist, 10)
        >>> print cpdist['run'].max()
        'NN'
        >>> print cpdist['run'].prob('NN')
        0.0813
    c         G   s‰   | |  _  | |  _ | |  _ | |  _ h  |  _ xU | i ƒ  D]G } | o | | | | | Œ } n | | | | Œ } | |  i | <q: Wd S(   sâ  
        Construct a new conditional probability distribution, based on
        the given conditional frequency distribution and C{ProbDist}
        factory.

        @type cfdist: L{ConditionalFreqDist}
        @param cfdist: The C{ConditionalFreqDist} specifying the
            frequency distribution for each condition.
        @type probdist_factory: C{class} or C{function}
        @param probdist_factory: The function or class that maps
            a condition's frequency distribution to its probability
            distribution.  The function is called with the frequency
            distribution as its first argument, the condition as its
            second argument (only if C{supply_condition=True}), and
            C{factory_args} as its remaining arguments.
        @type supply_condition: C{bool}
        @param supply_condition: If true, then pass the condition as
            the second argument to C{probdist_factory}.
        @type factory_args: (any)
        @param factory_args: Extra arguments for C{probdist_factory}.
            These arguments are usually used to specify extra
            properties for the probability distributions of individual
            conditions, such as the number of bins they contain.
        N(   t   _probdist_factoryt   _cfdistt   _supply_conditiont   _factory_argst   _pdistsRÅ   (   R   t   cfdistt   probdist_factoryt   supply_conditiont   factory_argsR   R½   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR   …  s    					 c         C   s   | |  i  j S(   N(   R×   (   R   RÄ   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyt   __contains__¬  s    c         C   sD   | |  i  j o) |  i t ƒ  |  i Œ } | |  i  | <n |  i  | S(   N(   R×   RÓ   R   RÖ   (   R   RÄ   R½   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR_   ¯  s    c         C   s   |  i  i ƒ  S(   N(   R×   R   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRÅ   ¹  s    c         C   s   t  |  i ƒ S(   N(   R   R×   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRÆ   ¼  s    (	   R`   Ra   Rb   R2   R   RÜ   R_   RÅ   RÆ   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRÒ   i  s   &		
	t   DictionaryConditionalProbDistc           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   s‡   
    An alternative ConditionalProbDist that simply wraps a dictionary of
    ProbDists rather than creating these from FreqDists.
    c         C   s   | |  _  d S(   s¥   
        @param probdist_dict: a dictionary containing the probdists indexed
            by the conditions
        @type probdist_dict: dict any -> probdist
        N(   t   _dict(   R   t   probdist_dict(    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR   Å  s    c         C   s   |  i  | S(   N(   RÞ   (   R   RÄ   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR_   Í  s    c         C   s   |  i  i ƒ  S(   N(   RÞ   R   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRÅ   Ò  s    (   R`   Ra   Rb   R   R_   RÅ   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRÝ   ¿  s   		g ÂëþKH´9i   c         C   si   |  | t  j  o | Sn | |  t  j  o |  Sn t |  | ƒ } | t i d |  | d | | d ƒ S(   s  
    Given two numbers C{logx}=M{log(x)} and C{logy}=M{log(y)}, return
    M{log(x+y)}.  Conceptually, this is the same as returning
    M{log(2**(C{logx})+2**(C{logy}))}, but the actual implementation
    avoids overflow errors that could result from direct computation.
    i   (   t   _ADD_LOGS_MAX_DIFFt   minRh   Ri   (   t   logxt   logyt   base(    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyt   add_logsÝ  s    c         C   s7   t  |  ƒ d j o t Sn t t |  d |  d ƒ Sd  S(   Ni    i   (   R   Rg   t   reduceRå   (   t   logs(    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR{   ë  s    t   ProbabilisticMixInc           B   s;   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   sÂ  
    A mix-in class to associate probabilities with other classes
    (trees, rules, etc.).  To use the C{ProbabilisticMixIn} class,
    define a new class that derives from an existing class and from
    ProbabilisticMixIn.  You will need to define a new constructor for
    the new class, which explicitly calls the constructors of both its
    parent classes.  For example:

        >>> class A:
        ...     def __init__(self, x, y): self.data = (x,y)
        ... 
        >>> class ProbabilisticA(A, ProbabilisticMixIn):
        ...     def __init__(self, x, y, **prob_kwarg):
        ...         A.__init__(self, x, y)
        ...         ProbabilisticMixIn.__init__(self, **prob_kwarg)

    See the documentation for the ProbabilisticMixIn
    L{constructor<__init__>} for information about the arguments it
    expects.

    You should generally also redefine the string representation
    methods, the comparison methods, and the hashing method.
    c         K   s{   d | j o5 d | j o t  d ƒ ‚ qw t i |  | d ƒ n6 d | j o t i |  | d ƒ n d |  _ |  _ d S(   sª  
        Initialize this object's probability.  This initializer should
        be called by subclass constructors.  C{prob} should generally be
        the first argument for those constructors.

        @kwparam prob: The probability associated with the object.
        @type prob: C{float}
        @kwparam logprob: The log of the probability associated with
            the object.
        @type logprob: C{float}
        Rf   Rk   s.   Must specify either prob or logprob (not both)N(   t	   TypeErrorRè   t   set_probt   set_logprobR   t   _ProbabilisticMixIn__probt   _ProbabilisticMixIn__logprob(   R   R=   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR     s    c         C   s   | |  _  d |  _ d S(   s“   
        Set the probability associated with this object to C{prob}.
        @param prob: The new probability
        @type prob: C{float}
        N(   Rì   R   Rí   (   R   Rf   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRê   &  s    	c         C   s   t  |  _ d |  _ d S(   sÿ   
        Set the log probability associated with this object to
        C{logprob}.  I.e., set the probability associated with this
        object to C{2**(logprob)}.
        @param logprob: The new log probability
        @type logprob: C{float}
        N(   Rf   Rí   R   Rì   (   R   Rk   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRë   /  s    	c         C   sC   |  i  t j o, |  i t j o t Sn d |  i |  _  n |  i  S(   s`   
        @return: The probability associated with this object.
        @rtype: C{float}
        i   (   Rì   R   Rí   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRf   :  s    c         C   sK   |  i  t j o4 |  i t j o t Sn t i |  i d ƒ |  _  n |  i  S(   s‚   
        @return: C{log(p)}, where C{p} is the probability associated
        with this object.

        @rtype: C{float}
        i   (   Rí   R   Rì   Rh   Ri   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRk   D  s    (   R`   Ra   Rb   R   Rê   Rë   Rf   Rk   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRè   ÷  s   					
t   ImmutableProbabilisticMixInc           B   s   e  Z d  „  Z d „  Z RS(   c         C   s   t  d |  i i ‚ d  S(   Ns   %s is immutable(   R/   Rd   R`   (   R   Rf   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRê   Q  s    c         C   s   t  d |  i i ‚ d  S(   Ns   %s is immutable(   R/   Rd   R`   (   R   Rf   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRë   S  s    (   R`   Ra   Rê   Rë   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyRî   P  s   	c         C   s,   | |  j o |  | } |  | =n | } | S(   N(    (   R=   RE   t   defaultt   arg(    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR1   X  s
    
c         C   s{   d d k  } d d k l } t ƒ  } xO t | ƒ D]A } | i d d |  d ƒ | i d |  d ƒ } | i | ƒ q2 W| S(   sÑ   
    Create a new frequency distribution, with random samples.  The
    samples are numbers from 1 to C{numsamples}, and are generated by
    summing two numbers, each of which has a uniform distribution.
    iÿÿÿÿN(   t   sqrti   i   i    (   Rm   Rh   Rñ   R   R7   t   randintR
   (   t
   numsamplest   numoutcomesRm   Rñ   RÇ   R   t   y(    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyt   _create_rand_fdistd  s    	 c         C   sl   t  ƒ  } xV t d d |  d d ƒ D]9 } x0 t d |  d d ƒ D] } | i | | ƒ qC Wq% Wt | ƒ S(   so   
    Return the true probability distribution for the experiment
    C{_create_rand_fdist(numsamples, x)}.
    i   i   i    (   R   R7   R
   R†   (   Ró   RÇ   R   Rõ   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyt   _create_sum_pdists  s    	  i   iô  c            sÕ  t  |  | ƒ } t  |  | ƒ } t  |  | ƒ } t | ƒ t | d |  ƒ t | | |  ƒ t | | |  ƒ t | | | g |  ƒ t |  ƒ g } g  } xe t d |  d ƒ D]P } | i t | | i	 | ƒ g g  } | D] ‰  | ˆ  i
 | ƒ qÍ ~ ƒ ƒ q¡ Wd |  |  | f GHd t | ƒ d GHd d t | ƒ d d	 }	 |	 t d
 „  | d  Dƒ ƒ GHd t | ƒ d GHd d t | ƒ d d }	 x | D] }
 |	 |
 GHq‰Wt | Œ  } d „  } g  } | d D] }
 | | |
 ƒ qÀ~ } d t | ƒ d GHd d t | ƒ d }	 |	 t | ƒ GHd  t | ƒ d GHt t | ƒ ƒ d j  o1 d Gt | ƒ GHd Gt | ƒ GHd Gt | ƒ GHn Hd GHxS | D]K ‰  t ‡  f d †  t d ƒ Dƒ ƒ } d ˆ  i i d  t | ƒ d  f GHqWHd S(!   sQ  
    A demonstration of frequency distributions and probability
    distributions.  This demonstration creates three frequency
    distributions with, and uses them to sample a random process with
    C{numsamples} samples.  Each frequency distribution is sampled
    C{numoutcomes} times.  These three frequency distributions are
    then used to build six probability distributions.  Finally, the
    probability estimates of these distributions are compared to the
    actual probability of each sample.

    @type numsamples: C{int}
    @param numsamples: The number of samples to use in each demo
        frequency distributions.
    @type numoutcomes: C{int}
    @param numoutcomes: The total number of outcomes for each
        demo frequency distribution.  These outcomes are divided into
        C{numsamples} bins.
    @rtype: C{None}
    g      à?i   s=   %d samples (1-%d); %d outcomes were sampled for each FreqDistt   =i	   i   s         FreqDist s   %8s s	   |  Actualc         s   s!   x |  ] } | d  d !Vq Wd S(   i   i	   N(    (   RT   R½   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pys	   <genexpr>­  s    iÿÿÿÿt   -s   %3d   %8.6f s   %8.6f s   | %8.6fc         S   s   t  d „  |  d ƒ S(   Nc         S   s   |  | S(    (    (   R   Rõ   (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyt   <lambda>µ  s    i    (   Ræ   (   t   lst(    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pyR|   µ  s    s   Total iF   s	     fdist1:s	     fdist2:s	     fdist3:s   Generating:c         3   s   x |  ] } ˆ  i  ƒ  Vq Wd  S(   N(   Rr   (   RT   RA   (   R½   (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pys	   <genexpr>Å  s    iˆ  s   %20s %si   i7   Ns	   =========s	   ---------s	   ---------s	   =========(   Rö   R†   R‰   R’   RŸ   R÷   R7   Rœ   t   tupleR!   Rf   R   t   zipR8   R   Rd   R`   (   Ró   Rô   R£   R¤   t   fdist3t   pdistst   valsRÐ   R   t	   FORMATSTRt   valt   zvalsR|   R?   t   sumsRÇ   (    (   R½   s&   /p/zhu/06/nlp/nltk/nltk/probability.pyt   demo~  sT    	 3 	+ "'t   __main__i
   i   iˆ  Rå   R¼   R{   R¿   (*   R    Rg   Rh   Rm   Ro   t   operatorR    t	   itertoolsR   R   R   t   objectRc   Rs   Rw   R†   R‰   R   R‘   R’   RŸ   R¨   R¬   R¯   R¼   R¿   RÀ   RÑ   RÒ   RÝ   Ri   Rà   Rå   R{   Rè   Rî   R1   Rö   R÷   R  R`   t   __all__(    (    (    s&   /p/zhu/06/nlp/nltk/nltk/probability.pys   <module>   sZ   ÿ §c?+\""›B[NZ		ã1V		Y			K				