B
    ²ô`I6  ã               @   s„   d Z ddlZddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z	 ddlm
Z
 dd	lmZ G d
d„ de	ƒZG dd„ deƒZdS )aà
  
Translation model that considers how a word can be aligned to
multiple words in another language.

IBM Model 3 improves on Model 2 by directly modeling the phenomenon
where a word in one language may be translated into zero or more words
in another. This is expressed by the fertility probability,
n(phi | source word).

If a source word translates into more than one word, it is possible to
generate sentences that have the same alignment in multiple ways. This
is modeled by a distortion step. The distortion probability, d(j|i,l,m),
predicts a target word position, given its aligned source word's
position. The distortion probability replaces the alignment probability
of Model 2.

The fertility probability is not applicable for NULL. Target words that
align to NULL are assumed to be distributed uniformly in the target
sentence. The existence of these words is modeled by p1, the probability
that a target word produced by a real source word requires another
target word that is produced by NULL.

The EM algorithm used in Model 3 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 target
             sentence is aligned to a particular position in the source
             sentence
         (c) count how many times a source word is aligned to phi number
             of target words
         (d) count how many times NULL is aligned to a target word

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

Because there are too many possible alignments, only the most probable
ones are considered. First, the best alignment is determined using prior
probabilities. Then, a hill climbing approach is used to find other good
candidates.


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
phi: Fertility, the number of target words produced by a source word
p1: Probability that a target word produced by a source word is
    accompanied by another target word that is aligned to NULL
p0: 1 - p1


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)Ú	factorial)ÚAlignedSent)Ú	Alignment)ÚIBMModel)Ú	IBMModel2)ÚCountsc                   sN   e Zd ZdZd‡ fdd„	Z‡ fdd„Zdd„ Zd	d
„ Zdd„ Zdd„ Z	‡  Z
S )Ú	IBMModel3u  
    Translation model that considers how a word can be aligned to
    multiple words in another language

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

    >>> ibm3 = IBMModel3(bitext, 5)

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

    >>> print(round(ibm3.distortion_table[1][1][2][2], 3))
    1.0
    >>> print(round(ibm3.distortion_table[1][2][2][2], 3))
    0.0
    >>> print(round(ibm3.distortion_table[2][2][4][5], 3))
    0.75

    >>> print(round(ibm3.fertility_table[2]['summarize'], 3))
    1.0
    >>> print(round(ibm3.fertility_table[1]['book'], 3))
    1.0

    >>> print(ibm3.p1)
    0.054...

    >>> 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, None), (4, 3)])

    Nc                sš   t t| ƒ |¡ |  ¡  |dkrFt||ƒ}|j| _|j| _|  |¡ n2|d | _|d | _|d | _|d | _	|d | _
xtd|ƒD ]}|  |¡ q„W dS )a  
        Train on ``sentence_aligned_corpus`` and create a lexical
        translation model, a distortion model, a fertility model, and a
        model for generating NULL-aligned words.

        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``,
            ``fertility_table``, ``p1``, ``distortion_table``.
            See ``IBMModel`` for the type and purpose of these tables.
        :type probability_tables: dict[str]: object
        NÚtranslation_tableÚalignment_tableÚfertility_tableÚp1Údistortion_tabler   )Úsuperr	   Ú__init__Úreset_probabilitiesr   r
   r   Úset_uniform_probabilitiesr   r   r   ÚrangeÚtrain)ÚselfÚsentence_aligned_corpusZ
iterationsZprobability_tablesZibm2Ún)Ú	__class__© úB/home/dcms/DCMS/lib/python3.7/site-packages/nltk/translate/ibm3.pyr   ‰   s    





zIBMModel3.__init__c                s$   t tˆ ƒ ¡  t‡ fdd„ƒˆ _d S )Nc                  s   t ‡ fdd„ƒS )Nc                  s   t ‡ fdd„ƒS )Nc                  s   t ‡ fdd„ƒS )Nc                  s   ˆ j S )N)ÚMIN_PROBr   )r   r   r   Ú<lambda>¹   ó    zeIBMModel3.reset_probabilities.<locals>.<lambda>.<locals>.<lambda>.<locals>.<lambda>.<locals>.<lambda>)r   r   )r   r   r   r   ¹   r   zSIBMModel3.reset_probabilities.<locals>.<lambda>.<locals>.<lambda>.<locals>.<lambda>)r   r   )r   r   r   r   ¹   r   zAIBMModel3.reset_probabilities.<locals>.<lambda>.<locals>.<lambda>)r   r   )r   r   r   r   ¸   s   z/IBMModel3.reset_probabilities.<locals>.<lambda>)r   r	   r   r   r   )r   )r   )r   r   r   µ   s    zIBMModel3.reset_probabilitiesc                s>  t ƒ }x¨|D ] }t|jƒ}t|jƒ}||f|kr| ||f¡ d| }|tjk rft dt	|ƒ d ¡ xDt
d|d ƒD ]2}x,t
d|d ƒD ]}|| j| | | |< qŠW qvW qW tdd„ ƒ| jd< tdd„ ƒ| jd< tdd„ ƒ| jd	< td
d„ ƒ| jd< d}	d|	d  ‰ x*t
d|	ƒD ]}
t‡ fdd„ƒ| j|
< qW d| _d S )Né   zA target sentence is too long (z& words). Results may be less accurate.r   c               S   s   dS )Ngš™™™™™É?r   r   r   r   r   r   Õ   r   z5IBMModel3.set_uniform_probabilities.<locals>.<lambda>c               S   s   dS )NgÍÌÌÌÌÌä?r   r   r   r   r   r   Ö   r   c               S   s   dS )Ngš™™™™™¹?r   r   r   r   r   r   ×   r   é   c               S   s   dS )Ng{®Gáz¤?r   r   r   r   r   r   Ø   r   é   é
   g{®Gáz„?é   c                  s   ˆ S )Nr   r   )Úinitial_fert_probr   r   r   Ü   r   g      à?)ÚsetÚlenÚmotsÚwordsÚaddr   r   ÚwarningsÚwarnÚstrr   r   r   r   r   )r   r   Zl_m_combinationsÚaligned_sentenceÚlÚmZinitial_probÚjÚiZMAX_FERTILITYÚphir   )r#   r   r   Á   s,    



"z#IBMModel3.set_uniform_probabilitiesc          
   C   sþ   t ƒ }x¶|D ]®}t|jƒ}t|jƒ}|  |¡\}}t| ¡ ƒ|_|  |¡}xn|D ]f}	|  	|	¡}
|
| }x6t
d|d ƒD ]$}| ||	|¡ | ||	|||¡ qvW | ||	¡ | ||	¡ qPW qW | j}|  ¡  || _|  |¡ |  |¡ |  |¡ |  |¡ d S )Nr   )ÚModel3Countsr%   r&   r'   Úsampler   Zzero_indexed_alignmentÚ	alignmentZprob_of_alignmentsÚprob_t_a_given_sr   Zupdate_lexical_translationÚupdate_distortionZupdate_null_generationZupdate_fertilityr   r   Z*maximize_lexical_translation_probabilitiesÚ!maximize_distortion_probabilitiesZ maximize_fertility_probabilitiesZ&maximize_null_generation_probabilities)r   Zparallel_corpusÚcountsr,   r-   r.   Zsampled_alignmentsZbest_alignmentZtotal_countÚalignment_infoÚcountZnormalized_countr/   Zexisting_alignment_tabler   r   r   r   à   s0    









zIBMModel3.trainc             C   s    t j}x”|j ¡ D ]†\}}x|| ¡ D ]p\}}xf| ¡ D ]Z\}}xP|D ]H}	|j| | | |	 |j| | |	  }
t|
|ƒ| j| | | |	< qDW q6W q$W qW d S )N)r   r   Ú
distortionÚitemsÚdistortion_for_any_jÚmaxr   )r   r8   r   r/   Zi_sr0   Zsrc_sentence_lengthsr-   Ztrg_sentence_lengthsr.   Zestimater   r   r   r7   	  s    
z+IBMModel3.maximize_distortion_probabilitiesc             C   sh  |j }|j}t|ƒd }t|ƒd }| j}d| }d}tj}	| d¡}
|t||
ƒt||d|
  ƒ 9 }||	k rr|	S x:td|
d ƒD ](}|||
 | d | 9 }||	k r‚|	S q‚W xJtd|d ƒD ]8}| |¡}|t	|ƒ| j
| ||   9 }||	k r¾|	S q¾W xhtd|d ƒD ]V}|| }|j| }|| }|| j| | | j| | | |  9 }||	k r
|	S q
W |S )zc
        Probability of target sentence and an alignment given the
        source sentence
        r   g      ð?r   r   )Úsrc_sentenceÚtrg_sentencer%   r   r   r   Zfertility_of_iÚpowr   r   r   r4   r
   r   )r   r9   r?   r@   r-   r.   r   Zp0Zprobabilityr   Znull_fertilityr0   Z	fertilityr/   ÚtÚsr   r   r   r5     s>    
 

&

zIBMModel3.prob_t_a_given_s)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r7   r5   Ú__classcell__r   r   )r   r   r	   W   s   0,)r	   c                   s(   e Zd ZdZ‡ fdd„Zdd„ Z‡  ZS )r2   zp
    Data object to store counts of various parameters during training.
    Includes counts for distortion.
    c                s.   t t| ƒ ¡  tdd„ ƒ| _tdd„ ƒ| _d S )Nc               S   s   t dd„ ƒS )Nc               S   s   t dd„ ƒS )Nc               S   s   t dd„ ƒS )Nc               S   s   dS )Ng        r   r   r   r   r   r   Q  r   z]Model3Counts.__init__.<locals>.<lambda>.<locals>.<lambda>.<locals>.<lambda>.<locals>.<lambda>)r   r   r   r   r   r   Q  r   zKModel3Counts.__init__.<locals>.<lambda>.<locals>.<lambda>.<locals>.<lambda>)r   r   r   r   r   r   Q  r   z9Model3Counts.__init__.<locals>.<lambda>.<locals>.<lambda>)r   r   r   r   r   r   Q  r   z'Model3Counts.__init__.<locals>.<lambda>c               S   s   t dd„ ƒS )Nc               S   s   t dd„ ƒS )Nc               S   s   dS )Ng        r   r   r   r   r   r   T  r   zKModel3Counts.__init__.<locals>.<lambda>.<locals>.<lambda>.<locals>.<lambda>)r   r   r   r   r   r   T  r   z9Model3Counts.__init__.<locals>.<lambda>.<locals>.<lambda>)r   r   r   r   r   r   T  r   )r   r2   r   r   r;   r=   )r   )r   r   r   r   N  s
    zModel3Counts.__init__c             C   sF   |j | }| j| | | |  |7  < | j| | |  |7  < d S )N)r4   r;   r=   )r   r:   r9   r/   r-   r.   r0   r   r   r   r6   W  s    
zModel3Counts.update_distortion)rD   rE   rF   rG   r   r6   rH   r   r   )r   r   r2   H  s   	r2   )rG   r)   Úcollectionsr   Úmathr   Znltk.translater   r   r   r   Znltk.translate.ibm_modelr   r	   r2   r   r   r   r   Ú<module>J   s    r