import LanguageModel
import math
EnglishFile = './training.en'
SpanishFile = './training.es'
GermanFile = './training.de'
testFile = './test'
N = 3
def sign(n):
    if n>0:
        return 1
    else:
        return -1
     
def RunExps(K):
    L = []
    for i in range(K):
        L.append(i+1)
        
    print "Building "+str(L)+"-Gram Models"
    EnLM = LanguageModel.LanguageModel(EnglishFile,K)
    EnLM.buildCountTables()
    EnLM.writeFile('./outEnFile.txt')

    EsLM = LanguageModel.LanguageModel(SpanishFile,K)
    EsLM.buildCountTables()
    EsLM.writeFile('./outEsFile.txt')

    DeLM = LanguageModel.LanguageModel(GermanFile,K)
    DeLM.buildCountTables()
    DeLM.writeFile('./outDeFile.txt')
    DP = []  # Descrimination Performance 
    APD = []
    for i in range(K):
        KDP = []
        KAPD = []
        print "Computing Perplexities of Test Data, using "+str(i+1)+"-Gram Model"
        pEn = math.exp(EnLM.getPerplexityUnsmooth(testFile,i+1))
        pEs = math.exp(EsLM.getPerplexityUnsmooth(testFile,i+1))
        pDe = math.exp(DeLM.getPerplexityUnsmooth(testFile,i+1))
        print "Perplexity without smoothing of Test Data using the "+str(i+1)+"-gram "+ "English Language Model is: " + str(pEn)
        print "Perplexity without smoothing of Test Data using the "+str(i+1)+"-gram "+ "Spanish Language Model is: "+ str(pEs)
        print "Perplexity without smoothing of Test Data using the "+str(i+1)+"-gram "+ "German Language Model is: " + str(pDe)+"\n"
        KDP.append((sign(pEs-pEn)+sign(pDe-pEn))/2)
        KAPD.append(((pEs-pEn)+(pDe-pEn))/2.0)
        
        pEn = math.exp(EnLM.getPerplexityLaplace(testFile,i+1))
        pEs = math.exp(EsLM.getPerplexityLaplace(testFile,i+1))
        pDe = math.exp(DeLM.getPerplexityLaplace(testFile,i+1))
        print "Perplexity with Laplace smoothing of Test Data using the "+str(i+1)+"-gram "+ " English Language Model is: " + str(pEn)
        print "Perplexity with Laplace smoothing of Test Data using the "+str(i+1)+"-gram "+ " Spanish Language Model is: "+ str(pEs)
        print "Perplexity with Laplace smoothing of Test Data using the "+str(i+1)+"-gram "+ " German Language Model is: " + str(pDe)+"\n"
        KDP.append((sign(pEs-pEn)+sign(pDe-pEn))/2)
        KAPD.append(((pEs-pEn)+(pDe-pEn))/2.0)
        
        pEn = math.exp(EnLM.getPerplexityBackoff(testFile,i+1))
        pEs = math.exp(EsLM.getPerplexityBackoff(testFile,i+1))
        pDe = math.exp(DeLM.getPerplexityBackoff(testFile,i+1))
        print "Perplexity with Backoff smoothing of Test Data using the "+str(i+1)+"-gram "+ " English Language Model is: " + str(pEn)
        print "Perplexity with Backoff smoothing of Test Data using the "+str(i+1)+"-gram "+ " Spanish Language Model is: "+ str(pEs)
        print "Perplexity with Backoff smoothing of Test Data using the "+str(i+1)+"-gram "+ " German Language Model is: " + str(pDe)+"\n"
        KDP.append((sign(pEs-pEn)+sign(pDe-pEn))/2)
        KAPD.append(((pEs-pEn)+(pDe-pEn))/2.0)
        
        pEn = math.exp(EnLM.getPerplexityInterpolate(testFile,i+1))
        pEs = math.exp(EsLM.getPerplexityInterpolate(testFile,i+1))
        pDe = math.exp(DeLM.getPerplexityInterpolate(testFile,i+1))
        print "Perplexity with Interpolate  of Test Data using the "+str(i+1)+"-gram "+ " English Language Model is: " + str(pEn)
        print "Perplexity with Interpolate  of Test Data using the "+str(i+1)+"-gram "+ " Spanish Language Model is: "+ str(pEs)
        print "Perplexity with Interpolate  of Test Data using the "+str(i+1)+"-gram "+ " German Language Model is: " + str(pDe)+"\n"
        KDP.append((sign(pEs-pEn)+sign(pDe-pEn))/2)
        KAPD.append(((pEs-pEn)+(pDe-pEn))/2.0)
        
        pEn = math.exp(EnLM.getPerplexityInterpolateSmooth(testFile,i+1))
        pEs = math.exp(EsLM.getPerplexityInterpolateSmooth(testFile,i+1))
        pDe = math.exp(DeLM.getPerplexityInterpolateSmooth(testFile,i+1))
        print "Perplexity with Interpolate smoothing of Test Data using the "+str(i+1)+"-gram "+ " English Language Model is: " + str(pEn)
        print "Perplexity with Interpolate smoothing of Test Data using the "+str(i+1)+"-gram "+ " Spanish Language Model is: "+ str(pEs)
        print "Perplexity with Interpolate smoothing of Test Data using the "+str(i+1)+"-gram "+ " German Language Model is: " + str(pDe)+"\n"
        KDP.append((sign(pEs-pEn)+sign(pDe-pEn))/2)
        KAPD.append(((pEs-pEn)+(pDe-pEn))/2.0)
        
        pEn = math.exp(EnLM.getPerplexityInterpolateFreq(testFile,i+1))
        pEs = math.exp(EsLM.getPerplexityInterpolateFreq(testFile,i+1))
        pDe = math.exp(DeLM.getPerplexityInterpolateFreq(testFile,i+1))
        print "Perplexity with Frequency Interpolate  of Test Data using the "+str(i+1)+"-gram "+ " English Language Model is: " + str(pEn)
        print "Perplexity with Frequency Interpolate  of Test Data using the "+str(i+1)+"-gram "+ " Spanish Language Model is: "+ str(pEs)
        print "Perplexity with Frequency Interpolate  of Test Data using the "+str(i+1)+"-gram "+ " German Language Model is: " + str(pDe)
        KDP.append((sign(pEs-pEn)+sign(pDe-pEn))/2)
        KAPD.append(((pEs-pEn)+(pDe-pEn))/2.0)
        print "...........................................................................................\n"
        
        DP.append(KDP)
        APD.append(KAPD)
    
    print " Average Perplexity............................................................................\n"    
    for i in range(K):
        print str(APD[i])+"\n"
    print " Average Descrimination Power............................................................................\n"
    
    for i in range(K):
        print str(DP[i])+"\n"
        
        
RunExps(N)