MetaTrans/UltralinguaNet.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« MetaTrans/SmsCz.pm ↑ Summary MetaTrans/WordbookCz.pm »
1 =head1 NAME
2  
3 MetaTrans::UltralinguaNet - MetaTrans plug-in for L<http://ultralingua.net/>
4  
5 =cut
6  
7 package MetaTrans::UltralinguaNet;
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 Encode;
15 use HTTP::Request;
16 use URI::Escape;
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<host_server> attribute.
30  
31 =back
32  
33 =cut
34  
35 sub new
36 {
37 my $class = shift;
38 my %options = @_;
39  
40 # set the host_server option ...
41 $options{host_server} = "ultralingua.net"
42 unless (defined $options{host_server});
43  
44 # ... and pass all the options to MetaTrans::Base constructor
45 my $self = new MetaTrans::Base(%options);
46 $self = bless $self, $class;
47  
48 # set supported languages
49 $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 $self->set_dir_1_to_all("eng");
54  
55 # ... and reversely
56 $self->set_dir_all_to_1("eng");
57  
58 # it also supports:
59 # French <-> Spanish
60 # French <-> German
61 # French <-> Italian
62 $self->set_dir_1_to_spec("fre", "spa", "ger", "ita");
63 $self->set_dir_spec_to_1("fre", "spa", "ger", "ita");
64  
65 # ...
66 # Spanish <-> German
67 # Spanish <-> Portuguese
68 $self->set_dir_1_to_spec("spa", "ger", "por");
69 $self->set_dir_spec_to_1("spa", "ger", "por");
70  
71 return $self;
72 }
73  
74 =head1 METHODS
75  
76 Methods are inherited from C<MetaTrans::Base>. 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<HTTP::Request> 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 my $self = shift;
93 my $expression = shift;
94 my $src_lang_code = shift;
95 my $dest_lang_code = shift;
96  
97 # language codes translation table
98 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 $expression = uri_escape($expression);
112  
113 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 my $request = new HTTP::Request("GET", $query);
122  
123 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 my $self = shift;
140 my $contents = shift;
141 my $src_lang_code = shift;
142 my $dest_lang_code = shift;
143  
144 my @result;
145 while ($contents =~ m|
146 <div\s+class="definitionResult">
147 \s*
148 <div\s+class="term"[^>]*>
149 \s*
150 <span\s+class="ultext">
151 (.*?)
152 </span>
153 .*?
154 </div>
155 \s*
156 <ol\s+class="definitions">
157 (.*?)
158 </ol>
159 \s*
160 </div>
161 |gsix)
162 {
163  
164 my $expr = _get_expr($1);
165 my @trans = _get_trans($2);
166  
167 foreach my $trans (@trans) {
168 push @result, ($expr, $trans);
169 }
170 }
171  
172 return @result;
173 }
174  
175 sub _get_expr
176 {
177 my $string = shift;
178 $string =~ s/<a[^>]*>//g;
179 $string =~ s/<\/a>//g;
180 return $string;
181 }
182  
183 sub _get_trans {
184 my $string = shift;
185 my @result;
186 while ($string =~ m|
187 <span\s+class="ultext">
188 (.*?)
189 </span>
190 |gsix)
191 {
192 push @result, _get_expr($1);
193 }
194 return @result;
195 }
196  
197 1;
198  
199 __END__
« MetaTrans/SmsCz.pm ↑ Summary MetaTrans/WordbookCz.pm »