
    [gZ7                     f    S r SSKJr   " S S5      r\" 5       rSS jr\R                  " \S5        g)	a  
Implementation of Porter Stemming Algorithm from
https://tartarus.org/martin/PorterStemmer/python.txt

Note: The Python implementation returns different results than the JS
version:
- Stemming "lay" returns "lai" in Python, but "lay" in JS
- Stemming "try" returns "try" in Python, but "tri" in JS


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 maked --DEPARTURE-- below.

See also http://www.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!

Vivake Gupta (v@nano.com)

Release 1: January 2001

Further adjustments by Santiago Bruno (bananabruno@gmail.com)
to allow word input not restricted to one word per line, leading
to:

release 2: July 2008
    )Pipelinec                   x    \ rS rSrS rS rS rS rS rS r	S r
S	 rS
 rS rS rS rS rS rS rSS jrSrg)PorterStemmer+   c                 <    SU l         SU l        SU l        SU l        g)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.
 r   N)bkk0jselfs    ./usr/lib/python3/dist-packages/lunr/stemmer.py__init__PorterStemmer.__init__,   s          c                 <   U R                   U   S:X  dL  U R                   U   S:X  d9  U R                   U   S:X  d&  U R                   U   S:X  d  U R                   U   S:X  a  gU R                   U   S:X  a)  XR                  :X  a  gU R                  US-
  5      (       + $ g)	z(cons(i) is TRUE <=> b[i] is a consonant.aeiour   y   )r	   r   consr   r   s     r   r   PorterStemmer.cons<   s     FF1IvvayCvvayCvvayCvvayC66!9GG|99QU+++r   c                 l   SnU R                   n X R                  :  a  U$ U R                  U5      (       d  OUS-   nM/  US-   n  X R                  :  a  U$ U R                  U5      (       a  OUS-   nM/  US-   nUS-   n X R                  :  a  U$ U R                  U5      (       d  OUS-   nM/  US-   nMq  )a  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   mPorterStemmer.mM   s     GG66z99Q<<AA  Evv:H99Q<<E  AAAAvv:Hyy||E  AA r   c                     [        U R                  U R                  S-   5       H  nU R                  U5      (       a  M    g   g)z2vowelinstem() is TRUE <=> k0,...j contains a vowelr   r   )ranger   r   r   r   s     r   vowelinstemPorterStemmer.vowelinstemr   s4    tww
+A99Q<< , r   c                     XR                   S-   :  a  gU R                  U   U R                  US-
     :w  a  gU R                  U5      $ )z:doublec(j) is TRUE <=> j,(j-1) contain a double consonant.r   r   )r   r	   r   )r   r   s     r   doublecPorterStemmer.doublecy   s@    !66!9q1u%yy|r   c                     XR                   S-   :  dH  U R                  U5      (       a2  U R                  US-
  5      (       d  U R                  US-
  5      (       d  gU R                  U   nUS:X  d  US:X  d  US:X  a  gg)zcvc(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   cvcPorterStemmer.cvc   sn     199Q<<yyQ99QU##VVAY9c	R3Yr   c                 *   [        U5      nXS-
     U R                  U R                     :w  a  gX R                  U R                  -
  S-   :  a  gU R                  U R                  U-
  S-   U R                  S-    U:w  a  gU R                  U-
  U l        g)z3ends(s) is TRUE <=> k0,...k ends with the string s.r   r   )lenr	   r
   r   r   r   slengths      r   endsPorterStemmer.ends   s    QaZ=DFF466N*VVdgg%)*66$&&6/A%
3q8&r   c                     [        U5      nU R                  SU R                  S-    U-   U R                  U R                  U-   S-   S -   U l        U R                  U-   U l        g)zJsetto(s) sets (j+1),...k to the characters in the string s, readjusting k.Nr   )r1   r	   r   r
   r2   s      r   settoPorterStemmer.setto   sW    Q$&&1*%)DFF466F?Q3F3H,II&r   c                 P    U R                  5       S:  a  U R                  U5        gg)zr(s) is used further down.r   N)r    r8   )r   r3   s     r   rPorterStemmer.r   s    668a<JJqM r   c                    U R                   U R                     S:X  a  U R                  S5      (       a  U R                  S-
  U l        O\U R                  S5      (       a  U R                  S5        O4U R                   U R                  S-
     S:w  a  U R                  S-
  U l        U R                  S5      (       a*  U R	                  5       S:  a  U R                  S-
  U l        ggU R                  S	5      (       d  U R                  S
5      (       Ga]  U R                  5       (       GaF  U R                  U l        U R                  S5      (       a  U R                  S5        gU R                  S5      (       a  U R                  S5        gU R                  S5      (       a  U R                  S5        gU R                  U R                  5      (       aU  U R                  S-
  U l        U R                   U R                     nUS:X  d  US:X  d  US:X  a  U R                  S-   U l        ggU R	                  5       S:X  a3  U R                  U R                  5      (       a  U R                  S5        ggggg)aF  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
r3   ssesr*   iesr   r   eedr   edingatateblbleizizelzr   N)	r	   r
   r5   r8   r    r$   r   r'   r.   )r   r-   s     r   step1abPorterStemmer.step1ab   s   * 66$&&>S yy  !5!!

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

5!4

5!4

5!dff%%!VVDFF^9c	R3Y!VVaZDF .7Q488DFF#3#3

3 $4 8J!1r   c                     U R                  S5      (       aU  U R                  5       (       a?  U R                  SU R                   S-   U R                  U R                  S-   S -   U l        ggg)zGstep1c() turns terminal y to i when there is another vowel in the stem.r   Nr   r   )r5   r$   r	   r
   r   s    r   step1cPorterStemmer.step1c   sW    99S>>d..00VVHdff%+dffTVVaZ\.BBDF 1>r   c                    U R                   U R                  S-
     S:X  aQ  U R                  S5      (       a  U R                  S5        g+U R                  S5      (       a  U R                  S5        g+g+U R                   U R                  S-
     S:X  aQ  U R                  S5      (       a  U R                  S	5        g+U R                  S
5      (       a  U R                  S5        g+g+U R                   U R                  S-
     S:X  a)  U R                  S5      (       a  U R                  S5        g+g+U R                   U R                  S-
     S:X  a  U R                  S5      (       a  U R                  S5        g+U R                  S5      (       a  U R                  S5        g+U R                  S5      (       a  U R                  S5        g+U R                  S5      (       a  U R                  S5        g+U R                  S5      (       a  U R                  S5        g+g+U R                   U R                  S-
     S:X  ay  U R                  S5      (       a  U R                  S5        g+U R                  S5      (       a  U R                  S5        g+U R                  S5      (       a  U R                  S5        g+g+U R                   U R                  S-
     S:X  a  U R                  S5      (       a  U R                  S5        g+U R                  S5      (       a  U R                  S 5        g+U R                  S!5      (       a  U R                  S"5        g+U R                  S#5      (       a  U R                  S5        g+g+U R                   U R                  S-
     S$:X  ay  U R                  S%5      (       a  U R                  S5        g+U R                  S&5      (       a  U R                  S 5        g+U R                  S'5      (       a  U R                  S5        g+g+U R                   U R                  S-
     S(:X  a)  U R                  S)5      (       a  U R                  S*5        g+g+g+),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   ationalrD   tionaltioncencienceanciancer   izerrH   rI   blirF   allialentlienteliousliousr   izationationatorr3   alismivenessivefulnessfulousnesstalitiivitibilitiglogilogNr	   r
   r5   r;   r   s    r   step2PorterStemmer.step2   s   
 66$&&1*$yy##u8$$v %VVDFFQJ3&yy  v6""v #VVDFFQJ3&yy  u !VVDFFQJ3&yyu 6""t7##u5!!s7##u $VVDFFQJ3&yy##u7##u6""u #VVDFFQJ3&yy!!t9%%u9%%u9%%u &VVDFFQJ3&yy!!t7##u8$$u %VVDFFQJ3&yy  u ! 'r   c                 $   U R                   U R                     S:X  ay  U R                  S5      (       a  U R                  S5        gU R                  S5      (       a  U R                  S5        gU R                  S5      (       a  U R                  S5        ggU R                   U R                     S:X  a)  U R                  S	5      (       a  U R                  S5        ggU R                   U R                     S
:X  aQ  U R                  S5      (       a  U R                  S5        gU R                  S5      (       a  U R                  S5        ggU R                   U R                     S:X  a)  U R                  S5      (       a  U R                  S5        ggg)zDstep3() dels with -ic-, -full, -ness etc. similar strategy to step2.r   icateicativer   alizer\   r   icitirI   icalri   r3   nessNrr   r   s    r   step3PorterStemmer.step3  s!   66$&&>S yy!!t7##r
7##t $VVDFF^s"yy!!t "VVDFF^s"yy  t5!!r
 "VVDFF^s"yy  r
 ! #r   c                 b   U R                   U R                  S-
     S:X  a  U R                  S5      (       a  GOQgU R                   U R                  S-
     S:X  a1  U R                  S5      (       a  GOU R                  S5      (       a  GO gU R                   U R                  S-
     S:X  a  U R                  S	5      (       a  GOgU R                   U R                  S-
     S
:X  a  U R                  S5      (       a  GOgU R                   U R                  S-
     S:X  a1  U R                  S5      (       a  GOUU R                  S5      (       a  GO=gU R                   U R                  S-
     S:X  aa  U R                  S5      (       a  GOU R                  S5      (       a  GOU R                  S5      (       a  GOU R                  S5      (       a  GOgU R                   U R                  S-
     S:X  ak  U R                  S5      (       a<  U R                   U R                     S:X  d  U R                   U R                     S:X  a  GOIU R                  S5      (       a  GO1gU R                   U R                  S-
     S:X  a  U R                  S5      (       a  OgU R                   U R                  S-
     S:X  a/  U R                  S5      (       a  OU R                  S5      (       a  OgU R                   U R                  S-
     S:X  a  U R                  S5      (       a  OrgU R                   U R                  S-
     S:X  a  U R                  S5      (       a  O:gU R                   U R                  S-
     S :X  a  U R                  S!5      (       a  OggU R	                  5       S:  a  U R                  U l        gg)"z:step4() takes off -ant, -ence etc., in context <c>vcvc<v>.r   r   r\   NrT   rX   rV   r   err   rw   rI   ableibler   antementmentr^   r   ionr3   rk   ouismrD   itir   ra   vrg   rJ   rH   )r	   r
   r5   r   r    r   s    r   step4PorterStemmer.step40  s   66$&&1*$yyVVDFFQJ3&yy  6""VVDFFQJ3&yyVVDFFQJ3&yyVVDFFQJ3&yy  6""VVDFFQJ3&yy7##6""5!!VVDFFQJ3&yyTVVDFF^s%:dffTVVnPS>S4 VVDFFQJ3&yyVVDFFQJ3&yy5!!VVDFFQJ3&yyVVDFFQJ3&yyVVDFFQJ3&yy668a<VVDF r   c                    U R                   U l        U R                  U R                      S:X  aS  U R                  5       nUS:  d)  US:X  a7  U R	                  U R                   S-
  5      (       d  U R                   S-
  U l         U R                  U R                      S:X  aK  U R                  U R                   5      (       a*  U R                  5       S:  a  U R                   S-
  U l         gggg)zIstep5() removes a final -e if m() > 1, and changes -ll to -l if
m() > 1.
r   r   rI   N)r
   r   r	   r    r.   r'   )r   r   s     r   step5PorterStemmer.step5  s     66$&&>S A1ua!(<(<!66$&&>S T\\$&&%9%9dffhlVVaZDF ?K%9 r   Nc                    Sn[        U5      S-
  nXl        X@l        X0l        U R                  U R                  S-   ::  a  U R                  $ U R	                  5         U R                  5         U R                  5         U R                  5         U R                  5         U R                  5         U R                  U R                  U R                  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   )
r1   r	   r
   r   rK   rN   rs   r}   r   r   )r   pmetadatar   r   s        r   stemPorterStemmer.stem  s     FQJ66TWWq[ 66M 	







vvdgg
++r   )r	   r   r
   r   )N)__name__
__module____qualname____firstlineno__r   r   r    r$   r'   r.   r5   r8   r;   rK   rN   rs   r}   r   r   r   __static_attributes__ r   r   r   r   +   sX     "#J(
!
- ^C
8v*Pd
 ,r   r   Nc                 @    U R                  [        R                  5      $ )a
  Wrapper around the PorterStemmer for inclusion in pipeline.

Args:
    language (str): ISO-639-1 code of the language.
    token (lunr.Token): The token to stem.
    i (int): The index of the token in a set.
    tokens (list): A list of tokens representing the set.
)updateporter_stemmerr   )tokenr   tokenss      r   stemmerr     s     <<++,,r   r   )NN)__doc__lunr.pipeliner   r   r   r   register_functionr   r   r   <module>r      s=   %N #B, B,J 	- 	  7I .r   