MetaTrans/SlovnikZcuCz.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/SlovnikCz.pm ↑ Summary MetaTrans/SmsCz.pm »
1 =head1 NAME
2  
3 MetaTrans::SlovnikZcuCz - MetaTrans plug-in for L<http://slovnik.zcu.cz/>
4  
5 =cut
6  
7 package MetaTrans::SlovnikZcuCz;
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 qw(convert_to_utf8);
Subroutine BEGIN: called CC=1 SCAR=2.6
13  
14 use Encode qw(decode_utf8 encode);
15 use HTTP::Request;
16 use URI::Escape;
17  
18 $VERSION = do { my @r = (q$Revision: 1.1 $ =~ /\d+/g); sprintf "%d."."%02d", @r };
19 @ISA = qw(MetaTrans::Base);
20  
21 =head1 CONSTRUCTOR METHODS
22  
23 =over 4
24  
25 =item MetaTrans::SlovnikZcuCz->new(%options)
26  
27 This method constructs a new MetaTrans::SlovnikZcuCz object and returns it. All
28 C<%options> are passed to C<< MetaTrans::Base->new >>. The method also sets
29 supported translation directions and the C<host_server> attribute.
30  
31 =back
32  
33 =cut
34  
35 sub new
36 {
37 my $class = shift;
38 my %options = @_;
39  
40 $options{host_server} = "slovnik.zcu.cz"
41 unless (defined $options{host_server});
42  
43 my $self = new MetaTrans::Base(%options);
44 $self = bless $self, $class;
45  
46 # set supported languages
47 $self->set_languages("cze", "eng");
48  
49 $self->set_dir_1_to_all("cze");
50 $self->set_dir_all_to_1("cze");
51  
52 return $self;
53 }
54  
55 =head1 METHODS
56  
57 Methods are inherited from C<MetaTrans::Base>. Following methods are overriden:
58  
59 =cut
60  
61 =over 4
62  
63 =item $plugin->create_request($expression, $src_lang_code, $dest_lang_code)
64  
65 Create and return a C<HTTP::Request> object to be used for retrieving
66 translation of the C<$expression> from C<$src_lang_code> language to
67 C<$dest_lang_code> language.
68  
69 =cut
70  
71 sub create_request
72 {
73 my $self = shift;
74 my $expression = shift;
75 my $src_lang_code = shift;
76 my $dest_lang_code = shift;
77  
78 # convert to perl internal form
79 $expression = decode_utf8($expression);
80  
81 # convert to iso-8859-2
82 $expression = uri_escape(encode('iso-8859-2', $expression));
83  
84 my $request = HTTP::Request->new(POST => "http://slovnik.zcu.cz/online/index.php");
85 $request->content_type('application/x-www-form-urlencoded');
86 $request->content("word=$expression");
87  
88 return $request;
89 }
90  
91 =item $plugin->process_response($contents, $src_lang_code, $dest_lang_code)
92  
93 Process the server response contents. Return the result of the translation in
94 an array of following form:
95  
96 (expression_1, translation_1, expression_2, translation_2, ...)
97  
98 =back
99  
100 =cut
101  
102 sub process_response
103 {
104 my $self = shift;
105 my $contents = shift;
106 my $src_lang_code = shift;
107 my $dest_lang_code = shift;
108  
109 # the output is in iso-8859-2 character encoding with HTML entities,
110 # let's convert it to UTF-8
111 $contents = convert_to_utf8('iso-8859-2', $contents);
112  
113 my @result;
114 while ($contents =~ m|
115 <form\s+name="addtranslate"[^>]*>\s+
116 <input\s+type="hidden"\s+name="word"[^>]*>\s+
117 <input\s+type="hidden"\s+name="page"\s+value="index.php">\s+
118 <table\s+align="center">
119 (.*?)
120 </table>\s+
121 <input\s+type="hidden"\s+name="polozek"[^>]*>
122 |gsix)
123 {
124  
125 push @result, _process_row($1, $src_lang_code);
126 }
127  
128 return @result;
129 }
130  
131 sub _process_row {
132 my $string = shift;
133 my $src_lang_code = shift;
134  
135 my @result;
136 my $actual;
137 while ($string =~ m|<tr[^>]*>(.*?)</tr>|gsix)
138 {
139 my $td = $1;
140 if ($td =~ m|<td[^>]*><h5>(.*?)</h5></td>|gsix)
141 {
142 $actual = $1;
143 }
144 elsif ($td =~ m|
145 <td>([^<>]+?)</td>\s+
146 <td>([^<>]+?)</td>\s+
147 <td>.*?</td>\s+
148 <td>.*?</td>\s+
149 <td>.*?</td>
150 |gsix)
151 {
152 my ($first, $second) = ($1, $2);
153 if ($src_lang_code eq 'eng' && $actual =~ m|^Anglicko-Český\ssměr$|msx)
154 {
155 push @result, $first, $second;
156 }
157 elsif ($src_lang_code eq 'cze'
158 && $actual =~ m|^Česko-Anglický\ssměr$|msx)
159 {
160 push @result, $second, $first;
161 }
162 }
163 }
164 return @result;
165 }
166  
167  
168 1;
169  
170 __END__
« MetaTrans/SlovnikCz.pm ↑ Summary MetaTrans/SmsCz.pm »