
    }c3                     $    d Z  G d d          ZdS )ax  Porter Stemming Algorithm

This is the Porter stemming algorithm, ported to Python from the
version coded up in ANSI C by the author. It may be be regarded
as canonical, in that it follows the algorithm presented in

Porter, 1980, An algorithm for suffix stripping, Program, Vol. 14,
no. 3, pp 130-137,

only differing from it at the points made --DEPARTURE-- below.

See also https://tartarus.org/martin/PorterStemmer/

The algorithm as described in the paper could be exactly replicated
by adjusting the points of DEPARTURE, but this is barely necessary,
because (a) the points of DEPARTURE are definitely improvements, and
(b) no encoding of the Porter stemmer I have seen is anything like
as exact as this version, even with the points of DEPARTURE!

Release 1: January 2001

:author: Vivake Gupta <v@nano.com>.
:license: Public Domain ("can be used free of charge for any purpose").
c                       e Zd ZddZdedefdZdefdZdefdZdedefd	Zdedefd
Z	de
defdZde
ddfdZde
ddfdZddZddZddZddZddZddZde
dedede
fdZdS )PorterStemmerreturnNc                 >    d| _         d| _        d| _        d| _        dS )a  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.
             N)bkk0jselfs    :lib/python3.11/site-packages/sphinx/util/stemmer/porter.py__init__zPorterStemmer.__init__   s$         ic                 "   | j         |         dk    sD| j         |         dk    s3| j         |         dk    s"| j         |         dk    s| j         |         dk    rdS | j         |         dk    r&|| j        k    rdS |                     |dz
             S dS )	z(cons(i) is TRUE <=> b[i] is a consonant.aer   our   y   )r   r
   consr   r   s     r   r   zPorterStemmer.cons-   s    6!9 	tvayC/ 	46!93C 	fQi3	"&&)s"2	16!9 	.DG| .q IIa!e,,,-qr   c                 <   d}| j         }	 || j        k    r|S |                     |          sn|dz   })|dz   }	 	 || j        k    r|S |                     |          rn|dz   })|dz   }|dz   }	 || j        k    r|S |                     |          sn|dz   })|dz   }d)aF  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
           ....
        r   r   )r
   r   r   )r   nr   s      r   mzPorterStemmer.m9   s     G	46z 99Q<< AA	 E	tv: H99Q<< E AAAAtv: Hyy|| E AA	r   c                 v    t          | j        | j        dz             D ]}|                     |          s dS dS )z2vowelinstem() is TRUE <=> k0,...j contains a vowelr   r   )ranger
   r   r   r   s     r   vowelinstemzPorterStemmer.vowelinstem^   sF    tw
++ 	 	A99Q<< qqqr   r   c                     || j         dz   k     rdS | j        |         | j        |dz
           k    rdS |                     |          S )z:doublec(j) is TRUE <=> j,(j-1) contain a double consonant.r   r   )r
   r   r   )r   r   s     r   doubleczPorterStemmer.doublece   sL    ! 	1F1IA& 	1yy||r   c                     || j         dz   k     sE|                     |          r0|                     |dz
            s|                     |dz
            sdS | j        |         }|dv rdS dS )a)  cvc(i) is TRUE <=> i-2,i-1,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  e.g.

           cav(e), lov(e), hop(e), crim(e), but
           snow, box, tray.
           r   r   )wxr   )r
   r   r   )r   r   chs      r   cvczPorterStemmer.cvcm   s~     ! 	DIIaLL 	DIIa!e4D4D 	))AE""	1VAY  	1qr   sc                    t          |          }||dz
           | j        | j                 k    rdS || j        | j        z
  dz   k    rdS | j        | j        |z
  dz   | j        dz            |k    rdS | j        |z
  | _        dS )z3ends(s) is TRUE <=> k0,...k ends with the string s.r   r   )lenr   r	   r
   r   r   r)   lengths      r   endszPorterStemmer.ends~   s    QVaZ=DF46N* 	1TVdg%)* 	16$&6/A%dfqj01Q6 	1&qr   c                     t          |          }| j        d| j        dz            |z   | j        | j        |z   dz   d         z   | _        | j        |z   | _        dS )zRsetto(s) sets (j+1),...k to the characters in the string s,
        readjusting k.Nr   )r+   r   r   r	   r,   s      r   settozPorterStemmer.setto   sZ     Q!$q(46$&6/A2E2F2F+GG&r   c                 d    |                                  dk    r|                     |           dS dS )zr(s) is used further down.r   N)r   r0   )r   r)   s     r   rzPorterStemmer.r   s3    6688a< 	JJqMMMMM	 	r   c                    | j         | j                 dk    rx|                     d          r| j        dz
  | _        nS|                     d          r|                     d           n(| j         | j        dz
           dk    r| j        dz
  | _        |                     d          r+|                                 dk    r| j        dz
  | _        dS dS |                     d	          s|                     d
          rB|                                 r+| j        | _        |                     d          r|                     d           dS |                     d          r|                     d           dS |                     d          r|                     d           dS |                     | j                  r8| j        dz
  | _        | j         | j                 }|dv r| j        dz   | _        dS dS |                                 dk    r5|                     | j                  r|                     d           dS dS dS dS dS )a  step1ab() gets rid of plurals and -ed or -ing. e.g.

           caresses  ->  caress
           ponies    ->  poni
           ties      ->  ti
           caress    ->  caress
           cats      ->  cat

           feed      ->  feed
           agreed    ->  agree
           disabled  ->  disable

           matting   ->  mat
           mating    ->  mate
           meeting   ->  meet
           milling   ->  mill
           messing   ->  mess

           meetings  ->  meet
        r)   ssesr$   iesr   r   eedr   edingatateblbleizize)lr)   zr   N)	r   r	   r.   r0   r   r    r   r"   r(   )r   r'   s     r   step1abzPorterStemmer.step1ab   sF   * 6$&>S  	$yy   $!5!! $

3
#s* $!99U 	 vvxx!| $!$ $iioo 	 5!1!1 	 t7G7G7I7I 	 VDFyy  

5!!!!!4 
 

5!!!!!4  

5!!!!!df%%  !VDF^( (!VaZDFFF( (&&((a-  DHHTV$4$4  

3	  	  	  	        r   c                     |                      d          rJ|                                 r8| j        d| j                 dz   | j        | j        dz   d         z   | _        dS dS dS )zOstep1c() turns terminal y to i when there is another vowel in
        the stem.r   Nr   r   )r.   r    r   r	   r   s    r   step1czPorterStemmer.step1c   su     IIcNN 	At//11 	AVGTVG_s*TVDFQJKK-@@DFFF	A 	A 	A 	Ar   c                    | j         | j        dz
           dk    rZ|                     d          r|                     d           d+S |                     d          r|                     d           d+S d+S | j         | j        dz
           dk    rZ|                     d          r|                     d	           d+S |                     d
          r|                     d           d+S d+S | j         | j        dz
           dk    r.|                     d          r|                     d           d+S d+S | j         | j        dz
           dk    r|                     d          r|                     d           d+S |                     d          r|                     d           d+S |                     d          r|                     d           d+S |                     d          r|                     d           d+S |                     d          r|                     d           d+S d+S | j         | j        dz
           dk    r|                     d          r|                     d           d+S |                     d          r|                     d           d+S |                     d          r|                     d           d+S d+S | j         | j        dz
           dk    r|                     d          r|                     d           d+S |                     d          r|                     d            d+S |                     d!          r|                     d"           d+S |                     d#          r|                     d           d+S d+S | j         | j        dz
           d$k    r|                     d%          r|                     d           d+S |                     d&          r|                     d            d+S |                     d'          r|                     d           d+S d+S | j         | j        dz
           d(k    r,|                     d)          r|                     d*           d+S d+S d+S ),zstep2() 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.
        r   r   ationalr:   tionaltioncencienceanciancer   izerr>   r?   blir<   allialentlienteliousliousr   izationationatorr)   alismivenessivefulnessfulousnesstalitiivitibilitiglogilogNr   r	   r.   r2   r   s    r   step2zPorterStemmer.step2   sc   
 6$&1*$ 3	yy## u8$$ v VDFQJ3& .	yy   v6"" v VDFQJ3& )	yy   u VDFQJ3& &	yy u 6"" t7## u5!! s7## u VDFQJ3& 	yy## u7## u6"" u VDFQJ3& 	yy!! t9%% u9%% u9%% u VDFQJ3& 		yy!! t7## u8$$ u VDFQJ3& 	yy   u	 	 r   c                 .   | j         | j                 dk    r|                     d          r|                     d           dS |                     d          r|                     d           dS |                     d          r|                     d           dS dS | j         | j                 dk    r.|                     d	          r|                     d           dS dS | j         | j                 d
k    rZ|                     d          r|                     d           dS |                     d          r|                     d           dS dS | j         | j                 dk    r,|                     d          r|                     d           dS dS dS )zLstep3() dels with -ic-, -full, -ness etc. similar strategy
        to step2.r   icateicativer   alizerP   r   icitir?   icalr]   r)   nessNrf   r   s    r   step3zPorterStemmer.step3  s    6$&>S  	yy!! t7## r




7## t VDF^s" 
	yy!! t VDF^s" 	yy   t5!! r




 VDF^s" 	yy   r




	 	 r   c                    | j         | j        dz
           dk    r|                     d          rndS | j         | j        dz
           dk    r0|                     d          rn|                     d          rndS | j         | j        dz
           dk    r|                     d	          rnvdS | j         | j        dz
           d
k    r|                     d          rnDdS | j         | j        dz
           dk    r0|                     d          rn|                     d          rndS | j         | j        dz
           dk    r^|                     d          rn|                     d          rn|                     d          rn|                     d          rndS | j         | j        dz
           dk    r\|                     d          r.| j         | j                 dk    s| j         | j                 dk    rn&|                     d          rndS | j         | j        dz
           dk    r|                     d          rndS | j         | j        dz
           dk    r.|                     d          rn|                     d          rndS | j         | j        dz
           dk    r|                     d          rnfdS | j         | j        dz
           dk    r|                     d          rn5dS | j         | j        dz
           d k    r|                     d!          rndS dS |                                 dk    r| j        | _        dS dS )"z:step4() takes off -ant, -ence etc., in context <c>vcvc<v>.r   r   rP   NrH   rL   rJ   r   err   rj   r?   ableibler   antementmentrR   r   ionr)   r_   ouismr:   itir   rU   vr[   r@   r>   )r   r	   r.   r   r   r   s    r   step4zPorterStemmer.step4  s   6$&1*$ M	yy VDFQJ3& H	yy   6"" VDFQJ3& A	yy VDFQJ3& <	yy VDFQJ3& 7	yy   6"" VDFQJ3& 0	yy 	7## 6"" 5!! VDFQJ3& %	yy TVDF^s%: %)VDF^s%:4  VDFQJ3& 	yy VDFQJ3& 	yy 5!! VDFQJ3& 	yy VDFQJ3& 	yy VDFQJ3& 	yy F6688a< 	VDFFF	 	r   c                    | j         | _        | j        | j                  dk    rL|                                 }|dk    s#|dk    r,|                     | j         dz
            s| j         dz
  | _         | j        | j                  dk    rC|                     | j                   r+|                                 dk    r| j         dz
  | _         dS dS dS dS )zYstep5() removes a final -e if m() > 1, and changes -ll to -l if
        m() > 1.
        r   r   r?   N)r	   r   r   r   r(   r"   )r   r   s     r   step5zPorterStemmer.step5o  s     6$&>S  	$A1u $a $!(<(< $!6$&>S  	 T\\$&%9%9 	 dffhhl 	 VaZDFFF	  	  	  	  	  	 r   pc                    || _         || _        || _        | j        | j        dz   k    r| j         S |                                  |                                  |                                  |                                  |                                  |                                  | j         | j        | j        dz            S )a  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.
        r   )	r   r	   r
   rA   rC   rg   rp   r}   r   )r   r   r   r   s       r   stemzPorterStemmer.stem{  s     6TWq[  	6M 	







vdgdfqj())r   )r   N)__name__
__module____qualname__r   intr   r   r    r"   r(   strr.   r0   r2   rA   rC   rg   rp   r}   r   r    r   r   r   r      s           
c 
c 
 
 
 
#3 # # # #JS         S S    "
c 
c 
 
 
 
!s !t ! ! ! !3 4    
-  -  -  - ^A A A A8 8 8 8v   ,Q Q Q Qf
  
  
  
 *c *c *c *c * * * * * *r   r   N)__doc__r   r   r   r   <module>r      sK    4{* {* {* {* {* {* {* {* {* {*r   