| 1 | package Treex::Tool::Parser::MSTperl::ModelAdditional; | ||||||||||||||
| 2 | { | ||||||||||||||
| 3 | 5 | $Treex::Tool::Parser::MSTperl::ModelAdditional::VERSION = '0.11949'; | |||||||||||||
| 4 | } | ||||||||||||||
| 5 | |||||||||||||||
| 6 | 5 | ▶ | use Data::Dumper; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 5 | ▶ | use autodie; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 5 | ▶ | use Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 5 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | 5 | require File::Temp; | |||||||||||||
| 12 | 5 | ▶ | use File::Temp (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | 5 | ▶ | use File::Temp qw/ :seekable /; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | |||||||||||||||
| 15 | 5 | has config => ( | |||||||||||||
| 16 | isa => 'Treex::Tool::Parser::MSTperl::Config', | ||||||||||||||
| 17 | is => 'ro', | ||||||||||||||
| 18 | required => '1', | ||||||||||||||
| 19 | ); | ||||||||||||||
| 20 | |||||||||||||||
| 21 | has model => ( | ||||||||||||||
| 22 | is => 'rw', | ||||||||||||||
| 23 | isa => 'HashRef', | ||||||||||||||
| 24 | 0 | ▶ | default => sub { {} }, | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 25 | 5 | ); | |||||||||||||
| 26 | |||||||||||||||
| 27 | 5 | has model_file => ( | |||||||||||||
| 28 | is => 'ro', | ||||||||||||||
| 29 | isa => 'Str', | ||||||||||||||
| 30 | default => '', | ||||||||||||||
| 31 | ); | ||||||||||||||
| 32 | |||||||||||||||
| 33 | 5 | has model_format => ( | |||||||||||||
| 34 | is => 'ro', | ||||||||||||||
| 35 | isa => 'Str', | ||||||||||||||
| 36 | default => 'tsv', | ||||||||||||||
| 37 | ); | ||||||||||||||
| 38 | |||||||||||||||
| 39 | 5 | has 'buckets' => ( | |||||||||||||
| 40 | is => 'rw', | ||||||||||||||
| 41 | isa => 'Maybe[ArrayRef[Int]]', | ||||||||||||||
| 42 | default => undef, | ||||||||||||||
| 43 | trigger => \&_buckets_set, | ||||||||||||||
| 44 | ); | ||||||||||||||
| 45 | |||||||||||||||
| 46 | # sets value2bucket, maxBucket and minBucket | ||||||||||||||
| 47 | 0 | ▶ | sub _buckets_set { | ||||||||||||
|
Subroutine
_buckets_set: not called CC=9 SCAR=45.0
| |||||||||||||||
| 48 | 0 | my ( $self, $buckets ) = @_; | |||||||||||||
| 49 | |||||||||||||||
| 50 | 0 | ▶ | if ( !defined $buckets ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 51 | 0 | return; | |||||||||||||
| 52 | } | ||||||||||||||
| 53 | |||||||||||||||
| 54 | 0 | my %value2bucket; | |||||||||||||
| 55 | 0 | $value2bucket{'?'} = '?'; | |||||||||||||
| 56 | |||||||||||||||
| 57 | # find maximal and minimal bucket & partly fill %value2bucket | ||||||||||||||
| 58 | 0 | my $minBucket = -1000; | |||||||||||||
| 59 | 0 | my $maxBucket = 1000; | |||||||||||||
| 60 | 0 | foreach my $bucket ( @{$buckets} ) { | |||||||||||||
| 61 | 0 | ▶ | if ( $value2bucket{$bucket} ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 62 | 0 | warn "Bucket '$bucket' is defined more than once; " | |||||||||||||
| 63 | . "disregarding its later definitions.\n"; | ||||||||||||||
| 64 | } | ||||||||||||||
| 65 | elsif ( $bucket > 0 ) { | ||||||||||||||
| 66 | 0 | croak "MSTperl config file error: " | |||||||||||||
| 67 | . "Error on bucket '$bucket' - " | ||||||||||||||
| 68 | . "buckets must be negative integers."; | ||||||||||||||
| 69 | } | ||||||||||||||
| 70 | else { | ||||||||||||||
| 71 | 0 | $value2bucket{$bucket} = $bucket; | |||||||||||||
| 72 | 0 | ▶ | if ( $bucket > $maxBucket ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 73 | 0 | $maxBucket = $bucket; | |||||||||||||
| 74 | } | ||||||||||||||
| 75 | elsif ( $bucket < $minBucket ) { | ||||||||||||||
| 76 | 0 | $minBucket = $bucket; | |||||||||||||
| 77 | } | ||||||||||||||
| 78 | } | ||||||||||||||
| 79 | } | ||||||||||||||
| 80 | |||||||||||||||
| 81 | # set maxBucket and minBucket | ||||||||||||||
| 82 | 0 | $self->maxBucket($maxBucket); | |||||||||||||
| 83 | 0 | $self->minBucket($minBucket); | |||||||||||||
| 84 | |||||||||||||||
| 85 | # fill %value2bucket from minBucket to maxBucket | ||||||||||||||
| 86 | 0 | my $lastBucket = $minBucket; | |||||||||||||
| 87 | 0 | ▶ | for ( my $value = $minBucket + 1; $value < $maxBucket; $value++ ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 88 | 0 | ▶ | if ( defined $value2bucket{$value} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 89 | |||||||||||||||
| 90 | # the value defines a bucket | ||||||||||||||
| 91 | 0 | $lastBucket = $value2bucket{$value}; | |||||||||||||
| 92 | } | ||||||||||||||
| 93 | else { | ||||||||||||||
| 94 | |||||||||||||||
| 95 | # the value falls into the highest lower bucket | ||||||||||||||
| 96 | 0 | $value2bucket{$value} = $lastBucket; | |||||||||||||
| 97 | } | ||||||||||||||
| 98 | } | ||||||||||||||
| 99 | 0 | $self->value2bucket( \%value2bucket ); | |||||||||||||
| 100 | |||||||||||||||
| 101 | 0 | return; | |||||||||||||
| 102 | } | ||||||||||||||
| 103 | |||||||||||||||
| 104 | has 'value2bucket' => ( | ||||||||||||||
| 105 | is => 'rw', | ||||||||||||||
| 106 | isa => 'HashRef', | ||||||||||||||
| 107 | 0 | ▶ | default => sub { { '?' => '?' } }, | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 108 | 5 | ); | |||||||||||||
| 109 | |||||||||||||||
| 110 | # if mapping is not found in the hash, maxBucket or minBucket is used | ||||||||||||||
| 111 | # -17 is the default as it seems to be the most frequent value | ||||||||||||||
| 112 | # (if no buckets are set. always -17 or '?' are returned) | ||||||||||||||
| 113 | |||||||||||||||
| 114 | # any higher value falls into this bucket | ||||||||||||||
| 115 | 5 | has 'maxBucket' => ( | |||||||||||||
| 116 | isa => 'Int', | ||||||||||||||
| 117 | is => 'rw', | ||||||||||||||
| 118 | default => '-17', | ||||||||||||||
| 119 | ); | ||||||||||||||
| 120 | |||||||||||||||
| 121 | # any lower value falls into this bucket | ||||||||||||||
| 122 | 5 | has 'minBucket' => ( | |||||||||||||
| 123 | isa => 'Int', | ||||||||||||||
| 124 | is => 'rw', | ||||||||||||||
| 125 | default => '-17', | ||||||||||||||
| 126 | ); | ||||||||||||||
| 127 | |||||||||||||||
| 128 | 0 | ▶ | sub load { | ||||||||||||
|
Subroutine
load: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||
| 129 | |||||||||||||||
| 130 | 0 | my ($self) = @_; | |||||||||||||
| 131 | |||||||||||||||
| 132 | 0 | ▶ | if ( $self->config->DEBUG >= 1 ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 133 | 0 | print "Loading additional model from '" . $self->model_file . "...\n"; | |||||||||||||
| 134 | } | ||||||||||||||
| 135 | |||||||||||||||
| 136 | 0 | my $result = undef; | |||||||||||||
| 137 | 0 | ▶ | if ( $self->model_format eq 'tsv' ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 138 | 0 | $result = $self->load_tsv( $self->model_file ); | |||||||||||||
| 139 | } | ||||||||||||||
| 140 | |||||||||||||||
| 141 | # probably TODO | ||||||||||||||
| 142 | # } elsif ( $self->model_format eq 'tsv.gz' ) { | ||||||||||||||
| 143 | # my $tmpfile = File::Temp->new( UNLINK => 1 ); | ||||||||||||||
| 144 | # system "gunzip -c $filename > $tmpfile"; | ||||||||||||||
| 145 | # $filename = $tmpfile->filename; | ||||||||||||||
| 146 | # } | ||||||||||||||
| 147 | else { | ||||||||||||||
| 148 | 0 | croak "Model format " . $self->model_format . " is not supported!"; | |||||||||||||
| 149 | } | ||||||||||||||
| 150 | |||||||||||||||
| 151 | 0 | ▶ | if ($result) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 152 | 0 | ▶ | if ( $self->config->DEBUG >= 1 ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 153 | 0 | print "Additional model loaded.\n"; | |||||||||||||
| 154 | } | ||||||||||||||
| 155 | 0 | return 1; | |||||||||||||
| 156 | } | ||||||||||||||
| 157 | else { | ||||||||||||||
| 158 | 0 | croak "MSTperl parser error: additional model file data error!"; | |||||||||||||
| 159 | } | ||||||||||||||
| 160 | } | ||||||||||||||
| 161 | |||||||||||||||
| 162 | 0 | ▶ | sub load_tsv { | ||||||||||||
|
Subroutine
load_tsv: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 163 | |||||||||||||||
| 164 | # (Str $filename) | ||||||||||||||
| 165 | 0 | my ( $self, $filename ) = @_; | |||||||||||||
| 166 | |||||||||||||||
| 167 | { | ||||||||||||||
| 168 | 0 | open my $file, '<:encoding(UTF-8)', $filename; | |||||||||||||
| 169 | 0 | my $line; | |||||||||||||
| 170 | 0 | ▶ | while ( $line = <$file> ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 171 | 0 | chomp $line; | |||||||||||||
| 172 | 0 | my ( $child, $parent, $value ) = split /\t/, $line; | |||||||||||||
| 173 | 0 | $self->model->{$child}->{$parent} = $value; | |||||||||||||
| 174 | } | ||||||||||||||
| 175 | 0 | close $file; | |||||||||||||
| 176 | } | ||||||||||||||
| 177 | |||||||||||||||
| 178 | 0 | return 1; | |||||||||||||
| 179 | } | ||||||||||||||
| 180 | |||||||||||||||
| 181 | 0 | ▶ | sub get_value { | ||||||||||||
|
Subroutine
get_value: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 182 | 0 | my ( $self, $child, $parent ) = @_; | |||||||||||||
| 183 | |||||||||||||||
| 184 | 0 | my $value = $self->model->{$child}->{$parent}; | |||||||||||||
| 185 | 0 | ▶ | if ( !defined $value ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 186 | 0 | $value = '?'; | |||||||||||||
| 187 | } | ||||||||||||||
| 188 | |||||||||||||||
| 189 | 0 | return $value; | |||||||||||||
| 190 | } | ||||||||||||||
| 191 | |||||||||||||||
| 192 | 0 | ▶ | sub get_rounded_value { | ||||||||||||
|
Subroutine
get_rounded_value: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||
| 193 | 0 | my ( $self, $child, $parent, $rounding ) = @_; | |||||||||||||
| 194 | |||||||||||||||
| 195 | 0 | my $value = $self->model->{$child}->{$parent}; | |||||||||||||
| 196 | 0 | ▶ | if ( defined $value ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 197 | |||||||||||||||
| 198 | # get the rounding coefficient | ||||||||||||||
| 199 | 0 | ▶ | if ( !defined $rounding ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 200 | 0 | $rounding = 0; | |||||||||||||
| 201 | } | ||||||||||||||
| 202 | 0 | my $coef = 1; | |||||||||||||
| 203 | 0 | ▶ | for ( my $i = 0; $i < $rounding; $i++ ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 204 | 0 | $coef *= 10; | |||||||||||||
| 205 | } | ||||||||||||||
| 206 | |||||||||||||||
| 207 | # get the value | ||||||||||||||
| 208 | 0 | $value = int( $value * $coef ) / $coef; | |||||||||||||
| 209 | } | ||||||||||||||
| 210 | else { | ||||||||||||||
| 211 | 0 | $value = '?'; | |||||||||||||
| 212 | } | ||||||||||||||
| 213 | |||||||||||||||
| 214 | 0 | return $value; | |||||||||||||
| 215 | } | ||||||||||||||
| 216 | |||||||||||||||
| 217 | 0 | ▶ | sub get_bucketed_value { | ||||||||||||
|
Subroutine
get_bucketed_value: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||
| 218 | 0 | my ( $self, $child, $parent ) = @_; | |||||||||||||
| 219 | |||||||||||||||
| 220 | 0 | my $value = $self->get_rounded_value( $child, $parent ); | |||||||||||||
| 221 | 0 | my $bucket = $self->value2bucket->{$value}; | |||||||||||||
| 222 | 0 | ▶ | if ( !defined $bucket ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 223 | 0 | ▶ | if ( $value <= $self->minBucket ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 224 | 0 | $bucket = $self->minBucket; | |||||||||||||
| 225 | } | ||||||||||||||
| 226 | else { | ||||||||||||||
| 227 | |||||||||||||||
| 228 | # assert $value > $self->maxBucket | ||||||||||||||
| 229 | 0 | $bucket = $self->maxBucket; | |||||||||||||
| 230 | } | ||||||||||||||
| 231 | } | ||||||||||||||
| 232 | |||||||||||||||
| 233 | 0 | return $bucket; | |||||||||||||
| 234 | } | ||||||||||||||
| 235 | |||||||||||||||
| 236 | 5 | 1; | |||||||||||||
| 237 | |||||||||||||||
| 238 | __END__ | ||||||||||||||