#!/usr/local/bin/perl

require "lexProbs.pl";

#$probfile = "/shared/data/Chinese/GIZA.egf";
$probfile = "/shared/data/Chinese/GIZA.fge";

&loadProbs;

binmode(STDOUT, ":utf8");

@lexfiles = ();
for ($i = 0; $i < @ARGV; $i++) {
    if ($ARGV[$i] eq "-i") { # Read from init file
	open(INI, $ARGV[$i+1]) or die $!;
	while ($line = <INI>) {
	    if ($line =~ m/^\s*loadlex/) {
		$line =~ s/;[^\"\n]*$//;
		$line =~ m/^\s*loadlex\s+(\S+)\s*$/;
		$lexfile = $1;
		$lexfile =~ s/\.ids$//;
		push @lexfiles, $lexfile;
	    }
	}
	close(INI);
	$i++;
    } else {
	push @lexfiles, $ARGV[$i];
    }
}


foreach $lexicon (@lexfiles) {
    $entries = "";
    print "Processing $lexicon\n";

    open(DICT, "<:encoding(gbk)", $lexicon) or die $!;
    while ($line = <DICT>) {
	if ($line =~ m/\|:/) {
	    #print $line;
	    $line =~ m/\|:\s*\[([^\]]+)\]/;
	    $src = $1;         # Chinese
	    $src =~ s/^\"//;
	    $src =~ s/\"$//;
	    $src =~ s/\"\s+\"/ /g;
	    $line =~ m/\-\>\s*\[([^\]]+)\]/;
	    $tgt = lc($1);         # English
	    $tgt =~ s/^\"//;
	    $tgt =~ s/\"$//;

	    $prob = &transProb($src, $tgt);
	    print "$src\t$tgt\t$prob\n";

	}
    }


}
