a
     ˜ëje1  ã                   @   s,   d Z ddlZddlmZ G dd„ deƒZdS )aÐ  
ARLSTem Arabic Stemmer
The details about the implementation of this algorithm are described in:
K. Abainia, S. Ouamour and H. Sayoud, A Novel Robust Arabic Light Stemmer ,
Journal of Experimental & Theoretical Artificial Intelligence (JETAI'17),
Vol. 29, No. 3, 2017, pp. 557-573.
The ARLSTem is a light Arabic stemmer that is based on removing the affixes
from the word (i.e. prefixes, suffixes and infixes). It was evaluated and
compared to several other stemmers using Paice's parameters (under-stemming
index, over-stemming index and stemming weight), and the results showed that
ARLSTem is promising and producing high performances. This stemmer is not
based on any dictionary and can be used on-line effectively.
é    N)ÚStemmerIc                   @   s€   e Zd Z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S )ÚARLSTemaY  
    ARLSTem stemmer : a light Arabic Stemming algorithm without any dictionary.
    Department of Telecommunication & Information Processing. USTHB University,
    Algiers, Algeria.
    ARLSTem.stem(token) returns the Arabic stem for the input token.
    The ARLSTem Stemmer requires that all tokens are encoded using Unicode
    encoding.
    c                 C   sÒ   t  d¡| _t  d¡| _t  d¡| _g d¢| _g d¢| _ddg| _g d¢| _d	d
g| _	ddg| _
ddg| _ddg| _g d¢| _ddg| _ddg| _ddg| _ddg| _g d¢| _ddg| _g d¢| _g d¢| _d S )Nz[\u0622\u0623\u0625]z[\u0649]z[\u064B-\u065F])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   u   Ø³Øªu   Ø³ÙŠu   Ø³Ø§u   Ø³Ù†)u   Ù„Ù†u   Ù„Øªu   Ù„ÙŠu   Ù„Ø£u   ØªÙ…Ø§u   ØªÙ†Ù‘)õ   Ù†Ø§u   ØªÙ…u   ØªØ§õ   ÙˆØ§)õ   Øªõ   Ø§õ   Ù†)ÚreÚcompileÚre_hamzated_alifÚre_alifMaqsuraÚre_diacriticsÚpr2Úpr3Úpr32Úpr4Úsu2Úsu22Úsu3Úsu32Úpl_si2Úpl_si3Úverb_su2Úverb_pr2Ú	verb_pr22Ú	verb_pr33Ú	verb_suf3Ú	verb_suf2Ú	verb_suf1)Úself© r"   úP/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/nltk/stem/arlstem.pyÚ__init__*   s(    















zARLSTem.__init__c              
   C   s¸   z†|du rt dƒ‚|  |¡}|  |¡}|dur2|}|  |¡}|  |¡}|du r||  |¡}|durf|W S |du r‚|  |¡W S n|W S |W S  t y² } zt|ƒ W Y d}~n
d}~0 0 dS )zN
        call this function to get the word's stem based on ARLSTem .
        NzUThe word could not be stemmed, because                                  it is empty !)Ú
ValueErrorÚnormÚprefÚsuffÚ	plur2singÚfem2mascÚverbÚprint)r!   ÚtokenÚpreZpsZfmÚer"   r"   r#   Ústemd   s*    ÿ




zARLSTem.stemc                 C   sP   | j  d|¡}| j d|¡}| j d|¡}| d¡rLt|ƒdkrL|dd… }|S )z±
        normalize the word by removing diacritics, replacing hamzated Alif
        with Alif replacing AlifMaqsura with Yaa and removing Waaw at the
        beginning.
        Ú r	   õ   ÙŠu   Ùˆé   é   N)r   Úsubr   r   Ú
startswithÚlen©r!   r-   r"   r"   r#   r&   ‡   s    zARLSTem.normc                 C   sÌ   t |ƒdkr2| jD ]}| |¡r|dd…   S qt |ƒdkrd| jD ]}| |¡rD|dd…   S qDt |ƒdkr–| jD ]}| |¡rv|dd…   S qvt |ƒdkrÈ| jD ]}| |¡r¨|dd…   S q¨dS )z<
        remove prefixes from the words' beginning.
        é   r3   Né   é   é   )r7   r   r6   r   r   r   )r!   r-   Zp3Zp4Úp2r"   r"   r#   r'   ™   s     







zARLSTem.prefc                 C   s<  |  d¡r"t|ƒdkr"|dd… S t|ƒdkrT| jD ]}|  |¡r4|dd…   S q4t|ƒdkr†| jD ]}|  |¡rf|dd…   S qf|  d	¡r¬t|ƒdkr¬|dd… }|S t|ƒdkrÞ| jD ]}|  |¡r¾|dd…   S q¾t|ƒdk�r| jD ]}|  |¡rò|dd…   S qò|  d
¡�r8t|ƒdk�r8|dd… S |S )z6
        remove suffixes from the word's end.
        u   Ùƒr3   Néÿÿÿÿr;   éþÿÿÿr9   éýÿÿÿu   Ù‡r   )Úendswithr7   r   r   r   r   )r!   r-   Ús2Zs3r"   r"   r#   r(   ®   s0    







zARLSTem.suffc                 C   s&   |  d¡r"t|ƒdkr"|dd… S dS )zR
        transform the word from the feminine form to the masculine form.
        u   Ø©r3   Nr>   )rA   r7   r8   r"   r"   r#   r*   Ë   s    zARLSTem.fem2mascc                 C   sú   t |ƒdkr2| jD ]}| |¡r|dd…   S qt |ƒdkrd| jD ]}| |¡rD|dd…   S qDt |ƒdkr†| d¡r†|dd… S t |ƒdkrÀ| d¡rÀ|d	 dkrÀ|dd	… |dd…  S t |ƒdkrö| d¡rö|d dkrö|d
d… |d  S dS )zO
        transform the word from the plural form to the singular form.
        r;   Nr?   r9   r@   r3   u   Ø§Øªr	   r<   r4   r>   )r7   r   rA   r   r6   )r!   r-   Zps2Zps3r"   r"   r#   r)   Ò   s    



""zARLSTem.plur2singc                 C   sx   |   |¡}|dur|S |  |¡}|dur,|S |  |¡}|durB|S |  |¡}|durX|S |  |¡}|durn|S |  |¡S )z=
        stem the verb prefixes and suffixes or both
        N)Úverb_t1Úverb_t2Úverb_t3Úverb_t4Úverb_t5Úverb_t6)r!   r-   Zvbr"   r"   r#   r+   å   s     




zARLSTem.verbc                 C   sZ  t |ƒdkr<| d¡r<| jD ]}| |¡r|dd…   S qt |ƒdkrx| d¡rx| jD ]}| |¡rX|dd…   S qXt |ƒdkrò| d¡ròt |ƒdkr°| d¡r°|dd… S | d¡rÆ|dd	… S | d¡rÜ|dd	… S | d
¡rò|dd	… S t |ƒdk�r$| d¡�r$| d
¡�r$|dd	… S t |ƒdk�rV| d¡�rV| d
¡�rV|dd	… S dS )z8
        stem the present prefixes and suffixes
        r9   r   r4   r?   r2   r;   r	   r   r>   r
   N)r7   r6   r   rA   r   ©r!   r-   rB   r"   r"   r#   rC   ú   s*    






&&zARLSTem.verb_t1c                 C   s  t |ƒdkrš| jD ].}| | jd ¡r| |¡r|dd…   S q| | jd ¡rn| | jd ¡rn|dd… S | | jd ¡rš| | jd ¡rš|dd… S t |ƒdkrÌ| | jd ¡rÌ| d¡rÌ|dd… S t |ƒdkrþ| | jd ¡rþ| d¡rþ|dd… S d	S )
z7
        stem the future prefixes and suffixes
        r:   r   r<   r?   r4   r9   r
   r>   N)r7   r   r6   r   rA   rI   r"   r"   r#   rD     s,    
  
ÿþý
ÿþýzARLSTem.verb_t2c                 C   sš   t |ƒdkr2| jD ]}| |¡r|dd…   S qt |ƒdkrd| jD ]}| |¡rD|dd…   S qDt |ƒdkr–| jD ]}| |¡rv|dd…   S qvdS )z+
        stem the present suffixes
        r9   Nr@   r;   r?   r3   r>   )r7   r   rA   r   r    )r!   r-   r   r   Zsu1r"   r"   r#   rE   8  s    





zARLSTem.verb_t3c                 C   sL   t |ƒdkrH| jD ]}| |¡r|dd…   S q| d¡rH|dd… S dS )z+
        stem the present prefixes
        r3   r4   Nr2   )r7   r    r6   )r!   r-   Zpr1r"   r"   r#   rF   I  s    


zARLSTem.verb_t4c                 C   s\   t |ƒdkrX| jD ]}| |¡r|dd…   S q| jD ]}| |¡r8|dd…   S q8|S )z*
        stem the future prefixes
        r;   r<   N)r7   r   r6   r   )r!   r-   r   r"   r"   r#   rG   T  s    



zARLSTem.verb_t5c                 C   s6   t |ƒdkr2| jD ]}| |¡r|dd…   S q|S )z)
        stem the order prefixes
        r;   r<   N)r7   r   r6   )r!   r-   r   r"   r"   r#   rH   a  s
    

zARLSTem.verb_t6N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r$   r0   r&   r'   r(   r*   r)   r+   rC   rD   rE   rF   rG   rH   r"   r"   r"   r#   r       s   	:# r   )rM   r   Znltk.stem.apir   r   r"   r"   r"   r#   Ú<module>   s   