| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Net::MW::API; |
|
2
|
1
|
|
|
1
|
|
35778
|
use v5.10; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
53
|
|
|
3
|
|
|
|
|
|
|
our $VERSION = '0.08'; |
|
4
|
1
|
|
|
1
|
|
1480
|
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( $self->word ) . "?key=" |
|
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
|
|
|
|
|
|
|
Net::MW::API - use Merriam-Webster dictionary API in Perl |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
use Net::MW::API; |
|
111
|
|
|
|
|
|
|
my $mw = Net::MW::API->new(dict => "collegiate", word => "$the-word-to-query", key => "$your-key-here"); |
|
112
|
|
|
|
|
|
|
my $xml = $mw->raw_xml; # a string |
|
113
|
|
|
|
|
|
|
my $dom = $mw->dom; # XML::LibXML::Document |
|
114
|
|
|
|
|
|
|
my @entries = $mw->entries(); # @entries is list of XML::LibXML::Element |
|
115
|
|
|
|
|
|
|
my $audio_uri = $mw->audio_url; # ex. http://media.merriam-webster.com/soundc11/t/test0001.wav |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
Net::MW::API is an api to merriam-webster.com dictionary. It gives you xml result based on your query. |
|
121
|
|
|
|
|
|
|
it use XML::LibXML to process the XML result (very basic). If you need to get the value of specific node, probably |
|
122
|
|
|
|
|
|
|
you need to deal with the xml yourself. |
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=head1 Attributes |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=head2 dict |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
has 'dict' => ( |
|
130
|
|
|
|
|
|
|
is => 'rw', |
|
131
|
|
|
|
|
|
|
isa => 'Str', |
|
132
|
|
|
|
|
|
|
required => 1 |
|
133
|
|
|
|
|
|
|
); |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
the dictionary you want to use |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head2 word |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
has 'word' => ( |
|
140
|
|
|
|
|
|
|
is => 'rw', |
|
141
|
|
|
|
|
|
|
isa => 'Str', |
|
142
|
|
|
|
|
|
|
required => 1 |
|
143
|
|
|
|
|
|
|
); |
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
the word to query |
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head2 key |
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
has 'key' => ( |
|
150
|
|
|
|
|
|
|
is => 'ro', |
|
151
|
|
|
|
|
|
|
isa => 'Str', |
|
152
|
|
|
|
|
|
|
required => 1 |
|
153
|
|
|
|
|
|
|
); |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=head2 url |
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
the query url based on $dict, $word, $key |
|
158
|
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
reference: http://www.dictionaryapi.com/api/v1/references/$dic/xml/$word?key=$key |
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=head2 raw_xml |
|
162
|
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
the xml you get based on your query |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=head2 dom |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
the dom (XML::LibXML::Document) base on your query |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=head1 Method |
|
171
|
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head2 entries |
|
173
|
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
the @entries of dom |
|
175
|
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=head2 audio_url |
|
177
|
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
the audio url of the query word |
|
179
|
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=head1 AUTHOR |
|
181
|
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
Hao Wu E<lt>echowuhao@gmail.comE<gt> |
|
183
|
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head1 COPYRIGHT |
|
185
|
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
Copyright 2013- Hao Wu |
|
187
|
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=head1 LICENSE |
|
189
|
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
|
191
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
|
192
|
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
194
|
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
L<XML::LibXML> |
|
196
|
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=cut |