Example #1
0
def _getAM(data_dir, num_sent, max_iter, fn_AM, use_cached=True):
    """
    Parameters
    ----------
    data_dir    : (string) The top-level directory continaing the data 
    num_sent    : (int) the maximum number of training sentences to consider
    max_iter    : (int) the maximum number of iterations of the EM algorithm
    fn_AM       : (string) the location to save the alignment model
    use_cached  : (boolean) optionally load cached AM using pickle.

    Returns
    -------
    An alignment model 
    """
    if use_cached:
        try:
            path = os.getcwd() + '/' + fn_AM + '.pickle'
            with open(path, 'rb') as f:
                AM = pickle.load(f)
        except FileNotFoundError:
            AM = align_ibm1(data_dir, num_sent, max_iter, fn_AM)
    else:
        AM = align_ibm1(data_dir, num_sent, max_iter, fn_AM)

    return AM
Example #2
0
def _getAM(data_dir, num_sent, max_iter, fn_AM, use_cached=True):
    """
    Parameters
    ----------
    data_dir    : (string) The top-level directory continaing the data 
    num_sent    : (int) the maximum number of training sentences to consider
    max_iter    : (int) the maximum number of iterations of the EM algorithm
    fn_AM       : (string) the location to save the alignment model
    use_cached  : (boolean) optionally load cached AM using pickle.

    Returns
    -------
    An alignment model 
    """
    AM = {}

    if use_cached:
        fname = fn_AM + ".pickle"
        if os.isfile(fname):
            file = open(fname, "rb")
            AM = pickle.load(file)
    else:
        AM = align_ibm1(data_dir, num_sent, max_iter, fn_AM)

    return AM
Example #3
0
def main():

    #lme = pickle.load(open(fn_lme,"rb"))
    #lmf = pickle.load(open(fn_lmf,"rb"))

    lme = lm_train(train_dir, "e", "lm_eng")
    vocabSize = len(lme)
    data_size = [1000, 10000, 15000, 30000]
    AMs = []
    for i in data_size:
        AMs.append(align_ibm1(train_dir, i, max_iter))

    f_sens = read_file(testF, "f")
    e_sens = read_file(teste, "e")
    google_e_sens = read_file(google_teste, "e")

    for i in range(len(f_sens)):
        print("processing:{}".format(f_sens[i]))
        fre = f_sens[i]
        ref_1 = e_sens[i]
        ref_2 = google_e_sens[i]
        print("ref1:{} ref2:{}".format(ref_1, ref_2))
        for j in range(len(data_size)):
            eng = decode(fre, lme, AMs[j])
            print("AM{} translation:{}".format(j, eng))
            for n in range(1, 4):
                bleu = BLEU_score(eng, [ref_1, ref_2], n)
                print("{} bleu:{}".format(n, bleu))
def generateAM(filename, train_dir, num_sent, iter):
    fullname = filename + '.pickle'
    if os.path.isfile(fullname):
        with (open(fullname, "rb")) as openfile:
            AM = pickle.load(openfile)
    else:
        AM = align_ibm1(train_dir, num_sent, iter, filename)
    return AM
Example #5
0
def main():
    f = open("Task5.txt", "w+")
    f.write("################################################## \r\n")
    f.write("# SUMMARY OF BLEU SCORES ON TRANSLATED SENTENCES # \r\n")
    f.write("################################################## \r\n\n")

    fre_path = '../data/Hansard/Testing/Task5.f'  # '/u/cs401/A1/data/' # CHANGE FOR SUBMISSION
    fre_file = open(fre_path, 'r')
    fre_sentences = fre_file.readlines()
    # e_LM = lm_train("..data/Hansard/Training/", "e","")
    e_LM = load_LMs("../models/e_language_model.pickle")  # LOAD ENGLISH LM
    reference_paths = [
        "../data/Hansard/Testing/Task5.e",
        "../data/Hansard/Testing/Task5.google.e"
    ]  # /u/cs401/A2_SMT/data/Hansard/Testing/Task5.e # /u/cs401/A2 SMT/data/Hansard/Testing/Task5.google.e # CHANGE FOR SUBMISSION
    f.write(
        "TLDR ANALYSIS:\n\nIt is clear that the two references differ from one anther simply by looking at the bleue_score results. In the Task5.e testing file, there are rarely any non-zero outputs beyond those associated with n=1. However, in the Task5.google.e testing file, there are significantly more non-zero outputs for n=1 and even some non-zero bleue_score scores for n=2. This is an indication that translations can vary among the machine translation source.\n\nComparing to a variety of references may be better because there is no longer dependency on one particular translator and its specific word choices. Furthermore, comparing to multiple references keeps semantics under consideration. What is meant by this is that it is possible to translate a sentence accurately using two different sets of words, as the meaning is more important than the words and their ordering.\n\nExample: Translating “ce travail est difficile”.\nIt is arguable that both english sentences below are correct translations of this sentence:\n1. this job is difficult\n2. this work is hard\n\nHowever, there is not one matching bigram.\n\n"
    )
    for ref_path in reference_paths:
        ref_file = open(ref_path, 'r')
        references = ref_file.readlines()
        f.write("==================================================\n")
        f.write(" REFERENCE: {} \n".format(ref_path))
        f.write("==================================================\n")
        for sample_size in [2, 25, 35, 72]:
            # GIVEN sample_size, DEFINE EQUIVALENT NUMBER OF SENTENCES IN ALL FILES
            if sample_size == 2: num_sentences = 1
            if sample_size == 25: num_sentences = 10
            if sample_size == 35: num_sentences = 15
            if sample_size == 72: num_sentences = 30

            AM = align_ibm1("../data/Hansard/Training/", sample_size, 15,
                            "AM")  # 10, 5 work well
            f.write("\n---------------------------------------\n")
            f.write("Using AM Model trained on {}K sentences\n".format(
                num_sentences))
            f.write("---------------------------------------\n")

            sent_num = 1
            for sentence in fre_sentences:
                french = preprocess(sentence, "f")
                english = decode(french, e_LM, AM)
                f.write("Sentence {}:\n".format(sent_num))

                ref_file = open(ref_path, 'r')
                references = ref_file.readlines()
                for n in [1, 2, 3]:
                    bleu_score = BLEU_score(sentence, references, n)
                    f.write("n = {}: {}\n".format(n, bleu_score))
                f.write("\n")
                sent_num += 1
def evalAlign():
    """

    :return: NONe

    """
    max_iteration = 10
    report = open('Temp', 'w')
    train_dir = '/u/cs401/A2_SMT/data/Hansard/Training'
    LM = lm_train(train_dir, "e", "englishLM")
    vocab_size = len(LM["uni"])
    report.write("Vocab size = " + str(vocab_size) + "\n")
    report.write("\n")
    corpus_size = [1000, 10000, 15000, 30000]
    n = [1, 2, 3]
    ref_file1 = open('/u/cs401/A2_SMT/data/Hansard/Testing/Task5.e', 'r')
    ref_file2 = open('/u/cs401/A2_SMT/data/Hansard/Testing/Task5.google.e',
                     'r')
    test_file = open('/u/cs401/A2_SMT/data/Hansard/Testing/Task5.f', 'r')
    ref_file1_ref = ref_file1.readlines()
    ref_file2_ref = ref_file2.readlines()
    test_lines = test_file.readlines()
    #train IBM
    for i in corpus_size:
        f = "".join(['am_hansard_', str(i)])
        AM = align_ibm1(train_dir, i, max_iteration, f)
        report.write("Model with " + str(i) + " sentences. \n")
        for line in test_lines:
            report.write("The sentence is: " + line + '\n')
            line = preprocess(line, "f")
            trans = decode(line, LM, AM)
            report.write('\n')
            bl1 = BLEU_score(trans, ref_file1_ref, 1)
            bl2 = BLEU_score(trans, ref_file1_ref, 2)
            bl3 = BLEU_score(trans, ref_file1_ref, 3)

            bl_google_1 = BLEU_score(trans, ref_file2_ref, 1)
            bl_google_2 = BLEU_score(trans, ref_file2_ref, 2)
            bl_google_3 = BLEU_score(trans, ref_file2_ref, 3)

            report.write("BLEU SCORE: \n")
            report.write("GOOGLE || SELF || i \n")
            report.write(str(bl_google_1) + " || " + str(bl1) + "|| 1 \n")
            report.write(str(bl_google_2) + " || " + str(bl2) + " || 2 \n")
            report.write(str(bl_google_3) + " || " + str(bl3) + " || 3 \n")

            report.write(
                "====================================================================================\n"
            )
            report.write("\n")
Example #7
0
def _getAM(data_dir, num_sent, max_iter, fn_AM, use_cached=True):
    """
    Parameters
    ----------
    data_dir    : (string) The top-level directory continaing the data
    num_sent    : (int) the maximum number of training sentences to consider
    max_iter    : (int) the maximum number of iterations of the EM algorithm
    fn_AM       : (string) the location to save the alignment model
    use_cached  : (boolean) optionally load cached AM using pickle.

    Returns
    -------
    An alignment model
    """
    return align_ibm1(data_dir, num_sent, max_iter, fn_AM)
Example #8
0
def evalAlign(max_iter):
    ''' 
    Translate the 25 French sentences in /u/cs401/A2 SMT/data/Hansard/Testing/Task5.f
    with the decode function and evaluate them using corresponding reference sentences,
    specifically:
    
    1. /u/cs401/A2 SMT/data/Hansard/Testing/Task5.e, from the Hansards.
    2. /u/cs401/A2 SMT/data/Hansard/Testing/Task5.google.e, Google’s translations of the French phrases2.
    
    To evaluate each translation, use the BLEU score from lecture 6,
    
    Repeat this task with at least four alignment models (trained on 1K, 10K, 15K, and 30K
    sentences, respectively) and with three values of n in the BLEU score (i.e., n = 1, 2, 
    3). You should therefore have 25×4×3 BLEU scores in your evaluation.
    '''

    bleu = np.zeros(shape=(25, 4, 3))

    train_dir = "/u/cs401/A2_SMT/data/Hansard/Training/"
    LM = lm_train(train_dir, "e", "fn_LM_e")
    num_sentences = [1000, 10000, 15000, 30000]
    for n in range(len(num_sentences)):

        n_s = num_sentences[n]
        AM = align_ibm1(train_dir, n_s, max_iter, "fm_AM_e_{}".format(n_s))

        with open(
                "/u/cs401/A2_SMT/data/Hansard/Testing/Task5.f"
        ) as candidate_sentences, open(
                "/u/cs401/A2_SMT/data/Hansard/Testing/Task5.e") as ref_1, open(
                    "/u/cs401/A2_SMT/data/Hansard/Testing/Task5.google.e"
                ) as ref_2:
            candidate_sentences = candidate_sentences.readlines()
            ref_1 = ref_1.readlines()
            ref_2 = ref_2.readlines()
            for i in range(len(candidate_sentences)):
                sentence = candidate_sentences[i].strip()
                sentence = preprocess(sentence, "f")
                ref_1_sentence = preprocess(ref_1[i].strip(), "e")
                ref_2_sentence = preprocess(ref_2[i].strip(), "e")
                english = decode(sentence, LM, AM)
                bleu[i][n][0] = BLEU_score(english,
                                           [ref_1_sentence, ref_2_sentence], 1)
                bleu[i][n][1] = BLEU_score(english,
                                           [ref_1_sentence, ref_2_sentence], 2)
                bleu[i][n][2] = BLEU_score(english,
                                           [ref_1_sentence, ref_2_sentence], 3)
    return bleu
def evalAlign(hansard_english, hansard_french, google_english, LM_PATH,
              train_dir, fn_AM, report_path):
    """
    Evaluates the alignment model created by IBM-1 algorithm by comparing french to english translations from AM
    with translations from the hansard_english and google_english files using BLEU_score

    hansard_english: /Hansard/Testing/Task5.e
    hansard_french: /Hansard/Testing/Task5.f
    google_english: /Hansard/Testing/Task5.google.e
    LM_PATH: English language_model path
    train_dir: directory for training set
    fn_AM: path to save AM
    report_path: path to save Task5.txt report
    """
    # which file to write the report
    report_file = open(report_path, 'w')

    # read in all the sentences
    hansard_english = open(hansard_english).read().split('\n')
    hansard_french = open(hansard_french).read().split('\n')
    google_english = open(google_english).read().split('\n')

    # create in the language model
    LME = lm_train(train_dir, "e", LM_PATH)

    # decode and calculate blue score for AM models trained on different num_sentences
    # and BLEU_scores calculated on different n-grams
    for num_sentences in [1000, 10000, 15000, 30000]:
        AM = align_ibm1(train_dir, num_sentences, 5, fn_AM)
        for f in range(25):
            fproc = preprocess(hansard_french[f], 'f')
            for n in range(1, 4):
                my_english = decode(fproc, LME, AM)

                hans_ref = preprocess(hansard_english[f], 'e')
                ggle_ref = preprocess(google_english[f], 'e')
                references = [hans_ref, ggle_ref]
                print('MY-CANDIDATE: {}\nHANS-REF: {}\nGOOGLE-REF: {}'.format(
                    my_english, hans_ref, ggle_ref))

                score = BLEU_score(my_english, references, n)
                report_string = 'french-sentence: {}, my-english-sentence: {}, num-sentences-for-AM: {}, n-grams: {}, BLEU-score: {}'.format(
                    fproc, my_english, num_sentences, n, score)
                report_file.write(report_string + '\n')

    report_file.close()
    languages = ['e', 'f']
    for language in languages:
        LM = pickle.load(open(language + '.pickle', 'rb'))
        vocabSize = len(LM['uni'])
        for filename in os.listdir(data_dir):
            if filename.endswith('.' + language):
                f = open(os.path.join(data_dir, filename), 'r')
                for line in f:
                    line = line.rstrip()
                    sentence = preprocess(line, language)
                    log_p = log_prob(sentence, LM, smoothing, delta, vocabSize)
                    #print("{}\tProb: {}".format(sentence, log_p))
        for d in deltas:
            print("Language: {}, delta: {}, perp: {}".format(
                language, d, preplexity(LM, test_dir, language, smoothing, d)))

if 4 in test_parts:
    print("Testing part 4")
    num_sentences = 1000
    max_iter = 10
    AM = align_ibm1(data_dir, num_sentences, max_iter, fn_AM)
    from pprint import pprint
    pprint(AM)

if 5 in test_parts:
    print("Testing part 5")
    fn_LM = "/h/u16/c8/00/charronh/Desktop/CSC401/A2/"
    fn_AM = "/h/u16/c8/00/charronh/Desktop/CSC401/A2/"
    output_filename = "./Task5.txt"
    evalAlign(fn_LM, fn_AM, output_filename)
Example #11
0
from align_ibm1 import *

train_dir = '/u/cs401/A2_SMT/data/Hansard/Testing/'

#e,f = read_hansard('/u/cs401/A2_SMT/data/Hansard/Testing/', 9000)

am = align_ibm1(train_dir, 9000, 100, 'AMTest')
Example #12
0
def main():

    # we are supposed to use Training dir, Testing dir is for Task 5
    print(align_ibm1(train_dir, num_sentences, max_iter, output_path))
Example #13
0
import numpy as np
from math import log
from preprocess import *
from lm_train import *
from log_prob import *
from align_ibm1 import *
from decode import * 
import math

results = open("/h/u6/c7/05/solomahy/CSC401A2/CSC401_Assign2/Task5.txt", 'w')

smoothBlue = True

LM = lm_train("/u/cs401/A2_SMT/data/Hansard/Training/", "e", "~")
for num_sent in [1000, 10000, 15000, 30000]:
    AM = align_ibm1("/u/cs401/A2_SMT/data/Hansard/Training/", num_sent, 40, "~") 
    for n in [1, 2, 3]:
        print("\n\n-----------------")
        print("EVALUATING WITH: ", num_sent, " TOTAL TRAINING SENTENCES")
        print("VALUE OF BLEU SCORE N: ", n)

        results.write("\n\n\n-----------------")
        results.write("\nEVALUATING WITH: " + str(num_sent) + " TOTAL TRAINING SENTENCES")
        results.write("\nVALUE OF BLEU SCORE N: " + str(n) + "\n")

        with open("/u/cs401/A2_SMT/data/Hansard/Testing/Task5.f") as freFile, \
        open("/u/cs401/A2_SMT/data/Hansard/Testing/Task5.google.e") as groundTruth, \
        open("/u/cs401/A2_SMT/data/Hansard/Testing/Task5.e") as groundTruthHansard:
            accum = 0.0
            for freLine, engTruth1, engTruth2 in zip(freFile, groundTruth, groundTruthHansard):
                engSentence = decode(preprocess(freLine, 'f', add_null=True), LM, AM)
Example #14
0
				blueRef.append(newline)

			blue1 = BLEU_score(english, blueRef, 1)
			blue2 = BLEU_score(english, blueRef, 2)
			blue3 = BLEU_score(english, blueRef, 3)

			print(blue1, blue2, blue3)
			
	for i in buff:
		i.close()

if __name__ == "__main__":

	references = ["/u/cs401/A2_SMT/data/Hansard/Testing/Task5.e", "/u/cs401/A2_SMT/data/Hansard/Testing/Task5.google.e"]
	LM = pickle.load(open("/h/u9/g6/00/ndukaeto/CSC401/401/A2/New Folder/train_english.pickle", "rb"))

	train = "/h/u9/g6/00/ndukaeto/CSC401/401/A2/Hansard/Training/"
	test = "/u/cs401/A2_SMT/data/Hansard/Testing/Task5.f"


	1K = align_ibm1(train, 1000, 5, "/h/u9/g6/00/ndukaeto/CSC401/401/A2/New Folder/AM1K")
	evalAlign(test, references, LM, 1K)

	10K = align_ibm1(train, 10000, 5, "/h/u9/g6/00/ndukaeto/CSC401/401/A2/New Folder/AM10K")
	evalAlign(test, references, LM, 10K)
	
	15K = align_ibm1(train, 15000, 5, "/h/u9/g6/00/ndukaeto/CSC401/401/A2/New Folder/AM15K")
	evalAlign(test, references, LM, 15K)
	
	30K = align_ibm1(train, 30000, 5, "/h/u9/g6/00/ndukaeto/CSC401/401/A2/New Folder/AM30K")
	evalAlign(test, references, LM, 30K)
Example #15
0
def evalAlign(fn_LM, fn_AM, output_filename):
    """
	Produces translations from french to English, obtains reference translations from Google and the Hansards, and use the latter to evaluate the former, with a BLEU score
	
	INPUTS:
	fn_LM : 		    (string) the location of the the language model
	fn_AM : 		    (string) the location to save the alignment model
	output_filename :	(string) the location and filename of the output file (ex: path/Task5.txt)

	
	OUTPUT:
	None - writes to a file output_filename instead
	"""

    # For evaluation, translate the 25 French sentences in /u/cs401/A2_SMT/data/Hansard/Testing/Task5.f with the decode function and evaluate them using corresponding reference sentences, specifically:
    #   1. /u/cs401/A2_SMT/data/Hansard/Testing/Task5.e, from the Hansards.
    #   2. /u/cs401/A2_SMT/data/Hansard/Testing/Task5.google.e, Google’s translations of the French phrases
    # Test files
    f_test_file = "/u/cs401/A2_SMT/data/Hansard/Testing/Task5.f"
    e_test_file = "/u/cs401/A2_SMT/data/Hansard/Testing/Task5.e"
    google_e_test_file = "/u/cs401/A2_SMT/data/Hansard/Testing/Task5.google.e"

    # Training directory and target save directories
    train_dir = "/u/cs401/A2_SMT/data/Hansard/Training/"
    #train_dir = "/h/u16/c8/00/charronh/Desktop/CSC401/A2/"

    # Training English model from part 2
    language = 'e'
    LM = lm_train(train_dir, language, fn_LM)

    # Set up output file
    out_file = open(output_filename, 'w')
    out_file.write(
        "n,\tnumber_sentences,\tBLEU Score,\t\t\t\t#Line:\tOutputted English sentence\n"
    )

    # Training
    n_vals = [1, 2, 3]
    num_sentence_array = [1000, 10000, 15000, 30000]

    # Pre-train the models to save tons of time
    AM_array = []
    max_iter = 5
    for i in range(len(num_sentence_array)):
        print("Training AM for max_iter: {} with # of sentences: {}".format(
            max_iter, num_sentence_array[i]))
        temp_AM = align_ibm1(train_dir, num_sentence_array[i], max_iter, fn_AM)
        AM_array.append(temp_AM)
        # As per assignment specifications, save the model for 1k number of sentences
        if num_sentence_array[i] == 1000:
            with open('am.pickle', 'wb') as handle:
                pickle.dump(AM_array, handle, protocol=pickle.HIGHEST_PROTOCOL)

    # Handle decoding and BLEU score evaluation
    for n in n_vals:
        for i in range(len(num_sentence_array)):
            print("n: {}, num_sentences: {}".format(n, num_sentence_array[i]))

            # Get preprocessed french lines for testing
            f_f = open(f_test_file, 'r')
            f_google_e = open(google_e_test_file, 'r')
            f_e = open(e_test_file, 'r')

            line_count = 0
            while True:
                line_count += 1
                # English read line
                e_line = f_e.readline()
                e_line = e_line.rstrip()
                if not e_line: break
                e_line = preprocess(e_line, 'e')

                # Google English read line
                e_google_line = f_google_e.readline()
                e_google_line = e_google_line.rstrip()
                if not e_google_line: break
                e_google_line = preprocess(e_google_line, 'e')

                # French read line
                f_line = f_f.readline()
                f_line = f_line.rstrip()
                if not f_line: break
                f_line = preprocess(f_line, 'f')

                #print("")
                #print("English:\t{}".format(e_line))
                #print("Google E:\t{}".format(e_google_line))
                #print("French:\t\t{}".format(f_line))

                decoded_eng = decode(f_line, LM, AM_array[i])
                #print(decoded_eng)

                #bleu_score = BLEU_score(candidate, references, n)
                bleu_score = BLEU_score(decoded_eng, [e_line, e_google_line],
                                        n)
                out_file.write("{},\t{},\t\t\t\t{},\t\t\t\t\t{}:\t{}\n".format(
                    n, num_sentence_array[i], bleu_score, line_count,
                    decoded_eng))

            # Close files
            f_f.close()
            f_google_e.close()
            f_e.close()

    out_file.close()
Example #16
0
def main(args):
    """
    #TODO: Perform outlined tasks in assignment, like loading alignment
    models, computing BLEU scores etc.

    (You may use the helper functions)

    It's entirely upto you how you want to write Task5.txt. This is just
    an (sparse) example.
    """
    

    ## Write Results to Task5.txt (See e.g. Task5_eg.txt for ideation). ##

    '''
    f = open("Task5.txt", 'w+')
    f.write(discussion) 
    f.write("\n\n")
    f.write("-" * 10 + "Evaluation START" + "-" * 10 + "\n")

    for i, AM in enumerate(AMs):
        
        f.write(f"\n### Evaluating AM model: {AM_names[i]} ### \n")
        # Decode using AM #
        # Eval using 3 N-gram models #
        all_evals = []
        for n in range(1, 4):
            f.write(f"\nBLEU scores with N-gram (n) = {n}: ")
            evals = _get_BLEU_scores(...)
            for v in evals:
                f.write(f"\t{v:1.4f}")
            all_evals.append(evals)

        f.write("\n\n")

    f.write("-" * 10 + "Evaluation END" + "-" * 10 + "\n")
    f.close()
    '''

    # Task 3
    # Record into Task3.txt (or print) (for each language):
        # Perplexity of /u/cs401/A2_SMT/data/Hansard/Testing/ with:
            # no smoothing
            # delta = 0.2
            # delta = 0.4
            # delta = 0.6
            # delta = 0.8
            # delta = 1.0

    print("Task 3")
    print("Training English")
    LM_e = _getLM('/u/cs401/A2_SMT/data/Hansard/Training', 'e', 'LM_e', use_cached=False)
    print("Training French")
    LM_f = _getLM('/u/cs401/A2_SMT/data/Hansard/Training', 'f', 'LM_f', use_cached=False)
    t3 = []
    for lm in [(LM_e, 'e'), (LM_f, 'f')]:
        perps = []
        print("Perplexity for " + lm[1])
        perps.append(preplexity(lm[0], '/u/cs401/A2_SMT/data/Hansard/Testing', lm[1], smoothing = False, delta = 0))
        for d in [0.2, 0.4, 0.6, 0.8, 1.0]:
            perps.append(preplexity(lm[0], '/u/cs401/A2_SMT/data/Hansard/Testing', lm[1], smoothing = True, delta = d))
        t3.append(perps)

    k = 0
    with open("Task3.txt", 'w') as file:
        for line in t3:
            if k == 0:
                file.write('e\n')
            else:
                file.write('f\n')
            file.write(str(line) + '\n')
            k += 1

    # Task 5
    # AMs = []
    # For each num_sentences in [1000, 10000, 15000, 30000]:
        # Train AM on num_sentences
            # AM = align_ibm1('/u/cs401/A2_SMT/data/Hansard/Training/', num_sentences, 1000, "align" + str(num_sentences))
            # AMs.append(AM)
        # Iterate through lines in Task5.f, Task5.e, and Task5.google.e
        # simultaneously and put them in three lists
        # Make sure to preprocess them as you iterate
        # bleu_scores = []
        # for n in range(1,4):
            # call _get_BLEU_scores on the lists with n to get score
            # bleu_scores.append(score)
    # Finally write bleu_scores to Task5.txt

    print("Task 5")
    french = []
    with open('/u/cs401/A2_SMT/data/Hansard/Testing/Task5.f') as file:
        for line in file:
            french.append(preprocess(line, 'f'))

    hansard = []
    with open('/u/cs401/A2_SMT/data/Hansard/Testing/Task5.e') as file:
        for line in file:
            hansard.append(preprocess(line, 'e'))

    google = []
    with open('/u/cs401/A2_SMT/data/Hansard/Testing/Task5.google.e') as file:
        for line in file:
            google.append(preprocess(line, 'e'))

    bleu_scores = []
    MAX_ITER = 50
    for num_sentences in [1000, 10000, 15000, 30000]:
        print("Align " + str(num_sentences))
        AM = align_ibm1('/u/cs401/A2_SMT/data/Hansard/Training/', num_sentences, MAX_ITER, "align" + str(num_sentences))
        print("Decoding")
        english = []
        for f in french:
            english.append(decode.decode(f, LM_e, AM))
        print("Getting bleu scores for " + str(num_sentences))
        for n in range(1,4):
            score = _get_BLEU_scores(english, hansard, google, n)
            bleu_scores.append(score)

    i = 1
    j = 0
    lengths = [1000, 10000, 15000, 30000]
    with open("Task5.txt", 'w') as file:
        for line in bleu_scores:
            file.write(str(lengths[j]) + " " + str(((i-1)%3)+1) + '\n')
            file.write(str(line) + '\n')
            if (i % 3) == 0:
                j += 1
            i += 1