File Coverage

blib/lib/MetaTrans/SeznamCz.pm
Criterion Covered Total %
statement 18 66 27.2
branch 0 10 0.0
condition n/a
subroutine 6 12 50.0
pod 3 3 100.0
total 27 91 29.6


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             MetaTrans::SeznamCz - MetaTrans plug-in for L
4              
5             =cut
6              
7             package MetaTrans::SeznamCz;
8              
9 1     1   2571 use strict;
  1         2  
  1         35  
10 1     1   6 use warnings;
  1         2  
  1         33  
11 1     1   5 use vars qw($VERSION @ISA);
  1         2  
  1         57  
12 1     1   5 use MetaTrans::Base;
  1         2  
  1         42  
13              
14 1     1   5 use HTTP::Request;
  1         2  
  1         33  
15 1     1   5 use URI::Escape;
  1         2  
  1         1002  
16              
17             $VERSION = do { my @r = (q$Revision: 1.2 $ =~ /\d+/g); sprintf "%d."."%02d", @r };
18             @ISA = qw(MetaTrans::Base);
19              
20             =head1 CONSTRUCTOR METHODS
21              
22             =over 4
23              
24             =item MetaTrans::SeznamCz->new(%options)
25              
26             This method constructs a new MetaTrans::SeznamCz object and returns it. All
27             C<%options> are passed to C<< MetaTrans::Base->new >>. The method also sets
28             supported translation directions and the C attribute.
29              
30             =back
31              
32             =cut
33              
34             sub new
35             {
36 0     0 1   my $class = shift;
37 0           my %options = @_;
38              
39 0 0         $options{host_server} = "slovnik.seznam.cz"
40             unless (defined $options{host_server});
41              
42 0           my $self = new MetaTrans::Base(%options);
43 0           $self = bless $self, $class;
44              
45 0           $self->set_languages("cze", "eng", "ger", "fre", "spa", "ita", "rus");
46              
47 0           $self->set_dir_1_to_all("cze");
48 0           $self->set_dir_all_to_1("cze");
49              
50 0           return $self;
51             }
52              
53             =head1 METHODS
54              
55             Methods are inherited from C. Following methods are overriden:
56              
57             =cut
58              
59             =over 4
60              
61             =item $plugin->create_request($expression, $src_lang_code, $dest_lang_code)
62              
63             Create and return a C object to be used for retrieving
64             translation of the C<$expression> from C<$src_lang_code> language to
65             C<$dest_lang_code> language.
66              
67             =cut
68              
69             sub create_request
70             {
71 0     0 1   my $self = shift;
72 0           my $expression = shift;
73 0           my $src_lang_code = shift;
74 0           my $dest_lang_code = shift;
75              
76 0           my %table = (
77             cze => "cz",
78             eng => "en",
79             ger => "de",
80             fre => "fr",
81             spa => "es",
82             ita => "it",
83             rus => "ru",
84             );
85              
86 0           my $query =
87             'http://slovnik.seznam.cz/?' .
88             "q=" . uri_escape($expression) .
89             "&lang=" . $table{$src_lang_code} . "_" . $table{$dest_lang_code};
90 0           my $request = HTTP::Request->new(GET => $query);
91              
92 0           return $request;
93             }
94              
95             =item $plugin->process_response($contents, $src_lang_code, $dest_lang_code)
96              
97             Process the server response contents. Return the result of the translation in
98             an array of following form:
99              
100             (expression_1, translation_1, expression_2, translation_2, ...)
101              
102             =back
103              
104             =cut
105              
106             sub process_response
107             {
108 0     0 1   my $self = shift;
109 0           my $contents = shift;
110 0           my $src_lang_code = shift;
111 0           my $dest_lang_code = shift;
112              
113 0           my @result;
114 0           while ($contents =~ m|
115            
116             \s*
117            
118             \s*
119             (.*?)
120             \s*
121            
122             \s*
123            
124             (.*?)
125            
126             \s*
127            
128             |gsix)
129             {
130 0           my $expr = _get_expr($1);
131 0           my @trans = _get_trans($2);
132              
133 0 0         $expr = _normalize_german($expr)
134             if $src_lang_code eq 'ger';
135              
136 0           foreach my $trans (@trans) {
137              
138 0 0         $trans = _normalize_german($trans)
139             if $dest_lang_code eq 'ger';
140              
141 0           push @result, ($expr, $trans);
142             }
143             }
144              
145 0           return @result;
146             }
147              
148             sub _get_expr
149             {
150 0     0     my $string = shift;
151 0           $string =~ s/]+>//g;
152 0           $string =~ s/]+"><\/a>//g;
153 0 0         if ($string =~ m/]+">(.*?)<\/a>/)
154             {
155 0           return $1;
156             }
157             else
158             {
159 0           return '';
160             }
161             }
162              
163             sub _get_trans
164             {
165 0     0     my $string = shift;
166 0           $string =~ s///g;
167 0           $string =~ s/]+>//g;
168 0           $string =~ s/]+"><\/a>//g;
169 0           $string =~ s/\s{2,}/ /g;
170 0           my @result;
171 0           while ($string =~ m/]+">(.*?)<\/a>/gimx)
172             {
173 0           push @result, $1;
174             }
175 0           return @result;
176             }
177              
178             # normalize german article: Hund, r -> Hund; r
179             sub _normalize_german
180             {
181 0     0     my $expr = shift;
182              
183             # normalize german article: Hund (der) -> Hund; r
184 0 0         $expr = $1 . "; " . substr($2, 2, 1)
185             if $expr =~ m/^(.*?),\s+(der|die|das)$/;
186              
187 0           return $expr;
188             }
189              
190             1;
191              
192             __END__