line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
=head1 NAME |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
MetaTrans::SmsCz - MetaTrans plug-in for L |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=cut |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
package MetaTrans::SmsCz; |
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
1259
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
38
|
|
10
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
31
|
|
11
|
1
|
|
|
1
|
|
5
|
use vars qw($VERSION @ISA); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
56
|
|
12
|
1
|
|
|
1
|
|
6
|
use MetaTrans::Base qw(convert_to_utf8); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
44
|
|
13
|
|
|
|
|
|
|
|
14
|
1
|
|
|
1
|
|
6
|
use Encode; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
84
|
|
15
|
1
|
|
|
1
|
|
6
|
use HTTP::Request; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
941
|
|
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::SmsCz->new(%options) |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
This method constructs a new MetaTrans::SmsCz 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} = "slovniky.sms.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
|
|
|
|
|
|
|
# convert to Perl's internal UTF-8 format |
87
|
0
|
0
|
|
|
|
|
$expression = Encode::decode_utf8($expression) |
88
|
|
|
|
|
|
|
unless Encode::is_utf8($expression); |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
# replace blanks with pluses (+) |
91
|
0
|
|
|
|
|
|
$expression =~ s/\s+/+/g; |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
# convert to cp1250 character encoding (that's what server expects) |
94
|
0
|
0
|
|
|
|
|
$expression = Encode::encode("cp1250", lc $expression) |
95
|
|
|
|
|
|
|
if $src_lang_code ne 'rus'; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# do some server-specific character escapings |
98
|
0
|
|
|
|
|
|
$expression = &_my_escape($expression); |
99
|
|
|
|
|
|
|
|
100
|
0
|
|
|
|
|
|
my $query = |
101
|
|
|
|
|
|
|
"http://slovniky.sms.cz/index.php?" . |
102
|
|
|
|
|
|
|
"P_id_kategorie=65456" . |
103
|
|
|
|
|
|
|
"&P_soubor=/slovniky/index.php" . |
104
|
|
|
|
|
|
|
"&send_data=1" . |
105
|
|
|
|
|
|
|
"&word=$expression" . |
106
|
|
|
|
|
|
|
"&bjvolba=" . $table{$src_lang_code} . "_" . $table{$dest_lang_code}; |
107
|
0
|
|
|
|
|
|
my $request = HTTP::Request->new(GET => $query); |
108
|
|
|
|
|
|
|
|
109
|
0
|
|
|
|
|
|
return $request; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=item $plugin->process_response($contents, $src_lang_code, $dest_lang_code) |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
Process the server response contents. Return the result of the translation in |
115
|
|
|
|
|
|
|
an array of following form: |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
(expression_1, translation_1, expression_2, translation_2, ...) |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=back |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=cut |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub process_response |
124
|
|
|
|
|
|
|
{ |
125
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
126
|
0
|
|
|
|
|
|
my $contents = shift; |
127
|
0
|
|
|
|
|
|
my $src_lang_code = shift; |
128
|
0
|
|
|
|
|
|
my $dest_lang_code = shift; |
129
|
|
|
|
|
|
|
|
130
|
0
|
|
|
|
|
|
my @result; |
131
|
0
|
|
|
|
|
|
while ($contents =~ m| |
132
|
|
|
|
|
|
|
]*>\s*([^<]*?)\s* |
133
|
|
|
|
|
|
|
\s* | \s*]*> |
134
|
|
|
|
|
|
|
\s* - |
135
|
|
|
|
|
|
|
\s* | \s*]*> |
136
|
|
|
|
|
|
|
\s*]*>\s*([^<]*?)\s* |
137
|
|
|
|
|
|
|
|gsix) |
138
|
|
|
|
|
|
|
{ |
139
|
|
|
|
|
|
|
# the output is in cp1250 character encoding with HTML entities, |
140
|
|
|
|
|
|
|
# let's convert it to UTF-8 |
141
|
0
|
|
|
|
|
|
my $expr = convert_to_utf8('cp1250', $1); |
142
|
0
|
|
|
|
|
|
my $trans = convert_to_utf8('cp1250', $2); |
143
|
|
|
|
|
|
|
|
144
|
0
|
0
|
|
|
|
|
$expr = &_normalize_german_article($expr) |
145
|
|
|
|
|
|
|
if $src_lang_code eq 'ger'; |
146
|
|
|
|
|
|
|
|
147
|
0
|
0
|
|
|
|
|
$trans = &_normalize_german_article($trans) |
148
|
|
|
|
|
|
|
if $dest_lang_code eq 'ger'; |
149
|
|
|
|
|
|
|
|
150
|
0
|
|
|
|
|
|
push @result, ($expr, $trans); |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
0
|
|
|
|
|
|
return @result; |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
# server specific character escaping |
157
|
|
|
|
|
|
|
# it's really strange, so don't worry if you don't understand it |
158
|
|
|
|
|
|
|
sub _my_escape |
159
|
|
|
|
|
|
|
{ |
160
|
0
|
|
|
0
|
|
|
my $unesc = shift; |
161
|
0
|
|
|
|
|
|
my $result; |
162
|
|
|
|
|
|
|
|
163
|
0
|
|
|
|
|
|
foreach my $char (split //, $unesc) |
164
|
|
|
|
|
|
|
{ |
165
|
0
|
|
|
|
|
|
my $ord = ord($char); |
166
|
0
|
0
|
|
|
|
|
$result .= $ord>>4 == 0x43 ? sprintf('%%E%X', $ord & 0xf) : |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
167
|
|
|
|
|
|
|
$ord>>4 == 0x44 ? sprintf('%%F%X', $ord & 0xf) : |
168
|
|
|
|
|
|
|
$ord == 0x2B ? '+' : |
169
|
|
|
|
|
|
|
sprintf('%%%X', $ord); |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
|
172
|
0
|
|
|
|
|
|
return $result; |
173
|
|
|
|
|
|
|
} |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
# der Hund -> Hund; r |
176
|
|
|
|
|
|
|
sub _normalize_german_article |
177
|
|
|
|
|
|
|
{ |
178
|
0
|
|
|
0
|
|
|
my $expr = shift; |
179
|
0
|
|
|
|
|
|
my $expr_dec = decode_utf8($expr); |
180
|
|
|
|
|
|
|
|
181
|
0
|
0
|
|
|
|
|
$expr_dec = $2 . "; " . substr($1, 2, 1) |
182
|
|
|
|
|
|
|
if $expr_dec =~ /^(der|die|das) (\w+)$/; |
183
|
|
|
|
|
|
|
|
184
|
0
|
|
|
|
|
|
return Encode::encode_utf8($expr_dec); |
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
1; |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
__END__ |