File Coverage

blib/lib/I22r/Translate/Backend.pm
Criterion Covered Total %
statement 19 32 59.3
branch 4 12 33.3
condition 4 8 50.0
subroutine 5 7 71.4
pod 1 2 50.0
total 33 61 54.1


line stmt bran cond sub pod time code
1             package I22r::Translate::Backend;
2 11     11   95931 use Moose::Role;
  11         557051  
  11         90  
3 11     11   65213 use I22r::Translate::Request;
  11         25  
  11         464  
4 11     11   1214 use I22r::Translate::Result;
  11         28  
  11         3352  
5              
6             our $VERSION = '0.96';
7             requires 'can_translate';
8             requires 'get_translations';
9             # requires 'config';
10              
11             sub name {
12 0     0 0 0 my $pkg = shift;
13 0         0 $pkg =~ s/.*:://;
14 0         0 return $pkg;
15             }
16              
17             # convenience method to update a configuration hashref,
18             # usually a lexical variable in another package.
19             #
20             # if no @opts provided: return the $config hashref
21             # if one @opts provided: return the value from the $config hashref
22             # if >1 @opts provided: update $config with key-value pairs from @opts
23             sub config {
24 129     129 1 3971 my ($class, @opts) = @_;
25 129   100     491 my $config = $I22r::Translate::config{$class} //= {};
26 129 50       323 if (@opts == 0) {
27 0         0 return $config;
28             }
29 129 100 66     716 if (@opts == 1 && ref($opts[0]) eq '') {
30 113         661 return $config->{$opts[0]};
31             }
32 16 50       104 my %opts = ref($opts[0]) eq 'HASH' ? %{$opts[0]} : @opts;
  0         0  
33 16         185 $config->{$_} = $opts{$_} for keys %opts;
34             }
35              
36             sub __config {
37 11     11   93 use Carp;
  11         22  
  11         2700  
38 0     0     confess;
39 0           my ($class, $config, @opts) = @_;
40 0 0 0       if (@opts == 0) {
    0          
41 0           return $config;
42             } elsif (@opts == 1 && ref($opts[0]) eq '') {
43 0           return $config->{ $opts[0] };
44             } else {
45 0 0         my %opts = ref($opts[0]) eq 'HASH' ? %{$opts[0]} : @opts;
  0            
46 0           $config->{$_} = $opts{$_} for keys %opts;
47             }
48             }
49              
50             1;
51              
52             __END__
53              
54             Backends to consider:
55              
56             _X_ Google
57             _X_ Microsoft/Azure
58             ___ SysTrans
59             ___ Apertium
60             ___ InterTran
61              
62             =head1 NAME
63              
64             I22r::Translate::Backend - role for I22r::Translate translation sources
65              
66             =head1 DESCRIPTION
67              
68             Packages that want to provide translation results in the
69             L<I22r::Translate> framework must fulfill the
70             C<I22r::Translate::Backend> role.
71              
72             The rest of this document should only be interesting to
73             backend developers.
74              
75             =head1 FUNCTIONS/METHODS
76              
77             Backend modules are typically accessed "statically", so
78             a backend does not need a constructor or need to manage
79             backend "objects". Configuration for a backend should
80             reside in the global configuration of the L<I22r::Translate>
81             module (so for a backend called C<My::I22r::Backend>,
82             configuration for that backend will be accessible in
83             C<< $I22r::Translate::config{"My::I22r::Backend"} >>).
84              
85             In the following function documentation, C<$backend> is a
86             string and the name of the backend package, B<not> a
87             backend "object".
88              
89             =head2 can_translate
90              
91             =head2 $quality = $backend->can_translate($lang1,$lang2)
92              
93             Informs the L<I22r::Translate> module about whether a backend
94             can perform a translation between the given language pair.
95             The return value should be a value less than or equal to 1,
96             and indicates the expected "quality" of the translation
97             in that language pair performed by this backend, where
98             1 indicates a "perfect" translation and 0 indicates a
99             very poor translation. The L<I22r::Translate> will call
100             this function on all available backends and try the
101             backends that return the highest values first. Backends
102             that return a value less than or equal to zero will not
103             be used to translate that language pair.
104              
105             =head2 config
106              
107             =head2 $config_hash = $backend->config
108              
109             =head2 $config_value = $backend->config( $key )
110              
111             =head2 $backend->config( %opts )
112              
113             Get or set configuration for this backend.
114              
115             =head2 get_translations
116              
117             =head2 @list = $backend->get_translations( $request )
118              
119             Function that performs the translations specified in
120             the given L<I22r::Translate::Request> object.
121              
122             If any translations are successful, this function should
123             set elements in C<< $request->results >> and return the
124             list of ids (the keys of the C<< $request->text >> hash)
125             of the inputs that were translated with this backend.
126              
127             =head1 SEE ALSO
128              
129             L<I22r::Translate>, L<I22r::Translate::Request>
130              
131             =cut