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

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

###########################################################################
#
# This program eliminates sentence delimiter tags in the SOURCE file(s) and 
# places sentences on new lines; a format required by the Brill Tagger.
# The sentences boundaries should be demarkated by <s>...</s>. 
# The output is stored in a DESTINATION file.
#
# USAGE :  newline.pl [OPTIONS] DESTINATION SOURCE 
#
###########################################################################

# 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 ) {
    &usage();
    exit; }

# Open the destination file specified in the command line arguement
$opfl = shift @ARGV;
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; } }

open(OUTPUT, ">$opfl") or die "could not open file";

# Read the source file(s) line by line
while(@ARGV) {
    $cntfl = shift @ARGV;
    open(INPUT, "<$cntfl") or die "could not open file";
    while(<INPUT>) {
	# Eliminate sentence delimiters and place newline character in their place
	s/<\/?s>/\n/g;

	# Print modified line to Destination file
	print OUTPUT; } }

# Subroutine to print help message
sub help {
    print "Usage: newline.pl [OPTIONS] DESTINATION SOURCE \n\n";
    print "This program eliminates sentence delimiter tags in the SOURCE file(s) and places sentences on new lines; a format required by the Brill Tagger. The sentences boundaries should be demarkated by <s>...</s>. The output is placed in the DESTINATION file.\n\n";
    print "OPTIONS:\n\n";
    print "  --help             Prints the help message.\n"; 
    print "  --version          Prints the version number.\n\n"; }

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

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