line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WebService::MerriamWebster; |
2
|
1
|
|
|
1
|
|
23005
|
use v5.10; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
54
|
|
3
|
|
|
|
|
|
|
our $VERSION = '0.11'; |
4
|
1
|
|
|
1
|
|
1457
|
use Moose; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
use XML::LibXML; |
6
|
|
|
|
|
|
|
use URI::Escape; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
has 'dict' => ( |
9
|
|
|
|
|
|
|
is => 'rw', |
10
|
|
|
|
|
|
|
isa => 'Str', |
11
|
|
|
|
|
|
|
required => 1 |
12
|
|
|
|
|
|
|
); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
has 'word' => ( |
15
|
|
|
|
|
|
|
is => 'rw', |
16
|
|
|
|
|
|
|
isa => 'Str', |
17
|
|
|
|
|
|
|
required => 1 |
18
|
|
|
|
|
|
|
); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
has 'key' => ( |
21
|
|
|
|
|
|
|
is => 'ro', |
22
|
|
|
|
|
|
|
isa => 'Str', |
23
|
|
|
|
|
|
|
required => 1 |
24
|
|
|
|
|
|
|
); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
## the query format |
27
|
|
|
|
|
|
|
## http://www.dictionaryapi.com/api/v1/references/$dic/xml/$word?key=$key |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
has 'url' => ( |
30
|
|
|
|
|
|
|
is => 'ro', |
31
|
|
|
|
|
|
|
lazy => 1, |
32
|
|
|
|
|
|
|
default => sub { |
33
|
|
|
|
|
|
|
my $self = shift; |
34
|
|
|
|
|
|
|
"http://www.dictionaryapi.com/api/v1/references/" |
35
|
|
|
|
|
|
|
. uri_escape( $self->dict ) . "/xml/" |
36
|
|
|
|
|
|
|
. uri_escape( lc $self->word ) . "?key=" #enforce lowercase |
37
|
|
|
|
|
|
|
. uri_escape( $self->key ); |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
has 'raw_xml' => ( |
42
|
|
|
|
|
|
|
is => 'ro', |
43
|
|
|
|
|
|
|
lazy => 1, |
44
|
|
|
|
|
|
|
builder => '_build_raw_xml', |
45
|
|
|
|
|
|
|
); |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub _build_raw_xml { |
48
|
|
|
|
|
|
|
use HTTP::Tiny; |
49
|
|
|
|
|
|
|
my $self = shift; |
50
|
|
|
|
|
|
|
my $response = HTTP::Tiny->new->get( $self->url ); |
51
|
|
|
|
|
|
|
die "Failed!\n" unless $response->{success}; |
52
|
|
|
|
|
|
|
$response->{content} if length $response->{content}; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
has 'dom' => ( |
56
|
|
|
|
|
|
|
is => 'ro', |
57
|
|
|
|
|
|
|
lazy => 1, |
58
|
|
|
|
|
|
|
default => sub { |
59
|
|
|
|
|
|
|
my $xml = shift->raw_xml; |
60
|
|
|
|
|
|
|
my $dom = XML::LibXML->load_xml( string => $xml ); |
61
|
|
|
|
|
|
|
}, |
62
|
|
|
|
|
|
|
); |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub entries { |
65
|
|
|
|
|
|
|
my $self = shift; |
66
|
|
|
|
|
|
|
my @entries = $self->dom->getElementsByTagName("entry"); |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
#In the XML, audio references look like this: |
70
|
|
|
|
|
|
|
#<wav>heart001.wav</wav> |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
#These need to be converted to a URL like this: |
73
|
|
|
|
|
|
|
#http://media.merriam-webster.com/soundc11/h/heart001.wav |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
#Start with the base URL: http://media.merriam-webster.com/soundc11/ |
76
|
|
|
|
|
|
|
#Add the first letter of the wav file as a subdirectory ("h" in the example above).* |
77
|
|
|
|
|
|
|
#Add the name of the wav file. |
78
|
|
|
|
|
|
|
#* Regarding the subdirectory element of the URL there are three exceptions: |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
#If the file name begins with "bix", the subdirectory should be "bix". |
81
|
|
|
|
|
|
|
#If the file name begins with "gg", the subdirectory should be "gg". |
82
|
|
|
|
|
|
|
#If the file name begins with a number, the subdirectory should be "number". |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub _subdir { |
85
|
|
|
|
|
|
|
my $filename = shift; |
86
|
|
|
|
|
|
|
if ( $filename =~ /^bix/ ) { return "bix" } |
87
|
|
|
|
|
|
|
if ( $filename =~ /^gg/ ) { return "gg" } |
88
|
|
|
|
|
|
|
if ( $filename =~ /^(\d+).*/ ) { return $1 } |
89
|
|
|
|
|
|
|
return substr $filename, 0, 1; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub audio_url { |
93
|
|
|
|
|
|
|
my $self = shift; |
94
|
|
|
|
|
|
|
my $tag = $self->dom->getElementsByTagName("wav") or die "can not find the audio uril for " . $self->word; |
95
|
|
|
|
|
|
|
my $wave = $tag->[0]->string_value; |
96
|
|
|
|
|
|
|
"http://media.merriam-webster.com/soundc11/" . _subdir($wave) . "/$wave"; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
1; |
100
|
|
|
|
|
|
|
__END__ |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=encoding utf-8 |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head1 NAME |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
WebService::MerriamWebster - use Merriam-Webster dictionary API in Perl |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=begin html |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
<a href="https://travis-ci.org/swuecho/WebService_MerriamWebster"><img src="https://travis-ci.org/swuecho/WebService_MerriamWebster.svg?branch=0.09"></a> |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=end html |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head1 SYNOPSIS |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
use WebService::MerriamWebster; |
117
|
|
|
|
|
|
|
my $mw = WebService::MerriamWebster->new(dict => "collegiate", word => "$the-word-to-query", key => "$your-key-here"); |
118
|
|
|
|
|
|
|
my $xml = $mw->raw_xml; # a string |
119
|
|
|
|
|
|
|
my $dom = $mw->dom; # XML::LibXML::Document |
120
|
|
|
|
|
|
|
my @entries = $mw->entries(); # @entries is list of XML::LibXML::Element |
121
|
|
|
|
|
|
|
my $audio_uri = $mw->audio_url; # ex. http://media.merriam-webster.com/soundc11/t/test0001.wav |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head1 DESCRIPTION |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
WebService::MerriamWebster is an api to merriam-webster.com dictionary. It gives you xml result based on your query. |
127
|
|
|
|
|
|
|
it use XML::LibXML to process the XML result (very basic). If you need to get the value of specific node, probably |
128
|
|
|
|
|
|
|
you need to deal with the xml yourself. |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=head1 Attributes |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head2 dict |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
has 'dict' => ( |
136
|
|
|
|
|
|
|
is => 'rw', |
137
|
|
|
|
|
|
|
isa => 'Str', |
138
|
|
|
|
|
|
|
required => 1 |
139
|
|
|
|
|
|
|
); |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
the dictionary you want to use |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=head2 word |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
has 'word' => ( |
146
|
|
|
|
|
|
|
is => 'rw', |
147
|
|
|
|
|
|
|
isa => 'Str', |
148
|
|
|
|
|
|
|
required => 1 |
149
|
|
|
|
|
|
|
); |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
the word to query |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=head2 key |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
has 'key' => ( |
156
|
|
|
|
|
|
|
is => 'ro', |
157
|
|
|
|
|
|
|
isa => 'Str', |
158
|
|
|
|
|
|
|
required => 1 |
159
|
|
|
|
|
|
|
); |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=head2 url |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
the query url based on $dict, $word, $key |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
reference: http://www.dictionaryapi.com/api/v1/references/$dic/xml/$word?key=$key |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=head2 raw_xml |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
the xml you get based on your query |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head2 dom |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
the dom (XML::LibXML::Document) base on your query |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=head1 Method |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=head2 entries |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
the @entries of dom |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=head2 audio_url |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
the audio url of the query word |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=head1 AUTHOR |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
Hao Wu E<lt>echowuhao@gmail.comE<gt> |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=head1 COPYRIGHT |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
Copyright 2013- Hao Wu |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=head1 LICENSE |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
197
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=head1 SEE ALSO |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
L<XML::LibXML> |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
=cut |