Interset/SimpleAtom.pm untested

filter: Statementstmt 0.0% 0 / 55 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod n/a totaltotal 0.0% 0 / 71 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 # ABSTRACT: Atomic driver for a surface feature.
2 # Copyright © 2014 Dan Zeman <zeman@ufal.mff.cuni.cz>
3  
4 package Lingua::Interset::SimpleAtom;
5 use strict;
6 use warnings;
7 our $VERSION = '3.015';
8  
9 use utf8;
10 use open ':utf8';
11 use namespace::autoclean;
12 use Moose;
13 use MooseX::SemiAffordanceAccessor; # attribute x is written using set_x($value) and read using x()
14 use Lingua::Interset;
15 use Lingua::Interset::FeatureStructure;
16 extends 'Lingua::Interset::Atom';
17  
18  
19  
20 has 'intfeature' => ( isa => 'Str', is => 'ro', required => 1, documentation => 'Name of the corresponding Interset feature.' );
21 # In this special case of Atom, decoding map is taken as a simple hash; Atom::(de|en)code_map will be automatically derived:
22 # { 1 => 'nom', 2 => 'gen', 3 => 'dat', 4 => 'acc', 5 => 'voc', 6 => 'loc', 7 => 'ins' }
23 has 'simple_decode_map' => ( isa => 'HashRef', is => 'ro', required => 1 );
24 # If you want a simple de/encoding table with an additional default encoding rule, e.g. {'@' => '-'}, then set 'encode_default' => '-'.
25 has 'encode_default' => ( isa => 'Str', is => 'ro', default => '' );
26  
27  
28  
29 #------------------------------------------------------------------------------
30 # This block will be called before object construction. It will take the
31 # simple_decode_map attribute from the user and use it to construct parent class
32 # decode_map and encode_map. Then it will pass all the attributes to the
33 # constructor.
34 #------------------------------------------------------------------------------
35 around BUILDARGS => sub
36 {
37 my $orig = shift;
38 my $class = shift;
39 # Call the default BUILDARGS in Moose::Object. It will take care of distinguishing between a hash reference and a plain hash.
40 my $attr = $class->$orig(@_);
41 if($attr->{intfeature} && $attr->{simple_decode_map})
42 {
43 # Construct decode_map in the form expected by Atom.
44 my %dm;
45 # Construct encode_map in the form expected by Atom.
46 my %em;
47 # The encode_default attribute has default value '' but it may not have been set yet.
48 $attr->{encode_default} = '' if(!defined($attr->{encode_default}));
49 my %valuehash = ('@' => $attr->{encode_default});
50 $em{$attr->{intfeature}} = \%valuehash;
51 my @survalues = keys(%{$attr->{simple_decode_map}});
52 foreach my $sv (@survalues)
53 {
54 my $iv = $attr->{simple_decode_map}{$sv};
55 $dm{$sv} = [$attr->{intfeature} => $iv];
56 $valuehash{$iv} = $sv;
57 }
58 # Now add the references to the driver functions to the attribute hash.
59 $attr->{surfeature} = $attr->{intfeature};
60 $attr->{decode_map} = \%dm;
61 $attr->{encode_map} = \%em;
62 }
63 else
64 {
65 confess("Missing attribute 'intfeature' or 'simple_decode_map'");
66 }
67 return $attr;
68 };
69  
70  
71  
72 1;
73  
74 __END__
75  
76 =pod
77  
78 =encoding UTF-8
79  
80 =head1 NAME
81  
82 Lingua::Interset::SimpleAtom - Atomic driver for a surface feature.
83  
84 =head1 VERSION
85  
86 version 3.015
87  
88 =head1 SYNOPSIS
89  
90 use Lingua::Interset::SimpleAtom;
91  
92 my $atom = Lingua::Interset::SimpleAtom->new
93 (
94 'intfeature' => 'case',
95 'simple_decode_map' => { 1 => 'nom', 2 => 'gen', 3 => 'dat', 4 => 'acc', 5 => 'voc', 6 => 'loc', 7 => 'ins' }
96 );
97  
98 =head1 DESCRIPTION
99  
100 SimpleAtom is a special simple case of L<Lingua::Interset::Atom>.
101 Unlike in general Atom, for SimpleAtom there is an I<injective> function mapping the surface strings to values of just one Interset feature.
102 This makes defining the decoding and encoding maps much easier.
103  
104 =head1 ATTRIBUTES
105  
106 =head2 intfeature
107  
108 Name of the Interset feature to which the atom maps.
109  
110 =head2 simple_decode_map
111  
112 A compact description of mapping from the surface tags to the Interset feature values.
113 It is a hash reference.
114 Hash keys are surface tags and hash values are the values of the corresponding Interset feature.
115  
116 =head2 encode_default
117  
118 If you want a simple decoding/encoding table with an additional default encoding rule, e.g. C<< {'@' => '-'} >>, then set C<< 'encode_default' => '-' >>.
119 By default, this attribute is set to the empty string.
120  
121 =head1 SEE ALSO
122  
123 L<Lingua::Interset::Atom>,
124 L<Lingua::Interset::Tagset>,
125 L<Lingua::Interset::FeatureStructure>
126  
127 =head1 AUTHOR
128  
129 Dan Zeman <zeman@ufal.mff.cuni.cz>
130  
131 =head1 COPYRIGHT AND LICENSE
132  
133 This software is copyright (c) 2019 by Univerzita Karlova (Charles University).
134  
135 This is free software; you can redistribute it and/or modify it under
136 the same terms as the Perl 5 programming language system itself.
137  
138 =cut