o
    rZhe1                     @   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"   @/var/www/auris/lib/python3.10/site-packages/nltk/stem/arlstem.py__init__*   s(   















zARLSTem.__init__c              
   C   s   zB|du r	t d| |}| |}|dur|}| |}| |}|du r@| |}|dur3|W S |du r=| |W S |W S |W S  t yY } zt| W Y d}~dS d}~ww )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psfmer"   r"   r#   stemd   s.   




zARLSTem.stemc                 C   sP   | j d|}| jd|}| jd|}|dr&t|dkr&|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r| jD ]}||r|dd   S q	t |dkr2| jD ]}||r1|dd   S q"t |dkrK| jD ]}||rJ|dd   S q;t |dkrd| jD ]}||rc|dd   S qTdS dS )z<
        remove prefixes from the words' beginning.
           r4   N         )r8   r   r7   r   r   r   )r!   r-   Zp3Zp4p2r"   r"   r#   r'      s,   







zARLSTem.prefc                 C   s6  | drt|dkr|dd S t|dkr*| jD ]}| |r)|dd   S qt|dkrC| jD ]}| |rB|dd   S q3| d	rVt|dkrV|dd }|S t|dkro| jD ]}| |rn|dd   S q_t|dkr| jD ]}| |r|dd   S qx| d
rt|dkr|dd S |S )z6
        remove suffixes from the word's end.
        u   كr4   Nr<   r:   u   هr   )endswithr8   r   r   r   r   )r!   r-   s2Zs3r"   r"   r#   r(      s8   







zARLSTem.suffc                 C   s*   | drt|dkr|dd S dS dS )zR
        transform the word from the feminine form to the masculine form.
        u   ةr4   Nr?   )rB   r8   r9   r"   r"   r#   r*      s   zARLSTem.fem2mascc                 C   s  t |dkr| jD ]}||r|dd   S q	t |dkr2| jD ]}||r1|dd   S q"t |dkrC|drC|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 dS dS )zO
        transform the word from the plural form to the singular form.
        r<   Nr@   r:   rA   r4   u   اتr	   r=   r5   r?   )r8   r   rB   r   r7   )r!   r-   Zps2Zps3r"   r"   r#   r)      s"   



""zARLSTem.plur2singc                 C   sx   |  |}|dur|S | |}|dur|S | |}|dur!|S | |}|dur,|S | |}|dur7|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   sV  t |dkr|dr| jD ]}||r|dd   S qt |dkr<|dr<| jD ]}||r;|dd   S q,t |dkry|dryt |dkrX|drX|dd S |drc|dd	 S |drn|dd	 S |d
ry|dd	 S t |dkr|dr|d
r|dd	 S t |dkr|dr|d
r|dd	 S dS dS dS )z8
        stem the present prefixes and suffixes
        r:   r   r5   r@   r3   r<   r	   r   r?   r
   N)r8   r7   r   rB   r   r!   r-   rC   r"   r"   r#   rD      s0   






  zARLSTem.verb_t1c                 C   s
  t |dkrM| jD ]}|| jd r ||r |dd   S q	|| jd r7|| jd r7|dd S || jd rM|| jd rM|dd S t |dkrf|| jd rf|drf|dd S t |dkr|| jd r|dr|dd S d	S d	S d	S )
z7
        stem the future prefixes and suffixes
        r;   r   r=   r@   r5   r:   r
   r?   N)r8   r   r7   r   rB   rJ   r"   r"   r#   rE     s,   
  zARLSTem.verb_t2c                 C   s   t |dkr| jD ]}||r|dd   S q	t |dkr2| jD ]}||r1|dd   S q"t |dkrK| jD ]}||rJ|dd   S q;dS dS )z+
        stem the present suffixes
        r:   NrA   r<   r@   r4   r?   )r8   r   rB   r   r    )r!   r-   r   r   Zsu1r"   r"   r#   rF   8  s"   





zARLSTem.verb_t3c                 C   sP   t |dkr$| jD ]}||r|dd   S q	|dr&|dd S dS dS )z+
        stem the present prefixes
        r4   r5   Nr3   )r8   r    r7   )r!   r-   Zpr1r"   r"   r#   rG   I  s   


zARLSTem.verb_t4c                 C   s\   t |dkr,| jD ]}||r|dd   S q	| jD ]}||r+|dd   S q|S )z*
        stem the future prefixes
        r<   r=   N)r8   r   r7   r   )r!   r-   r   r"   r"   r#   rH   T  s   



zARLSTem.verb_t5c                 C   s6   t |dkr| jD ]}||r|dd   S q	|S )z)
        stem the order prefixes
        r<   r=   N)r8   r   r7   )r!   r-   r   r"   r"   r#   rI   a  s   

zARLSTem.verb_t6N)__name__
__module____qualname____doc__r$   r1   r&   r'   r(   r*   r)   r+   rD   rE   rF   rG   rH   rI   r"   r"   r"   r#   r       s     	:# r   )rN   r   Znltk.stem.apir   r   r"   r"   r"   r#   <module>   s   