| 1 | =head1 NAME | ||
| 2 | |||
| 3 | MetaTrans::SmsCz - MetaTrans plug-in for L<http://slovniky.sms.cz/> | ||
| 4 | |||
| 5 | =cut | ||
| 6 | |||
| 7 | package MetaTrans::SmsCz; | ||
| 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; | ||
| 15 | use HTTP::Request; | ||
| 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<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} = "slovniky.sms.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 | # convert to Perl's internal UTF-8 format | ||
| 87 | $expression = Encode::decode_utf8($expression) | ||
| 88 | unless Encode::is_utf8($expression); | ||
| 89 | |||
| 90 | # replace blanks with pluses (+) | ||
| 91 | $expression =~ s/\s+/+/g; | ||
| 92 | |||
| 93 | # convert to cp1250 character encoding (that's what server expects) | ||
| 94 | $expression = Encode::encode("cp1250", lc $expression) | ||
| 95 | if $src_lang_code ne 'rus'; | ||
| 96 | |||
| 97 | # do some server-specific character escapings | ||
| 98 | $expression = &_my_escape($expression); | ||
| 99 | |||
| 100 | 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 | my $request = HTTP::Request->new(GET => $query); | ||
| 108 | |||
| 109 | 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 | my $self = shift; | ||
| 126 | my $contents = shift; | ||
| 127 | my $src_lang_code = shift; | ||
| 128 | my $dest_lang_code = shift; | ||
| 129 | |||
| 130 | my @result; | ||
| 131 | while ($contents =~ m| | ||
| 132 | <a\s[^>]*>\s*([^<]*?)\s*</a> | ||
| 133 | \s*</td>\s*<td\s[^>]*> | ||
| 134 | \s* - | ||
| 135 | \s*</td>\s*<td\s[^>]*> | ||
| 136 | \s*<a\s[^>]*>\s*([^<]*?)\s*</a> | ||
| 137 | |gsix) | ||
| 138 | { | ||
| 139 | # the output is in cp1250 character encoding with HTML entities, | ||
| 140 | # let's convert it to UTF-8 | ||
| 141 | my $expr = convert_to_utf8('cp1250', $1); | ||
| 142 | my $trans = convert_to_utf8('cp1250', $2); | ||
| 143 | |||
| 144 | $expr = &_normalize_german_article($expr) | ||
| 145 | if $src_lang_code eq 'ger'; | ||
| 146 | |||
| 147 | $trans = &_normalize_german_article($trans) | ||
| 148 | if $dest_lang_code eq 'ger'; | ||
| 149 | |||
| 150 | push @result, ($expr, $trans); | ||
| 151 | } | ||
| 152 | |||
| 153 | 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 | my $unesc = shift; | ||
| 161 | my $result; | ||
| 162 | |||
| 163 | foreach my $char (split //, $unesc) | ||
| 164 | { | ||
| 165 | my $ord = ord($char); | ||
| 166 | $result .= $ord>>4 == 0x43 ? sprintf('%%E%X', $ord & 0xf) : | ||
| 167 | $ord>>4 == 0x44 ? sprintf('%%F%X', $ord & 0xf) : | ||
| 168 | $ord == 0x2B ? '+' : | ||
| 169 | sprintf('%%%X', $ord); | ||
| 170 | } | ||
| 171 | |||
| 172 | return $result; | ||
| 173 | } | ||
| 174 | |||
| 175 | # der Hund -> Hund; r | ||
| 176 | sub _normalize_german_article | ||
| 177 | { | ||
| 178 | my $expr = shift; | ||
| 179 | my $expr_dec = decode_utf8($expr); | ||
| 180 | |||
| 181 | $expr_dec = $2 . "; " . substr($1, 2, 1) | ||
| 182 | if $expr_dec =~ /^(der|die|das) (\w+)$/; | ||
| 183 | |||
| 184 | return Encode::encode_utf8($expr_dec); | ||
| 185 | } | ||
| 186 | |||
| 187 | 1; | ||
| 188 | |||
| 189 | __END__ | ||