łň
4ŇÇIc           @   si   d  Z  d Z d d k Z d d k Z d d k Td e f d     YZ d   Z e d j o e   n d S(	   sŔ  Porter Stemming Algorithm

This is the Porter stemming algorithm, ported to Python from the
version coded up in ANSI C by the author. It follows the algorithm
presented in

Porter, M. "An algorithm for suffix stripping." Program 14.3 (1980): 130-137.

only differing from it at the points maked --DEPARTURE-- and --NEW--
below.

For a more faithful version of the Porter algorithm, see

    http://www.tartarus.org/~martin/PorterStemmer/

Later additions:

   June 2000
   
   The 'l' of the 'logi' -> 'log' rule is put with the stem, so that
   short stems like 'geo' 'theo' etc work like 'archaeo' 'philo' etc.

   This follows a suggestion of Barry Wilkins, reasearch student at
   Birmingham.


   February 2000

   the cvc test for not dropping final -e now looks after vc at the
   beginning of a word, so are, eve, ice, ore, use keep final -e. In this
   test c is any consonant, including w, x and y. This extension was
   suggested by Chris Emerson.

   -fully    -> -ful   treated like  -fulness -> -ful, and
   -tionally -> -tion  treated like  -tional  -> -tion

   both in Step 2. These were suggested by Hiranmay Ghosh, of New Delhi.

   Invariants proceed, succeed, exceed. Also suggested by Hiranmay Ghosh.

Additional modifications were made to incorperate this module into
nltk.  All such modifications are marked with "--NLTK--".  The nltk
version of this module is maintained by the NLTK developers, and is
available from <http://nltk.sourceforge.net>
t	   plaintexti˙˙˙˙N(   t   *t   PorterStemmerc           B   sż   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   Z d   Z d   Z d   Z d   Z d e d  Z d   Z d   Z d   Z RS(   st  
    A word stemmer based on the Porter stemming algorithm.
    
        Porter, M. "An algorithm for suffix stripping."
        Program 14.3 (1980): 130-137.

    A few minor modifications have been made to Porter's basic
    algorithm.  See the source code of this module for more
    information.

    The Porter Stemmer requires that all tokens have string types.
    c         C   s  d |  _  d |  _ d |  _ d |  _ h  d d g d <d g d <d g d <d	 g d
 <d g d <d d g d <d d g d <d d g d <d g d <d g d <d g d <d g d <} h  |  _ x6 | i   D]( } x | | D] } | |  i | <qí WqÜ Wd S(   sÔ  The main part of the stemming algorithm starts here.
        b is a buffer holding a word to be stemmed. The letters are in b[k0],
        b[k0+1] ... ending at b[k]. In fact k0 = 0 in this demo program. k is
        readjusted downwards as the stemming progresses. Zero termination is
        not in fact used in the algorithm.

        Note that only lower case sequences are stemmed. Forcing to lower case
        should be done before stem(...) is called.
        t    i    t   skyt   skiest   dyingt   diet   lyingt   liet   tyingt   tiet   newst   inningst   inningt   outingst   outingt   canningst   canningt   howet   proceedt   exceedt   succeedN(   t   bt   kt   k0t   jt   poolt   keys(   t   selft   irregular_formst   keyt   val(    (    s&   /p/zhu/06/nlp/nltk/nltk/stem/porter.pyt   __init__g   s.    					  c         C   s˛   |  i  | d j pP |  i  | d j p< |  i  | d j p( |  i  | d j p |  i  | d j o d Sn |  i  | d j o. | |  i j o d SqŽ |  i | d  Sn d S(	   s(   cons(i) is TRUE <=> b[i] is a consonant.t   at   et   it   ot   ui    t   yi   (   R   R   t   cons(   R   R$   (    (    s&   /p/zhu/06/nlp/nltk/nltk/stem/porter.pyR(      s    dc         C   sř   d } |  i  } x: | |  i j o | Sn |  i |  p Pn | d } q | d } x x: | |  i j o | Sn |  i |  o Pn | d } q\ | d } | d } x: | |  i j o | Sn |  i |  p Pn | d } q­ | d } qY d S(   sN  m() measures the number of consonant sequences between k0 and j.
        if c is a consonant sequence and v a vowel sequence, and <..>
        indicates arbitrary presence,
        
           <c><v>       gives 0
           <c>vc<v>     gives 1
           <c>vcvc<v>   gives 2
           <c>vcvcvc<v> gives 3
           ....
        i    i   N(   R   R   R(   (   R   t   nR$   (    (    s&   /p/zhu/06/nlp/nltk/nltk/stem/porter.pyt   mŞ   s2    	


c         C   s@   x9 t  |  i |  i d  D] } |  i |  p d Sq q Wd S(   s2   vowelinstem() is TRUE <=> k0,...j contains a voweli   i    (   t   rangeR   R   R(   (   R   R$   (    (    s&   /p/zhu/06/nlp/nltk/nltk/stem/porter.pyt   vowelinstemĎ   s
     c         C   sP   | |  i  d j  o d Sn |  i | |  i | d j o d Sn |  i |  S(   s:   doublec(j) is TRUE <=> j,(j-1) contain a double consonant.i   i    (   R   R   R(   (   R   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/stem/porter.pyt   doublecÖ   s
    c         C   sĆ   | d j o d Sn | d j o" |  i  d  o |  i  d  Sn |  i  |  p) |  i  | d  p |  i  | d  o d Sn |  i | } | d j p | d j p | d j o d Sn d S(   s˘  cvc(i) is TRUE <=>

        a) ( --NEW--) i == 1, and p[0] p[1] is vowel consonant, or

        b) p[i - 2], p[i - 1], p[i] has the form consonant -
           vowel - consonant and also if the second c is not w, x or y. this
           is used when trying to restore an e at the end of a short word.
           e.g.

               cav(e), lov(e), hop(e), crim(e), but
               snow, box, tray.        
        i    i   i   t   wt   xR'   (   R(   R   (   R   R$   t   ch(    (    s&   /p/zhu/06/nlp/nltk/nltk/stem/porter.pyt   cvcŢ   s    /B'c         C   s   t  |  } | | d |  i |  i j o d Sn | |  i |  i d j o d Sn |  i |  i | d |  i d !| j o d Sn |  i | |  _ d S(   s3   ends(s) is TRUE <=> k0,...k ends with the string s.i   i    (   t   lenR   R   R   R   (   R   t   st   length(    (    s&   /p/zhu/06/nlp/nltk/nltk/stem/porter.pyt   endső   s    )c         C   sQ   t  |  } |  i |  i d  | |  i |  i | d |  _ |  i | |  _ d S(   sJ   setto(s) sets (j+1),...k to the characters in the string s, readjusting k.i   N(   R2   R   R   R   (   R   R3   R4   (    (    s&   /p/zhu/06/nlp/nltk/nltk/stem/porter.pyt   setto  s    1c         C   s(   |  i    d j o |  i |  n d S(   s   r(s) is used further down.i    N(   R*   R6   (   R   R3   (    (    s&   /p/zhu/06/nlp/nltk/nltk/stem/porter.pyt   r  s    c         C   s  |  i  |  i d j o |  i d  o |  i d |  _ qś |  i d  o8 |  i d j o |  i d |  _ q˛ |  i d |  _ qś |  i  |  i d d j o |  i d |  _ qś n |  i d  o8 |  i d j o |  i d |  _ q|  i d |  _ n|  i d  o+ |  i   d j o |  i d |  _ qnJ|  i d	  p |  i d
  o)|  i   o|  i |  _ |  i d  o |  i d  q|  i d  o |  i d  q|  i d  o |  i d  q|  i |  i  o_ |  i d |  _ |  i  |  i } | d j p | d j p | d j o |  i d |  _ q~q|  i   d j o$ |  i |  i  o |  i d  qn d S(   sB  step1ab() gets rid of plurals and -ed or -ing. e.g.
        
           caresses  ->  caress
           ponies    ->  poni
           sties     ->  sti
           tie       ->  tie        (--NEW--: see below)
           caress    ->  caress
           cats      ->  cat
        
           feed      ->  feed
           agreed    ->  agree
           disabled  ->  disable
        
           matting   ->  mat
           mating    ->  mate
           meeting   ->  meet
           milling   ->  mill
           messing   ->  mess
        
           meetings  ->  meet
        R3   t   ssesi   t   iesi    i   t   iedt   eedt   edt   ingt   att   atet   blt   blet   izt   izet   lt   zR#   N(	   R   R   R5   R   R*   R,   R6   R-   R1   (   R   R0   (    (    s&   /p/zhu/06/nlp/nltk/nltk/stem/porter.pyt   step1ab  s8    -!!!'&c         C   sh   |  i  d  oT |  i d j oD |  i |  i d  o- |  i |  i  d |  i |  i d |  _ n d S(   s  step1c() turns terminal y to i when there is another vowel in the stem.
        --NEW--: This has been modified from the original Porter algorithm so that y->i
        is only done when y is preceded by a consonant, but not if the stem
        is only a single consonant, i.e.

           (*c and not c) Y -> I

        So 'happy' -> 'happi', but
          'enjoy' -> 'enjoy'  etc

        This is a much better rule. Formerly 'enjoy'->'enjoi' and 'enjoyment'->
        'enjoy'. Step 1c is perhaps done too soon; but with this modification that
        no longer really matters.

        Also, the removal of the vowelinstem(z) condition means that 'spy', 'fly',
        'try' ... stem to 'spi', 'fli', 'tri' and conflate with 'spied', 'tried',
        'flies' ...
        R'   i    i   R$   N(   R5   R   R(   R   R   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/stem/porter.pyt   step1cG  s    7c         C   s  |  i  |  i d d j oF |  i d  o |  i d  q˙|  i d  o |  i d  q˙n|  i  |  i d d j oF |  i d  o |  i d	  q˙|  i d
  o |  i d  q˙n>|  i  |  i d d j o% |  i d  o |  i d  q˙nţ|  i  |  i d d j oë |  i d  o |  i d  q˙|  i d  o2 |  i   d j o |  i d  |  i   qq˙|  i d  o |  i d  q˙|  i d  o |  i d  q˙|  i d  o |  i d  q˙|  i d  o |  i d  q˙nř|  i  |  i d d j og |  i d  o |  i d  q˙|  i d  o |  i d  q˙|  i d  o |  i d  q˙nv|  i  |  i d d  j o |  i d!  o |  i d  q˙|  i d"  o |  i d#  q˙|  i d$  o |  i d  q˙|  i d%  o |  i d  q˙nÓ |  i  |  i d d& j og |  i d'  o |  i d  q˙|  i d(  o |  i d#  q˙|  i d)  o |  i d  q˙nQ |  i  |  i d d* j o5 |  i d+  o! |  i d |  _ |  i d,  q˙n d- S(.   sł   step2() maps double suffices to single ones.
        so -ization ( = -ize plus -ation) maps to -ize etc. note that the
        string before the suffix must give m() > 0.
        i   R"   t   ationalR?   t   tionalt   tiont   ct   encit   encet   ancit   anceR#   t   izerRC   RD   t   bliRA   t   allii    t   alt   fullit   fult   entlit   entt   elit   ouslit   ousR%   t   izationt   ationt   atorR3   t   alismt   ivenesst   ivet   fulnesst   ousnesst   tt   alitit   ivitit   bilitit   gt   logit   ogN(   R   R   R5   R7   R*   R6   t   step2R   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/stem/porter.pyRj   ]  sF    !%!%%!!!!%!!%!!!%!!%c         C   sW  |  i  |  i d j og |  i d  o |  i d  qS|  i d  o |  i d  qS|  i d  o |  i d  qSnÖ |  i  |  i d j o% |  i d	  o |  i d  qSn |  i  |  i d
 j oF |  i d  o |  i d  qS|  i d  o |  i d  qSn= |  i  |  i d j o% |  i d  o |  i d  qSn d S(   sD   step3() dels with -ic-, -full, -ness etc. similar strategy to step2.R#   t   icatet   ict   ativeR   t   alizeRS   R$   t   icitiRD   t   icalRU   R3   t   nessN(   R   R   R5   R7   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/stem/porter.pyt   step3  s    !!%%!%c         C   sy  |  i  |  i d d j o |  i d  o qRd Sn|  i  |  i d d j o0 |  i d  o qR|  i d  o qRd SnŃ|  i  |  i d d j o |  i d	  o qRd Sn|  i  |  i d d
 j o |  i d  o qRd Snc|  i  |  i d d j o0 |  i d  o qR|  i d  o qRd Sn|  i  |  i d d j oX |  i d  o qR|  i d  o qR|  i d  o qR|  i d  o qRd SnĽ|  i  |  i d d j o^ |  i d  o2 |  i  |  i d j p |  i  |  i d j o qR|  i d  o qRd Sn,|  i  |  i d d j o |  i d  o qRd Snő |  i  |  i d d j o0 |  i d  o qR|  i d  o qRd SnŞ |  i  |  i d d j o |  i d  o qRd Sns |  i  |  i d d j o |  i d  o qRd Sn< |  i  |  i d d  j o |  i d!  o qRd Sn d S|  i   d j o |  i |  _ n d S("   s:   step4() takes off -ant, -ence etc., in context <c>vcvc<v>.i   R"   RS   NRK   RO   RM   R#   t   erR$   Rl   RD   t   ablet   ibleR)   t   antt   ementt   mentRW   R%   t   ionR3   Rc   t   out   ismR?   t   itiR&   RZ   t   vR`   RE   RC   (   R   R   R5   R   R*   (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/stem/porter.pyt   step4  s\    Bc         C   sÎ   |  i  |  _ |  i |  i  d j oV |  i   } | d j p% | d j o, |  i |  i  d  o |  i  d |  _  qy n |  i |  i  d j o: |  i |  i   o' |  i   d j o |  i  d |  _  n d S(   sY   step5() removes a final -e if m() > 1, and changes -ll to -l if
        m() > 1.
        R#   i   RD   N(   R   R   R   R*   R1   R-   (   R   R"   (    (    s&   /p/zhu/06/nlp/nltk/nltk/stem/porter.pyt   step5Ę  s    2=i    c         C   sü   | t  j o t |  d } n | |  _ | |  _ | |  _ |  i i |  i |  i |  i d ! o# |  i |  i |  i |  i d !Sn |  i |  i d j o |  i Sn |  i   |  i   |  i	   |  i
   |  i   |  i   |  i |  i |  i d !S(   sń  In stem(p,i,j), p is a char pointer, and the string to be stemmed
        is from p[i] to p[j] inclusive. Typically i is zero and j is the
        offset to the last character of a string, (p[j+1] == ' '). The
        stemmer adjusts the characters p[i] ... p[j] and returns the new
        end-point of the string, k. Stemming never increases word length, so
        i <= k <= j. To turn the stemmer into a module, declare 'stem' as
        extern, and delete the remainder of this file.
        i   (   t   NoneR2   R   R   R   R   t   has_keyRF   RG   Rj   Rr   R~   R   (   R   t   pR$   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/stem/porter.pyt	   stem_wordÖ  s     			'#





c         C   sh   | i    } d } xO t t |   D]; } | | | | j o | | | 7} q% | | | 7} q% W| S(   NR   (   t   lowert   xrangeR2   (   R   t   wordt   stemR   t   retR/   (    (    s&   /p/zhu/06/nlp/nltk/nltk/stem/porter.pyt   adjust_caseý  s     c         C   s5   |  i  | i   d t |  d  } |  i | |  S(   Ni    i   (   R   R   R2   R   (   R   R   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/stem/porter.pyR     s    %c         C   s   d S(   Ns   <PorterStemmer>(    (   R   (    (    s&   /p/zhu/06/nlp/nltk/nltk/stem/porter.pyt   __repr__$  s    (   t   __name__t
   __module__t   __doc__R!   R(   R*   R,   R-   R1   R5   R6   R7   RF   RG   Rj   Rr   R~   R   R   R   R   R   R   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/stem/porter.pyR   V   s(   	8		%							;		,		3	'	!	c    
   
   C   sU  d d k  l }  d d k l } | i   } g  } g  } x[ |  i   d  D]I } x@ |  i |  D]/ \ } } | i |  | i | i |   q_ WqI Wd i |  } t	 i
 d d | d  i   } d i |  }	 t	 i
 d d |	 d  i   }	 d i d	  i d d
  i d d  GH|	 GHd i d	  i d d
  i d d  GH| GHd
 d	 GHd S(   s^   
    A demonstration of the porter stemmer on a sample from
    the Penn Treebank corpus.
    i˙˙˙˙(   t   treebank(   R   i   t    s
   (.{,70})\ss   \1\ns
   -Original-iF   R   t   -s	   -Results-N(   t   nltk.corpusR   t   nltkR   R   t   filest   tagged_wordst   appendt   joint   ret   subt   rstript   centert   replace(
   R   R   t   stemmert   origt   stemmedt   itemR   t   tagt   resultst   original(    (    s&   /p/zhu/06/nlp/nltk/nltk/stem/porter.pyt   demo8  s(      &&t   __main__(	   R   t   __docformat__t   sysR   t   apit   StemmerIR   RŁ   R   (    (    (    s&   /p/zhu/06/nlp/nltk/nltk/stem/porter.pys   <module>I   s   
˙ ă	#