welle has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks,
I've started playing with this module. I use this simple code to get sentence examples of a given word (waste in my example). What I am fighting against is the output format... which is more or less a word salat, wihtout any further processing:
use WWW::Wordnik::API;
my %args = (
server_uri => 'http://api.wordnik.com/v4',
api_key => 'my API key',
version => '4',
format => 'json', # json | xml | perl
);
my $WN = WWW::Wordnik::API->new(%args);
my $content = $WN->examples('waste');
print $content;
The result is:
$VAR1 = '{"examples":[{"year":1891,"provider":{"name":"wordnik","id":7
+11},"url":"http://www.gutenberg.org/dirs/2/6/3/3/26330/26330-8.txt","
+word":"waste","text":"We have the total waste of $225,496,741 and thi
+s, reduced to its actual significance, means that of the total actual
+ terminations, 83.6 per cent. was _actual waste_ and only 16.4 per ce
+nt. legitimate terminations, while the great bulk of the last item of
+","title":"Frenzied Finance Vol. 1: The Crime of Amalgamated","docume
+ntId":18081426,"exampleId":1090100953,"rating":8923.145},{"year":2009
+,"provider":{"name":"wordnik","id":711},"url":"http://notasheepmaybea
+goat.blogspot.com/2009/02/g.html","word":"waste","text":"Do you think
+ that over a £billion a year in waste is a good thing in this system?
+","title":"Gordon Brown and the suspension of normal politics","docum
+entId":30226278,"exampleId":838783173,"rating":764.0},{"year":2006,"p
+rovider":{"name":"wordnik","id":711},"url":"http://scientificactivist
+.blogspot.com/2006/04/no-more-aspirin-please.html","word":"waste","te
+xt":"OK, if 13% admin waste is not enough to establish my point, then
+ say 40%.","title":"No More Aspirin, Please","documentId":30350472,"e
+xampleId":886062454,"rating":760.0},{"year":2011,"provider":{"name":"
+spinner","id":712},"url":"http://www.theglobeandmail.com/life/facts-a
+nd-arguments/the-essay/i-salvage-goods-from-the-curb-on-trash-day/art
+icle2149147/?utm_medium=Feeds%3A%20RSS%2FAtom&utm_source=Home&utm_con
+tent=2149147","word":"waste","text":"Growing up, the expression \\"wa
+ste not, want not\\" was branded into my brain.","title":"The Globe a
+nd Mail - Home RSS feed","documentId":32783691,"exampleId":645279785,
+"rating":749.0},{"year":2011,"provider":{"name":"huffingtonpost","id"
+:715},"url":"http://www.huffingtonpost.com/2011/01/02/2012-elections-
+gop-agenda_n_803311.html","word":"waste","text":"Issa, the incoming c
+hairman of the House Oversight and Government Reform Committee, has b
+een especially critical of what he calls waste in Obama\'s economic s
+timulus spending.","title":"2012 Elections Could Be Greatly Impacted
+By New GOP Agenda","documentId":31276093,"exampleId":186827020,"ratin
+g":604.0}]}';
What I should do to get only the examples any all other stuff (year, links, etc.)? Any advice would be appreciated.
Re: WWW::Wordnik::API output
by greengaroo (Hermit) on Nov 20, 2012 at 14:55 UTC
|
The output seems to be a serialized structure. I suggest you first convert it to a HashRef using the Data::Serializer::Raw module in conjunction with Data::Dumper.
For example, you can do this:
# Creating a serializer using Data::Dumper
my $serializer = Data::Serializer::Raw->new( 'serializer' => 'Data::Du
+mper' );
my $hashref = $serializer->deserialize( $content );
Then it will be easier to process as a HashRef.
UPDATE: I see your structure is JSON, let me give you another answer. The principle is the same, convert your JSON to a HashRef, using the JSON module:
use JSON;
my $hashref = from_json( $content );
I do this all the time! Much easier to deal with as a HashRef!
Testing never proves the absence of faults, it only shows their presence.
| [reply] [d/l] [select] |
|
foreach (sort keys %hashref) {
print "$_ : $hashref{$_}\n";
}
I don't get anything | [reply] [d/l] |
|
This extracts the text only:
#!/usr/bin/env perl
use strict;
use warnings;
use feature 'say';
use JSON;
my $content = '{"examples":[{"year":1891,"provider":{"name":"wordnik",
+"id":711},"url":"http://www.gutenberg.org/dirs/2/6/3/3/26330/26330-8.
+txt","word":"waste","text":"We have the total waste of $225,496,741 a
+nd this, reduced to its actual significance, means that of the total
+actual terminations, 83.6 per cent. was _actual waste_ and only 16.4
+per cent. legitimate terminations, while the great bulk of the last i
+tem of","title":"Frenzied Finance Vol. 1: The Crime of Amalgamated","
+documentId":18081426,"exampleId":1090100953,"rating":8923.145},{"year
+":2009,"provider":{"name":"wordnik","id":711},"url":"http://notasheep
+maybeagoat.blogspot.com/2009/02/g.html","word":"waste","text":"Do you
+ think that over a £billion a year in waste is a good thing in this s
+ystem?","title":"Gordon Brown and the suspension of normal politics",
+"documentId":30226278,"exampleId":838783173,"rating":764.0},{"year":2
+006,"provider":{"name":"wordnik","id":711},"url":"http://scientificac
+tivist.blogspot.com/2006/04/no-more-aspirin-please.html","word":"wast
+e","text":"OK, if 13% admin waste is not enough to establish my point
+, then say 40%.","title":"No More Aspirin, Please","documentId":30350
+472,"exampleId":886062454,"rating":760.0},{"year":2011,"provider":{"n
+ame":"spinner","id":712},"url":"http://www.theglobeandmail.com/life/f
+acts-and-arguments/the-essay/i-salvage-goods-from-the-curb-on-trash-d
+ay/article2149147/?utm_medium=Feeds%3A%20RSS%2FAtom&utm_source=Home&u
+tm_content=2149147","word":"waste","text":"Growing up, the expression
+ \\"waste not, want not\\" was branded into my brain.","title":"The G
+lobe and Mail - Home RSS feed","documentId":32783691,"exampleId":6452
+79785,"rating":749.0},{"year":2011,"provider":{"name":"huffingtonpost
+","id":715},"url":"http://www.huffingtonpost.com/2011/01/02/2012-elec
+tions-gop-agenda_n_803311.html","word":"waste","text":"Issa, the inco
+ming chairman of the House Oversight and Government Reform Committee,
+ has been especially critical of what he calls waste in Obama\'s econ
+omic stimulus spending.","title":"2012 Elections Could Be Greatly Imp
+acted By New GOP Agenda","documentId":31276093,"exampleId":186827020,
+"rating":604.0}]}';
my $hashref = from_json( $content );
foreach ( @{ $hashref->{'examples'} } ) {
say "TEXT: " . $_->{'text'};
}
__END__
TEXT: We have the total waste of $225,496,741 and this, reduced to its
+ actual significance, means that of the total actual terminations, 83
+.6 per cent. was _actual waste_ and only 16.4 per cent. legitimate te
+rminations, while the great bulk of the last item of
TEXT: Do you think that over a £billion a year in waste is a good thin
+g in this system?
TEXT: OK, if 13% admin waste is not enough to establish my point, then
+ say 40%.
TEXT: Growing up, the expression "waste not, want not" was branded int
+o my brain.
TEXT: Issa, the incoming chairman of the House Oversight and Governmen
+t Reform Committee, has been especially critical of what he calls was
+te in Obama's economic stimulus spending.
| [reply] [d/l] |
|
|
foreach (sort keys %hashref) {
print "$_ : $hashref{$_}\n";
}
Should be replace by this: foreach (sort keys %{$hashref}) {
print "$_ : ", $hashref->{$_}, "\n";
}
I was using a HashRef not a Hash, $hashref is not the same variable as %hashref, so if you loop through %hashref, of course you get nothing because it is an empty variable. Using strict would have prevent you from doing this. I hope this helps!
Testing never proves the absence of faults, it only shows their presence.
| [reply] [d/l] [select] |
Re: WWW::Wordnik::API output
by frozenwithjoy (Priest) on Nov 20, 2012 at 17:32 UTC
|
This Wordnik API looks pretty cool. I noticed that it only returned a subset of the examples for 'waste'. Do you know what limits they impose? In the FAQ, I saw a 5k calls/hour limit, but nothing about limits on # of results returned. | [reply] |
|
Yeah, I am playing with it and I am experiencing the same limitations. It seems that for "examples" the limit is 5 (in the Perl library. Other languages' library seem not to have such restrictions. I see other limitations also in other information (i.e. related words: I am not able to get all categories...)that it is possible to get from the service...
| [reply] |
|
|