line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Acme::eng2kor; |
2
|
|
|
|
|
|
|
# ABSTRACT: English to Korean Translator |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
39829
|
use utf8; |
|
2
|
|
|
|
|
13
|
|
|
2
|
|
|
|
|
11
|
|
6
|
2
|
|
|
2
|
|
1794
|
use Any::Moose; |
|
2
|
|
|
|
|
82244
|
|
|
2
|
|
|
|
|
18
|
|
7
|
2
|
|
|
2
|
|
1196
|
use Any::Moose '::Util::TypeConstraints'; |
|
2
|
|
|
|
|
10
|
|
|
2
|
|
|
|
|
12
|
|
8
|
2
|
|
|
2
|
|
3107
|
use JSON qw/decode_json/; |
|
2
|
|
|
|
|
40246
|
|
|
2
|
|
|
|
|
12
|
|
9
|
2
|
|
|
2
|
|
2066
|
use Const::Fast; |
|
2
|
|
|
|
|
5895
|
|
|
2
|
|
|
|
|
12
|
|
10
|
2
|
|
|
2
|
|
1736
|
use URI::Escape qw/uri_escape_utf8/; |
|
2
|
|
|
|
|
5010
|
|
|
2
|
|
|
|
|
131
|
|
11
|
2
|
|
|
2
|
|
1729
|
use HTTP::Request; |
|
2
|
|
|
|
|
66763
|
|
|
2
|
|
|
|
|
88
|
|
12
|
2
|
|
|
2
|
|
3645
|
use HTTP::Response; |
|
2
|
|
|
|
|
21830
|
|
|
2
|
|
|
|
|
82
|
|
13
|
2
|
|
|
2
|
|
7341
|
use LWP::UserAgent; |
|
2
|
|
|
|
|
65106
|
|
|
2
|
|
|
|
|
117
|
|
14
|
2
|
|
|
2
|
|
3388
|
use namespace::autoclean; |
|
2
|
|
|
|
|
54286
|
|
|
2
|
|
|
|
|
18
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
const my $GOOGLE_TRANSLATE_API_URL => "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=%s&langpair=%s"; |
17
|
|
|
|
|
|
|
const my @SUPPORT_LANG_TAGS => qw/ach af ak am ar az be bem bg bh bn br bs ca co cs cy da de el en eo es et eu fa fi fo fr fy ga gd gl gn gu ha haw hi hr ht hu hy ia id ig is it iw ja jw ka kg kk km kn ko ku ky la lg ln lo lt lua lv mfe mg mi mk ml mn mo mr ms mt ne nl nn no ny nyn oc om or pa pl ps qu rm rn ro ru rw sd sh si sk sl sn so sq sr st su sv sw ta te tg th ti tk tl tn to tr tt tw ug uk ur uz vi wo xh yi yo zu/; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
subtype 'LangTags' |
20
|
|
|
|
|
|
|
=> as 'Str' |
21
|
|
|
|
|
|
|
=> where { my $lang = $_; grep { /^$lang$/ } @SUPPORT_LANG_TAGS; }; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
has 'src' => ( |
24
|
|
|
|
|
|
|
is => 'rw', |
25
|
|
|
|
|
|
|
isa => 'LangTags', |
26
|
|
|
|
|
|
|
default => 'en' |
27
|
|
|
|
|
|
|
); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
has 'dst' => ( |
30
|
|
|
|
|
|
|
is => 'rw', |
31
|
|
|
|
|
|
|
isa => 'LangTags', |
32
|
|
|
|
|
|
|
default => 'ko' |
33
|
|
|
|
|
|
|
); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
has 'text' => ( |
36
|
|
|
|
|
|
|
is => 'rw', |
37
|
|
|
|
|
|
|
isa => 'Str' |
38
|
|
|
|
|
|
|
); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
has 'translated' => ( |
41
|
|
|
|
|
|
|
is => 'rw', |
42
|
|
|
|
|
|
|
isa => 'Str' |
43
|
|
|
|
|
|
|
); |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub translate { |
47
|
1
|
|
|
1
|
1
|
3
|
my ($self, $word) = @_; |
48
|
1
|
50
|
|
|
|
6
|
map { s/^\s+//; s/\s+$// } $word if defined $word; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
49
|
1
|
|
|
|
|
6
|
return $self->_google_translate($word); |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub _google_translate { |
54
|
1
|
|
|
1
|
|
3
|
my ($self, $word) = @_; |
55
|
1
|
50
|
|
|
|
5
|
$self->text($word) if defined $word; |
56
|
1
|
|
|
|
|
11
|
my $text = uri_escape_utf8($self->text); |
57
|
1
|
|
|
|
|
70
|
my $escaped_uri = sprintf($GOOGLE_TRANSLATE_API_URL, $text, $self->src . '|' . $self->dst); |
58
|
1
|
|
|
|
|
5
|
my $json = $self->get_json($escaped_uri); |
59
|
1
|
|
|
|
|
104
|
$self->translated($json->{responseData}{translatedText}); |
60
|
0
|
|
|
|
|
0
|
return $json; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub get_json { |
65
|
1
|
|
|
1
|
1
|
2
|
my ($self, $url) = @_; |
66
|
1
|
|
|
|
|
12
|
my $req = HTTP::Request->new( GET => $url ); |
67
|
1
|
|
|
|
|
9888
|
my $ua = LWP::UserAgent->new; |
68
|
1
|
|
|
|
|
297406
|
my $res = $ua->request($req); |
69
|
1
|
50
|
|
|
|
2377636
|
die $res->status_line, "\n" unless $res->is_success; |
70
|
1
|
|
|
|
|
26
|
return decode_json($res->content); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
1; |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
__END__ |