+
    ÖÇÜhÀ/  ã                   ón   € R t ^ RIt^ RIHt ^ RIHtHtHtHt ^ RI	H
t
  ! R R]4      t ! R R]
4      tR# )	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                   ój   a a€ ] tR t^;t oRtRV 3R lltR tR tR tR t	R t
R tR	 tR
 tRtVtV ;t# )Ú	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)])

c                ó<  <€ \         SV `  V4       Vf7   \        V^V,          4      pVP                  V n        V P	                  V4       MVR,          V n        VR,          V n        \        ^ V4       F  pV P                  V4       K  	  V P                  V4       R# )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_table)	ÚsuperÚ__init__r   r   Úset_uniform_probabilitiesr   ÚrangeÚtrainÚ	align_all)ÚselfÚsentence_aligned_corpusÚ
iterationsÚprobability_tablesÚibm1ÚnÚ	__class__s   &&&&  €Ú5/usr/lib/python3/dist-packages/nltk/translate/ibm2.pyr   ÚIBMModel2.__init__c   s”   ø€ ô, 	‰ÑÐ0Ô1àÒ%ô Ð4°a¸*µnÓEˆDØ%)×%;Ñ%;ˆDÔ"Ø×*Ñ*Ð+BÕCð &8Ð8KÕ%LˆDÔ"Ø#5Ð6GÕ#HˆDÔ ä�q˜*Ö%ˆAØ�J‰JÐ.Ö/ñ &ð 	�‰Ð.Ö/ó    c                ó
  € \        4       pV Fò  p\        VP                  4      p\        VP                  4      pWE3V9  g   K7  VP	                  WE34       ^V^,           ,          pV\
        P                  8  d.   \        P                  ! R\        V4      ,           R,           4       \        ^ V^,           4       F@  p\        ^V^,           4       F&  pW`P                  V,          V,          V,          V&   K(  	  KB  	  Kô  	  R# )é   zA source sentence is too long (z& words). Results may be less accurate.N)ÚsetÚlenÚmotsÚwordsÚaddr   ÚMIN_PROBÚwarningsÚwarnÚstrr   r   )	r   r   Úl_m_combinationsÚaligned_sentenceÚlÚmÚinitial_probÚiÚjs	   &&       r   r   Ú#IBMModel2.set_uniform_probabilitiesŒ   sØ   € ä›5ÐÛ 7ÐÜÐ$×)Ñ)Ó*ˆAÜÐ$×*Ñ*Ó+ˆAØˆvÐ-Ö-Ø ×$Ñ$ a VÔ,Ø  A¨¥E�{�Ø¤(×"3Ñ"3Ô3Ü—M’MØ9Ü˜a›&õ!àBõCôô ˜q ! a¥%ž�AÜ" 1 a¨!¥ež_˜Ø;G×,Ñ,¨QÕ/°Õ2°1Õ5°aÓ8ó -ó )ó !8r   c           
     óJ  € \        4       pV Fð  pR .VP                  ,           pR.VP                  ,           p\        VP                  4      p\        VP                  4      pV P	                  WE4      p\        ^V^,           4       Fs  p	WY,          p
\        ^ V^,           4       FQ  pWK,          pV P                  W¹WE4      pWØV
,          ,          pVP                  WìV
4       VP                  WëW–V4       KS  	  Ku  	  Kò  	  V P                  V4       V P                  V4       R # )NÚUNUSED)ÚModel2Countsr"   r#   r!   Úprob_all_alignmentsr   Úprob_alignment_pointÚupdate_lexical_translationÚupdate_alignmentÚ*maximize_lexical_translation_probabilitiesÚ maximize_alignment_probabilities)r   Úparallel_corpusÚcountsr*   Úsrc_sentenceÚtrg_sentencer+   r,   Útotal_countr/   Útr.   ÚsÚcountÚnormalized_counts   &&             r   r   ÚIBMModel2.train    s  € Ü“ˆÛ /ÐØ ˜6Ð$4×$9Ñ$9Õ9ˆLØ$˜:Ð(8×(>Ñ(>Õ>ˆLÜÐ$×)Ñ)Ó*ˆAÜÐ$×*Ñ*Ó+ˆAð ×2Ñ2°<ÓNˆKô ˜1˜a !�e–_�Ø •O�Ü˜q ! a¥%ž�AØ$��AØ ×5Ñ5°a¸LÓW�EØ',¸1­~Õ'=Ð$à×5Ñ5Ð6FÈ1ÔMØ×+Ñ+Ð,<ÀÀqÖIó )ó %ñ !0ð* 	×7Ñ7¸Ô?Ø×-Ñ-¨fÖ5r   c                óØ  € \         P                  pVP                  P                  4        F»  w  r4VP                  4        F¢  w  rVVP                  4        F‰  w  rxV F~  p	VP                  V,          V,          V,          V	,          VP                  V,          V,          V	,          ,          p
\        W¢4      V P                  V,          V,          V,          V	&   K€  	  K‹  	  K¤  	  K½  	  R # ©N)r   r%   Ú	alignmentÚitemsÚalignment_for_any_iÚmaxr   )r   r;   r%   r.   Új_sr/   Úsrc_sentence_lengthsr+   Útrg_sentence_lengthsr,   Úestimates   &&         r   r9   Ú*IBMModel2.maximize_alignment_probabilitiesº   sÂ   € Ü×$Ñ$ˆØ×&Ñ&×,Ñ,Ö.‰FˆAØ+.¯9©9®;Ñ'�Ø/C×/IÑ/IÖ/KÑ+�AÛ1˜à"×,Ñ,¨QÕ/°Õ2°1Õ5°aÕ8Ø$×8Ñ8¸Õ;¸AÕ>¸qÕAõBð !ô <?¸xÓ;R˜×,Ñ,¨QÕ/°Õ2°1Õ5°aÓ8ó 2ó 0Ló ,7ó /r   c                óì   € \        \        4      p\        ^\        V4      4       FK  pW$,          p\        ^ \        V4      4       F'  pW5;;,          V P	                  WdW4      ,          uu&   K)  	  KM  	  V# )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   Úfloatr   r!   r5   )r   r<   r=   Úalignment_prob_for_tr/   r?   r.   s   &&&    r   r4   ÚIBMModel2.prob_all_alignmentsÆ   sj   € ô  +¬5Ó1ÐÜ�qœ#˜lÓ+Ö,ˆAØ•ˆAÜ˜1œc ,Ó/Ö0�Ø$×'¨4×+DÑ+DØ˜,ó,õ Õ'ó 1ñ -ð $Ð#r   c                óø   € \        V4      ^,
          p\        V4      ^,
          pW1,          pWB,          pV P                  V,          V,          V P                  V,          V,          V,          V,          ,          # )zb
Probability that position j in ``trg_sentence`` is aligned to
position i in the ``src_sentence``
)r!   r   r   )	r   r.   r/   r<   r=   r+   r,   r@   r?   s	   &&&&&    r   r5   ÚIBMModel2.prob_alignment_pointÝ   se   € ô
 �Ó Õ!ˆÜ�Ó Õ!ˆØ�OˆØ�OˆØ×%Ñ% aÕ(¨Õ+¨d×.BÑ.BÀ1Õ.EÀaÕ.HÈÕ.KÈAÕ.NÕNÐNr   c                óà  € Rp\        VP                  4      ^,
          p\        VP                  4      ^,
          p\        VP                  4       F‚  w  rVV^ 8X  d   K  VP                  V,          pVP                  V,          pVV P
                  V,          V,          V P                  V,          V,          V,          V,          ,          ,          pK„  	  \        V\        P                  4      # )zK
Probability of target sentence and an alignment given the
source sentence
g      ð?)
r!   r<   r=   Ú	enumeraterF   r   r   rI   r   r%   )	r   Úalignment_infoÚprobr+   r,   r/   r.   Útrg_wordÚsrc_words	   &&       r   Úprob_t_a_given_sÚIBMModel2.prob_t_a_given_sè   sÈ   € ð
 ˆÜ�×+Ñ+Ó,¨qÕ0ˆÜ�×+Ñ+Ó,¨qÕ0ˆä˜n×6Ñ6Ö7‰DˆAØ�AŒvÙØ%×2Ñ2°1Õ5ˆHØ%×2Ñ2°1Õ5ˆHØØ×&Ñ& xÕ0°Õ:Ø×&Ñ& qÕ)¨!Õ,¨QÕ/°Õ2õ3õŠDñ 8ô �4œ×*Ñ*Ó+Ð+r   c                ó:   € V F  pV P                  V4       K  	  R # rE   )Úalign)r   r:   Úsentence_pairs   && r   r   ÚIBMModel2.align_allý   s   € Û,ˆMØ�J‰J�}Ö%ó -r   c                ó´  € . p\        VP                  4      p\        VP                  4      p\        VP                  4       EF  w  rVV P                  V,          R,          V P
                  ^ ,          V^,           ,          V,          V,          ,          p\        V\        P                  4      pRp\        VP                  4       Ff  w  ršV P                  V,          V
,          V P
                  V	^,           ,          V^,           ,          V,          V,          ,          pW·8¼  g   Kb  TpT	pKh  	  VP                  WX34       EK  	  \        V4      Vn        R# )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
N)r!   r"   r#   rV   r   r   rI   r   r%   Úappendr   rF   )r   r_   Úbest_alignmentr+   r,   r/   rY   Ú	best_probÚbest_alignment_pointr.   rZ   Ú
align_probs   &&          r   r^   ÚIBMModel2.align  s(  € ð ˆä�×"Ñ"Ó#ˆÜ�×#Ñ#Ó$ˆä$ ]×%8Ñ%8×9‰KˆAð ×&Ñ& xÕ0°Õ6Ø×&Ñ& qÕ)¨!¨a­%Õ0°Õ3°AÕ6õ7ð ô ˜I¤x×'8Ñ'8Ó9ˆIØ#'Ð Ü(¨×);Ñ);Ö<‘�à×*Ñ*¨8Õ4°XÕ>Ø×*Ñ*¨1¨q­5Õ1°!°aµ%Õ8¸Õ;¸AÕ>õ?ð ð Ö*Ø *�IØ+,Ò(ñ  =ð ×!Ñ! 1Ð";×<ñ# :ô& #,¨NÓ";ˆÖr   )r   r   rE   )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   r   r   r9   r4   r5   r[   r   r^   Ú__static_attributes__Ú__classdictcell__Ú__classcell__©r   Ú__classdict__s   @@r   r
   r
   ;   sA   ù‡ € ñ%÷N'0òRHò(6ò4
Sò$ò.	Oò,ò*&÷&<ò &<r   r
   c                   óB   a a€ ] tR tRt oRtV 3R ltR tR tRtVt	V ;t
# )r3   i*  zc
Data object to store counts of various parameters during training.
Includes counts for alignment.
c                óh   <€ \         SV `  4        \        R  4      V n        \        R 4      V n        R# )c                  ó   € \        R  4      # )c                  ó   € \        R  4      # )c                  ó    € \        \        4      # rE   ©r   rP   © r   r   Ú<lambda>ÚKModel2Counts.__init__.<locals>.<lambda>.<locals>.<lambda>.<locals>.<lambda>3  s	   € ¼KÌÔ<Nr   r   rx   r   r   ry   Ú9Model2Counts.__init__.<locals>.<lambda>.<locals>.<lambda>3  s
   € ¬Ñ4NÔ(Or   r   rx   r   r   ry   Ú'Model2Counts.__init__.<locals>.<lambda>3  s
   € ”KÑ OÔPr   c                  ó   € \        R  4      # )c                  ó    € \        \        4      # rE   rw   rx   r   r   ry   r{   6  s	   € ¬´EÔ(:r   r   rx   r   r   ry   r|   6  s
   € ”KÑ :Ô;r   N)r   r   r   rF   rH   )r   r   s   &€r   r   ÚModel2Counts.__init__0  s/   ø€ Ü‰ÑÔÜ$ÙPó
ˆŒô $/Ù;ó$
ˆÖ r   c                ó�   € V P                   V,          V;;,          V,          uu&   V P                  V;;,          V,          uu&   R # rE   )Ú	t_given_sÚany_t_given_s)r   rA   r@   r?   s   &&&&r   r6   Ú'Model2Counts.update_lexical_translation9  s1   € Ø�‰�qÕ˜!× Õ%ÓØ×Ñ˜1× Õ&Õr   c                óÈ   € V P                   V,          V,          V,          V;;,          V,          uu&   V P                  V,          V,          V;;,          V,          uu&   R # rE   ©rF   rH   )r   rA   r.   r/   r+   r,   s   &&&&&&r   r7   ÚModel2Counts.update_alignment=  sE   € Ø�‰�qÕ˜!Õ˜QÕ ×" eÕ+Ó"Ø× Ñ  Õ# AÕ& q×)¨UÕ2Õ)r   r…   )rh   ri   rj   rk   rl   r   r6   r7   rm   rn   ro   rp   s   @@r   r3   r3   *  s   ù‡ € ñõ

ò'÷3ò 3r   r3   )rl   r&   Úcollectionsr   Únltk.translater   r   r   r   Únltk.translate.ibm_modelr   r
   r3   rx   r   r   Ú<module>rŠ      s7   ðñ*óX Ý #ç FÓ FÝ +ôl<�ô l<ô^3�6ö 3r   