# -*- coding: utf-8 -*-
import codecs
import re

ground_truth = 'hi-wseg-gt.txt'
output = 'output'


file1=open(ground_truth,'r')
file2=open(output,'r')

# remove blank lines
temp1 = file1.read()
temp11 = []
for line in temp1.split('\n'):
    if len(line)<=2: continue
    temp11.append(line)
fd=open('temp11','w')
for i in temp11:
    fd.write(i+'\n')
fd.close()

temp2 = file2.read()
temp22 = []
for line in temp2.split('\n'):
    if len(line)<=2: continue
    temp22.append(line)
fd=open('temp22','w')
for i in temp22:
    fd.write(i+'\n')
fd.close()

file1.close()
file2.close()
file1=open('temp11','r')
file2=open('temp22','r')

Hits = 0.0
Deletions = 0.0
Insertions = 0.0

i=0
j=0
lineno=0

for line1,line2 in zip(file1.read().split('\n'),file2.read().split('\n')):
        line1.strip()
        line2.strip()
        i=0;j=0
        re.sub(' +',' ',line1)
        re.sub(' +',' ',line2)
	
	lineno=lineno+1
	#print lineno
	while i < len(line1) and j<len(line2):
		if line1[i]==line2[j]:
			if line1[i]==' ':
				Hits = Hits +1
			i+=1
			j+=1
	
		elif line1[i]==' ':
			Deletions=Deletions+1
			i+=1
	
		elif line2[j]==' ':
			Insertions=Insertions+1
			j+=1
		
		else: 
			#print 'abcd',lineno,'',line1[i],'-> ',line2[j], ' ', i
			i+=1
			j+=1
	
print 'Hits: %s, Insertions: %s, Deletions: %s' % (Hits,Insertions,Deletions)

precision = (float)(Hits/(Hits+Insertions))
recall = (float)(Hits/(Hits+Deletions))
fscore = (float) (2*precision*recall) / (precision+recall)
print 'precision=',precision
print 'recall=',recall
print 'fscore=',fscore

