| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | =head1 NAME | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | MetaTrans::SlovnikCz - MetaTrans plug-in for L | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  | =cut | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | package MetaTrans::SlovnikCz; | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 1 |  |  | 1 |  | 1084 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 33 |  | 
| 10 | 1 |  |  | 1 |  | 6 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 28 |  | 
| 11 | 1 |  |  | 1 |  | 6 | use vars qw($VERSION @ISA); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 49 |  | 
| 12 | 1 |  |  | 1 |  | 6 | use MetaTrans::Base; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 44 |  | 
| 13 |  |  |  |  |  |  |  | 
| 14 | 1 |  |  | 1 |  | 6 | use Encode; | 
|  | 1 |  |  |  |  | 9 |  | 
|  | 1 |  |  |  |  | 93 |  | 
| 15 | 1 |  |  | 1 |  | 6 | use HTTP::Request; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 20 |  | 
| 16 | 1 |  |  | 1 |  | 5 | use URI::Escape; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 964 |  | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | $VERSION = do { my @r = (q$Revision: 1.2 $ =~ /\d+/g); sprintf "%d."."%02d", @r }; | 
| 19 |  |  |  |  |  |  | @ISA     = qw(MetaTrans::Base); | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | =head1 CONSTRUCTOR METHODS | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | =over 4 | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | =item MetaTrans::SlovnikCz->new(%options) | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | This method constructs a new MetaTrans::SlovnikCz 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 attribute. | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | =back | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | =cut | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | sub new | 
| 36 |  |  |  |  |  |  | { | 
| 37 | 0 |  |  | 0 | 1 |  | my $class   = shift; | 
| 38 | 0 |  |  |  |  |  | my %options = @_; | 
| 39 |  |  |  |  |  |  |  | 
| 40 | 0 | 0 |  |  |  |  | $options{host_server} = "www.slovnik.cz" | 
| 41 |  |  |  |  |  |  | unless (defined $options{host_server}); | 
| 42 |  |  |  |  |  |  |  | 
| 43 | 0 |  |  |  |  |  | my $self = new MetaTrans::Base(%options); | 
| 44 | 0 |  |  |  |  |  | $self = bless $self, $class; | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | # set supported languages | 
| 47 | 0 |  |  |  |  |  | $self->set_languages("cze", "eng", "epo", "fre", "ger", "ita", "lat", | 
| 48 |  |  |  |  |  |  | "pol", "rus", "slo", "spa"); | 
| 49 |  |  |  |  |  |  |  | 
| 50 | 0 |  |  |  |  |  | $self->set_dir_1_to_spec("cze", "eng", "epo", "fre", "ger", "ita", "lat", | 
| 51 |  |  |  |  |  |  | "pol", "rus", "spa"); | 
| 52 | 0 |  |  |  |  |  | $self->set_dir_spec_to_1("cze", "eng", "epo", "fre", "ger", "ita", "lat", | 
| 53 |  |  |  |  |  |  | "pol", "rus", "spa"); | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | # it also supports: | 
| 56 |  |  |  |  |  |  | # Slovak <-> Esperanto | 
| 57 | 0 |  |  |  |  |  | $self->set_dir_1_to_spec("slo", "epo"); | 
| 58 | 0 |  |  |  |  |  | $self->set_dir_spec_to_1("slo", "epo"); | 
| 59 |  |  |  |  |  |  |  | 
| 60 | 0 |  |  |  |  |  | return $self; | 
| 61 |  |  |  |  |  |  | } | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | =head1 METHODS | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | Methods are inherited from C. Following methods are overriden: | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | =cut | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | =over 4 | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | =item $plugin->create_request($expression, $src_lang_code, $dest_lang_code) | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | Create and return a C object to be used for retrieving | 
| 74 |  |  |  |  |  |  | translation of the C<$expression> from C<$src_lang_code> language to | 
| 75 |  |  |  |  |  |  | C<$dest_lang_code> language. | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | =cut | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | sub create_request | 
| 80 |  |  |  |  |  |  | { | 
| 81 | 0 |  |  | 0 | 1 |  | my $self           = shift; | 
| 82 | 0 |  |  |  |  |  | my $expression     = shift; | 
| 83 | 0 |  |  |  |  |  | my $src_lang_code  = shift; | 
| 84 | 0 |  |  |  |  |  | my $dest_lang_code = shift; | 
| 85 |  |  |  |  |  |  |  | 
| 86 | 0 |  |  |  |  |  | my %table = ( | 
| 87 |  |  |  |  |  |  | cze => "cz", | 
| 88 |  |  |  |  |  |  | eng => "en", | 
| 89 |  |  |  |  |  |  | epo => "eo", | 
| 90 |  |  |  |  |  |  | fre => "fr", | 
| 91 |  |  |  |  |  |  | ger => "ge", | 
| 92 |  |  |  |  |  |  | ita => "it", | 
| 93 |  |  |  |  |  |  | lat => "la", | 
| 94 |  |  |  |  |  |  | pol => "pl", | 
| 95 |  |  |  |  |  |  | rus => "ru", | 
| 96 |  |  |  |  |  |  | slo => "sk", | 
| 97 |  |  |  |  |  |  | spa => "sp", | 
| 98 |  |  |  |  |  |  | ); | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  |  | 
| 101 | 0 |  |  |  |  |  | my $dict; | 
| 102 | 0 | 0 | 0 |  |  |  | if ($src_lang_code eq 'cze' || $dest_lang_code eq 'cze') { | 
|  |  | 0 | 0 |  |  |  |  | 
| 103 | 0 |  |  |  |  |  | $dict = $table{'cze'}; | 
| 104 |  |  |  |  |  |  | } elsif ($src_lang_code eq 'slo' || $dest_lang_code eq 'slo') { | 
| 105 | 0 |  |  |  |  |  | $dict = $table{'slo'}; | 
| 106 |  |  |  |  |  |  | } | 
| 107 | 0 | 0 | 0 |  |  |  | my $dir = ($src_lang_code eq 'cze' || $src_lang_code eq 'slo') ? | 
| 108 |  |  |  |  |  |  | $table{$dest_lang_code} . $dict . "." . $dict : | 
| 109 |  |  |  |  |  |  | $table{$src_lang_code}  . $dict . "." . $table{$src_lang_code}; | 
| 110 |  |  |  |  |  |  |  | 
| 111 | 0 |  |  |  |  |  | my $query = "http://www.slovnik.cz/bin/mld.fpl" . | 
| 112 |  |  |  |  |  |  | "?lines=50&hptxt=0&use_cookies=0&js=0" . | 
| 113 |  |  |  |  |  |  | "&vcb=" . uri_escape($expression) . | 
| 114 |  |  |  |  |  |  | "&dictdir=$dir"; | 
| 115 | 0 |  |  |  |  |  | my $request = new HTTP::Request("GET", $query); | 
| 116 |  |  |  |  |  |  |  | 
| 117 | 0 |  |  |  |  |  | return $request; | 
| 118 |  |  |  |  |  |  | } | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | =item $plugin->process_response($contents, $src_lang_code, $dest_lang_code) | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | Process the server response contents. Return the result of the translation in | 
| 123 |  |  |  |  |  |  | an array of following form: | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | (expression_1, translation_1, expression_2, translation_2, ...) | 
| 126 |  |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  | =back | 
| 128 |  |  |  |  |  |  |  | 
| 129 |  |  |  |  |  |  | =cut | 
| 130 |  |  |  |  |  |  |  | 
| 131 |  |  |  |  |  |  | sub process_response | 
| 132 |  |  |  |  |  |  | { | 
| 133 | 0 |  |  | 0 | 1 |  | my $self           = shift; | 
| 134 | 0 |  |  |  |  |  | my $contents       = shift; | 
| 135 | 0 |  |  |  |  |  | my $src_lang_code  = shift; | 
| 136 | 0 |  |  |  |  |  | my $dest_lang_code = shift; | 
| 137 |  |  |  |  |  |  |  | 
| 138 | 0 |  |  |  |  |  | my @result; | 
| 139 | 0 |  |  |  |  |  | while ($contents =~ m| | 
| 140 |  |  |  |  |  |  |   | 
| 141 |  |  |  |  |  |  | \s* | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | (.*?) | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | \s*-\s* | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | (.*?) | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  | \s* | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | |gsix) | 
| 152 |  |  |  |  |  |  | { | 
| 153 | 0 |  |  |  |  |  | my $expr = _remove_html($1); | 
| 154 | 0 |  |  |  |  |  | my $trans = _remove_html($2);; | 
| 155 |  |  |  |  |  |  |  | 
| 156 | 0 |  |  |  |  |  | push @result, ( | 
| 157 |  |  |  |  |  |  | &_postprocess_expr($expr, $src_lang_code), | 
| 158 |  |  |  |  |  |  | &_postprocess_expr($trans, $dest_lang_code), | 
| 159 |  |  |  |  |  |  | ); | 
| 160 |  |  |  |  |  |  | } | 
| 161 |  |  |  |  |  |  |  | 
| 162 | 0 |  |  |  |  |  | return @result; | 
| 163 |  |  |  |  |  |  | } | 
| 164 |  |  |  |  |  |  |  | 
| 165 |  |  |  |  |  |  | sub _postprocess_expr | 
| 166 |  |  |  |  |  |  | { | 
| 167 | 0 |  |  | 0 |  |  | my $expr     = shift; | 
| 168 | 0 |  |  |  |  |  | my $lang     = shift; | 
| 169 |  |  |  |  |  |  |  | 
| 170 |  |  |  |  |  |  | # convert $expr to perl's internal UTF-8 format | 
| 171 |  |  |  |  |  |  | # (to ensure correct regexes functionality) | 
| 172 | 0 |  |  |  |  |  | my $expr_dec = decode_utf8($expr); | 
| 173 |  |  |  |  |  |  |  | 
| 174 |  |  |  |  |  |  | # strip blanks | 
| 175 | 0 |  |  |  |  |  | $expr_dec =~ s/\s+/ /g; | 
| 176 | 0 |  |  |  |  |  | $expr_dec =~ s/^ //; | 
| 177 |  |  |  |  |  |  |  | 
| 178 |  |  |  |  |  |  | # insert missing blanks after . or , or ; | 
| 179 | 0 |  |  |  |  |  | $expr_dec =~ s/(\w)([.,;])(\w)/$1$2 $3/g; | 
| 180 |  |  |  |  |  |  |  | 
| 181 |  |  |  |  |  |  | # normalize german article: Hund (der) -> Hund; r | 
| 182 | 0 | 0 |  |  |  |  | if ($lang eq 'ger') | 
| 183 |  |  |  |  |  |  | { | 
| 184 | 0 | 0 |  |  |  |  | $expr_dec = $1 . "; " . substr($2, 2, 1) | 
| 185 |  |  |  |  |  |  | if $expr_dec =~ /^(.*) \((der|die|das)\)$/; | 
| 186 |  |  |  |  |  |  | } | 
| 187 |  |  |  |  |  |  |  | 
| 188 |  |  |  |  |  |  | # convert back from internal format | 
| 189 | 0 |  |  |  |  |  | return encode_utf8($expr_dec); | 
| 190 |  |  |  |  |  |  | } | 
| 191 |  |  |  |  |  |  |  | 
| 192 |  |  |  |  |  |  | sub _remove_html | 
| 193 |  |  |  |  |  |  | { | 
| 194 | 0 |  |  | 0 |  |  | my $string = shift; | 
| 195 | 0 |  |  |  |  |  | $string =~ s/g<\/i>//g; | 
| 196 | 0 |  |  |  |  |  | $string =~ s/]+">//g; | 
| 197 | 0 |  |  |  |  |  | $string =~ s/<\/a>//g; | 
| 198 | 0 |  |  |  |  |  | return $string; | 
| 199 |  |  |  |  |  |  | } | 
| 200 |  |  |  |  |  |  |  | 
| 201 |  |  |  |  |  |  | 1; | 
| 202 |  |  |  |  |  |  |  | 
| 203 |  |  |  |  |  |  | __END__ |