# Find left or right neighbors of a given word
# 1st argument: name of input file
# 2nd argument: name of output file
# 3rd argument  name of abbreviation file (optional)


if ( scalar( @ARGV ) >=  2 )
{ 
            $TheFile = $ARGV[0];
            $TheMainOutFile = ">" . $ARGV[1];
			$AbbreviationFile = $ARGV[2];
} 

 

else 
{           print  "Not enough arguments given to operate; goodbye."; 
            exit;
}

 

open (OUTFILE, $TheMainOutFile);
open (INFILE, $TheFile) or die "The file $TheFile could not be found";
if (! $AbbreviationFile eq "")
{
	open (ABBREV, $AbbreviationFile) or die "The abbreviation file $AbbreviationFile could not be found";
	while (<ABBREV>)
	{
		@Line = split (" ");		
		$Abbreviations{ $Line[0] } = 1;
	 
	}
}



$WordCount=0;
$LineCount =0;
$Counter = 0;
$LongestWordlength =0;



while (<INFILE>)        
{

            $TheLine= $_;                               # unnecessarily explicit in real PERL
            chomp ($TheLine); 
			if ( length $TheLine == 0 ) { next; }
            $LineCount =  $LineCount + 1;		

            @Words=split(/ /, $TheLine); 
            while ($Word= shift (@Words) )      
            {   
				$TestWord = $Word;
				$WordCount++;                

				$TestWord =~ s/^\"//;
				$TestWord =~ s/\"$//;
				if ( $TestWord =~ /\?$/ )
				{
					print OUTFILE $Word, "\n";
					next;
				}
                if ( $TestWord =~ /\.$/ )
				{	 
					if ( exists $Abbreviations{ $TestWord } )
					{	
						print  OUTFILE $Word, " " ;
					}
					else
					{
						
						print OUTFILE $Word, "\n";
					}
				}  
				else
				{
					print OUTFILE $Word, " ";
				}

						 
            }
}


print "finished reading.\n";

  

close OUTFILE;






