| 1 | package Treex::Block::W2A::ParseMSTperl; | ||||||||||
| 2 | 1 | $Treex::Block::W2A::ParseMSTperl::VERSION = '2.20151102'; | |||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use Treex::Core::Common; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | extends 'Treex::Block::W2A::BaseChunkParser'; | |||||||||
| 8 | 1 | with 'Treex::Block::W2A::AnalysisWithAlignedTrees'; | |||||||||
| 9 | |||||||||||
| 10 | 1 | ▶ | use Treex::Tool::Parser::MSTperl::Config; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 1 | ▶ | use Treex::Tool::Parser::MSTperl::Parser; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 1 | ▶ | use Treex::Tool::Parser::MSTperl::Sentence; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 1 | ▶ | use Treex::Tool::Parser::MSTperl::Node; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | 1 | ▶ | use Treex::Core::Resource qw(require_file_from_share); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | # Look for model under "model_dir/model_name.model" | ||||||||||
| 18 | # and its config "model_dir/model_name.config". | ||||||||||
| 19 | # Absolute path is needed if not a model from share. | ||||||||||
| 20 | 1 | has 'model_from_share' => ( | |||||||||
| 21 | is => 'ro', | ||||||||||
| 22 | isa => 'Bool', | ||||||||||
| 23 | default => '1', | ||||||||||
| 24 | ); | ||||||||||
| 25 | |||||||||||
| 26 | 1 | has 'model_name' => ( | |||||||||
| 27 | is => 'ro', | ||||||||||
| 28 | isa => 'Str', | ||||||||||
| 29 | required => 1, | ||||||||||
| 30 | ); | ||||||||||
| 31 | |||||||||||
| 32 | 1 | has 'model_dir' => ( | |||||||||
| 33 | is => 'ro', | ||||||||||
| 34 | isa => 'Str', | ||||||||||
| 35 | default => 'data/models/parser/mst_perl', | ||||||||||
| 36 | ); | ||||||||||
| 37 | |||||||||||
| 38 | 1 | has 'model_gz' => ( | |||||||||
| 39 | is => 'ro', | ||||||||||
| 40 | isa => 'Bool', | ||||||||||
| 41 | default => '0', | ||||||||||
| 42 | ); | ||||||||||
| 43 | |||||||||||
| 44 | 1 | has parser => ( | |||||||||
| 45 | is => 'ro', | ||||||||||
| 46 | isa => 'Treex::Tool::Parser::MSTperl::Parser', | ||||||||||
| 47 | init_arg => undef, | ||||||||||
| 48 | builder => '_build_parser', | ||||||||||
| 49 | lazy => 1, | ||||||||||
| 50 | ); | ||||||||||
| 51 | |||||||||||
| 52 | 1 | has 'additional_model_from_share' => ( | |||||||||
| 53 | is => 'ro', | ||||||||||
| 54 | isa => 'Bool', | ||||||||||
| 55 | default => '1', | ||||||||||
| 56 | ); | ||||||||||
| 57 | |||||||||||
| 58 | 0 | ▶ | sub _build_parser { | ||||||||
|
Subroutine
_build_parser: not called CC=7 SCAR=40.3
| |||||||||||
| 59 | 0 | my ($self) = @_; | |||||||||
| 60 | |||||||||||
| 61 | 0 | my $base_name = $self->model_dir . '/' . $self->model_name; | |||||||||
| 62 | |||||||||||
| 63 | 0 | ▶ | my $config_file = ( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | $self->model_from_share | ||||||||||
| 65 | ? | ||||||||||
| 66 | require_file_from_share( "$base_name.config", ref($self) ) | ||||||||||
| 67 | : | ||||||||||
| 68 | "$base_name.config" | ||||||||||
| 69 | ); | ||||||||||
| 70 | 0 | my $config = Treex::Tool::Parser::MSTperl::Config->new( | |||||||||
| 71 | config_file => $config_file, | ||||||||||
| 72 | training => 0, | ||||||||||
| 73 | DEBUG => 0, | ||||||||||
| 74 | lazyload => 1, | ||||||||||
| 75 | ); | ||||||||||
| 76 | |||||||||||
| 77 | 0 | my $parser = Treex::Tool::Parser::MSTperl::Parser->new( | |||||||||
| 78 | config => $config | ||||||||||
| 79 | ); | ||||||||||
| 80 | 0 | ▶ | my $model_file_name = ( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | $self->model_gz | ||||||||||
| 82 | ? | ||||||||||
| 83 | "$base_name.model.gz" | ||||||||||
| 84 | : | ||||||||||
| 85 | "$base_name.model" | ||||||||||
| 86 | ); | ||||||||||
| 87 | |||||||||||
| 88 | 0 | ▶ | my $model_file = ( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | $self->model_from_share | ||||||||||
| 90 | ? | ||||||||||
| 91 | require_file_from_share( $model_file_name, ref($self) ) | ||||||||||
| 92 | : | ||||||||||
| 93 | $model_file_name | ||||||||||
| 94 | ); | ||||||||||
| 95 | 0 | log_info "Loading MSTperl::Parser model $model_file"; | |||||||||
| 96 | 0 | $parser->load_model($model_file); | |||||||||
| 97 | |||||||||||
| 98 | 0 | ▶ | if ( $self->additional_model_from_share ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 99 | 0 | ▶ | if ( $config->use_pmi) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | 0 | my $filename = $self->model_dir . '/' . $config->pmi_model_file; | |||||||||
| 101 | 0 | my $loaded_filename = require_file_from_share( $filename, ref($self) ); | |||||||||
| 102 | 0 | $config->pmi_model_file($loaded_filename); | |||||||||
| 103 | } | ||||||||||
| 104 | 0 | ▶ | if ( $config->use_cprob) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 105 | 0 | my $filename = $self->model_dir . '/' . $config->cprob_model_file; | |||||||||
| 106 | 0 | my $loaded_filename = require_file_from_share( $filename, ref($self) ); | |||||||||
| 107 | 0 | $config->cprob_model_file($loaded_filename); | |||||||||
| 108 | } | ||||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | # filenames in config have been changed -- can now load | ||||||||||
| 112 | 0 | $config->load(); | |||||||||
| 113 | |||||||||||
| 114 | 0 | return $parser; | |||||||||
| 115 | } | ||||||||||
| 116 | |||||||||||
| 117 | 0 | ▶ | override 'process_start' => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 118 | 0 | my ($self) = @_; | |||||||||
| 119 | 0 | super(); | |||||||||
| 120 | |||||||||||
| 121 | # enforce parser initialization | ||||||||||
| 122 | 0 | $self->parser; | |||||||||
| 123 | |||||||||||
| 124 | 0 | return; | |||||||||
| 125 | 1 | }; | |||||||||
| 126 | |||||||||||
| 127 | 0 | ▶ | sub parse_chunk { | ||||||||
|
Subroutine
parse_chunk: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 128 | 0 | my ( $self, @a_nodes ) = @_; | |||||||||
| 129 | |||||||||||
| 130 | # assumes that a_nodes are ordered correctly | ||||||||||
| 131 | # (BaseChunkParser ensures that now) | ||||||||||
| 132 | |||||||||||
| 133 | # get alignment mapping | ||||||||||
| 134 | 0 | my $alignment_hash = | |||||||||
| 135 | $self->_get_alignment_hash( $a_nodes[0]->get_bundle() ); | ||||||||||
| 136 | |||||||||||
| 137 | # convert from treex data structures to parser data structures | ||||||||||
| 138 | 0 | my $sentence = $self->_get_sentence( $alignment_hash, @a_nodes ); | |||||||||
| 139 | |||||||||||
| 140 | # run the parser | ||||||||||
| 141 | 0 | my @node_parents = @{ $self->parser->parse_sentence($sentence) }; | |||||||||
| 142 | |||||||||||
| 143 | # set nodes' parents | ||||||||||
| 144 | 0 | my @roots = (); | |||||||||
| 145 | 0 | foreach my $a_node (@a_nodes) { | |||||||||
| 146 | 0 | my $parent_index_1based = shift @node_parents; | |||||||||
| 147 | 0 | ▶ | if ($parent_index_1based) { # node's parent is a real node | ||||||||
|
Branch: 1 branch
| |||||||||||
| 148 | 0 | my $parent_index_0based = $parent_index_1based - 1; | |||||||||
| 149 | 0 | my $parent_node = $a_nodes[$parent_index_0based]; | |||||||||
| 150 | 0 | $a_node->set_parent($parent_node); | |||||||||
| 151 | } else { | ||||||||||
| 152 | # == 0; node's parent is the technical root of the chunk | ||||||||||
| 153 | # keep the original parent (the technical root of the sentence) | ||||||||||
| 154 | 0 | push @roots, $a_node; | |||||||||
| 155 | } | ||||||||||
| 156 | } | ||||||||||
| 157 | |||||||||||
| 158 | # return roots of all parse subtrees | ||||||||||
| 159 | 0 | return @roots; | |||||||||
| 160 | } | ||||||||||
| 161 | |||||||||||
| 162 | # convert from treex data structures to parser data structures | ||||||||||
| 163 | 0 | ▶ | sub _get_sentence { | ||||||||
|
Subroutine
_get_sentence: not called CC=4 SCAR=30.0
| |||||||||||
| 164 | 0 | my ( $self, $alignment_hash, @a_nodes ) = @_; | |||||||||
| 165 | |||||||||||
| 166 | # create objects of class Treex::Tool::Parser::MSTperl::Node | ||||||||||
| 167 | 0 | my @nodes; | |||||||||
| 168 | 0 | foreach my $a_node (@a_nodes) { | |||||||||
| 169 | |||||||||||
| 170 | # get field values | ||||||||||
| 171 | 0 | my @field_values; | |||||||||
| 172 | 0 | foreach my $field_name ( @{ $self->parser->config->field_names } ) { | |||||||||
| 173 | 0 | my $field_value = $self->_get_field_value( | |||||||||
| 174 | $a_node, $field_name, $alignment_hash | ||||||||||
| 175 | ); | ||||||||||
| 176 | 0 | ▶ | if ( defined $field_value ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 177 | 0 | push @field_values, $field_value; | |||||||||
| 178 | } else { | ||||||||||
| 179 | 0 | push @field_values, ''; | |||||||||
| 180 | } | ||||||||||
| 181 | } | ||||||||||
| 182 | |||||||||||
| 183 | # create Node object | ||||||||||
| 184 | 0 | my $node = Treex::Tool::Parser::MSTperl::Node->new( | |||||||||
| 185 | fields => \@field_values, | ||||||||||
| 186 | config => $self->parser->config | ||||||||||
| 187 | ); | ||||||||||
| 188 | |||||||||||
| 189 | # store the Node object | ||||||||||
| 190 | 0 | push @nodes, $node; | |||||||||
| 191 | } | ||||||||||
| 192 | |||||||||||
| 193 | # create object of class Treex::Tool::Parser::MSTperl::Sentence | ||||||||||
| 194 | 0 | my $sentence = Treex::Tool::Parser::MSTperl::Sentence->new( | |||||||||
| 195 | nodes => \@nodes, | ||||||||||
| 196 | config => $self->parser->config | ||||||||||
| 197 | ); | ||||||||||
| 198 | |||||||||||
| 199 | 0 | return $sentence; | |||||||||
| 200 | } | ||||||||||
| 201 | |||||||||||
| 202 | 0 | ▶ | sub get_coarse_grained_tag { | ||||||||
|
Subroutine
get_coarse_grained_tag: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 203 | 0 | log_warn 'get_coarse_grained_tag should be implemented in derived classes'; | |||||||||
| 204 | 0 | my ( $self, $tag ) = @_; | |||||||||
| 205 | |||||||||||
| 206 | 0 | return substr( $tag, 0, 1 ); | |||||||||
| 207 | } | ||||||||||
| 208 | 1 | 1; | |||||||||
| 209 | |||||||||||
| 210 | __END__ | ||||||||||