o
    rZŽhÀ/  ã                   @   s`   d Z ddlZddlmZ ddlmZmZmZmZ ddl	m
Z
 G dd„ deƒZG dd	„ d	e
ƒZdS )
a¸  
Lexical translation model that considers word order.

IBM Model 2 improves on Model 1 by accounting for word order.
An alignment probability is introduced, a(i | j,l,m), which predicts
a source word position, given its aligned target word's position.

The EM algorithm used in Model 2 is:

:E step: In the training data, collect counts, weighted by prior
         probabilities.

         - (a) count how many times a source language word is translated
               into a target language word
         - (b) count how many times a particular position in the source
               sentence is aligned to a particular position in the target
               sentence

:M step: Estimate new probabilities based on the counts from the E step

Notations
---------

:i: Position in the source sentence
     Valid values are 0 (for NULL), 1, 2, ..., length of source sentence
:j: Position in the target sentence
     Valid values are 1, 2, ..., length of target sentence
:l: Number of words in the source sentence, excluding NULL
:m: Number of words in the target sentence
:s: A word in the source language
:t: A word in the target language

References
----------

Philipp Koehn. 2010. Statistical Machine Translation.
Cambridge University Press, New York.

Peter E Brown, Stephen A. Della Pietra, Vincent J. Della Pietra, and
Robert L. Mercer. 1993. The Mathematics of Statistical Machine
Translation: Parameter Estimation. Computational Linguistics, 19 (2),
263-311.
é    N©Údefaultdict)ÚAlignedSentÚ	AlignmentÚIBMModelÚ	IBMModel1)ÚCountsc                       sb   e Zd ZdZd‡ f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‡  ZS )Ú	IBMModel2u`  
    Lexical translation model that considers word order

    >>> bitext = []
    >>> bitext.append(AlignedSent(['klein', 'ist', 'das', 'haus'], ['the', 'house', 'is', 'small']))
    >>> bitext.append(AlignedSent(['das', 'haus', 'ist', 'ja', 'groÃŸ'], ['the', 'house', 'is', 'big']))
    >>> bitext.append(AlignedSent(['das', 'buch', 'ist', 'ja', 'klein'], ['the', 'book', 'is', 'small']))
    >>> bitext.append(AlignedSent(['das', 'haus'], ['the', 'house']))
    >>> bitext.append(AlignedSent(['das', 'buch'], ['the', 'book']))
    >>> bitext.append(AlignedSent(['ein', 'buch'], ['a', 'book']))

    >>> ibm2 = IBMModel2(bitext, 5)

    >>> print(round(ibm2.translation_table['buch']['book'], 3))
    1.0
    >>> print(round(ibm2.translation_table['das']['book'], 3))
    0.0
    >>> print(round(ibm2.translation_table['buch'][None], 3))
    0.0
    >>> print(round(ibm2.translation_table['ja'][None], 3))
    0.0

    >>> print(round(ibm2.alignment_table[1][1][2][2], 3))
    0.939
    >>> print(round(ibm2.alignment_table[1][2][2][2], 3))
    0.0
    >>> print(round(ibm2.alignment_table[2][2][4][5], 3))
    1.0

    >>> test_sentence = bitext[2]
    >>> test_sentence.words
    ['das', 'buch', 'ist', 'ja', 'klein']
    >>> test_sentence.mots
    ['the', 'book', 'is', 'small']
    >>> test_sentence.alignment
    Alignment([(0, 0), (1, 1), (2, 2), (3, 2), (4, 3)])

    Nc                    sr   t ƒ  |¡ |du rt|d| ƒ}|j| _|  |¡ n
|d | _|d | _td|ƒD ]}|  |¡ q*|  |¡ dS )a™  
        Train on ``sentence_aligned_corpus`` and create a lexical
        translation model and an alignment model.

        Translation direction is from ``AlignedSent.mots`` to
        ``AlignedSent.words``.

        :param sentence_aligned_corpus: Sentence-aligned parallel corpus
        :type sentence_aligned_corpus: list(AlignedSent)

        :param iterations: Number of iterations to run training algorithm
        :type iterations: int

        :param probability_tables: Optional. Use this to pass in custom
            probability values. If not specified, probabilities will be
            set to a uniform distribution, or some other sensible value.
            If specified, all the following entries must be present:
            ``translation_table``, ``alignment_table``.
            See ``IBMModel`` for the type and purpose of these tables.
        :type probability_tables: dict[str]: object
        Né   Útranslation_tableÚalignment_tabler   )	ÚsuperÚ__init__r   r   Úset_uniform_probabilitiesr   ÚrangeÚtrainÚ	align_all)ÚselfÚsentence_aligned_corpusZ
iterationsZprobability_tablesZibm1Ún©Ú	__class__© úB/var/www/auris/lib/python3.10/site-packages/nltk/translate/ibm2.pyr   c   s   

zIBMModel2.__init__c           	      C   s¬   t ƒ }|D ]N}t|jƒ}t|jƒ}||f|vrS| ||f¡ d|d  }|tjk r4t dt	|ƒ d ¡ t
d|d ƒD ]}t
d|d ƒD ]}|| j| | | |< qDq;qd S )Né   zA source sentence is too long (z& words). Results may be less accurate.r   )ÚsetÚlenÚmotsÚwordsÚaddr   ÚMIN_PROBÚwarningsÚwarnÚstrr   r   )	r   r   Zl_m_combinationsÚaligned_sentenceÚlÚmZinitial_probÚiÚjr   r   r   r   Œ   s*   


ÿþÿÿ€òz#IBMModel2.set_uniform_probabilitiesc              
   C   sÔ   t ƒ }|D ]X}d g|j }dg|j }t|jƒ}t|jƒ}|  ||¡}td|d ƒD ]2}	||	 }
td|d ƒD ]$}|| }|  ||	||¡}|||
  }| |||
¡ | |||	||¡ q7q*q|  	|¡ |  
|¡ d S )NZUNUSEDr   r   )ÚModel2Countsr   r   r   Úprob_all_alignmentsr   Úprob_alignment_pointÚupdate_lexical_translationÚupdate_alignmentZ*maximize_lexical_translation_probabilitiesÚ maximize_alignment_probabilities)r   Úparallel_corpusÚcountsr$   Úsrc_sentenceÚtrg_sentencer%   r&   Ztotal_countr(   Útr'   ÚsÚcountZnormalized_countr   r   r   r       s&   

úþ
zIBMModel2.trainc                 C   s   t j}|j ¡ D ]=\}}| ¡ D ]4\}}| ¡ D ]+\}}|D ]$}	|j| | | |	 |j| | |	  }
t|
|ƒ| j| | | |	< qqqqd S ©N)r   r    Ú	alignmentÚitemsÚalignment_for_any_iÚmaxr   )r   r0   r    r'   Zj_sr(   Zsrc_sentence_lengthsr%   Ztrg_sentence_lengthsr&   Úestimater   r   r   r.   º   s   ÿÿûÿÿÿz*IBMModel2.maximize_alignment_probabilitiesc                 C   sX   t tƒ}tdt|ƒƒD ]}|| }tdt|ƒƒD ]}||  |  ||||¡7  < qq|S )aï  
        Computes the probability of all possible word alignments,
        expressed as a marginal distribution over target words t

        Each entry in the return value represents the contribution to
        the total alignment probability by the target word t.

        To obtain probability(alignment | src_sentence, trg_sentence),
        simply sum the entries in the return value.

        :return: Probability of t for all s in ``src_sentence``
        :rtype: dict(str): float
        r   r   )r   Úfloatr   r   r+   )r   r1   r2   Zalignment_prob_for_tr(   r3   r'   r   r   r   r*   Æ   s   
ÿÿzIBMModel2.prob_all_alignmentsc           	      C   sL   t |ƒd }t |ƒd }|| }|| }| j| | | j| | | |  S )zz
        Probability that position j in ``trg_sentence`` is aligned to
        position i in the ``src_sentence``
        r   )r   r   r   )	r   r'   r(   r1   r2   r%   r&   r4   r3   r   r   r   r+   Ý   s
   $zIBMModel2.prob_alignment_pointc           	      C   s†   d}t |jƒd }t |jƒd }t|jƒD ]'\}}|dkrq|j| }|j| }|| j| | | j| | | |  9 }qt|tj	ƒS )zc
        Probability of target sentence and an alignment given the
        source sentence
        g      ð?r   r   )
r   r1   r2   Ú	enumerater7   r   r   r:   r   r    )	r   Zalignment_infoZprobr%   r&   r(   r'   Útrg_wordÚsrc_wordr   r   r   Úprob_t_a_given_sè   s   

ÿÿzIBMModel2.prob_t_a_given_sc                 C   s   |D ]}|   |¡ qd S r6   )Úalign)r   r/   Úsentence_pairr   r   r   r   ý   s   ÿzIBMModel2.align_allc                 C   sÐ   g }t |jƒ}t |jƒ}t|jƒD ]O\}}| j| d | jd |d  | |  }t|tjƒ}d}t|jƒD ]"\}	}
| j| |
 | j|	d  |d  | |  }||krX|}|	}q6| 	||f¡ qt
|ƒ|_dS )a  
        Determines the best word alignment for one sentence pair from
        the corpus that the model was trained on.

        The best alignment will be set in ``sentence_pair`` when the
        method returns. In contrast with the internal implementation of
        IBM models, the word indices in the ``Alignment`` are zero-
        indexed, not one-indexed.

        :param sentence_pair: A sentence in the source language and its
            counterpart sentence in the target language
        :type sentence_pair: AlignedSent
        Nr   r   )r   r   r   r=   r   r   r:   r   r    Úappendr   r7   )r   rB   Zbest_alignmentr%   r&   r(   r>   Z	best_probZbest_alignment_pointr'   r?   Z
align_probr   r   r   rA     s*   

ÿÿÿÿ€zIBMModel2.alignr6   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r.   r*   r+   r@   r   rA   Ú__classcell__r   r   r   r   r	   ;   s    ')r	   c                       s0   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Z‡  ZS )r)   zo
    Data object to store counts of various parameters during training.
    Includes counts for alignment.
    c                    s*   t ƒ  ¡  tdd„ ƒ| _tdd„ ƒ| _d S )Nc                   S   ó   t dd„ ƒS )Nc                   S   rI   )Nc                   S   ó   t tƒS r6   ©r   r<   r   r   r   r   Ú<lambda>3  ó    zKModel2Counts.__init__.<locals>.<lambda>.<locals>.<lambda>.<locals>.<lambda>r   r   r   r   r   rL   3  ó    ú9Model2Counts.__init__.<locals>.<lambda>.<locals>.<lambda>r   r   r   r   r   rL   3  rN   z'Model2Counts.__init__.<locals>.<lambda>c                   S   rI   )Nc                   S   rJ   r6   rK   r   r   r   r   rL   6  rM   rO   r   r   r   r   r   rL   6  rN   )r   r   r   r7   r9   )r   r   r   r   r   0  s   
ÿ
ÿzModel2Counts.__init__c                 C   s,   | j | |  |7  < | j|  |7  < d S r6   )Z	t_given_sZany_t_given_s)r   r5   r4   r3   r   r   r   r,   9  s   z'Model2Counts.update_lexical_translationc                 C   s<   | j | | | |  |7  < | j| | |  |7  < d S r6   )r7   r9   )r   r5   r'   r(   r%   r&   r   r   r   r-   =  s   zModel2Counts.update_alignment)rD   rE   rF   rG   r   r,   r-   rH   r   r   r   r   r)   *  s
    	r)   )rG   r!   Úcollectionsr   Znltk.translater   r   r   r   Znltk.translate.ibm_modelr   r	   r)   r   r   r   r   Ú<module>   s   , p