#!/usr/bin/perl -w

use strict;

# Author: Hoa Dang (hoa.dang@nist.gov)
#
# Date: August 7, 2014
# Version 1.0
# Adapted from check_kbp_slot-filling.pl Version 3.2
#
# Check a TAC 2014 KBP Track sentiment slot-filling task submission for various common errors, including:
#		* invalid run tag (should be a concatenation of your TAC 2014 team ID and the run number (1-5)
#		* multiple run tags
#		* invalid query id or slot name
#		* missing value for a question (at least one response required per question)
#		* provenance (Columns 4,6) from an invalid document (must be the same as the query document)
#		* offsets (Columns 4,6) that appear to be invalid or define a span that is longer than 150 characters
#		* the confidence score is a double value less or equal to 1
# [NB: "question" refers to a (query id, slot name) pair]
# Messages regarding submission are printed to an error log

# Usage is:
#    check_kbp_sentiment-slot-filling.pl queries_file results_file
#  where results_file is the name of the results submission file to be checked

# The queries file is an xml file with the following dtd
#   <!ELEMENT kbpslotfill (query+)>
#   <!ELEMENT query   (name, docid, beg, end, enttype, slot)>
#   <!ELEMENT name   (#PCDATA)>
#   <!ELEMENT docid   (#PCDATA)>
#   <!ELEMENT beg   (#PCDATA)>
#   <!ELEMENT end   (#PCDATA)>
#   <!ELEMENT enttype (#PCDATA)>
#   <!ELEMENT slot (#PCDATA)>
#   <!ATTLIST query id ID #REQUIRED>
#
# The results file has at least one response for each query
#
# A response is a line with the following tab-separated columns:
# Column 1: query id
# Column 2: slot name
# Column 3: a unique run id for the submission
# Column 4: NIL, if the system believes no information is learnable for this slot; or provenance for the relation otherwise, consisting of up to 4 triples docid:start-end; end-start must be less than MAX_LEN
# Column 5: a slot filler
# Column 6: up to 2 docid:start-end triples for representative mentions used to extract/normalize filler
# Column 7: confidence score
#
# If Column 4 is NIL, then Columns 5-7 must be empty.
# The slot filler (Column 5) must not contain any embeded tab characters

# This script also creates a new file, "input", in the current directory
# that has a more standard format (there will not be leading whitespace;
# and columns will be separated by one tab)


# Change this variable to the directory where the error log should be put
my $errlog_dir = ".";

my $MAX_ERRORS = 1000;

my $MAX_LEN = 150; # maximum length of each provenance span


my %queries;

# Slot names and filler types as defined in LDC publication "TAC KBP Sentiment Slot Filling Guidelines"; Version 1.0; July 29, 2013
# This variable is not used by the script and is for documentation purposes only
my %slots = ('PER' => {'per:pos-from' => {quantity => 'list',
				    content => 'name'}, 
		       'per:neg-from' => {quantity => 'list',
				    content => 'name'}, 
		       'per:neg-from' => {quantity => 'list',
				    content => 'name'}, 
		       'per:pos-towards' => {quantity => 'list',
				    content => 'name'}, 
		       'per:neg-towards' => {quantity => 'list',
				    content => 'name'}},
	     'ORG' => {'org:pos-from' => {quantity => 'list',
				    content => 'name'}, 
		       'org:neg-from' => {quantity => 'list',
				    content => 'name'}, 
		       'org:neg-from' => {quantity => 'list',
				    content => 'name'}, 
		       'org:pos-towards' => {quantity => 'list',
				    content => 'name'}, 
		       'org:neg-towards' => {quantity => 'list',
				    content => 'name'}},
	     'GPE' => {'gpe:pos-from' => {quantity => 'list',
				    content => 'name'}, 
		       'gpe:neg-from' => {quantity => 'list',
				    content => 'name'}, 
		       'gpe:neg-from' => {quantity => 'list',
				    content => 'name'}, 
		       'gpe:pos-towards' => {quantity => 'list',
				    content => 'name'}, 
		       'gpe:neg-towards' => {quantity => 'list',
				    content => 'name'}});


my $queries_file;               # list of eval queries
my $results_file;		# submission file to check/validate
my %qids;			# number of answers returned for question
my %nils;                       # number of NIL answers returned for question
my ($errlog,$num_errors,$line_num,$num_warnings);
my ($run_id, $tag);
my ($q,$etype,$qslot,$slot_name,$rel_just, $answer,$filler_just, $conf, $docid);
my ($i, $last_i, $line);

if ($#ARGV != 1) {
    print STDERR "Usage: $0 query_file resultsfile\n";
    die "\n";
}

$queries_file = $ARGV[0];
$results_file = $ARGV[1];
$num_errors = 0;
$num_warnings = 0;

# set up output files 
$last_i = -1;
while ( ($i=index($results_file,"/",$last_i+1)) > -1) {
    $last_i = $i;
}
$errlog = $errlog_dir . "/" . substr($results_file,$last_i+1) . ".errlog";
open ERRLOG, ">$errlog" ||
	die "Cannot open error log for writing\n";

open INPUT, ">input"   || die "Cannot create `input' file: $!\n";


# read in queries file
# only do limited error checking
open QUERIES, "<$queries_file" ||
    die "Unable to open queries file $queries_file: $!";
undef $q;
undef $etype;
undef $qslot;
undef $docid;
while ($line = <QUERIES>) {
    chomp $line;
    next if ($line =~ /^\s*$/);
    $line =~ s/^\s*(.*\S)\s*$/$1/g; # remove leading and trailing whitespace
    if($line =~/^<query\s+id\s*=\s*"([^"]+)"\s*>$/) {
	$q= $1;
	if (defined $queries{$q}) {
	    &error("duplicate query ids in queries file");
	    next;
	}
    } elsif ($line =~ /^<\/query>$/) {
	if (!defined $etype) {
	    &error("undefined enttype for query $q");
	    next;
	}
	if (!defined $qslot) {
	    &error("undefined slot for query $q");
	    next;
	}
	if (!defined $docid) {
	    &error("undefined docid for query $q");
	    next;
	}
	
	$queries{$q}{'type'} = $etype;
	$queries{$q}{'slot'} = $qslot;
	$queries{$q}{'docid'} = $docid;
	undef $q;
	undef $etype;
	undef $qslot;
	undef $docid;
    } elsif ($line =~ /^<enttype>([A-Z]+)<\/enttype>$/) {
	if((defined $etype)) {
	    &error("entity type $etype already defined");
	    next;
	}
	$etype = $1;
    } elsif ($line =~ /^<slot>([-a-z:]+)<\/slot>$/) {
	if ((defined $qslot)) {
	    &error("slot $qslot already defined for query $q");
	    next;
	}
	$qslot = $1;
    } elsif ($line =~ /^<docid>([^<]+)<\/docid>$/) {
	if ((defined $docid)) {
	    &error("docid $docid already defined for query $q");
	    next;
	}
	$docid = $1;
    } 
}


open RESULTS, "<$results_file" ||
    die "Unable to open results file $results_file: $!";
$line_num = 0;
$run_id = "";
while ($line = <RESULTS>) {
    chomp $line;
    $line_num++;

    next if ($line =~ /^\s*$/);

# Disabled UTF8 checking. This code is not robust!
#    if (&invalid_UTF8($line)) {
#		&error("invalid character (non-UTF8)");
#		next;
#    }

    undef $slot_name;
    undef $tag;
    undef $rel_just;
    undef $answer;
    undef $filler_just;
    undef $conf;
    ($q, $slot_name, $tag, $rel_just, $answer, $filler_just, $conf) = split "\t", $line, 7;
    
    if (!defined $rel_just || length($rel_just) == 0 || !defined $tag || !defined $slot_name) {
		&error("Wrong number of fields -- missing fields");
		next;
    }
	
    # make sure runtag is ok
    if (! $run_id) { 	# very first line --- remember tag 
		$run_id = $tag;
        if ($run_id !~ /^[A-Za-z0-9._]{1,12}[1-5]$/) {
            &error("Run tag `$run_id' is malformed)");
            next;
        }
    }
    else {			# otherwise just make sure one tag used
		if ($tag ne $run_id) {
	    	&error("Run tag inconsistent (`$tag' and `$run_id')");
	    	next;
		}
    }

    # get query id
    if (!defined $queries{$q}) {
   		&error("Invalid query id ($q)");
		next;
    }
    # make sure slot name is the same as the slot requested in the query
    if ($queries{$q}{'slot'} ne $slot_name) {
	   	&error("Invalid slot name $slot_name for query $q");
		next;
    }

    $qids{$q}{$slot_name}++;

    if ($rel_just ne "NIL") {

		if (!defined($conf)) {
	    	&error("Wrong number of fields -- missing fields");
	    	next;
		}
	
		# make sure answer exists
        if (!defined $answer || length($answer) == 0) {
	    	&error("Missing answer-string for slot $slot_name for query $q");
	    	next;
       	}

		# check relation and filler provenance
		my $checkMsg = &check_justification($rel_just, 4, $slot_name);
		if(length($checkMsg) > 0) {
		    &error("invalid relation justification: `$rel_just' ($checkMsg)");
		    next;
		}
		$checkMsg = &check_justification($filler_just, 2, "");
		if(length($checkMsg) > 0) {
		    &error("invalid filler justification: `$filler_just' ($checkMsg)");
		    next;
		}

		# make sure the confidence is a double with a valid value
		if(!($conf =~ /^\d+\.\d+$/ && $conf <= 1.0)) {
	    	&error("invalid confidence value `$conf'");
	    	next;
		}

		print INPUT "$q\t$slot_name\t$tag\t$rel_just\t$answer\t$filler_just\t$conf\n";

    } else {
		if ($answer) {
	    	&error ("Answer string given when relation provenance is NIL");
	    	next;
		}
		$answer = "";
		$nils{$q}{$slot_name}++;

	    print INPUT "$q\t$slot_name\t$tag\t$rel_just\n";
    }

}


# Do global checks:
#   error if some question is missing a response
#   error if more than one response is given for a question where NIL has been given as an answer
foreach $q (keys %queries) {
    $slot_name = $queries{$q}{'slot'};
    if (! defined $qids{$q}{$slot_name}) {
	&error("No response given for slot $slot_name for query $q");
    }
    if (defined $qids{$q}{$slot_name} && $qids{$q}{$slot_name} > 1 && defined $nils{$q}{$slot_name}) {
	&error("More than one response given for slot $slot_name for query $q, where NIL has been given as an answer");
    }
}
print ERRLOG "Finished processing $results_file\n";

close INPUT || die "Close failed for `input' file: $!\n";
close ERRLOG || die "Close failed for error log $errlog: $!\n";
if ($num_errors) { exit 255; }
exit 0;



# checks offsets for relation and filler
# it makes sure that:
#	no more than N pairs are given
#   each docid is valid 
#	each offset is an integer value
#	each end offset is larger than the corresponding start
#   end offset - start offset + 1 <= MAX_LEN
sub check_justification {
	my $slotName = pop(@_);
	my $N = pop(@_);
	my $offsetString = pop(@_);
	my $spanlength;

	my @tokens = split(',', $offsetString);

	# at least one offset pair must be present 
	if($#tokens < 0) {
		return "At least 1 offset pair must be present!";
	}

	# at most $N offset pairs accepted
	if($#tokens >= $N) {
		return "At most $N offset pairs accepted!";
	}

	# check each offset pair given
	foreach my $tuple (@tokens) {
		(my $docid, my $start_end) = split(':', $tuple, 2);
		# make sure docid is valid 
		if ($docid =~ /(\.sgm)/) {
		    return "Unknown document ID `$docid' (looks like a file name instead of a document ID)";
		} else {
		    if ($queries{$q}{'docid'} ne $docid) {
			return "Invalid provenance document `$docid', should be the same as query document `$queries{$q}{'docid'}'";
		    }
		}

		if (!defined($start_end)) {
		    return "Missing offsets into document";
		}

		(my $start, my $end) = split('\-', $start_end, 2);

		if(!($start =~ /^\d+$/)) {
			return "Start offset $start must be a positive integer value!";
		}
		if (!defined($end)) {
		    return "Missing end offset";
		}
		if(!($end =~ /^\d+$/)) {
			return "End offset $end must be a positive integer value!";
		}
		if($end < $start) {
			return "End offset $end is smaller than start offset $start!";
		}
		$spanlength = $end - $start + 1;
		if($spanlength > $MAX_LEN) {
		        return "provenance span is too long -- $spanlength characters";
		}
	}

	return "";
}

 # return 1 if every document in entityJustification is also a document in relationJustification,
 # otherwise, return 0
sub docInclusion {
        my $entityJustification = pop(@_);
	my $relationJustification = pop(@_);

	my @entityDocs = extractDocs ($entityJustification);
	my @relationDocs = extractDocs ($relationJustification);

	foreach my $docid (@entityDocs) {
	    if (-1 == &is_member($docid,\@relationDocs)) {
		    return 0;
	    }
	}
	return 1;
}

sub extractDocs {
	my $offsetString = pop(@_);
	my @tokens = split(',', $offsetString);
	my @docs = ();
	foreach my $tuple (@tokens) {
		(my $docid, my $start_end) = split(':', $tuple, 2);
		push(@docs, $docid);
	}
	return @docs;
}

 # print error message, keeping track of total number of errors
sub error {
   my $msg_string = pop(@_);

    print ERRLOG 
    "$0 of $results_file: Error on line $line_num --- $msg_string\n";

    $num_errors++;
    if ($num_errors > $MAX_ERRORS) {
        print ERRLOG "$0 of $results_file: Quit. Too many errors!\n";
        close ERRLOG ||
		die "Close failed for error log $errlog: $!\n";
		exit 255;
    }
}

# print warning message, keeping track of total number of warnings
sub warn {
   my $msg_string = pop(@_);

    print ERRLOG 
    "$0 of $results_file: Warning on line $line_num --- $msg_string\n";

    $num_warnings++;
    # You can add stopping after N warnings here.
    # Currently, we do not stop because of warnings.
}

#  Returns index of string element in array if present, else -1
sub is_member {
    my($element,$arrayref) = @_;
 
    my $i;
    for ($i=0; $i<= $#{$arrayref}; $i++) {
        if ($element eq $$arrayref[$i]) {
            return($i);
        }
    }
    return(-1);
}  

# Return 0 iff line is valid UTF-8; else return 1.
# Regular expression from: http://www.w3.org/International/questions/qa-forms-utf-8
sub invalid_UTF8 {
    my ($line) = @_;
    if($line =~
       m/\A(
	    [\x09\x0A\x0D\x20-\x7E]            # ASCII
	    | [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte
	    |  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
	    | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
	    |  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
	    |  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
	    | [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
	    |  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
	    )*\z/x) {
	return 0;
    } else {
	return 1;
    }
}
    
