#!/usr/bin/env perl

use encoding "utf8";

use IO::Handle;
use IPC::Open2;



my $logonroot = $ENV{LOGONROOT} ;

my $tnt= $logonroot . "/coli/bin/linux.x86.32/tnt";
my $taggermodel= $logonroot . "/coli/tnt/models/negra";
my $supertaggermodel= $logonroot . "/dfki/gg/tiger-supertag";



open2 (\*TAG_OUT, \*TAG_IN, "$tnt -z100 $taggermodel -");

open2 (\*STAG_OUT, \*STAG_IN, "$tnt -a3 -z1000 $supertaggermodel -");



TAG_IN->autoflush;
STAG_IN->autoflush;
$|=1;

my $input;

while (my $output = <STAG_OUT> )  {
    
    chomp $output;
    
    if ($output =~ /^\%\% Thorsten/ ) {last;}
    
}

while (my $output = <TAG_OUT> )  {
    
    chomp $output;
    print STDOUT "$output\n";
    
    if ($output =~ /^\%\% Thorsten/ ) {
	last;}

}

my $eline;
while (my $line = <>) {
    
    chomp $line;
    $input .= "$line\n";
    
    if (not defined $line or $line =~ /^[\s]*$/ ) {
	$eline++;
    }

    if ($eline == 2) {

	print TAG_IN "$input";

	my (@tag_output, @stag_output) ;
	while (my $output = <TAG_OUT> )  {
	    chomp $output;

	    if ($output =~ /^[\s]*$/ ) {$eline++}
	    
	    if ($eline == 2) {
		last; 
	    }

	    push @tag_output,  "$output";
	    
	}

	print STAG_IN lc($input);
	
	my $eline=0;

	while (my $output = <STAG_OUT> )  {
	    chomp $output;


	    if ($output =~ /^[\s]*$/ ) {$eline++}
	    
	    if ($eline == 2) {
		last; 
	    }
	    

	    push @stag_output, "$output";
	    
	} 
	
	foreach  (0..$#tag_output) {

	    ($stag_output[$_] = $stag_output[$_]) =~ s/^[^\s]+[\s]+(.*)$/\t\1/;

	    print $tag_output[$_] . "\t###\t0" . $stag_output[$_] . "\n";
	    
	}
	print "\n\n";
	
	
	$input = "";
	
    }

    
    
}

