| 1 | =head1 NAME | ||
| 2 | |||
| 3 | MetaTrans::WordbookCz - MetaTrans plug-in for L<http://www.wordbook.cz/> | ||
| 4 | |||
| 5 | =cut | ||
| 6 | |||
| 7 | package MetaTrans::WordbookCz; | ||
| 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 | |||
| 16 | $VERSION = do { my @r = (q$Revision: 1.3 $ =~ /\d+/g); sprintf "%d."."%02d", @r }; | ||
| 17 | @ISA = qw(MetaTrans::Base); | ||
| 18 | |||
| 19 | =head1 CONSTRUCTOR METHODS | ||
| 20 | |||
| 21 | =over 4 | ||
| 22 | |||
| 23 | =item MetaTrans::WordbookCz->new(%options) | ||
| 24 | |||
| 25 | This method constructs a new MetaTrans::WordbookCz object and returns it. All | ||
| 26 | C<%options> are passed to C<< MetaTrans::Base->new >>. The method also sets | ||
| 27 | supported translation directions and the C<host_server> attribute. | ||
| 28 | |||
| 29 | =back | ||
| 30 | |||
| 31 | =cut | ||
| 32 | |||
| 33 | sub new | ||
| 34 | { | ||
| 35 | my $class = shift; | ||
| 36 | my %options = @_; | ||
| 37 | |||
| 38 | $options{host_server} = "www.wordbook.cz" | ||
| 39 | unless (defined $options{host_server}); | ||
| 40 | |||
| 41 | my $self = new MetaTrans::Base(%options); | ||
| 42 | $self = bless $self, $class; | ||
| 43 | |||
| 44 | $self->set_languages("cze", "eng", "fre", "ger", "spa"); | ||
| 45 | |||
| 46 | $self->set_dir_1_to_all("cze"); | ||
| 47 | $self->set_dir_all_to_1("cze"); | ||
| 48 | |||
| 49 | return $self; | ||
| 50 | } | ||
| 51 | |||
| 52 | =head1 METHODS | ||
| 53 | |||
| 54 | Methods are inherited from C<MetaTrans::Base>. Following methods are overriden: | ||
| 55 | |||
| 56 | =cut | ||
| 57 | |||
| 58 | =over 4 | ||
| 59 | |||
| 60 | =item $plugin->create_request($expression, $src_lang_code, $dest_lang_code) | ||
| 61 | |||
| 62 | Create and return a C<HTTP::Request> object to be used for retrieving | ||
| 63 | translation of the C<$expression> from C<$src_lang_code> language to | ||
| 64 | C<$dest_lang_code> language. | ||
| 65 | |||
| 66 | =cut | ||
| 67 | |||
| 68 | sub create_request | ||
| 69 | { | ||
| 70 | my $self = shift; | ||
| 71 | my $expression = shift; | ||
| 72 | my $src_lang_code = shift; | ||
| 73 | my $dest_lang_code = shift; | ||
| 74 | |||
| 75 | my %table = ( | ||
| 76 | eng => "enu", | ||
| 77 | fre => "fra", | ||
| 78 | ger => "ger", | ||
| 79 | spa => "spa", | ||
| 80 | ); | ||
| 81 | |||
| 82 | my $fsmer; | ||
| 83 | my $fslovnik; | ||
| 84 | |||
| 85 | if ($src_lang_code eq 'cze') | ||
| 86 | { | ||
| 87 | $fsmer = 1; | ||
| 88 | $fslovnik = $table{$dest_lang_code}; | ||
| 89 | } | ||
| 90 | elsif ($dest_lang_code eq 'cze') | ||
| 91 | { | ||
| 92 | $fsmer = 0; | ||
| 93 | $fslovnik = $table{$src_lang_code}; | ||
| 94 | } | ||
| 95 | |||
| 96 | my $request = HTTP::Request->new(POST => "http://www.wordbook.cz/index.php"); | ||
| 97 | $request->content_type('application/x-www-form-urlencoded'); | ||
| 98 | my $query = | ||
| 99 | "fextend=1" . | ||
| 100 | "&fslovo=$expression" . | ||
| 101 | "&fsmer=$fsmer" . | ||
| 102 | "&fslovnik=$fslovnik"; | ||
| 103 | $request->content($query); | ||
| 104 | |||
| 105 | return $request; | ||
| 106 | } | ||
| 107 | |||
| 108 | =item $plugin->process_response($contents, $src_lang_code, $dest_lang_code) | ||
| 109 | |||
| 110 | Process the server response contents. Return the result of the translation in | ||
| 111 | an array of following form: | ||
| 112 | |||
| 113 | (expression_1, translation_1, expression_2, translation_2, ...) | ||
| 114 | |||
| 115 | =back | ||
| 116 | |||
| 117 | =cut | ||
| 118 | |||
| 119 | sub process_response | ||
| 120 | { | ||
| 121 | my $self = shift; | ||
| 122 | my $contents = shift; | ||
| 123 | my $src_lang_code = shift; | ||
| 124 | my $dest_lang_code = shift; | ||
| 125 | |||
| 126 | my @result; | ||
| 127 | while ($contents =~ m| | ||
| 128 | <tr\s+[^>]*?> | ||
| 129 | (<td\s+class="vyslradek"\s*>.*?) | ||
| 130 | </tr> | ||
| 131 | |gsix) | ||
| 132 | { | ||
| 133 | my $row = $1; | ||
| 134 | $row =~ s/ //g; | ||
| 135 | my @data; | ||
| 136 | while ($row =~ m/<td\s+class="vyslradek">(.*?)<\/td>/gixm) | ||
| 137 | { | ||
| 138 | push @data, $1; | ||
| 139 | } | ||
| 140 | my ($expr, $trans, $note); | ||
| 141 | |||
| 142 | # exact words. | ||
| 143 | if (@data == 5) { | ||
| 144 | (undef, $expr, undef, $trans, $note) = @data; | ||
| 145 | |||
| 146 | # similar words. | ||
| 147 | } elsif (@data == 3) { | ||
| 148 | ($expr, $trans, $note) = @data; | ||
| 149 | } | ||
| 150 | |||
| 151 | # skip blank values. | ||
| 152 | next if $expr =~ /^\s*$/ || $trans =~ /^\s*$/; | ||
| 153 | |||
| 154 | # normalize german. | ||
| 155 | $expr = _normalize_german($expr, $note) | ||
| 156 | if $src_lang_code eq 'ger'; | ||
| 157 | $trans = _normalize_german($trans, $note) | ||
| 158 | if $dest_lang_code eq 'ger'; | ||
| 159 | |||
| 160 | # new result. | ||
| 161 | push @result, ($expr, $trans); | ||
| 162 | } | ||
| 163 | |||
| 164 | return @result; | ||
| 165 | } | ||
| 166 | |||
| 167 | sub _normalize_german | ||
| 168 | { | ||
| 169 | my $expr = shift; | ||
| 170 | my $note = shift; | ||
| 171 | |||
| 172 | if ($note && ($note eq 'die' || $note eq 'das' || $note eq 'der')) | ||
| 173 | { | ||
| 174 | $expr .= '; ' . substr($note, 2, 1); | ||
| 175 | } | ||
| 176 | |||
| 177 | return $expr; | ||
| 178 | } | ||
| 179 | |||
| 180 | 1; | ||
| 181 | |||
| 182 | __END__ | ||