from nltk.corpus import comtrans

"""
Explanation: Machine Translation in Python 3 with NLTK
(C) 2017 by Damir Cavar
Version: 1.0, November 2017
License: Creative Commons Attribution-ShareAlike 4.0 International License (CA BY-SA 4.0)
This is a brief introduction to the Machine Translation components in NLTK.
Loading an Aligned Corpus
Import the comtrans module from nltk.corpus.
End of explanation
"""


words = comtrans.words("alignment-en-fr.txt")

"""
Explanation: We can load a word-level alignment corpus for English and French from the NLTK dataset:
End of explanation
"""


for word in words[:20]:
    print(word)
print("...")

"""
Explanation: Print out the words in the corpus as a list:
End of explanation
"""


print(words[0])

"""
Explanation: Access a word by index in the list:
End of explanation
"""


als = comtrans.aligned_sents("alignment-en-fr.txt")[0]
als

print(" ".join(als.words))
print(" ".join(als.mots))

"""
Explanation: We can load the aligned sentences. Here we will load just one sentence, the firs one in the corpus:
End of explanation
"""


als.alignment

"""
Explanation: The alignments can be accessed via the alignment property:
End of explanation
"""


als.invert()

"""
Explanation: We can display the alignment using the invert function:
End of explanation
"""


from nltk.translate import Alignment, AlignedSent

"""
Explanation: We can also create alignments directly using the NLTK translate module. We import the translation modules from NLTK:
End of explanation
"""


als = AlignedSent( ["Reprise", "de", "la", "session" ], \
    ["Resumption", "of", "the", "session" ] , \
    Alignment( [ (0 , 0), (1 , 1), (2 , 2), (3 , 3) ] ) )

"""
Explanation: We can create an alignment example:
End of explanation
"""


from nltk.translate import IBMModel1

"""
Explanation: Translating with IBM Model 1 in NLTK
We already imported comtrans from NLTK in the code above. We have to import IBMModel1 from nltk.translate:
End of explanation
"""


com_ibm1 = IBMModel1(comtrans.aligned_sents()[:10], 100)

print(round(com_ibm1.translation_table["bitte"]["Please"], 3) )

print(round(com_ibm1.translation_table["Sitzungsperiode"]["session"] , 3) )

"""
Explanation: We can create an IBMModel1 using 20 iterations to run the learning algorithm using the first 10 sentences from the aligned corpus; see the EM explanation on the slides and the following publications:


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.
End of explanation
"""


