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

###########################################################################
#
# parse2xml.pl version 0.1
#
###########################################################################
#
# This program takes as input the output of the Collins parser. The 
# tree format output is ignored. The bracketed form of output is placed in 
# DESTINATION. The parse information and part of speech tags are placed in 
# angular braces. Open brace `(' signifies the start of a phrase and the 
# token(TOK) that follows is placed within angular braces: <P="TOK">
# The `P' tells us that this xml tag has parse information.
# A sample of the conversion is provided below:
#
#      (NPB~company~2~2 The/DT company/NN )
#                is converted to
#  <P="NPB~company~2~2"> The <p="DT"/> company <p="NN"/> </P>
#
# The Collins Parser adds token-part of speech pairs `t/TRACE' and
# `T/TRACE'. These tokens were not part of the original sentence and are 
# hence eliminated.
# SOURCE has the output of the parser. The xmlized output is placed in
# DESTINATION. parse2xml.pl has the following usage:
#
# USAGE :  parse2xml.pl OPTIONS DESTINATION SOURCE
#
#	--version       Prints the version number.
#	--help		Prints help message.
#
###########################################################################
#
# Copyright (C) 2000-2002
#
# Mohammad Saif, University of Minnesota, Duluth
# moha0149@d.umn.edu
#
# Ted Pedersen, University of Minnesota, Duluth
# tpederse@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;}

$opfl = shift();
$inpfl = shift();

if(-f $opfl) {
    print "Destination file 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; } }

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

# Open file
open(INP,"<$inpfl");
open(OP,">$opfl");

# Read lines from input file
while($ln = <INP>) {
    # Skip lines which do not have open bracket at start
    if($ln !~ /^\(/)	{ next; }

    # Eliminate TRACE
    $ln =~ s/\s[tT]\/TRACE\s/ /g;

    # XMLIZE
    $ln =~ s/\(([^\s\/][^\s]*)/<P=\"$1\">/g;
    while ($ln =~ /(^|\s)\)(\s|$)/) {
    	$ln =~ s/(^|\s)\)(\s|$)/$1<\/P>$2/g;
    }
    $ln =~ s/(^|\s)([^<\s][^\s]*)\/([^a-z>\s][^\s]*)/$1$2 <p=\"$3\"\/>/g;
    print OP $ln;
}

# Subroutine to print help message
sub help{
    print "Usage: parse2xml.pl OPTIONS DESTINATION SOURCE\n\n"; 
    print "This program takes as input the output of the Collins parser. It places the parse information and part of speech tags in angular braces. Thus, all besides the contextual tokens are parse2xmld. SOURCE has the output of the parser. The parse2xmld output is placed in DESTINATION.\n\n";
    print "OPTIONS:\n\n";
    print "  --version    Prints the version number.\n";
    print "  --help       Prints the help message.\n\n";}

sub usage{
    print "Usage: parse2xml.pl OPTIONS DESTINATION SOURCE\n"; 
    AskHelp();}
# Message describing way to get help
sub AskHelp{
    print "Type parse2xml.pl --help for help.\n";}

# function to output the version number
sub showVersion
{
    print STDERR "parse2xml.pl      -        version 0.1\n";
    print STDERR "A component of the Sval2Parser 0.1\n";
    print STDERR "Copyright (C) 2000-2003, Saif Mohammad and Ted Pedersen\n";
}


