$\ = "\n";
$, = "\t";
use XML::LibXML;

binmode( STDOUT, ":utf8" );

open FILE, "/var/www/html/teitok/egpa/Scripts/pos.vrt";
binmode( FILE, ":utf8" );
while ( <FILE> ) {
    chop;
    if ( /^<.*id="(.*?)".*lang="(.*?)"/ ) { $id = $1; $lang= $2; next; }
    if ( /^<.*/ ) { next }
    ( $mform, $wid, $pos ) = split ( "\t" );
    if ( $mform ne '' && $pos ne "--" ) {
        $reg{$lang}{$mform}{$pos}++;
    }
}
close FILE;

$filename = shift;

if ( $filename eq '' ) {
    while ( ( $lang, $val ) = each ( %reg ) ) {
        while ( ( $mform, $val2 ) = each ( %{$val} ) ) {
            while ( ( $pos, $num ) = each ( %{$val2} ) ) {
                print $lang, $mform, $pos, $num;
            }
        }
    }
    exit;
}

$parser = XML::LibXML->new();
eval {
    $tmpdoc = $parser->load_xml(location => $filename, load_ext_dtd => 0);
};
if ( !$tmpdoc ) { print " - Unable to parse"; exit; }
$tmpdoc->setEncoding('UTF-8');

#$lang = "ES";

print "Treating $filename - language: $lang";

foreach $node ($tmpdoc->findnodes("//tok")) {
    $mform = $node->getAttribute('mform');
    $id = $node->getAttribute('id');
    if ( $mform eq '' ) { $mform = $node->getAttribute('mform') }
    if ( $mform eq '' ) { $mform = $node->textContent }

    $opos = $node->getAttribute('pos');
    if ( $opos eq '' ) { $opos = $node->getAttribute('mform') }
    if ( $opos eq '' ) { $opos = $node->getAttribute('mform') }
    if ( $opos eq '' ) { $opos = $node->textContent }

    $allpos = join ( "|", keys %{$reg{$lang}{$mform}} );
    $m = 0; $pos = "";
    while ( ( $a, $b ) = each ( %{$reg{$lang}{$mform}} ) ) {
        if ( $b > $m ) { $pos = $a; $m = $b }
    }

    if ( $mform eq '' ) { $yn = ""; $ignore++ }
    elsif ( $opos eq $pos ) { $yn = '+'; $yes++; if ( $pos eq $mform ) { $gmod++ } }
    elsif ( $pos eq '' ) { $yn = '?'; $uk++ }
    else { $yn = '-'; $no++ }

    if ( $pos eq $mform ) { $nomod++ } else {
        $mod++;
        if ( $node->getAttribute('pos') eq '' && $node->getAttribute('mform') ne $pos ) {
            # Modify the 'pos' value before setting it in the XML
            $node->setAttribute('pos', $pos);
        }
    }

    # Remove '&amp;#13;' and '&#13;' from the 'pos' attribute
    my $pos_value = $node->getAttribute('pos');
    $pos_value =~ s/(&amp;#13;|&#13;)//g;
    $node->setAttribute('pos', $pos_value);

    print $id, $mform, $yn, $pos, $m, $opos, $allpos;
}

print "$yes good, $no bad, $uk new, $ignore empty";
print "$mod modernized, $nomod not, $gmod correctly modernized";

$text = $tmpdoc->toString;

# Remove '&amp;#13;' and '&#13;' from the entire XML content
$text =~ s/(&amp;#13;|&#13;)//g;

open FILE, ">$filename";
print FILE $text;
close FILE;
