#!/usr/bin/python

############################################################
# Main Program
############################################################

if __name__ == '__main__':
    import sys
    import string
    try:
        import psyco
        psyco.full()
    except:
        print >> sys.stderr, 'Warning: No psyco available'
    # COMMANDLINE: evaluator.py gold_file test_file
    if len(sys.argv) > 2:
        gold_file = sys.argv[1]
        test_file = sys.argv[2]
    elif len(sys.argv) > 1:
        print "No test file!"
    else: print "Usage: evaluator.py <gold_file> <test_file>"
    try:
        fsock_gold=open(gold_file,'r',0)
        fsock_test=open(test_file,'r',0)
        print 'Reading %s and %s' % (gold_file, test_file)
        sentences = 0
        s_right = 0
        words = 0
        w_right = 0
        for gline in fsock_gold:
             gsplitline=gline.rstrip().split()
             tline = fsock_test.readline() 
             if not tline: break
             tsplitline=tline.rstrip().split()
             glinelen = len(gsplitline)
             tlinelen = len(tsplitline)
             if not glinelen == tlinelen:
                 print 'Unmatched gold standard and test'
                 print 'gold: %s' % gline
                 print 'test: %s' % tline
                 # for index in xrange(len(tline)):
                 #    print '%s %s %s' % (tline[index],gline[index],tline[index]==gline[index])
                 break
             index=glinelen-1
             serror=0
             if glinelen>0:
                 while index > -1:
                     words = words + 1
                     gelem=gsplitline[index]
                     telem=tsplitline[index] 
                     if gelem == telem:
                         w_right = w_right +1
                     else: serror=1
                     index = index - 1
                 sentences = sentences + 1
                 if not serror: s_right = s_right + 1
             else: continue
        fsock_gold.close()
        fsock_test.close()
        print 'Computing scores'
        print '%d right out of %d tags correct\n' % (w_right, words)
        print '%.2f per cent word accuracy\n' % (float(w_right)/float(words) * 100.0, )
        print '%.2f per cent sentence accuracy\n' % (float(s_right)/float(sentences) * 100.0, )
    except IOError:
        print "Unable to open %s or %s" % (gold_file, test_file)


