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
|
#!/usr/bin/env perl
#
# Find the details of a given word using the Words API
# (https://www.wordsapi.com/docs/)
#
# Dependencies: HTTP-Message, LWP, JSON
################################################################################
use strict;
use warnings;
use open ':std', ':encoding(UTF-8)'; # To correctly display pronunciations
use HTTP::Request;
use LWP::UserAgent;
use JSON qw<decode_json>;
# API key read from environment
my $WORDSAPI_KEY = $ENV{'WORDSAPI_KEY'} or die "WORDSAPI_KEY not defined";
my $WORD = shift or die "Usage: $0 <word>\n";
my $ua = LWP::UserAgent->new;
$ua->agent("gramos's script for perlweeklychallenge.org");
my $uri = "https://wordsapiv1.p.mashape.com/words/$WORD";
my @headers = ("X-Mashape-Key", $WORDSAPI_KEY);
my $response = $ua->request(HTTP::Request->new("GET", $uri, \@headers));
# Check for errors in API request
if ($response->is_error()) {
if ($response->code == 404) {
die "Word '$WORD' not found\n";
} else {
die "Unknown error: " . $response->message;
}
}
my $json_resp = decode_json $response->content;
my $syllables = $json_resp->{'syllables'};
if ($syllables && $syllables->{'count'} > 1) {
printf "Word: '$WORD' (%s)\n", join("-", @{$syllables->{'list'}});
} else {
print "Word: '$WORD'\n";
}
my $pronunciation = $json_resp->{'pronunciation'};
if (ref($pronunciation) eq "HASH") {
my %pronunciations = %{$pronunciation};
if ($pronunciations{'all'}) {
print "Pronunciation: /$pronunciations{'all'}/\n";
} else {
print "Pronunciation:\n";
foreach my $k (keys %pronunciations) {
print " as $k: /$pronunciations{$k}/\n";
}
}
} elsif ($pronunciation) {
# This case is not documented, thanks WordsAPI.
print "Pronunciation: /$pronunciation/\n";
}
my $frequency = $json_resp->{'frequency'};
if ($frequency) {
print "Frequency: ";
print $frequency > 5 ? "frequently used"
: $frequency > 3 ? "occasionally used"
: $frequency > 1 ? "rarely used"
: "never used";
print "\n";
}
my $results = $json_resp->{'results'};
if ($results) {
print "Definitions:\n";
foreach my $res (@$results) {
printf " - (%s) %s\n", $res->{'partOfSpeech'}, $res->{'definition'};
my $synonyms = $res->{'synonyms'};
if ($synonyms) {
printf " synonyms: %s\n", join(", ", @$synonyms);
}
my $examples = $res->{'examples'};
if ($examples) {
foreach my $example (@$examples) {
print qq' "$example"\n';
}
}
print "\n";
}
}
|