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

=comment

  YamCha -- Yet Another Multipurpose CHunk Annotator
 
  $Id: showse.in,v 1.4 2004/03/26 13:33:03 taku-ku Exp $;

  Copyright (C) 2000-2004 Taku Kudo <taku-ku@is.aist-nara.ac.jp>
  This is free software with ABSOLUTELY NO WARRANTY.
  
  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.
  
  This library 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
  Lesser General Public License for more details.
  
  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
=cut


my %paramHash = ();
while(<>) {
    chomp;
    next if (/^\#/);
    last if (/^$/);
    $paramHash{lc($1)} = $2 if (/^([^:]+)\s*:\s*(.+)$/);
}

my %id2strHash = ();
while(<>) {
    chomp;
    last if (/^$/);
    my($i,$v) = split(/\s+/,$_);
    $id2strHash{$i} = $v;
}

my %scHash = ();
my %dataHash = ();
my $POS = "+1";
my $NEG = "+1";
my ($param_r, $param_g, $param_s, $param_degree, $kernel_type);

while(<>) {
    chomp;
    if (/^(\S+) \# kernel type/) {
        if ($1 eq "1") {
            $kernel_type = 'polynomial';
        } elsif ($1 eq "3") {
            $kernel_type = 'polynomial' if ($1 eq "3");
        } else {
            die "FATAL: cannot supnport Kernel Func, type $1\n";
        }
    } elsif (/^(\d+) \# kernel parameter -d/) {
        $param_degree = $1;
    } elsif (/^(\S+) \# kernel parameter -g/) {
        $param_g = $1;
    } elsif (/^(\S+) \# kernel parameter -s/) {
        $param_s = $1;
    } elsif (/^(\S+) \# kernel parameter -r/) {
	$param_r = $1
    } elsif (/MULTI_CLASS (\S+) (\S+)/) {
        $POS = $1; $NEG = $2;
    } elsif (/^$/ || /^SVM-light/ || /^TinySVM/ || /\#/) {
        # do nothing
    } elsif (! /\#/) {
        my($l,$v) = split(/\s+/,$_,2);
	$scHash{$v}  += abs(1000*$l);
	if ($l > 0) {
	    $dataHash{$v} = $POS
	} else {
	    $dataHash{$v} = $NEG;
	}
    }
}

my $maxrow = 0;
my $minrow = 0;
my $maxcol = -1024;
my $isReverse = 0;
for (split(/\s+/,$paramHash{"features"})) {
    my($row,$col) = split(/:/);
    $maxrow = $maxrow<$row?$row:$maxrow;
    $minrow = $minrow>$row?$row:$minrow;
    $maxcol = $maxcol<$col?$col:$maxcol;
}

$isReverse = 1 if ($paramHash{"parsing_direction"} eq "backward");

# OUTPUT
my $data;
for $data (sort {$scHash{$b} <=> $scHash{$a}} keys %scHash) {
    my $score = $scHash{$data};
    my $label = $dataHash{$data};
    my(%e) = ();
    my $a;

    for $a (split(/\s+/,$data)) {
       my($i,$v) = split(/:/,$a);
       die "FATAL: $i cannot be found in this parameter\n" if (! defined $id2strHash{$i});
       $e{$1} = $3 if ($id2strHash{$i} =~  /([FT]:[\+\-]\d+(:\d+)?):(.+)/);
   }

    my ($i,$j);
    my @out;
    my $num = 0;
    for ($i = $minrow; $i <= $maxrow; $i++) {
	my(@tmp);
	for ($j = 0; $j <= $maxcol; $j++) {
	    my $f = $e{sprintf("F:%+d:%d",$i,$j)};
	    if (defined $f) {
		$num++;
	    } else {
		$f = "";
	    }
	    push(@tmp, $f);
	}
	if ($i == 0) {
	    push(@tmp, $label);
	} elsif ($i < 0) {
	    my $f = $e{sprintf("T:%+d",$i)};
	    if (defined $f) {
		push(@tmp, $f);
		$num++;
	    }
	}
	push(@out,[@tmp]);
    }

    print "<Support Example Score=$score>\n";
    for (@out) {
	print join("\t",@{$_}), "\n";
    }
    print "</Support Example>\n\n";       
}
