|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use utf8; |
|
use open ':utf8'; |
|
binmode(STDIN, ':utf8'); |
|
binmode(STDOUT, ':utf8'); |
|
binmode(STDERR, ':utf8'); |
|
use Unicode::Normalize; |
|
use Getopt::Long; |
|
|
|
my $connect_to_root = 0; |
|
GetOptions |
|
( |
|
'connect-to-root' => \$connect_to_root |
|
); |
|
|
|
my @sentence = (); |
|
my $isent = 0; |
|
while(<>) |
|
{ |
|
push(@sentence, NFC($_)); |
|
if(m/^\s*$/) |
|
{ |
|
process_sentence(@sentence); |
|
@sentence = (); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
sub process_sentence |
|
{ |
|
$isent++; |
|
my @sentence = @_; |
|
my $sentid; |
|
my $text; |
|
my $resttext; |
|
my $collected_text = ''; |
|
my $mwtto; |
|
foreach my $line (@sentence) |
|
{ |
|
$line =~ s/\r?\n$//; |
|
if($line =~ m/^\#\s*sent_id\s*=\s*(\S+)$/) |
|
{ |
|
$sentid = $1; |
|
} |
|
elsif($line =~ m/^\#\s*text\s*=\s*(.+)$/) |
|
{ |
|
$text = $1; |
|
$resttext = $text; |
|
} |
|
|
|
if($line =~ m/^\d/) |
|
{ |
|
my @f = split(/\t/, $line); |
|
while(scalar(@f)<10) |
|
{ |
|
push(@f, '_'); |
|
} |
|
splice(@f, 10); |
|
|
|
$f[2] = fix_lemma($f[0], $f[2]); |
|
$f[3] = fix_upos($f[0], $f[3]); |
|
$f[4] = fix_xpos($f[0], $f[4]); |
|
$f[5] = fix_feats($f[0], $f[5]); |
|
($f[6], $f[7]) = fix_head_deprel($f[0], $f[6], $f[7]); |
|
$f[8] = fix_deps($f[0], $f[8]); |
|
$line = join("\t", @f); |
|
} |
|
if($line =~ m/^\d+-(\d+)\t/) |
|
{ |
|
$mwtto = $1; |
|
my @f = split(/\t/, $line); |
|
|
|
my @misc = split(/\|/, $f[9]); |
|
$collected_text .= $f[1]; |
|
|
|
if(defined($resttext)) |
|
{ |
|
my $l = length($f[1]); |
|
if($f[1] eq substr($resttext, 0, $l)) |
|
{ |
|
$resttext = substr($resttext, $l); |
|
|
|
unless($resttext eq '') |
|
{ |
|
if($resttext =~ s/^\s+//) |
|
{ |
|
|
|
@misc = grep {!m/^SpaceAfter=No$/} (@misc); |
|
if(scalar(@misc) == 0) |
|
{ |
|
push(@misc, '_'); |
|
} |
|
} |
|
else |
|
{ |
|
|
|
if(scalar(@misc) == 1 && $misc[0] eq '_') |
|
{ |
|
$misc[0] = 'SpaceAfter=No'; |
|
} |
|
elsif(!grep {m/^SpaceAfter=No$/} (@misc)) |
|
{ |
|
push(@misc, 'SpaceAfter=No'); |
|
} |
|
} |
|
} |
|
} |
|
else |
|
{ |
|
print STDERR ("WARNING: sentence text comment exists and the form '$f[1]' does not match the rest of the text '$resttext'; giving up.\n"); |
|
$resttext = undef; |
|
} |
|
} |
|
unless(grep {m/^SpaceAfter=No$/} (@misc)) |
|
{ |
|
$collected_text .= ' '; |
|
} |
|
$f[9] = join('|', @misc); |
|
$line = join("\t", @f); |
|
} |
|
|
|
elsif($line =~ m/^\d+\t/) |
|
{ |
|
my @f = split(/\t/, $line); |
|
unless(defined($mwtto) && $f[0]<=$mwtto) |
|
{ |
|
|
|
my @misc = split(/\|/, $f[9]); |
|
$collected_text .= $f[1]; |
|
|
|
if(defined($resttext)) |
|
{ |
|
my $l = length($f[1]); |
|
if($f[1] eq substr($resttext, 0, $l)) |
|
{ |
|
$resttext = substr($resttext, $l); |
|
|
|
unless($resttext eq '') |
|
{ |
|
if($resttext =~ s/^\s+//) |
|
{ |
|
|
|
@misc = grep {!m/^SpaceAfter=No$/} (@misc); |
|
if(scalar(@misc) == 0) |
|
{ |
|
push(@misc, '_'); |
|
} |
|
} |
|
else |
|
{ |
|
|
|
if(scalar(@misc) == 1 && $misc[0] eq '_') |
|
{ |
|
$misc[0] = 'SpaceAfter=No'; |
|
} |
|
elsif(!grep {m/^SpaceAfter=No$/} (@misc)) |
|
{ |
|
push(@misc, 'SpaceAfter=No'); |
|
} |
|
} |
|
} |
|
} |
|
else |
|
{ |
|
print STDERR ("WARNING: sentence text comment exists and the form '$f[1]' does not match the rest of the text '$resttext'; giving up.\n"); |
|
$resttext = undef; |
|
} |
|
} |
|
unless(grep {m/^SpaceAfter=No$/} (@misc)) |
|
{ |
|
$collected_text .= ' '; |
|
} |
|
$f[9] = join('|', @misc); |
|
} |
|
else |
|
{ |
|
my @misc = split(/\|/, $f[9]); |
|
@misc = grep {!m/^SpaceAfter=No$/} (@misc); |
|
if(scalar(@misc) == 0) |
|
{ |
|
push(@misc, '_'); |
|
} |
|
$f[9] = join('|', @misc); |
|
} |
|
$line = join("\t", @f); |
|
} |
|
$line .= "\n"; |
|
} |
|
|
|
if(!defined($text)) |
|
{ |
|
$collected_text =~ s/\s+$//; |
|
unshift(@sentence, "\# text = $collected_text\n"); |
|
} |
|
|
|
if(!defined($sentid)) |
|
{ |
|
unshift(@sentence, "\# sent_id = $isent\n"); |
|
} |
|
|
|
|
|
if($connect_to_root) |
|
{ |
|
@sentence = make_all_nodes_reachable(@sentence); |
|
} |
|
|
|
print(join('', @sentence)); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
sub fix_lemma |
|
{ |
|
my $id = shift; |
|
my $lemma = shift; |
|
|
|
if($id =~ m/^\d+-\d+$/) |
|
{ |
|
return '_'; |
|
} |
|
|
|
if($id =~ m/^\d+\.\d+$/ && $lemma eq '_') |
|
{ |
|
return $lemma; |
|
} |
|
|
|
|
|
$lemma =~ s/^\s+//; |
|
$lemma =~ s/\s+$//; |
|
$lemma = '_' if($lemma eq ''); |
|
return $lemma; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
sub fix_upos |
|
{ |
|
my $id = shift; |
|
my $upos = shift; |
|
|
|
if($id =~ m/^\d+-\d+$/) |
|
{ |
|
return '_'; |
|
} |
|
|
|
if($id =~ m/^\d+\.\d+$/ && $upos eq '_') |
|
{ |
|
return $upos; |
|
} |
|
|
|
if($upos !~ m/^(NOUN|PROPN|PRON|ADJ|DET|NUM|VERB|AUX|ADV|ADP|SCONJ|CCONJ|PART|INTJ|SYM|PUNCT|X)$/) |
|
{ |
|
$upos = 'X'; |
|
} |
|
return $upos; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
sub fix_xpos |
|
{ |
|
my $id = shift; |
|
my $xpos = shift; |
|
|
|
if($id =~ m/^\d+-\d+$/) |
|
{ |
|
return '_'; |
|
} |
|
|
|
if($id =~ m/^\d+\.\d+$/ && $xpos eq '_') |
|
{ |
|
return $xpos; |
|
} |
|
|
|
|
|
$xpos =~ s/^\s+//; |
|
$xpos =~ s/\s+$//; |
|
$xpos = '_' if($xpos eq ''); |
|
return $xpos; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
sub fix_feats |
|
{ |
|
my $id = shift; |
|
my $feats = shift; |
|
|
|
if($id =~ m/^\d+-\d+$/) |
|
{ |
|
return '_'; |
|
} |
|
|
|
if($id =~ m/^\d+\.\d+$/ && $feats eq '_') |
|
{ |
|
return $feats; |
|
} |
|
|
|
|
|
if($feats ne '_') |
|
{ |
|
my @feats = split(/\|/, $feats); |
|
|
|
|
|
|
|
|
|
foreach my $fv (@feats) |
|
{ |
|
my ($f, $v); |
|
if($fv =~ m/^(.+)=(.+)$/) |
|
{ |
|
$f = $1; |
|
$v = $2; |
|
} |
|
else |
|
{ |
|
$f = $fv; |
|
$v = 'Yes'; |
|
} |
|
$f =~ s/[^A-Za-z\[\]]//g; |
|
$f =~ s/^(.)/\u\1/; |
|
$f = 'X'.$f if($f !~ m/^[A-Z]/); |
|
$v =~ s/[^A-Za-z0-9,]//g; |
|
$v =~ s/^(.)/\u\1/; |
|
$v = 'X'.$v if($v !~ m/^[A-Z0-9]/); |
|
$fv = "$f=$v"; |
|
} |
|
@feats = sort {lc($a) cmp lc($b)} (@feats); |
|
$feats = join('|', @feats); |
|
} |
|
return $feats; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
sub fix_head_deprel |
|
{ |
|
my $id = shift; |
|
my $head = shift; |
|
my $deprel = shift; |
|
|
|
if($id =~ m/^\d+(-|\.)\d+$/) |
|
{ |
|
return ('_', '_'); |
|
} |
|
|
|
if($head !~ m/^\d+$/) |
|
{ |
|
|
|
|
|
|
|
|
|
if($id == 1) |
|
{ |
|
$head = 0; |
|
$deprel = 'root'; |
|
} |
|
else |
|
{ |
|
$head = 1; |
|
if($deprel eq '_' || $deprel eq '') |
|
{ |
|
$deprel = 'dep'; |
|
} |
|
} |
|
} |
|
|
|
my $udeprels = 'nsubj|obj|iobj|csubj|ccomp|xcomp|obl|vocative|expl|dislocated|advcl|advmod|discourse|aux|cop|mark|nmod|appos|nummod|acl|amod|det|clf|case|conj|cc|fixed|flat|compound|list|parataxis|orphan|goeswith|reparandum|punct|root|dep'; |
|
if($deprel =~ m/^root(:|$)/ && $head != 0) |
|
{ |
|
$deprel = 'dep'; |
|
} |
|
if($deprel !~ m/^root(:|$)/ && $head == 0) |
|
{ |
|
$deprel = 'root'; |
|
} |
|
if($deprel !~ m/^($udeprels)(:[a-z]+)?$/) |
|
{ |
|
|
|
$deprel =~ s/:.*//; |
|
} |
|
if($deprel !~ m/^($udeprels)(:[a-z]+)?$/) |
|
{ |
|
|
|
$deprel = 'dep'; |
|
} |
|
return ($head, $deprel); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
sub fix_deps |
|
{ |
|
my $id = shift; |
|
my $deps = shift; |
|
|
|
if($id =~ m/^\d+-\d+$/) |
|
{ |
|
return '_'; |
|
} |
|
|
|
unless($deps eq '_') |
|
{ |
|
my @deps = split(/\|/, $deps); |
|
|
|
@deps = grep {my $h; if(m/^(\d+(?:\.\d+)?):/) {$h = $1;} defined($h) && $h!=$id} (@deps); |
|
if(scalar(@deps)==0) |
|
{ |
|
|
|
push(@deps, '0:root'); |
|
} |
|
|
|
foreach my $dep (@deps) |
|
{ |
|
my ($h, $d); |
|
if($dep =~ m/^(\d+(?:\.\d+)?):(.+)$/) |
|
{ |
|
$h = $1; |
|
$d = $2; |
|
} |
|
else |
|
{ |
|
$h = 1; |
|
$d = $dep; |
|
} |
|
if($d !~ m/^[a-z]+(:[a-z]+)?(:[\p{Ll}\p{Lm}\p{Lo}\p{M}]+(_[\p{Ll}\p{Lm}\p{Lo}\p{M}]+)*)?(:[a-z]+)?$/) |
|
{ |
|
|
|
$d = lc($d); |
|
$d =~ s/[^:_a-z\p{Ll}\p{Lm}\p{Lo}\p{M}]//g; |
|
} |
|
if($d !~ m/^[a-z]+(:[a-z]+)?(:[\p{Ll}\p{Lm}\p{Lo}\p{M}]+(_[\p{Ll}\p{Lm}\p{Lo}\p{M}]+)*)?(:[a-z]+)?$/) |
|
{ |
|
|
|
$d =~ s/^([^:]*):.*$/$1:extra/; |
|
} |
|
if($d !~ m/^[a-z]+(:[a-z]+)?(:[\p{Ll}\p{Lm}\p{Lo}\p{M}]+(_[\p{Ll}\p{Lm}\p{Lo}\p{M}]+)*)?(:[a-z]+)?$/) |
|
{ |
|
|
|
$d = 'dep'; |
|
} |
|
|
|
if($h==0 && $d !~ m/^root(:|$)/) |
|
{ |
|
$d = 'root'; |
|
} |
|
if($h!=0 && $d =~ m/^root(:|$)/) |
|
{ |
|
$d = 'dep'; |
|
} |
|
$dep = "$h:$d"; |
|
} |
|
|
|
my %deps; map {$deps{$_}++} (@deps); |
|
@deps = keys(%deps); |
|
@deps = sort |
|
{ |
|
my @a = split(/:/, $a); |
|
my @b = split(/:/, $b); |
|
my $ah = shift(@a); |
|
my $bh = shift(@b); |
|
my $r = $ah <=> $bh; |
|
unless($r) |
|
{ |
|
my $ad = join(':', @a); |
|
my $bd = join(':', @b); |
|
$r = $ad cmp $bd; |
|
} |
|
$r |
|
} |
|
(@deps); |
|
$deps = join('|', @deps); |
|
} |
|
return $deps; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub make_all_nodes_reachable |
|
{ |
|
my @sentence = @_; |
|
my @nodes = grep {m/^\d+(\.\d+)?\t/} (@sentence); |
|
my %unreachable; |
|
my %parents; |
|
foreach my $node (@nodes) |
|
{ |
|
my @f = split(/\t/, $node); |
|
my $id = $f[0]; |
|
$unreachable{$id}++; |
|
my $deps = $f[8]; |
|
my @nodeparents = map {m/^(.+):/; $1} (split(/\|/, $deps)); |
|
my %nodeparents; map {$nodeparents{$_}++} (@nodeparents); |
|
@nodeparents = keys(%nodeparents); |
|
$parents{$id} = \@nodeparents; |
|
} |
|
|
|
my %reachable; |
|
$reachable{'0'}++; |
|
my %attach_to_root; |
|
while(1) |
|
{ |
|
my @unreachable = sort(keys(%unreachable)); |
|
last if(scalar(@unreachable)==0); |
|
my $found_new = 0; |
|
foreach my $id (@unreachable) |
|
{ |
|
foreach my $parent (@{$parents{$id}}) |
|
{ |
|
if($reachable{$parent}) |
|
{ |
|
$found_new++; |
|
$reachable{$id}++; |
|
delete($unreachable{$id}); |
|
last; |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@unreachable = sort(keys(%unreachable)); |
|
if(scalar(@unreachable)>0 && !$found_new) |
|
{ |
|
my $node = shift(@unreachable); |
|
$attach_to_root{$node}++; |
|
$reachable{$node}++; |
|
delete($unreachable{$node}); |
|
$found_new = 1; |
|
} |
|
} |
|
|
|
foreach my $line (@sentence) |
|
{ |
|
if($line =~ m/^\d+(\.\d+)?\t/) |
|
{ |
|
my @f = split(/\t/, $line); |
|
if($attach_to_root{$f[0]}) |
|
{ |
|
my @deps = split(/\|/, $f[8]); |
|
@deps = () if(scalar(@deps)==1 && $deps[0] eq '_'); |
|
unshift(@deps, '0:root'); |
|
$f[8] = join('|', @deps); |
|
} |
|
$line = join("\t", @f); |
|
} |
|
} |
|
return @sentence; |
|
} |
|
|