|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use utf8; |
|
use open ':utf8'; |
|
binmode(STDIN, ':utf8'); |
|
binmode(STDOUT, ':utf8'); |
|
binmode(STDERR, ':utf8'); |
|
use List::MoreUtils qw(any); |
|
|
|
|
|
|
|
|
|
|
|
BEGIN |
|
{ |
|
use Cwd; |
|
my $path = $0; |
|
my $currentpath = getcwd(); |
|
$currentpath =~ s/\r?\n$//; |
|
$libpath = $currentpath; |
|
if($path =~ m:/:) |
|
{ |
|
$path =~ s:/[^/]*$:/:; |
|
chdir($path); |
|
$libpath = getcwd(); |
|
chdir($currentpath); |
|
} |
|
$libpath =~ s/\r?\n$//; |
|
|
|
} |
|
use lib $libpath; |
|
use Graph; |
|
use Node; |
|
|
|
my @sentence; |
|
while(<>) |
|
{ |
|
if(m/^\s*$/) |
|
{ |
|
process_sentence(@sentence); |
|
@sentence = (); |
|
} |
|
else |
|
{ |
|
s/\r?\n$//; |
|
push(@sentence, $_); |
|
} |
|
} |
|
|
|
if(scalar(@sentence) > 0) |
|
{ |
|
process_sentence(@sentence); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
sub process_sentence |
|
{ |
|
my @sentence = @_; |
|
my $graph = Graph::from_conllu_lines(@sentence); |
|
|
|
collapse_empty_nodes($graph); |
|
|
|
@sentence = $graph->to_conllu_lines(); |
|
print_sentence(@sentence); |
|
|
|
$graph->remove_all_nodes(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
sub print_sentence |
|
{ |
|
my @sentence = @_; |
|
print(join("\n", @sentence), "\n\n"); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
sub collapse_empty_nodes |
|
{ |
|
my $graph = shift; |
|
my @nodes = $graph->get_nodes(); |
|
my @edges; |
|
foreach my $node (@nodes) |
|
{ |
|
my $cid = $node->id(); |
|
my @iedges = @{$node->iedges()}; |
|
foreach my $iedge (@iedges) |
|
{ |
|
my $pid = $iedge->{id}; |
|
my $deprel = $iedge->{deprel}; |
|
push(@edges, [$pid, $deprel, $cid]); |
|
} |
|
} |
|
my @okedges = grep {$_->[0] =~ m/^\d+$/ && $_->[-1] =~ m/^\d+$/} (@edges); |
|
my @epedges = grep {$_->[0] =~ m/^\d+\.\d+$/} (@edges); |
|
my @ecedges = grep {$_->[-1] =~ m/^\d+\.\d+$/} (@edges); |
|
my %epedges; |
|
foreach my $epedge (@epedges) |
|
{ |
|
$epedges{join(' ', @{$epedge})}++; |
|
} |
|
my %ecedges; |
|
foreach my $ecedge (@ecedges) |
|
{ |
|
$ecedges{join(' ', @{$ecedge})}++; |
|
} |
|
while(@epedges) |
|
{ |
|
my $epedge = shift(@epedges); |
|
my @myecedges = grep {$_->[-1] eq $epedge->[0]} (@ecedges); |
|
if(scalar(@myecedges)==0) |
|
{ |
|
print STDERR ('Ignoring enhanced path because the empty source node does not have any parent: '.join('>', @epedge)."\n"); |
|
} |
|
foreach my $ecedge (@myecedges) |
|
{ |
|
my @newedge = @{$ecedge}; |
|
pop(@newedge); |
|
push(@newedge, @{$epedge}); |
|
|
|
|
|
|
|
|
|
|
|
if($newedge[0] =~ m/^\d+$/ && $newedge[-1] =~ m/^\d+$/) |
|
{ |
|
push(@okedges, \@newedge); |
|
} |
|
else |
|
{ |
|
|
|
|
|
my $cycle = 0; |
|
my %map; |
|
for(my $i = 0; $i <= $#newedge; $i += 2) |
|
{ |
|
if(exists($map{$newedge[$i]})) |
|
{ |
|
$cycle = 1; |
|
last; |
|
} |
|
$map{$newedge[$i]}++; |
|
} |
|
unless($cycle) |
|
{ |
|
if($newedge[0] =~ m/^\d+\.\d+$/) |
|
{ |
|
my $serialized = join(' ', @newedge); |
|
unless(exists($epedges{$serialized})) |
|
{ |
|
push(@epedges, \@newedge); |
|
$epedges{$serialized}++; |
|
} |
|
} |
|
if($newedge[-1] =~ m/^\d+\.\d+$/) |
|
{ |
|
my $serialized = join(' ', @newedge); |
|
unless(exists($ecedges{$serialized})) |
|
{ |
|
push(@ecedges, \@newedge); |
|
} |
|
} |
|
} |
|
else |
|
{ |
|
print STDERR ('Cyclic enhanced path will not be used to construct longer paths: '.join('>', @newedge)."\n"); |
|
} |
|
} |
|
} |
|
} |
|
|
|
|
|
@okedges = sort {my $r = $a->[-1] <=> $b->[-1]; unless($r) {$r = $a->[0] <=> $b->[0]} $r} (@okedges); |
|
|
|
|
|
my %oknodes; |
|
foreach my $okedge (@okedges) |
|
{ |
|
foreach my $item (@{$okedge}) |
|
{ |
|
if($item =~ m/^\d+\.\d+$/) |
|
{ |
|
$oknodes{$item}++; |
|
} |
|
} |
|
} |
|
foreach my $ecedge (@ecedges) |
|
{ |
|
my $item = $ecedge->[-1]; |
|
if(!exists($oknodes{$item})) |
|
{ |
|
print STDERR ('Incoming path to an empty node ignored because the node has no children: '.join('>', @{$ecedge})."\n"); |
|
} |
|
} |
|
|
|
foreach my $node (@nodes) |
|
{ |
|
if($node->id() =~ m/\./) |
|
{ |
|
my @iedges = @{$node->iedges()}; |
|
foreach my $iedge (@iedges) |
|
{ |
|
$graph->remove_edge($iedge->{id}, $node->id(), $iedge->{deprel}); |
|
} |
|
my @oedges = @{$node->oedges()}; |
|
foreach my $oedge (@oedges) |
|
{ |
|
$graph->remove_edge($node->id(), $oedge->{id}, $oedge->{deprel}); |
|
} |
|
$graph->remove_node($node->id()); |
|
} |
|
} |
|
|
|
foreach my $edge (@okedges) |
|
{ |
|
my @edge = @{$edge}; |
|
my $pid = shift(@edge); |
|
my $cid = pop(@edge); |
|
|
|
next unless(scalar(@edge) > 1); |
|
my $deprel = join('>', @edge); |
|
|
|
|
|
|
|
|
|
$deprel =~ s/>\d+\.\d+>/>/g; |
|
|
|
$graph->add_edge($pid, $cid, $deprel); |
|
} |
|
} |
|
|