U
    en                     @   s8   d Z dZddlZddlmZ G dd deZdd ZdS )	a  
Porter Stemmer

This is the Porter stemming algorithm. It follows the algorithm
presented in

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

with some optional deviations that can be turned on or off with the
`mode` argument to the constructor.

Martin Porter, the algorithm's inventor, maintains a web page about the
algorithm at

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

which includes another Python implementation and other implementations
in many languages.
Z	plaintext    N)StemmerIc                   @   s   e Zd ZdZdZdZdZefddZdd Zd	d
 Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd-d(d)Zd*d+ Zd,S ).PorterStemmeraY  
    A word stemmer based on the Porter stemming algorithm.

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

    See https://www.tartarus.org/~martin/PorterStemmer/ for the homepage
    of the algorithm.

    Martin Porter has endorsed several modifications to the Porter
    algorithm since writing his original paper, and those extensions are
    included in the implementations on his website. Additionally, others
    have proposed further improvements to the algorithm, including NLTK
    contributors. There are thus three modes that can be selected by
    passing the appropriate constant to the class constructor's `mode`
    attribute:

    - PorterStemmer.ORIGINAL_ALGORITHM

        An implementation that is faithful to the original paper.

        Note that Martin Porter has deprecated this version of the
        algorithm. Martin distributes implementations of the Porter
        Stemmer in many languages, hosted at:

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

        and all of these implementations include his extensions. He
        strongly recommends against using the original, published
        version of the algorithm; only use this mode if you clearly
        understand why you are choosing to do so.

    - PorterStemmer.MARTIN_EXTENSIONS

        An implementation that only uses the modifications to the
        algorithm that are included in the implementations on Martin
        Porter's website. He has declared Porter frozen, so the
        behaviour of those implementations should never change.

    - PorterStemmer.NLTK_EXTENSIONS (default)

        An implementation that includes further improvements devised by
        NLTK contributors or taken from other modified implementations
        found on the web.

    For the best stemming, you should use the default NLTK_EXTENSIONS
    version. However, if you need to get the same results as either the
    original algorithm or one of Martin Porter's hosted versions for
    compatibility with an existing implementation or dataset, you can use
    one of the other modes instead.
    NLTK_EXTENSIONSMARTIN_EXTENSIONSORIGINAL_ALGORITHMc                 C   s   || j | j| jfkrtd|| _| j| j krddgdgdgdgdgdd	gd
dgddgdgdgdgdgd}i | _|D ]}|| D ]}|| j|< qqvtdddddg| _d S )NzwMode must be one of PorterStemmer.NLTK_EXTENSIONS, PorterStemmer.MARTIN_EXTENSIONS, or PorterStemmer.ORIGINAL_ALGORITHMskyZskiesZdyingZlyingZtyingnewsZinningsinningZoutingsoutingZcanningscanninghoweproceedexceedsucceed)r   ZdieZlieZtier   r	   r
   r   r   r   r   r   aeiou)r   r   r   
ValueErrormodepool	frozensetvowels)selfr   Zirregular_formskeyval r   Q/var/www/html/assets/scripts/venv/lib/python3.8/site-packages/nltk/stem/porter.py__init__V   s8    zPorterStemmer.__init__c                 C   s@   || | j krdS || dkr<|dkr*dS | ||d  S dS )a  Returns True if word[i] is a consonant, False otherwise

        A consonant is defined in the paper as follows:

            A consonant in a word is a letter other than A, E, I, O or
            U, and other than Y preceded by a consonant. (The fact that
            the term `consonant' is defined to some extent in terms of
            itself does not make it ambiguous.) So in TOY the consonants
            are T and Y, and in SYZYGY they are S, Z and G. If a letter
            is not a consonant it is a vowel.
        Fyr   T   )r   _is_consonant)r   wordr   r   r   r   r"   ~   s    zPorterStemmer._is_consonantc                 C   s>   d}t t|D ]"}| ||r*|d7 }q|d7 }q|dS )a  Returns the 'measure' of stem, per definition in the paper

        From the paper:

            A consonant will be denoted by c, a vowel by v. A list
            ccc... of length greater than 0 will be denoted by C, and a
            list vvv... of length greater than 0 will be denoted by V.
            Any word, or part of a word, therefore has one of the four
            forms:

                CVCV ... C
                CVCV ... V
                VCVC ... C
                VCVC ... V

            These may all be represented by the single form

                [C]VCVC ... [V]

            where the square brackets denote arbitrary presence of their
            contents. Using (VC){m} to denote VC repeated m times, this
            may again be written as

                [C](VC){m}[V].

            m will be called the \measure\ of any word or word part when
            represented in this form. The case m = 0 covers the null
            word. Here are some examples:

                m=0    TR,  EE,  TREE,  Y,  BY.
                m=1    TROUBLE,  OATS,  TREES,  IVY.
                m=2    TROUBLES,  PRIVATE,  OATEN,  ORRERY.
         cvvc)rangelenr"   count)r   stemZcv_sequencer   r   r   r   _measure   s    "

zPorterStemmer._measurec                 C   s   |  |dkS )Nr   r,   )r   r+   r   r   r   _has_positive_measure   s    z#PorterStemmer._has_positive_measurec                 C   s(   t t|D ]}| ||s dS qdS )z1Returns True if stem contains a vowel, else FalseTF)r(   r)   r"   )r   r+   r   r   r   r   _contains_vowel   s    zPorterStemmer._contains_vowelc                 C   s0   t |dko.|d |d ko.| |t |d S )zjImplements condition *d from the paper

        Returns True if word ends with a double consonant
           r!   r)   r"   r   r#   r   r   r   _ends_double_consonant   s
    z$PorterStemmer._ends_double_consonantc                 C   s   t |dkrV| |t |d rV| |t |d  rV| |t |d rV|d dkp| j| jkot |dko| |d o| |dS )zImplements condition *o from the paper

        From the paper:

            *o  - the stem ends cvc, where the second c is not W, X or Y
                  (e.g. -WIL, -HOP).
           r0   r!   r1   )wxr    r   )r)   r"   r   r   r4   r   r   r   	_ends_cvc   s     	


zPorterStemmer._ends_cvcc                 C   s<   | |std|dkr"|| S |dt|  | S dS )z-Replaces `suffix` of `word` with `replacementz(Given word doesn't end with given suffixr$   N)endswithAssertionErrorr)   )r   r#   suffixreplacementr   r   r   _replace_suffix   s    zPorterStemmer._replace_suffixc                 C   s   |D ]}|\}}}|dkrT|  |rT|dd }|dks@||rL||   S |  S ||r| ||d}|dks|||r||   S |  S q|S )a  Applies the first applicable suffix-removal rule to the word

        Takes a word and a list of suffix-removal rules represented as
        3-tuples, with the first element being the suffix to remove,
        the second element being the string to replace it with, and the
        final element being the condition for the rule to be applicable,
        or None if the rule is unconditional.
        *dNr2   r$   )r5   r:   r>   )r   r#   rulesruler<   r=   	conditionr+   r   r   r   _apply_rule_list   s    	


zPorterStemmer._apply_rule_listc                 C   sD   | j | jkr0|dr0t|dkr0| |ddS | |ddddgS )a  Implements Step 1a from "An algorithm for suffix stripping"

        From the paper:

            SSES -> SS                         caresses  ->  caress
            IES  -> I                          ponies    ->  poni
                                               ties      ->  ti
            SS   -> SS                         caress    ->  caress
            S    ->                            cats      ->  cat
        ies   ie)ZssesssN)rD   r   N)rG   rG   N)sr$   N)r   r   r:   r)   r>   rC   r4   r   r   r   _step1a  s    zPorterStemmer._step1ac                    s   j jkr>|dr>t|dkr0|ddS |ddS |drp|dd}|dkrl|d S |S d	}d
D ].}||rx||d  rxd} qqx|s|S  dddd d  fddfddfddfgS )a=  Implements Step 1b from "An algorithm for suffix stripping"

        From the paper:

            (m>0) EED -> EE                    feed      ->  feed
                                               agreed    ->  agree
            (*v*) ED  ->                       plastered ->  plaster
                                               bled      ->  bled
            (*v*) ING ->                       motoring  ->  motor
                                               sing      ->  sing

        If the second or third of the rules in Step 1b is successful,
        the following is done:

            AT -> ATE                       conflat(ed)  ->  conflate
            BL -> BLE                       troubl(ed)   ->  trouble
            IZ -> IZE                       siz(ed)      ->  size
            (*d and not (*L or *S or *Z))
               -> single letter
                                            hopp(ing)    ->  hop
                                            tann(ed)     ->  tan
                                            fall(ing)    ->  fall
                                            hiss(ing)    ->  hiss
                                            fizz(ed)     ->  fizz
            (m=1 and *o) -> E               fail(ing)    ->  fail
                                            fil(ing)     ->  file

        The rule to map to a single letter causes the removal of one of
        the double letter pair. The -E is put back on -AT, -BL and -IZ,
        so that the suffixes -ATE, -BLE and -IZE can be recognised
        later. This E may be removed in step 4.
        ZiedrE   rF   r   Zeedr$   r   eeF)ZedZingT)atateN)blbleN)ZizizeNr?   r1   c                    s    d dkS )Nr1   )lrH   zr   r+   )intermediate_stemr   r   <lambda>x      z'PorterStemmer._step1b.<locals>.<lambda>r   c                    s     | dko | S Nr!   )r,   r9   rR   r   r   r   rT   ~  rU   )r   r   r:   r)   r>   r,   r/   rC   )r   r#   r+   Zrule_2_or_3_succeededr<   r   )rS   r   r   _step1b/  sD    #





zPorterStemmer._step1bc                    s<    fdd} fdd}  |dd j jkr2|n|fgS )zImplements Step 1c from "An algorithm for suffix stripping"

        From the paper:

        Step 1c

            (*v*) Y -> I                    happy        ->  happi
                                            sky          ->  sky
        c                    s    t | dko | t | d S )a  
            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 contains_vowel(z) condition means
            that 'spy', 'fly', 'try' ... stem to 'spi', 'fli', 'tri' and
            conflate with 'spied', 'tried', 'flies' ...
            r!   r3   rR   rW   r   r   nltk_condition  s    z-PorterStemmer._step1c.<locals>.nltk_conditionc                    s
     | S )N)r/   rR   rW   r   r   original_condition  s    z1PorterStemmer._step1c.<locals>.original_conditionr    r   )rC   r   r   )r   r#   rY   rZ   r   rW   r   _step1c  s    
zPorterStemmer._step1cc                    s   j  jkr>dr>  ddr>  ddS dd jf}dd jf}dd	 jfd
d jfdd jfdd jfdd jf j  jkr|n|dd jfdd jfdd jfdd jfdd jfdd	 jfdd	 jfdd jfdd jfdd jfd d jfd!d jfd"d jfd#d jfg} j  jkrd|d$d jf |d%d& fd'd(f  j  jkr|d%d& jf  	|S ))a  Implements Step 2 from "An algorithm for suffix stripping"

        From the paper:

        Step 2

            (m>0) ATIONAL ->  ATE       relational     ->  relate
            (m>0) TIONAL  ->  TION      conditional    ->  condition
                                        rational       ->  rational
            (m>0) ENCI    ->  ENCE      valenci        ->  valence
            (m>0) ANCI    ->  ANCE      hesitanci      ->  hesitance
            (m>0) IZER    ->  IZE       digitizer      ->  digitize
            (m>0) ABLI    ->  ABLE      conformabli    ->  conformable
            (m>0) ALLI    ->  AL        radicalli      ->  radical
            (m>0) ENTLI   ->  ENT       differentli    ->  different
            (m>0) ELI     ->  E         vileli        - >  vile
            (m>0) OUSLI   ->  OUS       analogousli    ->  analogous
            (m>0) IZATION ->  IZE       vietnamization ->  vietnamize
            (m>0) ATION   ->  ATE       predication    ->  predicate
            (m>0) ATOR    ->  ATE       operator       ->  operate
            (m>0) ALISM   ->  AL        feudalism      ->  feudal
            (m>0) IVENESS ->  IVE       decisiveness   ->  decisive
            (m>0) FULNESS ->  FUL       hopefulness    ->  hopeful
            (m>0) OUSNESS ->  OUS       callousness    ->  callous
            (m>0) ALITI   ->  AL        formaliti      ->  formal
            (m>0) IVITI   ->  IVE       sensitiviti    ->  sensitive
            (m>0) BILITI  ->  BLE       sensibiliti    ->  sensible
        Zallir$   alZblirN   ZabliableZationalrL   ZtionalZtionZencienceZancianceZizerrO   ZentlientZelir   ZousliousZizationZationZatorZalismZivenessiveZfulnessfulZousnessZalitiZivitiZbilitiZfulliZlogilogc                    s     d d S )N)r.   rR   r4   r   r   rT     rU   z&PorterStemmer._step2.<locals>.<lambda>)
r   r   r:   r.   r>   _step2r   appendr   rC   )r   r#   Zbli_ruleZ	abli_ruler@   r   r4   r   rf     sH    


















zPorterStemmer._step2c                 C   sR   |  |dd| jfdd| jfdd| jfdd| jfdd| jfd	d| jfd
d| jfgS )aV  Implements Step 3 from "An algorithm for suffix stripping"

        From the paper:

        Step 3

            (m>0) ICATE ->  IC              triplicate     ->  triplic
            (m>0) ATIVE ->                  formative      ->  form
            (m>0) ALIZE ->  AL              formalize      ->  formal
            (m>0) ICITI ->  IC              electriciti    ->  electric
            (m>0) ICAL  ->  IC              electrical     ->  electric
            (m>0) FUL   ->                  hopeful        ->  hope
            (m>0) NESS  ->                  goodness       ->  good
        ZicateicZativer$   Zalizer\   ZicitiZicalrc   Zness)rC   r.   r4   r   r   r   _step3  s    






zPorterStemmer._step3c                    s    fdd}  |dd|fdd|fdd|fdd|fdd|fd	d|fd
d|fdd|fdd|fdd|fdd|fdd fddfdd|fdd|fdd|fdd|fdd|fdd|fdd|fgS )a  Implements Step 4 from "An algorithm for suffix stripping"

        Step 4

            (m>1) AL    ->                  revival        ->  reviv
            (m>1) ANCE  ->                  allowance      ->  allow
            (m>1) ENCE  ->                  inference      ->  infer
            (m>1) ER    ->                  airliner       ->  airlin
            (m>1) IC    ->                  gyroscopic     ->  gyroscop
            (m>1) ABLE  ->                  adjustable     ->  adjust
            (m>1) IBLE  ->                  defensible     ->  defens
            (m>1) ANT   ->                  irritant       ->  irrit
            (m>1) EMENT ->                  replacement    ->  replac
            (m>1) MENT  ->                  adjustment     ->  adjust
            (m>1) ENT   ->                  dependent      ->  depend
            (m>1 and (*S or *T)) ION ->     adoption       ->  adopt
            (m>1) OU    ->                  homologou      ->  homolog
            (m>1) ISM   ->                  communism      ->  commun
            (m>1) ATE   ->                  activate       ->  activ
            (m>1) ITI   ->                  angulariti     ->  angular
            (m>1) OUS   ->                  homologous     ->  homolog
            (m>1) IVE   ->                  effective      ->  effect
            (m>1) IZE   ->                  bowdlerize     ->  bowdler

        The suffixes are now removed. All that remains is a little
        tidying up.
        c                    s     | dkS rV   r-   rR   rW   r   r   rT   ;  rU   z&PorterStemmer._step4.<locals>.<lambda>r\   r$   r_   r^   Zerrh   r]   ZibleantZementZmentr`   Zionc                    s     | dko| d dkS )Nr!   r1   )rH   tr-   rR   rW   r   r   rT   O  rU   ZouZismrL   Zitira   rb   rO   rC   )r   r#   Zmeasure_gt_1r   rW   r   _step4  s6    
zPorterStemmer._step4c                 C   sJ   | drF| |dd}| |dkr*|S | |dkrF| |sF|S |S )a=  Implements Step 5a from "An algorithm for suffix stripping"

        From the paper:

        Step 5a

            (m>1) E     ->                  probate        ->  probat
                                            rate           ->  rate
            (m=1 and not *o) E ->           cease          ->  ceas
        r   r$   r!   )r:   r>   r,   r9   )r   r#   r+   r   r   r   _step5a[  s    
zPorterStemmer._step5ac                    s     dd fddfgS )a  Implements Step 5a from "An algorithm for suffix stripping"

        From the paper:

        Step 5b

            (m > 1 and *d and *L) -> single letter
                                    controll       ->  control
                                    roll           ->  roll
        llrP   c                    s     d d dkS )Nr1   r!   r-   rR   r4   r   r   rT     rU   z'PorterStemmer._step5b.<locals>.<lambda>rl   r4   r   r4   r   _step5b  s     zPorterStemmer._step5bTc                 C   s   |r|  n|}| j| jkr0|| jkr0| j| S | j| jkrLt|dkrL|S | |}| |}| |}| 	|}| 
|}| |}| |}| |}|S )zW
        :param to_lowercase: if `to_lowercase=True` the word always lowercase
        r0   )lowerr   r   r   r   r)   rI   rX   r[   rf   ri   rm   rn   rp   )r   r#   Zto_lowercaser+   r   r   r   r+     s    








zPorterStemmer.stemc                 C   s   dS )Nz<PorterStemmer>r   rW   r   r   r   __repr__  s    zPorterStemmer.__repr__N)T)__name__
__module____qualname____doc__r   r   r   r   r"   r,   r.   r/   r5   r9   r>   rC   rI   rX   r[   rf   ri   rm   rn   rp   r+   rr   r   r   r   r   r      s.   5(3T0P<$
r   c            
      C   s  ddl m}  ddlm} |  }g }g }| dd D ]2}||D ]"\}}|| ||| qFq8d|}t	
dd|d  }d|}	t	
dd|	d  }	td	d
dddd t|	 tdd
dddd t| td dS )z^
    A demonstration of the porter stemmer on a sample from
    the Penn Treebank corpus.
    r   rR   )treebankNr6    z
(.{,70})\sz\1\nz
-Original-F   *-z	-Results-zF**********************************************************************)Znltkr+   Znltk.corpusrw   r   ZfileidsZtagged_wordsrg   joinresubrstripprintcenterreplace)
r+   rw   ZstemmerorigZstemmeditemr#   tagresultsoriginalr   r   r   demo  s$    


r   )rv   __docformat__r}   Znltk.stem.apir   r   r   r   r   r   r   <module>   s        