# for ginen dataset
#gen = open('outputs/wordgen_big.txt', "r")
#ans = open('../wordans.txt', "r")
#out = open('outputs/final_output_big.txt', "w")
#score = open('outputs/scores_big.txt', "w")

#for validation dataset
gen = open('outputs/wordgen_big_val.txt', "r")
ans = open('../wordans_val.txt', "r")
out = open('outputs/final_output_big_val.txt', "w")
score = open('outputs/scores_big_val.txt', "w")

ins=0
dels=0
hits=0

for genline in gen:
    ansline = ans.readline()
    genar = genline.split()
    ansar = ansline.split()
    thits = 0
    tdels = 0
    tins = 0
    for i in range(len(ansar)-1):
        found=False
        for j in range(len(genar)-1):
            if ansar[i][len(ansar[i])-1] == genar[j][len(genar[j])-1] and ansar[i+1][0] == genar[j+1][0]:
                #print(len(ansar), len(genar))
                hits += 1
                thits += 1
                found=True
                break
        if found==False:
            dels += 1
            tdels += 1

    for i in range(len(genar)-1):
        found=False
        for j in range(len(ansar)-1):
            if ansar[j][len(ansar[j])-1] == genar[i][len(genar[i])-1] and ansar[j+1][0] == genar[i+1][0]:
                found=True
        if found==False:
            ins += 1
            tins += 1

    print("Correct segmentation(): " + ansline, end="", file=out)
    print("Generated segmentation: " + genline, end="", file=out)
    print("hits = "+str(thits)+" deletions = "+str(tdels)+" insertions = "+str(tins), end="\n", file=out)
    print("Cumulated: hits = "+str(hits)+" deletions = "+str(dels)+" insertions = "+str(ins), end="\n\n", file=out)

precision = hits*1.0/(hits+ins)
recall = hits*1.0/(hits+dels)
print("precision = "+str(precision), end="\n", file=out)
print("recall = "+str(recall), end="\n\n", file=out)
print("f-score = "+str(2*precision*recall/(precision+recall)), end="\n", file=out)
print("precision = "+str(precision), end="\n", file=score)
print("recall = "+str(recall), end="\n\n", file=score)
print("f-score = "+str(2*precision*recall/(precision+recall)), end="\n", file=score)
