a
     ˜ëj9  ã                   @   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                 C   sº   g d¢| _ ddg| _g d¢| _g d¢| _g d¢| _g d¢| _dgd	gg d
¢dgdœ| _d	dgg d¢g d¢g d¢ddgd	dgd	dgdœ| _t 	d¡| _
t 	d¡| _t 	d¡| _g d¢| _d S )N)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   u   Ùƒr   r   r   õ   Ù…r   )r   r   r   r	   )r   é   é   é   r   )r   r   r   )r   r   r
   )r
   r   r   r   )r   r   r   r   é   é   é   z[\u064B-\u0652]z[\u0621\u0624\u0626]z^[\u0622\u0623\u0625])1u   ÙŠÙƒÙˆÙ†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   úM/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/nltk/stem/isri.pyÚ__init__3   s.    



	

ü	ù
zISRIStemmer.__init__c                 C   sð   |   |d¡}|| jv r|S |  |¡}|  |¡}|  |¡}|   |d¡}t|ƒdkr\|  |¡}n�t|ƒ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   Néýÿÿÿr   éþÿÿÿ)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 v r |dd… }n”|d | j d v rL|dd… |dd…  }nh|d | j d v rt|dd… |d  }n@|d | j d v 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 v r6|d dkr6|d |dd…  }�nô|d | j d v rl|d dkrl|dd… |d  }�n¾|d | j d v rš|d d	krš|dd… }�n�|d | j d v rÐ|d d
krÐ|d |dd…  }�nZ|d | j d v �r
|d dk�r
|d |dd…  }�n |d | j d v �rD|d d	k�rD|dd… |d  }�næ|d | j d v �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 v 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   