#!/usr/bin/perl -s
use DB_File;
use Fcntl ;
use Lingua::PT::PLNbase;
use XML::TMX::Reader;
use Digest::MD5 qw(md5_hex);
use Encode;

our ($cont,$l,$id,$dig,$tok,$clean,$o);

my ($l1,$l2);

if($l =~ /(.+):(.+)/){ ($l1,$l2)=($1,$2) }
die ("usage: $0 [-cont] -l=en:pt tmx ...\n") unless @ARGV ;

if ($cont) {
    tie %dic, 'DB_File', "__tmxuniq_$$.db", O_RDWR|O_CREAT , 0640, $DB_BTREE;
} else {
    tie %dic, 'DB_File', "__tmxuniq_$$.db", O_RDWR|O_CREAT|O_TRUNC , 0640, $DB_BTREE;
}

my $cid = 0;

for my $file (@ARGV){
    my $tm = XML::TMX::Reader->new($file);

    if (not defined $l1) {
        ($l1,$l2) = sort ($tm->languages);
        print STDERR "Using languages: $l1/$l2\n";
    }
    print STDERR "Processing...";

    $tm->for_tu2
      (
       { output => $o || "$file._" },
       sub {
           my $tu = shift;
           $cid ++;
           $tu->{-prop}{id}= $cid if $id;
           $tu->{$l1} = n($tu->{$l1});
           $tu->{$l2} = n($tu->{$l2});
           if($clean and $tu->{$l1} eq $tu->{$l2} || $tu->{$l1} !~ /\w/ ){
               $rem ++;
               return undef
           }
           my $digest = md5_hex(encode_utf8("$tu->{$l1},$tu->{$l2}"));

           unless ($cid % 10000) {
               my $size = -s "__tmxuniq_$$.db";
               printf STDERR
                 "\rTotal: %10d  Removed: %8d (%.2f%%)  Database size: %10d bytes",
                   $cid, $rem, (100*$rem/$cid), $size;
           }

           if ($dic{$digest}) {
               $dic{$digest} .= "$cid;";
               $rem ++;
               return undef
           } else {
               $dic{$digest} = "$cid;";
               $tu->{-prop}{digest}=$digest if $dig;
               return {%$tu} ;
           }
       }
      );

    my $size = -s "__tmxuniq_$$.db";
    printf STDERR "\rTotal: %10d  Removed: %8d (%.2f%%)  Database size: %10d bytes\n",
      $cid, $rem, (100*$rem/$cid), $size;
    undef $tm;
}
untie %h;


sub n {
    my $a = shift;

    $a =~ s/\.{6,}/...../g;

    $a = tokenize({rs=>' '},$a) if $tok;

    $a =~ s/\s+/ /g;
    $a =~ s/ $//;
    $a =~ s/^ //;
    $a;
}

__END__

=head1 NAME

tmxuniq - removes duplicated translation units from TMXs

=head1 SYNOPSIS

 tmxuniq [options] -l=en:pt tmx1 ... 

=head1 DESCRIPTION

Removes duplicated translation units from a set of TMX (Translation
Memory eXange format).

=head1 OPTIONS

 -id  -- insert a uniq id property in each TU
 -dig -- insert a digest property in each TU
 -tok -- tokenize/normalize text
 -clean     --  remove if seg(l1) = seg(l2) or if no letter found
 -o=out.tmx -- (with 1 argument) redefine output (default = input._)

=head1 AUTHOR

J.Joao Almeida, jj@di.uminho.pt

=head1 SEE ALSO

perl(1).

=cut      
