package parser;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

import gnu.trove.map.hash.TIntIntHashMap;
import gnu.trove.map.hash.TObjectIntHashMap;
import utils.Utils;

public class Evaluator
{
	int uas, las, tot;
	int whole, nsents;
	
	boolean learnLabel;
	String[] labels;
	
	public Evaluator(Options options, DependencyPipe pipe)
	{
		uas = las = tot = 0;
		whole = nsents = 0;
		learnLabel = options.learnLabel;
		if (learnLabel) labels = pipe.types;
	}
	
	
	public double UAS()
	{
		return uas/(tot+1e-20);
	}
	
	public double LAS()
	{
		return las/(tot+1e-20);
	}
	
	public double CAS()
	{
		return whole/(nsents+1e-20);
	}
	
	
	public void add(DependencyInstance gold, DependencyInstance predict, boolean evalWithPunc)
	{
		evaluateDependencies(gold, predict, evalWithPunc);
	}
	
    public void evaluateDependencies(DependencyInstance gold, 
    		DependencyInstance pred, boolean evalWithPunc) 
    {
    	++nsents;
    	int tt = 0, ua = 0, la = 0;
    	for (int i = 1, N = gold.length; i < N; ++i) {

            if (!evalWithPunc)
            	if (gold.forms[i].matches("[-!\"%&'()*,./:;?@\\[\\]_{}、]+")) continue;

            ++tt;
    		if (gold.heads[i] == pred.heads[i]) {
    			++ua;
    			if (learnLabel && gold.deprels[i].equals(labels[pred.deplbids[i]])) ++la;
    		}
    	
    	}    		
    	
    	tot += tt;
    	uas += ua;
    	las += la;
    	whole += (tt == ua) && (tt == la || !learnLabel) ? 1 : 0;
    }
    
}

@@ -95,16 +95,48 @@
 ception;
+%0Aimport java.util.regex.Pattern;
 %0A%0Aimport
@@ -345,16 +345,106 @@
 bels;%0A%09%0A
+%09Pattern puncRegex = Pattern.compile(%22%5B%5C%5Cp%7BPunct%7D%5D+%22, Pattern.UNICODE_CHARACTER_CLASS);%0A%09%0A
 %09public 
@@ -1212,24 +1212,97 @@
         %09if 
+(puncRegex.matcher(gold.forms%5Bi%5D).matches()) continue;%0A            %09//if 
 (gold.forms%5B

punctuation match using regex