#!/usr/bin/env python
import argparse
import sys
import models
from alignment import marginalize, DecodingError

parser = argparse.ArgumentParser(description='Compute unnormalized translation probability by marginalizing over alignments.')
parser.add_argument('-i', '--input', dest='input', default='data/input', help='File containing sentences to translate (default=data/input)')
parser.add_argument('-t', '--translation-model', dest='tm', default='data/tm', help='File containing translation model (default=data/tm)')
parser.add_argument('-l', '--language-model', dest='lm', default='data/lm', help='File containing ARPA-format language model (default=data/lm)')
opts = parser.parse_args()

tm = models.TM(opts.tm,sys.maxint)
lm = models.LM(opts.lm)
french_sents = [tuple(line.strip().split()) for line in open(opts.input).readlines()]
english_sents = [tuple(line.strip().split()) for line in sys.stdin]
if (len(french_sents) != len(english_sents)):
    sys.stderr.write("ERROR: French and English files are not the same length! Only complete output can be graded!\n")
    sys.exit(1)

total_logprob = 0.0
unaligned_sentences = 0
for sent_num, (f, e) in enumerate(zip(french_sents, english_sents)):
    try:
        total_logprob += marginalize(e, f, lm, tm)
    except DecodingError:
        sys.stderr.write("ERROR: COULD NOT ALIGN SENTENCE %d\n" % sent_num)
        unaligned_sentences += 1

if unaligned_sentences > 0:
    sys.stderr.write("ERROR: There were %d unaligned sentences! Only sentences that align under the model can be graded!\n" % unaligned_sentences)
    sys.exit(1)

sys.stdout.write("%f\n" % total_logprob)

