line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Lingua::Translate::InterTran; |
2
|
|
|
|
|
|
|
BEGIN { |
3
|
1
|
|
|
1
|
|
967
|
$Lingua::Translate::InterTran::AUTHORITY = 'cpan:HINRIK'; |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
BEGIN { |
6
|
1
|
|
|
1
|
|
19
|
$Lingua::Translate::InterTran::VERSION = '0.05'; |
7
|
|
|
|
|
|
|
} |
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
8
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
40
|
|
10
|
1
|
|
|
1
|
|
6
|
use warnings FATAL => 'all'; |
|
1
|
|
|
|
|
13
|
|
|
1
|
|
|
|
|
48
|
|
11
|
1
|
|
|
1
|
|
6
|
use Carp qw(croak); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
85
|
|
12
|
1
|
|
|
1
|
|
1219
|
use Encode qw(encode decode); |
|
1
|
|
|
|
|
15443
|
|
|
1
|
|
|
|
|
160
|
|
13
|
1
|
|
|
1
|
|
237036
|
use LWP::UserAgent (); |
|
1
|
|
|
|
|
276818
|
|
|
1
|
|
|
|
|
26
|
|
14
|
1
|
|
|
1
|
|
11
|
use URI (); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
18
|
|
15
|
1
|
|
|
1
|
|
4
|
use base qw(Lingua::Translate); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
1132
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# languages supported by InterTran as of February 2008 |
18
|
|
|
|
|
|
|
my %lang = ( |
19
|
|
|
|
|
|
|
'bg' => ['bul', 'cp1251'], # Bulgarian (CP 1251) |
20
|
|
|
|
|
|
|
'cs' => ['che', 'cp1250'], # Czech (CP 1250) |
21
|
|
|
|
|
|
|
'cy' => ['wel', 'cp1252'], # Welsh |
22
|
|
|
|
|
|
|
'da' => ['dan', 'cp1252'], # Danish |
23
|
|
|
|
|
|
|
'de' => ['ger', 'cp1252'], # German |
24
|
|
|
|
|
|
|
'el' => ['grk', 'cp1252'], # Greek |
25
|
|
|
|
|
|
|
'en' => ['eng', 'cp1252'], # English |
26
|
|
|
|
|
|
|
'es-es' => ['spa', 'cp1252'], # Spanish |
27
|
|
|
|
|
|
|
'es-us' => ['spl', 'cp1252'], # Latin American Spanish |
28
|
|
|
|
|
|
|
'fi' => ['fin', 'cp1252'], # Finnis |
29
|
|
|
|
|
|
|
'fr' => ['fre', 'cp1252'], # Frenchh |
30
|
|
|
|
|
|
|
'hr' => ['cro', 'cp1250'], # Croatian (CP 1250) |
31
|
|
|
|
|
|
|
'hu' => ['hun', 'cp1250'], # Hungarian (CP 1250) |
32
|
|
|
|
|
|
|
'is' => ['ice', 'cp1252'], # Icelandic |
33
|
|
|
|
|
|
|
'it' => ['ita', 'cp1252'], # Italian |
34
|
|
|
|
|
|
|
'ja' => ['jpn', 'shiftjis'], # Japanese (Shift JIS) |
35
|
|
|
|
|
|
|
'la' => ['ltt', 'cp1252'], # Latin |
36
|
|
|
|
|
|
|
'nl' => ['dut', 'cp1252'], # Dutch |
37
|
|
|
|
|
|
|
'no' => ['nor', 'cp1252'], # Norwegian |
38
|
|
|
|
|
|
|
'pl' => ['pol', 'iso-8859-2'], # Polish (ISO 8859-2) |
39
|
|
|
|
|
|
|
'pt-br' => ['pob', 'cp1252'], # Brazilian Portuguese |
40
|
|
|
|
|
|
|
'pt-pt' => ['poe', 'cp1252'], # Portuguese |
41
|
|
|
|
|
|
|
'ro' => ['rom', 'cp1250'], # Romanian (CP 1250) |
42
|
|
|
|
|
|
|
'ru' => ['rus', 'cp1251'], # Russian (CP 1251) |
43
|
|
|
|
|
|
|
'sl' => ['slo', 'cp1250'], # Slovenian (CP 1250) |
44
|
|
|
|
|
|
|
'sr' => ['sel', 'cp1250'], # Serbian (CP 1250) |
45
|
|
|
|
|
|
|
'sv' => ['swe', 'cp1252'], # Swedish |
46
|
|
|
|
|
|
|
'tl' => ['tag', 'cp1252'], # Filipino (Tagalog) |
47
|
|
|
|
|
|
|
'tr' => ['tur', 'cp1254'], # Turkish (CP 1254) |
48
|
|
|
|
|
|
|
); |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub new { |
51
|
0
|
|
|
0
|
0
|
|
my ($package, %args) = @_; |
52
|
0
|
|
|
|
|
|
my $self = bless \%args, $package; |
53
|
|
|
|
|
|
|
|
54
|
0
|
0
|
|
|
|
|
if (!exists $lang{ $self->{src} }) { |
55
|
0
|
|
|
|
|
|
croak "Source language '" . $self->{src} . "' is not supported"; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
0
|
0
|
|
|
|
|
if (!exists $lang{ $self->{dest} }) { |
59
|
0
|
|
|
|
|
|
croak "Destination language '" . $self->{dest} . "' is not supported"; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
0
|
|
|
|
|
|
@{ $self }{'src_lang', 'src_enc'} = @{ $lang{ $self->{src} } }; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
63
|
0
|
|
|
|
|
|
@{ $self }{'dest_lang', 'dest_enc'} = @{ $lang{ $self->{dest} } }; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
64
|
0
|
|
|
|
|
|
$self->{agent} = LWP::UserAgent->new(); |
65
|
0
|
|
|
|
|
|
$self->{uri} = URI->new('http://www.tranexp.com:2000/InterTran'); |
66
|
|
|
|
|
|
|
|
67
|
0
|
|
|
|
|
|
return $self; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub translate { |
71
|
0
|
|
|
0
|
0
|
|
my ($self, $text) = @_; |
72
|
0
|
|
|
|
|
|
$text = encode($self->{src_enc}, $text); |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# Construct GET parameters |
75
|
0
|
|
|
|
|
|
$self->{uri}->query_form( |
76
|
|
|
|
|
|
|
type => 'text', |
77
|
|
|
|
|
|
|
text => $text, |
78
|
|
|
|
|
|
|
from => $self->{src_lang}, |
79
|
|
|
|
|
|
|
to => $self->{dest_lang}, |
80
|
|
|
|
|
|
|
); |
81
|
|
|
|
|
|
|
|
82
|
0
|
|
|
|
|
|
my $response = $self->{agent}->get($self->{uri}->as_string); |
83
|
0
|
0
|
|
|
|
|
croak 'Failed to get response from server' if !$response->is_success; |
84
|
0
|
|
|
|
|
|
my $content = $response->content; |
85
|
0
|
|
|
|
|
|
$content = decode($self->{dest_enc}, $content); |
86
|
|
|
|
|
|
|
|
87
|
0
|
|
|
|
|
|
my $translated; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
# Try to parse it with TreeBuilder |
90
|
0
|
|
|
|
|
|
eval { |
91
|
0
|
|
|
|
|
|
require HTML::TreeBuilder; |
92
|
0
|
|
|
|
|
|
my $tree = HTML::TreeBuilder->new_from_content($content); |
93
|
0
|
|
|
|
|
|
$translated = $tree->look_down( |
94
|
|
|
|
|
|
|
_tag => 'textarea', |
95
|
|
|
|
|
|
|
name => 'translation', |
96
|
|
|
|
|
|
|
)->attr('_content')->[0]; |
97
|
|
|
|
|
|
|
}; |
98
|
0
|
0
|
|
|
|
|
return $translated if $translated; |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
# fall back to parsing it with a regex |
101
|
0
|
|
|
|
|
|
($translated) = $content =~ m[ |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
]xs; |
104
|
|
|
|
|
|
|
|
105
|
0
|
|
|
|
|
|
return $translated; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
1; |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head1 NAME |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
Lingua::Translate::InterTran - A L backend for InterTran. |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head1 SYNOPSIS |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
use Lingua::Translate; |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
my $en2is = Lingua::Translate->new( |
119
|
|
|
|
|
|
|
back_end => 'InterTran', |
120
|
|
|
|
|
|
|
src => 'en', |
121
|
|
|
|
|
|
|
dest => 'is', |
122
|
|
|
|
|
|
|
); |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
my $is2en = Lingua::Translate->new( |
125
|
|
|
|
|
|
|
back_end => 'InterTran', |
126
|
|
|
|
|
|
|
src => 'is', |
127
|
|
|
|
|
|
|
dest => 'en', |
128
|
|
|
|
|
|
|
); |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
# prints 'ÉG vilja til hafa kynlíf í kvöld' |
131
|
|
|
|
|
|
|
print $en2is->translate('I want to have sex tonight') . "\n"; |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
# prints 'Myself yearn snuggle up to pursue sex this evening' |
134
|
|
|
|
|
|
|
print $is2en->translate('Mig langar að stunda kynlíf i kvöld') . "\n"; |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head1 DESCRIPTION |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Lingua::Translate::InterTran is a translation back-end for |
139
|
|
|
|
|
|
|
L that uses the online translator available |
140
|
|
|
|
|
|
|
at L. The author felt |
141
|
|
|
|
|
|
|
compelled to write a CPAN module for it since it is the only online translator |
142
|
|
|
|
|
|
|
that can handle his native language, Icelandic (albeit amusingly poorly). |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=head1 AUTHOR |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
Hinrik Ern SigurEsson, hinrik.sig@gmail.com |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
Copyright 2008 Hinrik Ern SigurEsson |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
This program is free software, you can redistribute it and/or modify |
153
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=cut |