| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
=head1 NAME |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
MetaTrans::UltralinguaNet - MetaTrans plug-in for L |
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=cut |
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
package MetaTrans::UltralinguaNet; |
|
8
|
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
1065
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
33
|
|
|
10
|
1
|
|
|
1
|
|
47
|
use warnings; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
30
|
|
|
11
|
1
|
|
|
1
|
|
7
|
use vars qw($VERSION @ISA); |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
58
|
|
|
12
|
1
|
|
|
1
|
|
6
|
use MetaTrans::Base; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
70
|
|
|
13
|
|
|
|
|
|
|
|
|
14
|
1
|
|
|
1
|
|
5
|
use Encode; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
82
|
|
|
15
|
1
|
|
|
1
|
|
6
|
use HTTP::Request; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
32
|
|
|
16
|
1
|
|
|
1
|
|
5
|
use URI::Escape; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
778
|
|
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
$VERSION = do { my @r = (q$Revision: 1.3 $ =~ /\d+/g); sprintf "%d."."%02d", @r }; |
|
19
|
|
|
|
|
|
|
@ISA = qw(MetaTrans::Base); # we derrive from MetaTrans::Base |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=head1 CONSTRUCTOR METHODS |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=over 4 |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=item MetaTrans::UltralinguaNet->new(%options) |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
This method constructs a new MetaTrans::UltralinguaNet object and returns |
|
28
|
|
|
|
|
|
|
it. All C<%options> are passed to C<< MetaTrans::Base->new >>. The method |
|
29
|
|
|
|
|
|
|
also sets 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
|
|
|
|
|
|
|
# set the host_server option ... |
|
41
|
0
|
0
|
|
|
|
|
$options{host_server} = "ultralingua.net" |
|
42
|
|
|
|
|
|
|
unless (defined $options{host_server}); |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# ... and pass all the options to MetaTrans::Base constructor |
|
45
|
0
|
|
|
|
|
|
my $self = new MetaTrans::Base(%options); |
|
46
|
0
|
|
|
|
|
|
$self = bless $self, $class; |
|
47
|
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# set supported languages |
|
49
|
0
|
|
|
|
|
|
$self->set_languages("eng", "fre", "spa", "ger", "ita", "por", "lat", "epo"); |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# ulralingua.net enables translating from English to any of supported |
|
52
|
|
|
|
|
|
|
# languages... |
|
53
|
0
|
|
|
|
|
|
$self->set_dir_1_to_all("eng"); |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
# ... and reversely |
|
56
|
0
|
|
|
|
|
|
$self->set_dir_all_to_1("eng"); |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# it also supports: |
|
59
|
|
|
|
|
|
|
# French <-> Spanish |
|
60
|
|
|
|
|
|
|
# French <-> German |
|
61
|
|
|
|
|
|
|
# French <-> Italian |
|
62
|
0
|
|
|
|
|
|
$self->set_dir_1_to_spec("fre", "spa", "ger", "ita"); |
|
63
|
0
|
|
|
|
|
|
$self->set_dir_spec_to_1("fre", "spa", "ger", "ita"); |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# ... |
|
66
|
|
|
|
|
|
|
# Spanish <-> German |
|
67
|
|
|
|
|
|
|
# Spanish <-> Portuguese |
|
68
|
0
|
|
|
|
|
|
$self->set_dir_1_to_spec("spa", "ger", "por"); |
|
69
|
0
|
|
|
|
|
|
$self->set_dir_spec_to_1("spa", "ger", "por"); |
|
70
|
|
|
|
|
|
|
|
|
71
|
0
|
|
|
|
|
|
return $self; |
|
72
|
|
|
|
|
|
|
} |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head1 METHODS |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
Methods are inherited from C. Following methods are overriden: |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=cut |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=over 4 |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=item $plugin->create_request($expression, $src_lang_code, $dest_lang_code) |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
Create and return a C object to be used for retrieving |
|
85
|
|
|
|
|
|
|
translation of the C<$expression> from C<$src_lang_code> language to |
|
86
|
|
|
|
|
|
|
C<$dest_lang_code> language. |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=cut |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub create_request |
|
91
|
|
|
|
|
|
|
{ |
|
92
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
93
|
0
|
|
|
|
|
|
my $expression = shift; |
|
94
|
0
|
|
|
|
|
|
my $src_lang_code = shift; |
|
95
|
0
|
|
|
|
|
|
my $dest_lang_code = shift; |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# language codes translation table |
|
98
|
0
|
|
|
|
|
|
my %table = ( |
|
99
|
|
|
|
|
|
|
eng => "english", |
|
100
|
|
|
|
|
|
|
fre => "french", |
|
101
|
|
|
|
|
|
|
spa => "spanish", |
|
102
|
|
|
|
|
|
|
ger => "german", |
|
103
|
|
|
|
|
|
|
ita => "italian", |
|
104
|
|
|
|
|
|
|
por => "portuguese", |
|
105
|
|
|
|
|
|
|
lat => "latin", |
|
106
|
|
|
|
|
|
|
epo => "esperanto", |
|
107
|
|
|
|
|
|
|
); |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
# we may need to escape some characters to be able to pass the expression |
|
110
|
|
|
|
|
|
|
# as a part of URL without causing any trouble |
|
111
|
0
|
|
|
|
|
|
$expression = uri_escape($expression); |
|
112
|
|
|
|
|
|
|
|
|
113
|
0
|
|
|
|
|
|
my $query = "http://ultralingua.com/onlinedictionary/ulod.py" . # script name |
|
114
|
|
|
|
|
|
|
"?action=define&clang=english" . # `static' options |
|
115
|
|
|
|
|
|
|
"&searchtype=stemming&nlang=english" . # `static' options |
|
116
|
|
|
|
|
|
|
"&text=" . uri_escape($expression) . # expr. to be translated |
|
117
|
|
|
|
|
|
|
"&srclang=" . $table{$src_lang_code} . # translation from |
|
118
|
|
|
|
|
|
|
"&dstlang=" . $table{$dest_lang_code}; # translation to |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
# construct the HTTP::Request object |
|
121
|
0
|
|
|
|
|
|
my $request = new HTTP::Request("GET", $query); |
|
122
|
|
|
|
|
|
|
|
|
123
|
0
|
|
|
|
|
|
return $request; |
|
124
|
|
|
|
|
|
|
} |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=item $plugin->process_response($contents, $src_lang_code, $dest_lang_code) |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
Process the server response contents. Return the result of the translation in |
|
129
|
|
|
|
|
|
|
an array of following form: |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
(expression_1, translation_1, expression_2, translation_2, ...) |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=back |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=cut |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
sub process_response |
|
138
|
|
|
|
|
|
|
{ |
|
139
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
140
|
0
|
|
|
|
|
|
my $contents = shift; |
|
141
|
0
|
|
|
|
|
|
my $src_lang_code = shift; |
|
142
|
0
|
|
|
|
|
|
my $dest_lang_code = shift; |
|
143
|
|
|
|
|
|
|
|
|
144
|
0
|
|
|
|
|
|
my @result; |
|
145
|
0
|
|
|
|
|
|
while ($contents =~ m| |
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
\s* |
|
148
|
|
|
|
|
|
|
]*> |
|
149
|
|
|
|
|
|
|
\s* |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
(.*?) |
|
152
|
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
.*? |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
\s* |
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
(.*?) |
|
158
|
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
\s* |
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
|gsix) |
|
162
|
|
|
|
|
|
|
{ |
|
163
|
|
|
|
|
|
|
|
|
164
|
0
|
|
|
|
|
|
my $expr = _get_expr($1); |
|
165
|
0
|
|
|
|
|
|
my @trans = _get_trans($2); |
|
166
|
|
|
|
|
|
|
|
|
167
|
0
|
|
|
|
|
|
foreach my $trans (@trans) { |
|
168
|
0
|
|
|
|
|
|
push @result, ($expr, $trans); |
|
169
|
|
|
|
|
|
|
} |
|
170
|
|
|
|
|
|
|
} |
|
171
|
|
|
|
|
|
|
|
|
172
|
0
|
|
|
|
|
|
return @result; |
|
173
|
|
|
|
|
|
|
} |
|
174
|
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
sub _get_expr |
|
176
|
|
|
|
|
|
|
{ |
|
177
|
0
|
|
|
0
|
|
|
my $string = shift; |
|
178
|
0
|
|
|
|
|
|
$string =~ s/]*>//g; |
|
179
|
0
|
|
|
|
|
|
$string =~ s/<\/a>//g; |
|
180
|
0
|
|
|
|
|
|
return $string; |
|
181
|
|
|
|
|
|
|
} |
|
182
|
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
sub _get_trans { |
|
184
|
0
|
|
|
0
|
|
|
my $string = shift; |
|
185
|
0
|
|
|
|
|
|
my @result; |
|
186
|
0
|
|
|
|
|
|
while ($string =~ m| |
|
187
|
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
(.*?) |
|
189
|
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
|gsix) |
|
191
|
|
|
|
|
|
|
{ |
|
192
|
0
|
|
|
|
|
|
push @result, _get_expr($1); |
|
193
|
|
|
|
|
|
|
} |
|
194
|
0
|
|
|
|
|
|
return @result; |
|
195
|
|
|
|
|
|
|
} |
|
196
|
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
1; |
|
198
|
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
__END__ |