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