#!/usr/bin/perl

# This script converts lines of the form (1.2.3.4\t<something>)
# into form in which the integers are converted to words
# according to a conversion table given

$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");
    }

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

while ($input = <INFILE>) { 
    chop ($input); 
    @items = split (/\t/, $input);
    @episode = split (/[.]/, $items[0]);
    $conv_items = "";
    for ($i=0; $i < @episode; $i++) {
	$conv_items .= " ".$base{$episode[$i]};
    }

    print OUTFILE ("$conv_items\t$items[1]\n");
}


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

    $filecount++;
}
