U
    ƒe[  ã                   @   s4   d dl Z d dlmZ d dlmZ G dd„ deƒZdS )é    N)ÚTuple)ÚStemmerIc                   @   sÆ   e Zd ZdZe d¡Ze d¡Ze d¡Ze d¡Z	e d¡Z
e d¡Ze d¡Zded
œdd„Zeeedœdd„ƒZeeedœdd„ƒZeedœdd„Zeeeef dœdd„Zeedœdd„ZdS )ÚCistema:  
    CISTEM Stemmer for German

    This is the official Python implementation of the CISTEM stemmer.
    It is based on the paper
    Leonie Weissweiler, Alexander Fraser (2017). Developing a Stemmer for German
    Based on a Comparative Analysis of Publicly Available Stemmers.
    In Proceedings of the German Society for Computational Linguistics and Language
    Technology (GSCL)
    which can be read here:
    https://www.cis.lmu.de/~weissweiler/cistem/

    In the paper, we conducted an analysis of publicly available stemmers,
    developed two gold standards for German stemming and evaluated the stemmers
    based on the two gold standards. We then proposed the stemmer implemented here
    and show that it achieves slightly better f-measure than the other stemmers and
    is thrice as fast as the Snowball stemmer for German while being about as fast
    as most other stemmers.

    case_insensitive is a a boolean specifying if case-insensitive stemming
    should be used. Case insensitivity improves performance only if words in the
    text may be incorrectly upper case. For all-lowercase and correctly cased
    text, best performance is achieved by setting case_insensitive for false.

    :param case_insensitive: if True, the stemming is case insensitive. False by default.
    :type case_insensitive: bool
    z
^ge(.{4,})z(.)\1ze[mr]$znd$zt$z[esn]$z(.)\*F)Úcase_insensitivec                 C   s
   || _ d S )N)Ú_case_insensitive)Úselfr   © r   úQ/var/www/html/assets/scripts/venv/lib/python3.8/site-packages/nltk/stem/cistem.pyÚ__init__5   s    zCistem.__init__)ÚwordÚreturnc                 C   s6   |   dd¡} |   dd¡} |   dd¡} tj d| ¡} | S )NÚschú$Úeiú%Úieú&z\1*)Úreplacer   Úrepl_xxÚsub©r   r   r   r	   Ú
replace_to8   s
    zCistem.replace_toc                 C   s6   t j d| ¡} |  dd¡} |  dd¡} |  dd¡} | S )Nz\1\1r   r   r   r   r   r   )r   Úrepl_xx_backr   r   r   r   r   r	   Úreplace_backA   s
    zCistem.replace_backc                 C   sr   t |ƒdkr|S |d  ¡ }| ¡ }| dd¡}| dd¡}| dd¡}| dd	¡}tj d
|¡}|  ||¡d S )u«  Stems the input word.

        :param word: The word that is to be stemmed.
        :type word: str
        :return: The stemmed word.
        :rtype: str

        >>> from nltk.stem.cistem import Cistem
        >>> stemmer = Cistem()
        >>> s1 = "SpeicherbehÃ¤ltern"
        >>> stemmer.stem(s1)
        'speicherbehalt'
        >>> s2 = "Grenzpostens"
        >>> stemmer.stem(s2)
        'grenzpost'
        >>> s3 = "Ausgefeiltere"
        >>> stemmer.stem(s3)
        'ausgefeilt'
        >>> stemmer = Cistem(True)
        >>> stemmer.stem(s1)
        'speicherbehal'
        >>> stemmer.stem(s2)
        'grenzpo'
        >>> stemmer.stem(s3)
        'ausgefeil'
        r   õ   Ã¼Úuõ   Ã¶Úoõ   Ã¤Úaõ   ÃŸÚssz\1)ÚlenÚisupperÚlowerr   r   Ústrip_ger   Ú_segment_inner©r   r   Úupperr   r   r	   ÚstemJ   s    zCistem.stemc                 C   s0   t |ƒdkrdS |d  ¡ }| ¡ }|  ||¡S )uÅ  
        This method works very similarly to stem (:func:'cistem.stem'). The difference is that in
        addition to returning the stem, it also returns the rest that was removed at
        the end. To be able to return the stem unchanged so the stem and the rest
        can be concatenated to form the original word, all subsitutions that altered
        the stem in any other way than by removing letters at the end were left out.

        :param word: The word that is to be stemmed.
        :type word: str
        :return: A tuple of the stemmed word and the removed suffix.
        :rtype: Tuple[str, str]

        >>> from nltk.stem.cistem import Cistem
        >>> stemmer = Cistem()
        >>> s1 = "SpeicherbehÃ¤ltern"
        >>> stemmer.segment(s1)
        ('speicherbehÃ¤lt', 'ern')
        >>> s2 = "Grenzpostens"
        >>> stemmer.segment(s2)
        ('grenzpost', 'ens')
        >>> s3 = "Ausgefeiltere"
        >>> stemmer.segment(s3)
        ('ausgefeilt', 'ere')
        >>> stemmer = Cistem(True)
        >>> stemmer.segment(s1)
        ('speicherbehÃ¤l', 'tern')
        >>> stemmer.segment(s2)
        ('grenzpo', 'stens')
        >>> stemmer.segment(s3)
        ('ausgefeil', 'tere')
        r   )Ú r*   )r"   r#   r$   r&   r'   r   r   r	   Úsegmentt   s
     zCistem.segment)r   r(   c                 C   sú   d}|dd… }t  |¡}d}t|ƒdkrÖt|ƒdkr~t j d|¡\}}|dkrZ|d7 }qt j d|¡\}}|dkr~|d7 }q|rˆ| jr¬t j d|¡\}}|dkr¬|d7 }qt j d|¡\}}|dkrÖ|d7 }qqqÖqt  	|¡}|rò|| d… }||fS )a5  Inner method for iteratively applying the code stemming regexes.
        This method receives a pre-processed variant of the word to be stemmed,
        or the word to be segmented, and returns a tuple of the word and the
        removed suffix.

        :param word: A pre-processed variant of the word that is to be stemmed.
        :type word: str
        :param upper: Whether the original word started with a capital letter.
        :type upper: bool
        :return: A tuple of the stemmed word and the removed suffix.
        :rtype: Tuple[str, str]
        r   Nr*   é   é   é   é   )
r   r   r"   Ú	strip_emrÚsubnÚstrip_ndr   Ústrip_tÚ	strip_esnr   )r   r   r(   Zrest_lengthZ	word_copyÚrestÚnr   r   r	   r&   œ   s8    


zCistem._segment_innerN)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚreÚcompiler%   r   r0   r2   r3   r4   r   Úboolr
   ÚstaticmethodÚstrr   r   r)   r   r+   r&   r   r   r   r	   r      s    






*(r   )r;   Útypingr   Znltk.stem.apir   r   r   r   r   r	   Ú<module>
   s   