U
    e:                     @   s,   d Z ddlZddlmZ G dd deZdS )u^  
ISRI Arabic Stemmer

The algorithm for this stemmer is described in:

Taghva, K., Elkoury, R., and Coombs, J. 2005. Arabic Stemming without a root dictionary.
Information Science Research Institute. University of Nevada, Las Vegas, USA.

The Information Science Research Institute’s (ISRI) Arabic stemmer shares many features
with the Khoja stemmer. However, the main difference is that ISRI stemmer does not use root
dictionary. Also, if a root is not found, ISRI stemmer returned normalized form, rather than
returning the original unmodified word.

Additional adjustments were made to improve the algorithm:

1- Adding 60 stop words.
2- Adding the pattern (تفاعيل) to ISRI pattern set.
3- The step 2 in the original algorithm was normalizing all hamza. This step is discarded because it
increases the word ambiguities and changes the original root.

    N)StemmerIc                   @   s   e Zd ZdZdd Zdd Zd"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!S )#ISRIStemmera&  
    ISRI Arabic stemmer based on algorithm: Arabic Stemming without a root dictionary.
    Information Science Research Institute. University of Nevada, Las Vegas, USA.

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

    isri.stem(token) returns Arabic root for the given token.

    The ISRI Stemmer requires that all tokens have Unicode string types.
    If you use Python IDLE on Arabic Windows you have to decode text first
    using Arabic '1256' coding.
    c              1   C   s^  ddddg| _ ddg| _ddd	d
dddddg	| _dddddg| _dddddddddddd d!d"d#d$g| _d%d&dd'dddg| _d(gdgdddgd%gd)| _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*| _t	d+| _
t	d,| _t	d-| _d.d/d0d1d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOdPdQdRdSdTdUdVdWdXdYdZd[d\d]d^g1| _d S )_Nu   كالu   بالu   وللu   والu   الu   للu   لu   بu   فu   س   و   ي   ت   ن   اu   تملu   هملu   تانu   تينu   كملu   ونu   اتu   انu   ينu   تنu   كمu   هنu   ناu   ياu   هاu   تمu   كنu   نيu   واu   ماu   هم   ةu   هu   ك   م)r            )r   r   r   r            z[\u064B-\u0652]z[\u0621\u0624\u0626]z^[\u0622\u0623\u0625]u   يكونu   وليسu   وكانu   كذلكu   التيu   وبينu
   عليهاu   مساءu   الذيu
   وكانتu   ولكنu
   والتيu   تكونu
   اليومu   اللذينu   عليهu   كانتu   لذلكu   أمامu   هناكu   منهاu
   مازالu
   لازالu   لايزالu   مايزالu   اصبحu   أصبحu   أمسىu   امسىu   أضحىu   اضحىu
   مابرحu
   مافتئu   ماانفكu   لاسيماu   ولايزالu   الحاليu
   اليهاu
   الذينu   فانهu
   والذيu   وهذاu   لهذاu   فكانu
   ستكونu   اليهu   يمكنu   بهذاu   الذى)p3p2p1s3s2s1pr4pr53recompilere_short_vowelsZre_hamzare_initial_hamza
stop_words)self r   O/var/www/html/assets/scripts/venv/lib/python3.8/site-packages/nltk/stem/isri.py__init__3   s    

	
zISRIStemmer.__init__c                 C   s   |  |d}|| jkr|S | |}| |}| |}|  |d}t|dkr\| |}nt|dkr~| |}| |}nnt|dkr| 	|}| 
|}nLt|dkr| |}t|dkr| |}t|dkr| 	|}| 
|}|S )z?
        Stemming a word token using the ISRI stemmer.
        r   r   r   r   r      )normr   pre32suf32wawlenpro_w4pro_w53end_w5pro_w6end_w6suf1pre1)r   tokenr   r   r    stem   s@     






zISRIStemmer.stemr   c                 C   sX   |dkr| j d|}n<|dkr0| jd|}n$|dkrT| j d|}| jd|}|S )z
        normalization:
        num=1  normalize diacritics
        num=2  normalize initial hamza
        num=3  both 1&2
        r    r   r   r   )r   subr   )r   wordnumr   r   r    r#      s    zISRIStemmer.normc                 C   sh   t |dkr2| jD ]}||r|dd   S qt |dkrd| jD ]}||rD|dd   S qD|S )z9remove length three and length two prefixes in this orderr   r   Nr   r   )r'   r   
startswithr   )r   r3   Zpre3Zpre2r   r   r    r$      s    



zISRIStemmer.pre32c                 C   sh   t |dkr2| jD ]}||r|dd   S qt |dkrd| jD ]}||rD|dd   S qD|S )z9remove length three and length two suffixes in this orderr   Nr   )r'   r   endswithr   )r   r3   Zsuf3Zsuf2r   r   r    r%      s    



zISRIStemmer.suf32c                 C   s,   t |dkr(|dd dkr(|dd }|S )uH   remove connective ‘و’ if it precedes a word beginning with ‘و’r   Nr   u   ووr   )r'   r   r3   r   r   r    r&     s    zISRIStemmer.wawc                 C   s   |d | j d kr |dd }n|d | j d krL|dd |dd  }nh|d | j d krt|dd |d  }n@|d | j d kr|dd }n | |}t|dkr| |}|S )z;process length four patterns and extract length three rootsr   r   Nr   r   r   )r   r-   r'   r.   r9   r   r   r    r(   
  s    

zISRIStemmer.pro_w4c                 C   s.  |d | j d kr6|d dkr6|d |dd  }n|d | j d krl|d dkrl|dd |d  }n|d | j d kr|d d	kr|dd }n|d | j d kr|d d
kr|d |dd  }nZ|d | j d kr
|d dkr
|d |dd  }n |d | j d krD|d d	krD|dd |d  }n|d | j d krv|d dkrv|dd }n|d dkr|d dkr|dd |d  }n|d dkr|d dkr|dd }nT|d dkr
|d d
kr
|dd |d  }n |d dkr@|d dkr@|d |d  |d  }n|d dkrr|d dkrr|d |dd  }n|d dkr|d dkr|dd |d  }n|d d	kr|d dkr|d |dd  }nT|d dkr|d dkr|dd |d  }n"| |}t|dkr*| |}|S )z;process length five patterns and extract length three rootsr   r   r   r   r   Nr
   r   r	   r   r   r   r   r   r   u   ئ)r   r-   r'   r.   r9   r   r   r    r)     sD    """

zISRIStemmer.pro_w53c                 C   sb   |d | j d kr |dd }n>|d dkr:|dd }n$|d dkr^|dd |dd  }|S )	z:process length five patterns and extract length four rootsr   r   r   Nr   r	   r   r   )r   r9   r   r   r    pro_w54@  s    zISRIStemmer.pro_w54c                 C   s2   t |dkr| |}nt |dkr.| |}|S )z!ending step (word of length five)r   r   )r'   r(   r;   r9   r   r   r    r*   J  s
    
zISRIStemmer.end_w5c                 C   sD  | ds| dr$|dd }n|d dkr^|d dkr^|d d	kr^|d
d |d  }n|d dkr|d dkr|d dkr|d
 |d  |d  }n|d dkr|d dkr|d |d kr|d
 |dd  }nf|d dkr|d dkr|d dkr|d
 |d  |d  }n"| |}t|dkr@| |}|S )z:process length six patterns and extract length three rootsu   استu   مستr   Nr   r
   r   r   r	   r   r   r   r   r   r   r   )r5   r-   r'   r.   r9   r   r   r    r+   R  sH    












zISRIStemmer.pro_w6c                 C   sH   |d dkr.|d dkr.|dd |d  }n| drD|dd }|S )	z9process length six patterns and extract length four rootsr   r   r   r   r   u   متr   N)r5   r9   r   r   r    pro_w64n  s
    
zISRIStemmer.pro_w64c                 C   s<   t |dkr"| |}| |}nt |dkr8| |}|S )z ending step (word of length six)r   r   )r'   r)   r*   r<   r9   r   r   r    r,   v  s    

zISRIStemmer.end_w6c                 C   s*   | j D ]}||r|dd   S q|S )znormalize short sufixNr:   )r   r8   )r   r3   Zsf1r   r   r    r-     s    

zISRIStemmer.suf1c                 C   s*   | j D ]}||r|dd   S q|S )znormalize short prefixr   N)r   r5   )r   r3   Zsp1r   r   r    r.     s    

zISRIStemmer.pre1N)r   )__name__
__module____qualname____doc__r!   r0   r#   r$   r%   r&   r(   r)   r;   r*   r+   r<   r,   r-   r.   r   r   r   r    r   $   s"    %
&
	r   )r@   r   Znltk.stem.apir   r   r   r   r   r    <module>
   s   