#!/usr/local/bin/perl -w

#### LAST UPDATED: Jul, 2003 ####
# numparse.pl version 0.1

###########################################################################
#
# This program counts the number of sentences which are parsed and the
# number of sentences which remain unparsed in the SOURCE file. The same 
# counts with respect to only those sentences which house the target word 
# are also made.The format of the parse expected is the xmlized form of the
# output by Collin's parser. Output of collin's Parser can be xmlized using 
# parse2xml.pl and xml2sval.pl of the parseSenseval package.
# All unparsed sentences are placed in DESTINATION. The counts are also 
# printed at the bottom of the DESTINATION file.
#
# USAGE :  numparse.pl [OPTIONS] DESTINATION SOURCE
#
#       --version       Prints the version number.
#       --help          Prints help message.
#
###########################################################################

# Copyright (C) 2000-2002
# Ted Pedersen, University of Minnesota, Duluth
# tpederse@d.umn.edu
# Mohammad Saif, University of Minnesota, Duluth
# moha0149@d.umn.edu
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

# START OF PROGRAM
use Getopt::Long;

# Check if any options specified
GetOptions("version", "help");
if ( defined $opt_help )    { $help = $opt_help; }
else                        { $help = 0; }
# Print help if asked
if ( $help ) {
    &help();
    exit; }
# if version has been requested, show version
if ( defined $opt_version ) {
    $opt_version = 1;
    &showVersion();
    exit; }

# Check if command line options have been provided
# If not print usage information
if ( $#ARGV < 1 ) {
    print "Insufficient command line arguments.\n";
    &usage();
    exit;}

# Accept SOURCE and DESTINATION names
$opfl = shift();
$inpfl = shift();

# If output file exists, ask permission to over-write
if(-e $opfl) {  
    print "File/Directory with same name as DESTINATION already exists. Press `y' to overwrite\n";
    $resp = <STDIN>;
    chomp($resp);
    if($resp eq "y" || $resp eq "Y") {
	print "Overwriting the file.\n"; }
    else{
	exit; } }

# Check if SOURCE exists
if(!(-f $inpfl)) {
    print "SOURCE file does not exist.\n";
    &usage();
    exit; } 

# Open the various files
open(INP,"<$inpfl");
open(OP,">$opfl");

$parsed = 0; $unparsed = 0;
$hdparsed = 0; $hdunparsed = 0;
# Read lines from input file
while($ln = <INP>) {
	# Count unparsed lines
	if($ln =~ /^.*?<P=\"TOP\">/){
	    print OP $ln;
	    $unparsed++;
	    if($ln =~ /<head[^>\s]*>/) {
		$hdunparsed++;
	    }
	    next;}

	# Count parsed lines
        if($ln =~ /^.*?<P=\"TOP~/){
            $parsed++;
	    if($ln =~ /<head[^>\s]*>/) {
                $hdparsed++;
            }
            next;}
}

# Calcullate percentages
if($parsed == 0 && $unparsed == 0) {
	$Pparsed = 0; $Punparsed = 0; }
else {
	$Pparsed = $parsed * 100 / ($parsed + $unparsed);
	$Punparsed = $unparsed * 100 / ($parsed + $unparsed); }

if($hdparsed == 0 && $hdunparsed == 0) {
	$Phdparsed = 0; $Phdunparsed = 0; }
else {
	$Phdparsed = $hdparsed * 100 / ($hdparsed + $hdunparsed);
	$Phdunparsed = $hdunparsed * 100 / ($hdparsed + $hdunparsed); }
	
print OP "\nParsed Sentences: $parsed ";
printf OP ("(%3.2f", $Pparsed);
print "\nParsed Sentences: $parsed ";
printf "\(%3.2f", $Pparsed;
print OP ")\nUnparsed Sentences: $unparsed ";
printf OP ("(%3.2f\)\n", $Punparsed);
print ")\nUnparsed Sentences: $unparsed ";
printf "\(%3.2f\)\n", $Punparsed;

print OP "\nParsed Head Word Sentences: $hdparsed ";
printf OP ("(%3.2f", $Phdparsed);
print "\nParsed Head Word Sentences: $hdparsed ";
printf "\(%3.2f", $Phdparsed;
print OP ")\nUnparsed Head Word Sentences: $hdunparsed ";
printf OP ("(%3.2f\)\n", $Phdunparsed);
print ")\nUnparsed Head Word Sentences: $hdunparsed ";
printf "\(%3.2f\)\n", $Phdunparsed;

# Subroutine to print help message
sub help{
    print "Usage: numparse.pl [OPTIONS] DESTINATION SOURCE\n\n"; 
    print "This program counts the number of sentences which are parsed and the number of sentences which remain unparsed in the SOURCE file. The same counts with respect to only those sentences which house the target word are also made. The format of the parse expected is the xmlized form of the output by Collin's Parser. Output of collin's Parser can be xmlized using parse2xml.pl and xml2sval.pl of the parseSenseval package. All unparsed sentences are placed in DESTINATION. The counts are also printed at the bottom of the DESTINATION file.\n\n";
    print "OPTIONS:\n";
    print "  --version    Prints the version number.\n";
    print "  --help       Prints the help message.\n\n";}

sub usage{
    print "Usage: numparse.pl [OPTIONS] DESTINATION SOURCE\n"; 
    AskHelp();}
# Message describing way to get help
sub AskHelp{
    print "Type numparse.pl --help for help.\n";}
# function to output the version number
sub showVersion
{
    print STDERR "numparse.pl      -        version 0.1\n";
    print STDERR "A component of the parSenseval 0.1\n";
    print STDERR "Copyright (C) 2000-2003, Saif Mohammad and Ted Pedersen\n";
}

