| | package Node; |
| |
|
| | use utf8; |
| | use namespace::autoclean; |
| |
|
| | use Moose; |
| | use MooseX::SemiAffordanceAccessor; |
| | use List::MoreUtils qw(any); |
| | use Graph; |
| |
|
| |
|
| |
|
| | has 'graph' => (is => 'rw', isa => 'Maybe[Graph]', documentation => 'Refers to the graph (sentence) this node belongs to.'); |
| | has 'id' => (is => 'rw', isa => 'Str', required => 1, documentation => 'The ID column in CoNLL-U file.'); |
| | has 'form' => (is => 'rw', isa => 'Str', documentation => 'The FORM column in CoNLL-U file.'); |
| | has 'lemma' => (is => 'rw', isa => 'Str', documentation => 'The LEMMA column in CoNLL-U file.'); |
| | has 'upos' => (is => 'rw', isa => 'Str', documentation => 'The UPOS column in CoNLL-U file.'); |
| | has 'xpos' => (is => 'rw', isa => 'Str', documentation => 'The XPOS column in CoNLL-U file.'); |
| | has 'feats' => (is => 'rw', isa => 'HashRef', documentation => 'Hash holding the features from the FEATS column in CoNLL-U file.'); |
| | has 'misc' => (is => 'rw', isa => 'ArrayRef', documentation => 'Array holding the attributes from the MISC column in CoNLL-U file.'); |
| | has '_head' => (is => 'rw', isa => 'Str', documentation => 'Temporary storage for the HEAD column before the graph structure is built.'); |
| | has '_deprel' => (is => 'rw', isa => 'Str', documentation => 'Temporary storage for the DEPREL column before the graph structure is built.'); |
| | has '_deps' => (is => 'rw', isa => 'Str', documentation => 'Temporary storage for the DEPS column before the graph structure is built.'); |
| | has 'iedges' => (is => 'rw', isa => 'ArrayRef', default => sub {[]}, documentation => 'Array of records of incoming edges. Each record is a hash ref, keys are id, deprel.'); |
| | has 'oedges' => (is => 'rw', isa => 'ArrayRef', default => sub {[]}, documentation => 'Array of records of outgoing edges. Each record is a hash ref, keys are id, deprel.'); |
| | has 'bparent' => (is => 'rw', isa => 'Str', documentation => 'Parent node in the basic tree.'); |
| | has 'bdeprel' => (is => 'rw', isa => 'Str', documentation => 'Type of relation between this node and its parent in the basic tree.'); |
| | has 'bchildren' => (is=>'rw', isa => 'ArrayRef', default => sub {[]}, documentation => 'Array of ids of children in the basic tree.'); |
| | has 'predicate' => (is => 'rw', isa => 'Str', documentation => 'Lemma and frame identifier of the predicate.'); |
| | has 'argedges' => (is => 'rw', isa => 'ArrayRef', default => sub {[]}, documentation => 'Array of records of edges from a predicate to its arguments, labeled with argument labels.'); |
| | has 'argpattern' => (is => 'rw', isa => 'Str', documentation => 'Predicate with the pattern of deprels of its arguments.'); |
| |
|
| |
|
| |
|
| | |
| | |
| | |
| | |
| | sub clone |
| | { |
| | my $self = shift; |
| | my %feats = %{$self->feats()}; |
| | my @misc = @{$self->misc()}; |
| | my @bchildren = @{$self->bchildren()}; |
| | my @iedges = @{$self->iedges()}; |
| | my @oedges = @{$self->oedges()}; |
| | my @argedges = @{$self->argedges()}; |
| | |
| | |
| | |
| | |
| | |
| | my $clone = new Node |
| | ( |
| | 'id' => $self->id(), |
| | 'form' => $self->form(), |
| | 'lemma' => $self->lemma(), |
| | 'upos' => $self->upos(), |
| | 'xpos' => $self->xpos(), |
| | 'feats' => \%feats, |
| | '_head' => $self->_head(), |
| | 'bparent' => $self->bparent(), |
| | '_deprel' => $self->_deprel(), |
| | 'bdeprel' => $self->bdeprel(), |
| | 'bchildren' => \@bchildren, |
| | '_deps' => $self->_deps(), |
| | 'iedges' => \@iedges, |
| | 'oedges' => \@oedges, |
| | 'misc' => \@misc, |
| | 'predicate' => $self->predicate(), |
| | 'argedges' => \@argedges, |
| | 'argpattern' => $self->argpattern() |
| | ); |
| | return $clone; |
| | } |
| |
|
| |
|
| |
|
| | |
| | |
| | |
| | |
| | |
| | sub set_feats_from_conllu |
| | { |
| | confess('Incorrect number of arguments') if(scalar(@_) != 2); |
| | my $self = shift; |
| | my $feats = shift; |
| | unless($feats eq '_') |
| | { |
| | my @fvpairs = split(/\|/, $feats); |
| | my %feats; |
| | foreach my $fv (@fvpairs) |
| | { |
| | if($fv =~ m/^([A-Za-z\[\]]+)=([A-Za-z0-9,]+)$/) |
| | { |
| | my $f = $1; |
| | my $v = $2; |
| | if(exists($feats{$f})) |
| | { |
| | print STDERR ("WARNING: Duplicite feature definition: '$f=$feats{$f}' will be overwritten with '$f=$v'.\n"); |
| | } |
| | $feats{$f} = $v; |
| | } |
| | else |
| | { |
| | print STDERR ("WARNING: Unrecognized feature-value pair '$fv'.\n"); |
| | } |
| | } |
| | |
| | |
| | if(scalar(keys(%feats))>0) |
| | { |
| | $self->set_feats(\%feats); |
| | } |
| | } |
| | } |
| |
|
| |
|
| |
|
| | |
| | |
| | |
| | sub get_feats_string |
| | { |
| | confess('Incorrect number of arguments') if(scalar(@_) != 1); |
| | my $self = shift; |
| | if(!defined($self->feats())) |
| | { |
| | return '_'; |
| | } |
| | my %feats = %{$self->feats()}; |
| | my @keys = sort {lc($a) cmp lc($b)} (keys(%feats)); |
| | if(scalar(@keys)==0) |
| | { |
| | return '_'; |
| | } |
| | else |
| | { |
| | return join('|', map {"$_=$feats{$_}"} (@keys)); |
| | } |
| | } |
| |
|
| |
|
| |
|
| | |
| | |
| | |
| | |
| | |
| | sub set_misc_from_conllu |
| | { |
| | confess('Incorrect number of arguments') if(scalar(@_) != 2); |
| | my $self = shift; |
| | my $misc = shift; |
| | |
| | |
| | |
| | $misc =~ s/^\s+//; |
| | $misc =~ s/\s+$//; |
| | unless($misc eq '_') |
| | { |
| | my @misc = split(/\|/, $misc); |
| | $self->set_misc(\@misc); |
| | } |
| | } |
| |
|
| |
|
| |
|
| | |
| | |
| | |
| | sub get_misc_string |
| | { |
| | confess('Incorrect number of arguments') if(scalar(@_) != 1); |
| | my $self = shift; |
| | if(!defined($self->misc())) |
| | { |
| | return '_'; |
| | } |
| | my @misc = @{$self->misc()}; |
| | if(scalar(@misc)==0) |
| | { |
| | return '_'; |
| | } |
| | else |
| | { |
| | return join('|', @misc); |
| | } |
| | } |
| |
|
| |
|
| |
|
| | |
| | |
| | |
| | |
| | sub basic_depends_on |
| | { |
| | confess('Incorrect number of arguments') if(scalar(@_) != 2); |
| | my $self = shift; |
| | confess('Node is not member of a graph') if(!defined($self->graph())); |
| | my $aid = shift; |
| | |
| | |
| | |
| | my $graph = $self->graph(); |
| | my $id = $self->bparent(); |
| | while(defined($id)) |
| | { |
| | if($id==$aid) |
| | { |
| | return 1; |
| | } |
| | else |
| | { |
| | $id = $graph->get_node($id)->bparent(); |
| | } |
| | } |
| | return 0; |
| | } |
| |
|
| |
|
| |
|
| | |
| | |
| | |
| | |
| | |
| | sub set_basic_dep_from_conllu |
| | { |
| | confess('Incorrect number of arguments') if(scalar(@_) != 1); |
| | my $self = shift; |
| | confess('Node is not member of a graph') if(!defined($self->graph())); |
| | my $head = $self->_head(); |
| | my $deprel = $self->_deprel(); |
| | unless(!defined($head) || $head eq '' || $head eq '_') |
| | { |
| | |
| | |
| | |
| | |
| | if(defined($self->bparent())) |
| | { |
| | confess('Basic parent already exists'); |
| | } |
| | if(!$self->graph()->has_node($head)) |
| | { |
| | confess("Basic dependency '$deprel' from a non-existent node '$head'"); |
| | } |
| | if($head == $self->id()) |
| | { |
| | confess("Cannot attach node '$head' to itself in the basic tree"); |
| | } |
| | if($self->graph()->get_node($head)->basic_depends_on($self->id())) |
| | { |
| | my $id = $self->id(); |
| | confess("Cannot attach node '$id' to '$head' in the basic tree because it would make a cycle"); |
| | } |
| | $self->set_bparent($head); |
| | $self->set_bdeprel($deprel); |
| | push(@{$self->graph()->get_node($head)->bchildren()}, $self->id()); |
| | } |
| | |
| | |
| | else |
| | { |
| | $self->set_bparent('_'); |
| | $self->set_bdeprel('_'); |
| | } |
| | } |
| |
|
| |
|
| |
|
| | |
| | |
| | |
| | |
| | |
| | sub set_deps_from_conllu |
| | { |
| | confess('Incorrect number of arguments') if(scalar(@_) != 1); |
| | my $self = shift; |
| | confess('Node is not member of a graph') if(!defined($self->graph())); |
| | my $deps = $self->_deps(); |
| | unless(!defined($deps) || $deps eq '' || $deps eq '_') |
| | { |
| | my @deps = split(/\|/, $deps); |
| | foreach my $dep (@deps) |
| | { |
| | if($dep =~ m/^(\d+(?:\.\d+)?):(.+)$/) |
| | { |
| | my $h = $1; |
| | my $d = $2; |
| | |
| | if(!$self->graph()->has_node($h)) |
| | { |
| | confess("Incoming dependency '$d' from a non-existent node '$h'"); |
| | } |
| | |
| | my %pr = |
| | ( |
| | 'id' => $h, |
| | 'deprel' => $d |
| | ); |
| | |
| | if(any {$_->{id} == $h && $_->{deprel} eq $d} (@{$self->iedges()})) |
| | { |
| | print STDERR ("WARNING: Ignoring repeated declaration of edge '$h --- $d ---> $self->{id}'.\n"); |
| | } |
| | else |
| | { |
| | push(@{$self->iedges()}, \%pr); |
| | |
| | my %cr = |
| | ( |
| | 'id' => $self->id(), |
| | 'deprel' => $d |
| | ); |
| | push(@{$self->graph()->get_node($h)->oedges()}, \%cr); |
| | } |
| | } |
| | else |
| | { |
| | print STDERR ("WARNING: Cannot understand dep '$dep'\n"); |
| | } |
| | } |
| | } |
| | } |
| |
|
| |
|
| |
|
| | |
| | |
| | |
| | sub get_deps_string |
| | { |
| | confess('Incorrect number of arguments') if(scalar(@_) != 1); |
| | my $self = shift; |
| | my @iedges = sort |
| | { |
| | my $r = cmpids($a->{id}, $b->{id}); |
| | unless($r) |
| | { |
| | $r = $a->{deprel} cmp $b->{deprel}; |
| | } |
| | $r |
| | } |
| | (@{$self->iedges()}); |
| | if(scalar(@iedges)==0) |
| | { |
| | return '_'; |
| | } |
| | else |
| | { |
| | return join('|', map {"$_->{id}:$_->{deprel}"} (@iedges)); |
| | } |
| | } |
| |
|
| |
|
| |
|
| | |
| | |
| | |
| | |
| | |
| | sub cmpids |
| | { |
| | my $a = shift; |
| | my $b = shift; |
| | |
| | |
| | |
| | |
| | |
| | $a =~ m/^(\d+)(?:\.(\d+))?(?:-(\d+))?$/; |
| | my $amaj = $1; confess("Unexpected node id '$a->{id}'") if(!defined($amaj)); |
| | my $amin = defined($2) ? $2 : 0; |
| | my $amwt = defined($3) ? $3 : 0; |
| | $b =~ m/^(\d+)(?:\.(\d+))?(?:-(\d+))?$/; |
| | my $bmaj = $1; confess("Unexpected node id '$b->{id}'") if(!defined($bmaj)); |
| | my $bmin = defined($2) ? $2 : 0; |
| | my $bmwt = defined($3) ? $3 : 0; |
| | my $r = $amaj <=> $bmaj; |
| | unless($r) |
| | { |
| | $r = $amin <=> $bmin; |
| | unless($r) |
| | { |
| | |
| | |
| | $r = $bmwt <=> $amwt; |
| | } |
| | } |
| | return $r; |
| | } |
| |
|
| |
|
| |
|
| | |
| | |
| | |
| | sub get_in_degree |
| | { |
| | confess('Incorrect number of arguments') if(scalar(@_) != 1); |
| | my $self = shift; |
| | return scalar(@{$self->iedges()}); |
| | } |
| |
|
| |
|
| |
|
| | |
| | |
| | |
| | sub get_out_degree |
| | { |
| | confess('Incorrect number of arguments') if(scalar(@_) != 1); |
| | my $self = shift; |
| | return scalar(@{$self->oedges()}); |
| | } |
| |
|
| |
|
| |
|
| | |
| | |
| | |
| | |
| | sub get_args_string |
| | { |
| | confess('Incorrect number of arguments') if(scalar(@_) != 1); |
| | my $self = shift; |
| | my @argedges = @{$self->argedges()}; |
| | if(scalar(@argedges)==0) |
| | { |
| | return '_'; |
| | } |
| | else |
| | { |
| | return join('|', map {my $a = $_; my $ids = ref($a->{id}) eq 'ARRAY' ? join(',', @{$a->{id}}) : $a->{id}; "$a->{deprel}:$ids"} (@argedges)); |
| | } |
| | } |
| |
|
| |
|
| |
|
| | __PACKAGE__->meta->make_immutable(); |
| |
|
| | 1; |
| |
|
| |
|
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |