|
|
|
|
|
|
|
|
|
|
|
use utf8; |
|
use open ':utf8'; |
|
binmode(STDIN, ':utf8'); |
|
binmode(STDOUT, ':utf8'); |
|
binmode(STDERR, ':utf8'); |
|
use List::MoreUtils qw(any); |
|
use Getopt::Long; |
|
|
|
|
|
|
|
|
|
|
|
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 $report_cycles = 0; |
|
GetOptions |
|
( |
|
'report-cycles' => \$report_cycles |
|
); |
|
|
|
my %stats = |
|
( |
|
'n_graphs' => 0, |
|
'n_nodes' => 0, |
|
'n_empty_nodes' => 0, |
|
'n_overt_nodes' => 0, |
|
'n_edges' => 0, |
|
'n_single' => 0, |
|
'n_in2plus' => 0, |
|
'n_top1' => 0, |
|
'n_top2' => 0, |
|
'n_indep' => 0, |
|
'n_cyclic_graphs' => 0, |
|
'n_unconnected_graphs' => 0, |
|
'gapping' => 0, |
|
'conj_effective_parent' => 0, |
|
'conj_shared_dependent' => 0, |
|
'xsubj' => 0, |
|
'relcl' => 0, |
|
'case_deprel' => 0 |
|
); |
|
my @sentence; |
|
while(<>) |
|
{ |
|
if(m/^\s*$/) |
|
{ |
|
process_sentence(@sentence); |
|
@sentence = (); |
|
} |
|
else |
|
{ |
|
s/\r?\n$//; |
|
push(@sentence, $_); |
|
} |
|
} |
|
|
|
if(scalar(@sentence) > 0) |
|
{ |
|
process_sentence(@sentence); |
|
} |
|
|
|
print("$stats{n_graphs} graphs\n"); |
|
print("$stats{n_nodes} nodes\n"); |
|
print(" $stats{n_overt_nodes} overt surface nodes\n"); |
|
print(" $stats{n_empty_nodes} empty nodes\n"); |
|
if(exists($stats{graphs_with_n_empty_nodes})) |
|
{ |
|
my @counts = sort {$a <=> $b} (keys(%{$stats{graphs_with_n_empty_nodes}})); |
|
if(scalar(@counts)>1 || scalar(@counts)==1 && $counts[0]!=0) |
|
{ |
|
foreach my $count (@counts) |
|
{ |
|
print(" $stats{graphs_with_n_empty_nodes}{$count} graphs with $count empty nodes\n"); |
|
} |
|
} |
|
} |
|
print("$stats{n_edges} edges (not counting dependencies on 0)\n"); |
|
print("$stats{n_single} singletons\n"); |
|
print("$stats{n_in2plus} nodes with in-degree greater than 1\n"); |
|
print("$stats{n_top1} top nodes only depending on 0\n"); |
|
print("$stats{n_top2} top nodes with in-degree greater than 1\n"); |
|
print("$stats{n_indep} independent non-top nodes (zero in, nonzero out)\n"); |
|
print("$stats{n_cyclic_graphs} graphs that contain at least one cycle\n"); |
|
print("$stats{n_unconnected_graphs} graphs with multiple non-singleton components\n"); |
|
print("Enhancements defined in Enhanced Universal Dependencies v2 (number of observed signals that the enhancement is applied):\n"); |
|
print("* Gapping: $stats{gapping}\n"); |
|
print("* Coord shared parent: $stats{conj_effective_parent}\n"); |
|
print("* Coord shared depend: $stats{conj_shared_dependent}\n"); |
|
print("* Controlled subject: $stats{xsubj}\n"); |
|
print("* Relative clause: $stats{relcl}\n"); |
|
print("* Deprel with case: $stats{case_deprel}\n"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
sub process_sentence |
|
{ |
|
my @sentence = @_; |
|
my $graph = Graph::from_conllu_lines(@sentence); |
|
|
|
|
|
find_singletons($graph); |
|
|
|
find_cycles($graph); |
|
|
|
find_components($graph); |
|
|
|
find_enhancements($graph); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
sub print_sentence |
|
{ |
|
my @sentence = @_; |
|
print(join("\n", @sentence), "\n\n"); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub find_singletons |
|
{ |
|
my $graph = shift; |
|
|
|
$stats{n_graphs}++; |
|
foreach my $node ($graph->get_nodes()) |
|
{ |
|
|
|
$stats{n_nodes}++; |
|
if($node->id() =~ m/\./) |
|
{ |
|
$stats{n_empty_nodes}++; |
|
} |
|
else |
|
{ |
|
$stats{n_overt_nodes}++; |
|
} |
|
my $indegree = $node->get_in_degree(); |
|
my $outdegree = $node->get_out_degree(); |
|
|
|
$stats{n_edges} += $outdegree; |
|
if($indegree==0 && $outdegree==0) |
|
{ |
|
$stats{n_single}++; |
|
} |
|
elsif($indegree==0 && $outdegree >= 1) |
|
{ |
|
|
|
|
|
$stats{n_indep}++; |
|
} |
|
elsif($indegree==1 && $node->iedges()->[0]{id} == 0) |
|
{ |
|
$stats{n_top1}++; |
|
} |
|
elsif($indegree > 1) |
|
{ |
|
$stats{n_in2plus}++; |
|
if(any {$_->{id}==0} (@{$node->iedges()})) |
|
{ |
|
$stats{n_top2}++; |
|
} |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub find_cycles |
|
{ |
|
my $graph = shift; |
|
|
|
|
|
|
|
my @stack = map {[$_]} ($graph->get_nodes()); |
|
my %processed_node_ids; |
|
while(my $curpath = pop(@stack)) |
|
{ |
|
|
|
|
|
my @curpath = @{$curpath}; |
|
|
|
my $curnode = $curpath[-1]; |
|
|
|
unless(exists($processed_node_ids{$curnode->id()})) |
|
{ |
|
my @curidpath = map {$_->id()} (@curpath); |
|
|
|
|
|
|
|
my @oedges = @{$curnode->oedges()}; |
|
foreach my $oedge (@oedges) |
|
{ |
|
my $childnode = $graph->node($oedge->{id}); |
|
my $childid = $childnode->id(); |
|
if(grep {$_==$childid} (@curidpath)) |
|
{ |
|
$stats{n_cyclic_graphs}++; |
|
if($report_cycles) |
|
{ |
|
print("Found a cycle in this sentence:\n"); |
|
my @comments = @{$graph->comments()}; |
|
foreach my $comment (@comments) |
|
{ |
|
print("$comment\n"); |
|
} |
|
my @cycle = map {$_->id().':'.$_->form()} (@curpath, $childnode); |
|
|
|
shift(@cycle) while($cycle[0] ne $cycle[-1]); |
|
print("The cycle: ".join(' ', @cycle)."\n"); |
|
print("\n"); |
|
} |
|
return 1; |
|
} |
|
my @extpath = @curpath; |
|
push(@extpath, $childnode); |
|
push(@stack, \@extpath); |
|
} |
|
|
|
|
|
|
|
|
|
$processed_node_ids{$curnode->id()}++; |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
sub find_components |
|
{ |
|
my $graph = shift; |
|
my %component_node_ids; |
|
my $component_size = 0; |
|
foreach my $node ($graph->get_nodes()) |
|
{ |
|
my $indegree = $node->get_in_degree(); |
|
my $outdegree = $node->get_out_degree(); |
|
|
|
unless($indegree+$outdegree==0) |
|
{ |
|
|
|
if($component_size==0) |
|
{ |
|
|
|
my @nodes_to_process = ($node); |
|
my %processed_node_ids; |
|
while(my $curnode = pop(@nodes_to_process)) |
|
{ |
|
next if(exists($processed_node_ids{$curnode->id()})); |
|
foreach my $iedge (@{$curnode->iedges()}) |
|
{ |
|
unless($iedge->{id}==0 || exists($processed_node_ids{$iedge->{id}})) |
|
{ |
|
push(@nodes_to_process, $graph->node($iedge->{id})); |
|
} |
|
} |
|
foreach my $oedge (@{$curnode->oedges()}) |
|
{ |
|
unless(exists($processed_node_ids{$oedge->{id}})) |
|
{ |
|
push(@nodes_to_process, $graph->node($oedge->{id})); |
|
} |
|
} |
|
$processed_node_ids{$curnode->id()}++; |
|
} |
|
%component_node_ids = %processed_node_ids; |
|
$component_size = scalar(keys(%component_node_ids)); |
|
} |
|
|
|
|
|
|
|
else |
|
{ |
|
if(!exists($component_node_ids{$node->id()})) |
|
{ |
|
$stats{n_unconnected_graphs}++; |
|
return 1; |
|
} |
|
} |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub relation |
|
{ |
|
my $p = shift; |
|
my $c = shift; |
|
my $graph = shift; |
|
my @oedges = @{$graph->node($p)->oedges()}; |
|
my @matching_children = grep {$_->{id} == $c} (@oedges); |
|
if(scalar(@matching_children)==0) |
|
{ |
|
return undef; |
|
} |
|
else |
|
{ |
|
if(scalar(@matching_children)>1) |
|
{ |
|
print STDERR ("WARNING: Enhanced graph should not connect the same two nodes twice.\n"); |
|
} |
|
return $matching_children[0]->{deprel}; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
sub find_enhancements |
|
{ |
|
my $graph = shift; |
|
my $n_empty_nodes_in_this_graph = 0; |
|
foreach my $curnode ($graph->get_nodes()) |
|
{ |
|
my @iedges = @{$curnode->iedges()}; |
|
my @oedges = @{$curnode->oedges()}; |
|
|
|
if($curnode->id() =~ m/\./) |
|
{ |
|
$stats{gapping}++; |
|
$n_empty_nodes_in_this_graph++; |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if(scalar(@iedges) >= 2 && |
|
scalar(grep {$_->{deprel} =~ m/^conj(:|$)/} (@iedges)) > 0 && |
|
scalar(grep {$_->{deprel} !~ m/^conj(:|$)/} (@iedges)) > 0) |
|
{ |
|
$stats{conj_effective_parent}++; |
|
} |
|
if(scalar(@iedges) >= 2) |
|
{ |
|
|
|
my %gpconj; |
|
foreach my $iedge (@iedges) |
|
{ |
|
if($iedge->{deprel} !~ m/^conj(:|$)/) |
|
{ |
|
my @gpedges = @{$graph->node($iedge->{id})->iedges()}; |
|
foreach my $gpedge (@gpedges) |
|
{ |
|
if($gpedge->{deprel} =~ m/^conj(:|$)/) |
|
{ |
|
$gpconj{$gpedge->{id}}++; |
|
} |
|
} |
|
} |
|
} |
|
|
|
my $found = 0; |
|
foreach my $iedge (@iedges) |
|
{ |
|
if($iedge->{deprel} !~ m/^conj(:|$)/ && exists($gpconj{$iedge->{id}})) |
|
{ |
|
$found = 1; |
|
last; |
|
} |
|
} |
|
if($found) |
|
{ |
|
$stats{conj_shared_dependent}++; |
|
} |
|
} |
|
|
|
|
|
my @subjpr = grep {$_->{deprel} =~ m/^nsubj(:|$)/} (@iedges); |
|
my @xcompgpr; |
|
foreach my $pr (@subjpr) |
|
{ |
|
my @xgpr = grep {$_->{deprel} =~ m/^xcomp(:|$)/} (@{$graph->node($pr->{id})->iedges()}); |
|
push(@xcompgpr, @xgpr); |
|
} |
|
my @coreprxgpr = grep {my $r = relation($_->{id}, $curnode->{id}, $graph); defined($r) && $r =~ m/^(nsubj|obj|iobj)(:|$)/} (@xcompgpr); |
|
if(scalar(@coreprxgpr) > 0) |
|
{ |
|
$stats{xsubj}++; |
|
} |
|
|
|
my @refpr = grep {$_->{deprel} =~ m/^ref(:|$)/} (@iedges); |
|
if(scalar(@refpr) > 0) |
|
{ |
|
$stats{relcl}++; |
|
} |
|
|
|
|
|
|
|
|
|
my @unusual = grep {$_->{deprel} =~ m/(:.*:|[^a-z:])/} (@iedges); |
|
if(scalar(@unusual) > 0) |
|
{ |
|
$stats{case_deprel}++; |
|
} |
|
else |
|
{ |
|
my $basic_parent = $curnode->bparent(); |
|
my $basic_deprel = $curnode->bdeprel(); |
|
my @matchingpr = grep {$_->{id} == $basic_parent} (@iedges); |
|
my @extendedpr = grep {$_->{deprel} =~ m/^$basic_deprel:.+/} (@matchingpr); |
|
if(scalar(@extendedpr) > 0) |
|
{ |
|
$stats{case_deprel}++; |
|
} |
|
} |
|
} |
|
$stats{graphs_with_n_empty_nodes}{$n_empty_nodes_in_this_graph}++; |
|
} |
|
|