a
     ˜ëjT*  ã                   @   sj   d dl Z d dlmZ G dd„ dƒZG dd„ deƒZdd„ Zd	d
„ Zdd„ ZedddgƒZ	G dd„ dƒZ
dS )é    N)Ú
namedtuplec                   @   st   e Zd ZdZddd„Zedd„ ƒZedd„ ƒZd	d
„ Zdd„ Z	eee	ƒZ
dd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚAlignedSenta#  
    Return an aligned sentence object, which encapsulates two sentences
    along with an ``Alignment`` between them.

    Typically used in machine translation to represent a sentence and
    its translation.

        >>> from nltk.translate import AlignedSent, Alignment
        >>> algnsent = AlignedSent(['klein', 'ist', 'das', 'Haus'],
        ...     ['the', 'house', 'is', 'small'], Alignment.fromstring('0-3 1-2 2-0 3-1'))
        >>> algnsent.words
        ['klein', 'ist', 'das', 'Haus']
        >>> algnsent.mots
        ['the', 'house', 'is', 'small']
        >>> algnsent.alignment
        Alignment([(0, 3), (1, 2), (2, 0), (3, 1)])
        >>> from nltk.corpus import comtrans
        >>> print(comtrans.aligned_sents()[54])
        <AlignedSent: 'Weshalb also sollten...' -> 'So why should EU arm...'>
        >>> print(comtrans.aligned_sents()[54].alignment)
        0-0 0-1 1-0 2-2 3-4 3-5 4-7 5-8 6-3 7-9 8-9 9-10 9-11 10-12 11-6 12-6 13-13

    :param words: Words in the target language sentence
    :type words: list(str)
    :param mots: Words in the source language sentence
    :type mots: list(str)
    :param alignment: Word-level alignments between ``words`` and ``mots``.
        Each alignment is represented as a 2-tuple (words_index, mots_index).
    :type alignment: Alignment
    Nc                 C   s:   || _ || _|d u r tg ƒ| _nt|ƒtu s0J ‚|| _d S ©N)Ú_wordsÚ_motsÚ	AlignmentÚ	alignmentÚtype)ÚselfÚwordsÚmotsr   © r   úQ/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/nltk/translate/api.pyÚ__init__/   s    zAlignedSent.__init__c                 C   s   | j S r   )r   ©r
   r   r   r   r   8   s    zAlignedSent.wordsc                 C   s   | j S r   )r   r   r   r   r   r   <   s    zAlignedSent.motsc                 C   s   | j S r   )Ú
_alignmentr   r   r   r   Ú_get_alignment@   s    zAlignedSent._get_alignmentc                 C   s"   t t| jƒt| jƒ|ƒ || _d S r   )Ú_check_alignmentÚlenr   r   r   )r
   r   r   r   r   Ú_set_alignmentC   s    zAlignedSent._set_alignmentc                 C   sN   dd  dd„ | jD ƒ¡ }dd  dd„ | jD ƒ¡ }d|› d|› d| j›d�S )z_
        Return a string representation for this ``AlignedSent``.

        :rtype: str
        z[%s]z, c                 s   s   | ]}d | V  qdS ©z'%s'Nr   ©Ú.0Úwr   r   r   Ú	<genexpr>O   ó    z'AlignedSent.__repr__.<locals>.<genexpr>c                 s   s   | ]}d | V  qdS r   r   r   r   r   r   r   P   r   zAlignedSent(ú))Újoinr   r   r   )r
   r   r   r   r   r   Ú__repr__I   s    zAlignedSent.__repr__c                    s  d}|d7 }|d  dd„ ˆ jD ƒ¡7 }|d  dd„ ˆ jD ƒ¡7 }|d  ‡ fdd„ˆ jD ƒ¡7 }ttˆ jƒd ƒD ]$}|d	 ˆ j| ˆ j|d  ¡7 }qpttˆ jƒd ƒD ]$}|d
 ˆ j| ˆ j|d  ¡7 }q¨|dd  dd„ ˆ jD ƒ¡ 7 }|dd  dd„ ˆ jD ƒ¡ 7 }|d7 }|S )z<
        Dot representation of the aligned sentence
        zgraph align {
znode[shape=plaintext]
Ú c                 S   s   g | ]}d |› d|› d�‘qS )ú"z_source" [label="ú"] 
r   r   r   r   r   Ú
<listcomp>\   r   z'AlignedSent._to_dot.<locals>.<listcomp>c                 S   s   g | ]}d |› d|› d�‘qS )r    z_target" [label="r!   r   r   r   r   r   r"   ]   r   c                    s.   g | ]&\}}d ˆ j | › dˆ j| › d�‘qS )r    z_source" -- "z
_target" 
)r   r   )r   ÚuÚvr   r   r   r"   a   s   ÿé   z)"{}_source" -- "{}_source" [style=invis]
z)"{}_target" -- "{}_target" [style=invis]
z{rank = same; %s}
ú c                 s   s   | ]}d | V  qdS )z"%s_source"Nr   r   r   r   r   r   v   r   z&AlignedSent._to_dot.<locals>.<genexpr>c                 s   s   | ]}d | V  qdS )z"%s_target"Nr   r   r   r   r   r   w   r   Ú})r   r   r   r   Úranger   Úformat)r
   ÚsÚir   r   r   Ú_to_dotT   s.    
þÿþþzAlignedSent._to_dotc              
   C   s~   |   ¡  d¡}d}z$tjdd| gtjtjtjd�}W n. tyd } ztdƒ|‚W Y d}~n
d}~0 0 | |¡\}}| d¡S )zR
        Ipython magic : show SVG representation of this ``AlignedSent``.
        Úutf8ÚsvgÚdotz-T%s)ÚstdinÚstdoutÚstderrz0Cannot find the dot binary from Graphviz packageN)	r,   ÚencodeÚ
subprocessÚPopenÚPIPEÚOSErrorÚ	ExceptionÚcommunicateÚdecode)r
   Z
dot_stringZoutput_formatÚprocessÚeÚoutÚerrr   r   r   Ú
_repr_svg_}   s    
ü
 zAlignedSent._repr_svg_c                 C   sB   d  | j¡dd… d }d  | j¡dd… d }d|› d|› d�S )zn
        Return a human-readable string representation for this ``AlignedSent``.

        :rtype: str
        r&   Né   z...z<AlignedSent: 'z' -> 'z'>)r   r   r   )r
   ÚsourceÚtargetr   r   r   Ú__str__�   s    zAlignedSent.__str__c                 C   s   t | j| j| j ¡ ƒS )zm
        Return the aligned sentence pair, reversing the directionality

        :rtype: AlignedSent
        )r   r   r   r   Úinvertr   r   r   r   rD   š   s    zAlignedSent.invert)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr   r   r   r   r   r   r,   r?   rC   rD   r   r   r   r   r      s   
	


)
r   c                   @   sV   e Zd ZdZdd„ Zedd„ ƒZdd„ Zdd	„ Zddd„Z	dd„ Z
dd„ Zdd„ Zd
S )r   ac  
    A storage class for representing alignment between two sequences, s1, s2.
    In general, an alignment is a set of tuples of the form (i, j, ...)
    representing an alignment between the i-th element of s1 and the
    j-th element of s2.  Tuples are extensible (they might contain
    additional data, such as a boolean to indicate sure vs possible alignments).

        >>> from nltk.translate import Alignment
        >>> a = Alignment([(0, 0), (0, 1), (1, 2), (2, 2)])
        >>> a.invert()
        Alignment([(0, 0), (1, 0), (2, 1), (2, 2)])
        >>> print(a.invert())
        0-0 1-0 2-1 2-2
        >>> a[0]
        [(0, 1), (0, 0)]
        >>> a.invert()[2]
        [(2, 1), (2, 2)]
        >>> b = Alignment([(0, 0), (0, 1)])
        >>> b.issubset(a)
        True
        >>> c = Alignment.fromstring('0-0 0-1')
        >>> b == c
        True
    c                 C   s:   t  | |¡}|t g ƒkr*tdd„ |D ƒƒnd|_d |_|S )Nc                 s   s   | ]}|d  V  qdS ©r   Nr   ©r   Úpr   r   r   r   ¿   r   z$Alignment.__new__.<locals>.<genexpr>r   )Ú	frozensetÚ__new__ÚmaxÚ_lenÚ_index)ÚclsÚpairsr
   r   r   r   rN   ½   s    $zAlignment.__new__c                 C   s   t dd„ | ¡ D ƒƒS )a�  
        Read a giza-formatted string and return an Alignment object.

            >>> Alignment.fromstring('0-0 2-1 9-2 21-3 10-4 7-5')
            Alignment([(0, 0), (2, 1), (7, 5), (9, 2), (10, 4), (21, 3)])

        :type s: str
        :param s: the positional alignments in giza format
        :rtype: Alignment
        :return: An Alignment object corresponding to the string representation ``s``.
        c                 S   s   g | ]}t |ƒ‘qS r   )Ú
_giza2pair)r   Úar   r   r   r"   Ñ   r   z(Alignment.fromstring.<locals>.<listcomp>)r   Úsplit)rR   r*   r   r   r   Ú
fromstringÃ   s    zAlignment.fromstringc                 C   s   | j s|  ¡  | j  |¡S )zN
        Look up the alignments that map from a given index or slice.
        )rQ   Ú_build_indexÚ__getitem__)r
   Úkeyr   r   r   rY   Ó   s    zAlignment.__getitem__c                 C   s   t dd„ | D ƒƒS )zI
        Return an Alignment object, being the inverted mapping.
        c                 s   s*   | ]"}|d  |d f|dd…  V  qdS )r%   r   é   Nr   rK   r   r   r   r   ß   r   z#Alignment.invert.<locals>.<genexpr>)r   r   r   r   r   rD   Û   s    zAlignment.invertNc                 C   sV   t ƒ }| js|  ¡  |s*ttt| jƒƒƒ}|D ]}| dd„ | j| D ƒ¡ q.t|ƒS )z�
        Work out the range of the mapping from the given positions.
        If no positions are specified, compute the range of the entire mapping.
        c                 s   s   | ]\}}|V  qd S r   r   )r   Ú_Úfr   r   r   r   ì   r   z"Alignment.range.<locals>.<genexpr>)ÚsetrQ   rX   Úlistr(   r   ÚupdateÚsorted)r
   Z	positionsÚimagerL   r   r   r   r(   á   s    zAlignment.rangec                 C   s   dt | ƒ S )úM
        Produce a Giza-formatted string representing the alignment.
        zAlignment(%r))ra   r   r   r   r   r   ï   s    zAlignment.__repr__c                 C   s   d  dd„ t| ƒD ƒ¡S )rc   r&   c                 s   s   | ]}d |dd…  V  qdS )z%d-%dNr[   r   rK   r   r   r   r   ù   r   z$Alignment.__str__.<locals>.<genexpr>)r   ra   r   r   r   r   rC   õ   s    zAlignment.__str__c                 C   s<   dd„ t | jd ƒD ƒ| _| D ]}| j|d   |¡ qdS )z€
        Build a list self._index such that self._index[i] is a list
        of the alignments originating from word i.
        c                 S   s   g | ]}g ‘qS r   r   )r   r\   r   r   r   r"      r   z*Alignment._build_index.<locals>.<listcomp>r%   r   N)r(   rP   rQ   Úappend)r
   rL   r   r   r   rX   û   s    zAlignment._build_index)N)rE   rF   rG   rH   rN   ÚclassmethodrW   rY   rD   r(   r   rC   rX   r   r   r   r   r   £   s   

r   c                 C   s   |   d¡\}}t|ƒt|ƒfS ©Nú-©rV   Úint)Úpair_stringr+   Újr   r   r   rT     s    rT   c                 C   s    |   d¡\}}}t|ƒt|ƒfS rf   rh   )rj   r+   rk   rL   r   r   r   Ú_naacl2pair
  s    rl   c                    sP   t |ƒtu sJ ‚t‡fdd„|D ƒƒs.tdƒ‚t‡ fdd„|D ƒƒsLtdƒ‚dS )ab  
    Check whether the alignments are legal.

    :param num_words: the number of source language words
    :type num_words: int
    :param num_mots: the number of target language words
    :type num_mots: int
    :param alignment: alignment to be checked
    :type alignment: Alignment
    :raise IndexError: if alignment falls outside the sentence
    c                 3   s*   | ]"}d |d    koˆ k n  V  qdS rJ   r   ©r   Úpair)Ú	num_wordsr   r   r     r   z#_check_alignment.<locals>.<genexpr>z&Alignment is outside boundary of wordsc                 3   s6   | ].}|d  du p,d|d    ko(ˆ k n  V  qdS )r%   Nr   r   rm   )Únum_motsr   r   r      r   z%Alignment is outside boundary of motsN)r	   r   ÚallÚ
IndexError)ro   rp   r   r   )rp   ro   r   r     s
    r   ÚPhraseTableEntryÚ
trg_phraseÚlog_probc                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚPhraseTablezs
    In-memory store of translations for a given phrase, and the log
    probability of the those translations
    c                 C   s   t ƒ | _d S r   )ÚdictÚsrc_phrasesr   r   r   r   r   -  s    zPhraseTable.__init__c                 C   s
   | j | S )aÅ  
        Get the translations for a source language phrase

        :param src_phrase: Source language phrase of interest
        :type src_phrase: tuple(str)

        :return: A list of target language phrases that are translations
            of ``src_phrase``, ordered in decreasing order of
            likelihood. Each list element is a tuple of the target
            phrase and its log probability.
        :rtype: list(PhraseTableEntry)
        ©rx   ©r
   Ú
src_phraser   r   r   Útranslations_for0  s    zPhraseTable.translations_forc                 C   sL   t ||d�}|| jvr g | j|< | j|  |¡ | j| jdd„ dd� dS )zä
        :type src_phrase: tuple(str)
        :type trg_phrase: tuple(str)

        :param log_prob: Log probability that given ``src_phrase``,
            ``trg_phrase`` is its translation
        :type log_prob: float
        )rt   ru   c                 S   s   | j S r   )ru   )r<   r   r   r   Ú<lambda>L  r   z!PhraseTable.add.<locals>.<lambda>T)rZ   ÚreverseN)rs   rx   rd   Úsort)r
   r{   rt   ru   Úentryr   r   r   Úadd?  s
    	

zPhraseTable.addc                 C   s
   || j v S r   ry   rz   r   r   r   Ú__contains__N  s    zPhraseTable.__contains__N)rE   rF   rG   rH   r   r|   r�   r‚   r   r   r   r   rv   '  s
   rv   )r4   Úcollectionsr   r   rM   r   rT   rl   r   rs   rv   r   r   r   r   Ú<module>   s    b