#!/usr/bin/perl -w

use strict;
# utf8 definitions
binmode( STDIN, ':utf8' );
binmode( STDOUT, ':utf8' );
binmode( STDERR, ':utf8' );
use open 'utf8';

use XML::Twig;

my $dictfile = "/Users/saara/words/dicts/smenob/src/smenob.xml";
my $out_dict = "/Users/saara/words/script/dict.out";
my %dictionary;

#read_dict_xml();
my $fst;
my $lang1;
my $lang2;

use Getopt::Long;

GetOptions ("lang1=s" => \$lang1,
			"lang2=s" => \$lang2,
			"fst=s" => \$fst
			);

my $utilitydir = "/opt/sami/xerox/c-fsm/ix86-linux2.6-gcc3.4/bin";
my $fstflags = "-flags mbTT -utf8";
my $lookup = "$utilitydir/lookup $fstflags $fst";


while (<>) {

	my $out = $_;

	# ignore empty lines
	next if $out =~ /^\s*$/;
	chomp $out;
	# Test the start of the cohort.
	if ($out =~ /^\"</) {
		print "$out";
		next;
	}
	# If analysis line, split it to lemma and analysis
	$out =~ s/\s+$//;
	$out =~ s/^\s+//;
	$out =~ /^(\".*?\")\s+(.*)$/;
	my $lemma = $1;
	my $analysis = $2;
	next if (! $lemma);
	next if (! $analysis);
	
	# Search dictionary for lemma
	my $trans_out = `echo $lemma | $lookup 2>/dev/null`;
	if ($trans_out =~ /\+\?/) { $trans_out = "";}
	my @transl = split (/\n/, $trans_out);
	my $line = "\t";
	for my $t (@transl) {
		if ($line ne "\t") { $line .= ", "; }
		my ($orig, $translation) = split (/\t/, $t); 
		$line .= $translation;

	}
	# Format and print new analysis line
	if (@transl) {
		$line .= "\n\t$lemma" . $analysis . "\n";
	}
	else { $line = "\n\t" . $lemma . $analysis . "\n"; }
	
	print $line;
}


sub read_dict_txt {

	open DFH, "<$out_dict";

	while (<DFH>) {
		my ($lemma, $translation) = split ("\t", $_);
		$dictionary{$lemma} = $translation;
	}
	close DFH;
}


# Start reading the dictionary
sub read_dict_xml {
	
	open DFH, ">$out_dict";

	my $document = XML::Twig->new(twig_handlers => { e => sub { read_entry(@_); $_->delete; } });
	if (! $document->safe_parsefile ("$dictfile")) {
		print STDERR "Parsing the XML-file failed: $@\n";
		exit;
	}   
	close DFH;
}

# Read one entry from the xml-file
# and store it to the data structure.
sub read_entry {
	my ($twig, $e) = @_;

	my @lemmas = $e->descendants('l');
	my $l_text = $lemmas[0]->text;
	my @translations = $e->descendants('t');
	my $all_trans;
	for my $t (@translations) {
		my $t_text = $t->text;
		$all_trans .= $t_text . " ";
	}
#	$dictionary{$l_text} = $all_trans;
	print DFH "$l_text\t$all_trans\n";
	return;
}
