

sub transProb {
    my($f, $e) = @_;
    my @fwords = split(/\s+/, lc($f)); # Chinese
    my @ewords = split(/\s+/, lc($e)); # English
    my($findex, $eindex);
    my($sum, $product);
    my(@falign);
    my $debug = 0;

    $product = 1;
    for ($findex = 0; $findex < @fwords; $findex++) {
 	$sum = 0;
 	for ($eindex = 0; $eindex < @ewords; $eindex++) {
	    if (defined($probs{$fwords[$findex] . "\t" .  $ewords[$eindex]})) {
		$feprob = $probs{$fwords[$findex] . "\t" .  $ewords[$eindex]};
	    } else {
		$feprob = 0.0000000001;
	    }
	    print "$fwords[$findex] $ewords[$eindex] " . $feprob . "\n" if $debug;
 	    $sum += $feprob;
 	}
	print "Sum $sum\n" if $debug;
 	$product *= $sum;
    }

    return $product;


    $sum = 0;

    my($allalign) = 0;
    my $alignindex = 0;

    for ($alignindex = 0; $alignindex < @fwords; $alignindex++) {
	push @falign, 0;
    }

    while (!$allalign) {
	$product = 1;
	for ($findex = 0; $findex < @fwords; $findex++) {
	    #print "$fwords[$findex]  $ewords[$falign[$findex]] ", $probs{$fwords[$findex] . "\t" .  $ewords[$falign[$findex]]}, "\n";
	    $product *= $probs{$fwords[$findex] . "\t" .  $ewords[$falign[$findex]]};
	}
	$sum += $product;

	$allalign = 1;
	for ($alignindex = 0; $alignindex < @falign; $alignindex++) {
	    if ($falign[$alignindex]+1 < @ewords) {
		$falign[$alignindex] += 1;
		$allalign = 0;
		last;
	    } else {
		$falign[$alignindex] = 0;
	    }
	}
    }

    return $sum;
}

sub loadProbs {
    my($probfile) = shift;
    open(PROB, "<:utf8", $probfile) or die $!;
    my($line, $e, $f, $p);
    my $linecount = 0;
    while ($line = <PROB>) {
	chomp($line);
	if ($linecount % 1000000 == 0) {
	    print STDERR "0";
	} elsif ($linecount % 100000 == 0) {
	    print STDERR ".";
	}
	$linecount++;
	($f, $e, $p) = split(/\s+/, $line);
	$probs{"$f\t$e"} = $p; # log($p)/log(10);
    }
    close(PROB);
    print STDERR "\n";
    
}

sub loadProbsGIZA {
    my($probfile) = shift;
    open(PROB, "<:utf8", $probfile) or die $!;
    my($line, $e, $f, $p);
    my $linecount = 0;
    while ($line = <PROB>) {
	chomp($line);
	if ($linecount % 1000000 == 0) {
	    print STDERR "0";
	} elsif ($linecount % 100000 == 0) {
	    print STDERR ".";
	}
	$linecount++;
	($e, $f, $p) = split(/\s+/, $line);
	$probs{"$f\t$e"} = $p; # log($p)/log(10);
    }
    close(PROB);
    print STDERR "\n";
    
}


1;
