#!/usr/local/bin/perl

# May, 2005

# Change these variables to match local situation
$javaexec = "/afs/cs.cmu.edu/user/cmonson/java"; # or just "java" if in your path

$dirdelim = ":";                                     # or ";" for Windows

# dir with jar/parse subdirs
$morphdir = "/afs/cs.cmu.edu/project/avenue-1/Avenue/Mapu-MT/Morph/netBeansProjectFolder/"; 

@classpaths = ("");
$ENV{'CLASSPATH'} = join $dirdelim, @classpaths;

use IPC::Open2;

# Auto-flush
$| = 1;

$jarfile        = $morphdir . "dist/MapuMorph.jar";
$stemDataFile   = $morphdir . "mapudungunStemLexicon_2June2005.txt";
$suffixDataFile = $morphdir . "mapudungunSuffixData.txt";

@args = ("-jar", $jarfile, $stemDataFile, $suffixDataFile);

$childpid = open2($childsOut, $childsIn, $javaexec, @args)
    or die "can't open pipe to $javaexec: $!";

# Read Mapudungun sentence
while ($sentence = <STDIN>) {
    if ($sentence =~ m/\*\*EXIT\*\*/) {
	# Send the input to the child java morphology analyzer
	print $childsIn $sentence;
	last;
    }

    # remove final newlines etc.
    chomp $sentence;
    
    # Split on whitespace
    @words = split /\s+/, $sentence;
    
    foreach $word (@words) {

	# The java Mapudungun morphology analyzer uses readln() which requires an end-of-line
	#   character after each input
	$word .= "\n";

	print "The input that is being sent to the java morphology analyzer is : |$word|\n";

	# Send the input to the child java morphology analyzer
	print $childsIn $word;
    
	# Read back the result of the analysis
	$resultFromMorphologyAnalyzer = <$childsOut>;
	
	print "The result from the analyzer is : |";
	print $resultFromMorphologyAnalyzer;
	print "|\n";

    }
}

print STDOUT "\nExiting the perl wrapper for the Mapudungun morphology analyzer.\n\n";

close $childsOut;
close $childsIn;
waitpid($childpid, 0);




