| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package WWW::Translate::interNOSTRUM; |
|
2
|
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
43625
|
use strict; |
|
|
2
|
|
|
|
|
5
|
|
|
|
2
|
|
|
|
|
97
|
|
|
4
|
2
|
|
|
2
|
|
11
|
use warnings; |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
77
|
|
|
5
|
2
|
|
|
2
|
|
13
|
use Carp qw(carp); |
|
|
2
|
|
|
|
|
8
|
|
|
|
2
|
|
|
|
|
173
|
|
|
6
|
2
|
|
|
2
|
|
3167
|
use WWW::Mechanize; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
use Encode; |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '0.13'; |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
my %lang_pairs = ( |
|
14
|
|
|
|
|
|
|
'ca-es' => 'Catalan -> Spanish', # default |
|
15
|
|
|
|
|
|
|
'es-ca' => 'Spanish -> Catalan', |
|
16
|
|
|
|
|
|
|
'es-va' => 'Spanish -> Catalan with Valencian forms', |
|
17
|
|
|
|
|
|
|
); |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
my %output = ( |
|
20
|
|
|
|
|
|
|
plain_text => 'txtf', # default |
|
21
|
|
|
|
|
|
|
marked_text => 'txt', |
|
22
|
|
|
|
|
|
|
); |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
my %defaults = ( |
|
25
|
|
|
|
|
|
|
lang_pair => 'ca-es', |
|
26
|
|
|
|
|
|
|
output => 'plain_text', |
|
27
|
|
|
|
|
|
|
store_unknown => 0, |
|
28
|
|
|
|
|
|
|
); |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub new { |
|
32
|
|
|
|
|
|
|
my $class = shift; |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
# validate overrides |
|
35
|
|
|
|
|
|
|
my %overrides = @_; |
|
36
|
|
|
|
|
|
|
foreach (keys %overrides) { |
|
37
|
|
|
|
|
|
|
# check key; warn if illegal |
|
38
|
|
|
|
|
|
|
carp "Unknown parameter: $_\n" unless exists $defaults{$_}; |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# check value; warn and delete if illegal |
|
41
|
|
|
|
|
|
|
if ($_ eq 'output' && !exists $output{$overrides{output}}) { |
|
42
|
|
|
|
|
|
|
carp _message('output', $overrides{output}); |
|
43
|
|
|
|
|
|
|
delete $overrides{output}; |
|
44
|
|
|
|
|
|
|
} |
|
45
|
|
|
|
|
|
|
if ($_ eq 'lang_pair' && !exists $lang_pairs{$overrides{lang_pair}}) { |
|
46
|
|
|
|
|
|
|
carp _message('lang_pair', $overrides{lang_pair}); |
|
47
|
|
|
|
|
|
|
delete $overrides{lang_pair}; |
|
48
|
|
|
|
|
|
|
} |
|
49
|
|
|
|
|
|
|
} |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# replace defaults with overrides |
|
52
|
|
|
|
|
|
|
my %args = (%defaults, %overrides); |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# remove invalid parameters |
|
55
|
|
|
|
|
|
|
my @fields = keys %defaults; |
|
56
|
|
|
|
|
|
|
my %this; |
|
57
|
|
|
|
|
|
|
@this{@fields} = @args{@fields}; |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
if ($this{store_unknown}) { |
|
60
|
|
|
|
|
|
|
$this{unknown} = (); |
|
61
|
|
|
|
|
|
|
} |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
$this{agent} = WWW::Mechanize->new(); |
|
64
|
|
|
|
|
|
|
$this{agent}->env_proxy(); |
|
65
|
|
|
|
|
|
|
$this{url} = 'http://www.internostrum.com/welcome.php'; |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
return bless(\%this, $class); |
|
68
|
|
|
|
|
|
|
} |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub translate { |
|
72
|
|
|
|
|
|
|
my $self = shift; |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
my $string; |
|
75
|
|
|
|
|
|
|
if (@_ > 0) { |
|
76
|
|
|
|
|
|
|
$string = shift; |
|
77
|
|
|
|
|
|
|
} else { |
|
78
|
|
|
|
|
|
|
carp "Nothing to translate\n"; |
|
79
|
|
|
|
|
|
|
return ''; |
|
80
|
|
|
|
|
|
|
} |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
return '' if ($string eq ''); |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
$string = _fix_source($string); |
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
my $mech = $self->{agent}; |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
$mech->get($self->{url}); |
|
89
|
|
|
|
|
|
|
unless ($mech->success) { |
|
90
|
|
|
|
|
|
|
carp $mech->response->status_line; |
|
91
|
|
|
|
|
|
|
return undef; |
|
92
|
|
|
|
|
|
|
} |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
$mech->field("quadretext", $string); |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
if ($self->{lang_pair} eq 'es-va') { |
|
97
|
|
|
|
|
|
|
$self->{lang_pair} = 'es-ca'; |
|
98
|
|
|
|
|
|
|
$mech->tick('valen', 1); |
|
99
|
|
|
|
|
|
|
} |
|
100
|
|
|
|
|
|
|
$mech->select("direccio", $self->{lang_pair}); |
|
101
|
|
|
|
|
|
|
$mech->select("tipus", $output{$self->{output}}); |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
$mech->click(); |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
my $response = $mech->content(); |
|
106
|
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
my $translated; |
|
108
|
|
|
|
|
|
|
if ($response =~ |
|
109
|
|
|
|
|
|
|
/spelling\.<\/div>\s* (.+?)<\/p>/s) { |
|
110
|
|
|
|
|
|
|
$translated = $1; |
|
111
|
|
|
|
|
|
|
} else { |
|
112
|
|
|
|
|
|
|
carp "Didn't receive a translation from the interNostrum server.\n" . |
|
113
|
|
|
|
|
|
|
"Please check the length of the source text.\n"; |
|
114
|
|
|
|
|
|
|
return ''; |
|
115
|
|
|
|
|
|
|
} |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
# remove double spaces |
|
118
|
|
|
|
|
|
|
$translated =~ s/(?<=\S)\s{2}(?=\S)/ /g; |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
# store unknown words |
|
121
|
|
|
|
|
|
|
if ($self->{store_unknown} && $self->{output} eq 'marked_text') { |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
if ($translated =~ /(?:^|\W)\*/) { |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
my $source_lang = substr($self->{lang_pair}, 0, 2); |
|
126
|
|
|
|
|
|
|
my $utf8 = decode('iso-8859-1', $translated); |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
while ($utf8 =~ /(?:^|\W)\*(\w+?)\b/g) { |
|
129
|
|
|
|
|
|
|
my $detected = encode('iso-8859-1', $1); |
|
130
|
|
|
|
|
|
|
$self->{unknown}->{$source_lang}->{$detected}++; |
|
131
|
|
|
|
|
|
|
} |
|
132
|
|
|
|
|
|
|
} |
|
133
|
|
|
|
|
|
|
} |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
return $translated; |
|
136
|
|
|
|
|
|
|
} |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
sub from_into { |
|
139
|
|
|
|
|
|
|
my $self = shift; |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
if (@_) { |
|
142
|
|
|
|
|
|
|
my $pair = shift; |
|
143
|
|
|
|
|
|
|
if (!exists $lang_pairs{$pair}) { |
|
144
|
|
|
|
|
|
|
carp _message('lang_pair', $pair); |
|
145
|
|
|
|
|
|
|
$self->{lang_pair} = $defaults{'lang_pair'}; |
|
146
|
|
|
|
|
|
|
} else { |
|
147
|
|
|
|
|
|
|
$self->{lang_pair} = $pair; |
|
148
|
|
|
|
|
|
|
} |
|
149
|
|
|
|
|
|
|
} else { |
|
150
|
|
|
|
|
|
|
return $self->{lang_pair}; |
|
151
|
|
|
|
|
|
|
} |
|
152
|
|
|
|
|
|
|
} |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
sub output_format { |
|
155
|
|
|
|
|
|
|
my $self = shift; |
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
if (@_) { |
|
158
|
|
|
|
|
|
|
my $format = shift; |
|
159
|
|
|
|
|
|
|
$self->{output} = $format if exists $output{$format}; |
|
160
|
|
|
|
|
|
|
} else { |
|
161
|
|
|
|
|
|
|
return $self->{output}; |
|
162
|
|
|
|
|
|
|
} |
|
163
|
|
|
|
|
|
|
} |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
sub get_unknown { |
|
166
|
|
|
|
|
|
|
my $self = shift; |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
if (@_ && $self->{store_unknown}) { |
|
169
|
|
|
|
|
|
|
my $lang_code = shift; |
|
170
|
|
|
|
|
|
|
if ($lang_code =~ /^(?:es|ca)$/) { |
|
171
|
|
|
|
|
|
|
return $self->{unknown}->{$lang_code}; |
|
172
|
|
|
|
|
|
|
} else { |
|
173
|
|
|
|
|
|
|
carp "Invalid language code\n"; |
|
174
|
|
|
|
|
|
|
} |
|
175
|
|
|
|
|
|
|
} else { |
|
176
|
|
|
|
|
|
|
carp "I'm not configured to store unknown words\n"; |
|
177
|
|
|
|
|
|
|
} |
|
178
|
|
|
|
|
|
|
} |
|
179
|
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
sub _message { |
|
181
|
|
|
|
|
|
|
my ($key, $value) = @_; |
|
182
|
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
my $string = "Invalid value for parameter $key, $value.\n" . |
|
184
|
|
|
|
|
|
|
"Will use the default value instead.\n"; |
|
185
|
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
return $string; |
|
187
|
|
|
|
|
|
|
} |
|
188
|
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
sub _fix_source { |
|
190
|
|
|
|
|
|
|
my ($string) = @_; |
|
191
|
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
# fix geminated l; replace . by chr(183) = hex B7 |
|
193
|
|
|
|
|
|
|
$string =~ s/l\.l/l\xB7l/g; |
|
194
|
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
return $string; |
|
196
|
|
|
|
|
|
|
} |
|
197
|
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
1; |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
__END__ |