MetaTrans/SeznamCz.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« MetaTrans/Languages.pm ↑ Summary MetaTrans/SlovnikCz.pm »
1 =head1 NAME
2  
3 MetaTrans::SeznamCz - MetaTrans plug-in for L<http://slovnik.seznam.cz/>
4  
5 =cut
6  
7 package MetaTrans::SeznamCz;
8  
9 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use vars qw($VERSION @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 0 ▶use MetaTrans::Base;
Subroutine BEGIN: called CC=1 SCAR=2.6
13  
14 use HTTP::Request;
15 use URI::Escape;
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<host_server> attribute.
29  
30 =back
31  
32 =cut
33  
34 sub new
35 {
36 my $class = shift;
37 my %options = @_;
38  
39 $options{host_server} = "slovnik.seznam.cz"
40 unless (defined $options{host_server});
41  
42 my $self = new MetaTrans::Base(%options);
43 $self = bless $self, $class;
44  
45 $self->set_languages("cze", "eng", "ger", "fre", "spa", "ita", "rus");
46  
47 $self->set_dir_1_to_all("cze");
48 $self->set_dir_all_to_1("cze");
49  
50 return $self;
51 }
52  
53 =head1 METHODS
54  
55 Methods are inherited from C<MetaTrans::Base>. 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<HTTP::Request> 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 my $self = shift;
72 my $expression = shift;
73 my $src_lang_code = shift;
74 my $dest_lang_code = shift;
75  
76 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 my $query =
87 'http://slovnik.seznam.cz/?' .
88 "q=" . uri_escape($expression) .
89 "&lang=" . $table{$src_lang_code} . "_" . $table{$dest_lang_code};
90 my $request = HTTP::Request->new(GET => $query);
91  
92 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 my $self = shift;
109 my $contents = shift;
110 my $src_lang_code = shift;
111 my $dest_lang_code = shift;
112  
113 my @result;
114 while ($contents =~ m|
115 <tr>
116 \s*
117 <td\sclass="word">
118 \s*
119 (.*?)
120 \s*
121 </td>
122 \s*
123 <td\sclass="translated">
124 (.*?)
125 </td>
126 \s*
127 </tr>
128 |gsix)
129 {
130 my $expr = _get_expr($1);
131 my @trans = _get_trans($2);
132  
133 $expr = _normalize_german($expr)
134 if $src_lang_code eq 'ger';
135  
136 foreach my $trans (@trans) {
137  
138 $trans = _normalize_german($trans)
139 if $dest_lang_code eq 'ger';
140  
141 push @result, ($expr, $trans);
142 }
143 }
144  
145 return @result;
146 }
147  
148 sub _get_expr
149 {
150 my $string = shift;
151 $string =~ s/<img[^>]+>//g;
152 $string =~ s/<a\s+href="[^>]+"><\/a>//g;
153 if ($string =~ m/<a\s+href="[^>]+">(.*?)<\/a>/)
154 {
155 return $1;
156 }
157 else
158 {
159 return '';
160 }
161 }
162  
163 sub _get_trans
164 {
165 my $string = shift;
166 $string =~ s/<br\s*\/>//g;
167 $string =~ s/<img[^>]+>//g;
168 $string =~ s/<a\s+href="[^>]+"><\/a>//g;
169 $string =~ s/\s{2,}/ /g;
170 my @result;
171 while ($string =~ m/<a\s+href="[^>]+">(.*?)<\/a>/gimx)
172 {
173 push @result, $1;
174 }
175 return @result;
176 }
177  
178 # normalize german article: Hund, r -> Hund; r
179 sub _normalize_german
180 {
181 my $expr = shift;
182  
183 # normalize german article: Hund (der) -> Hund; r
184 $expr = $1 . "; " . substr($2, 2, 1)
185 if $expr =~ m/^(.*?),\s+(der|die|das)$/;
186  
187 return $expr;
188 }
189  
190 1;
191  
192 __END__
« MetaTrans/Languages.pm ↑ Summary MetaTrans/SlovnikCz.pm »