#!/usr/bin/perl

# This script converts lines of the form <integer\t index>,
# into lines of the form <word\t index> according to the given conversion 
# table
# \t = tabulator 

$filecount = 0;

while ($filecount <= @ARGV-1) {
    unless (open (INFILE, $ARGV[$filecount])){
	die (" Can't open input file $ARGV[$filecount]\n");
    }

    $filename = $ARGV[$filecount];
    @name = split (/[.]/, $filename);

    unless (open (TTFILE, "$name[0].tt")){
	die (" Can't open input file $name[0].tt\n");
    }

    unless (open (OUTFILE, ">$name[0].w")){
	die (" Can't open output file $name[0].w\n");
    }

# load the conversion table

while ($input = <TTFILE>) {
    chop ($input);
    @inds = split (/ /, $input);
    $base{$inds[1]} = $inds[0];
} 

#convert

while ($input = <INFILE>) { 
    chop ($input); 
    ($word_ind, $index) = split (/\t/, $input);

    print OUTFILE ("$base{$word_ind}\t$index\n");
}


    close (INFILE);
    close (TTFILE);
    close (OUTFILE);

    $filecount++;
}
