Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

How to transmit text to Yahoo Content Analysis

by cormanaz (Chaplain)
on Jul 19, 2014 at 19:46 UTC ( #1094320=perlquestion: print w/replies, xml ) Need Help??
cormanaz has asked for the wisdom of the Perl Monks concerning the following question:

Good day bros. I am trying to use LWP to access Yahoo's content analysis API. The code I am using follows:
my $text = qq("Researchers from the University of Minnesota's Institut +e on the Environment looked at 17 key crops that produce 86 percent o +f the world crop calories.); my $ua = LWP::UserAgent->new(); my $response = $ua->post( '', ['q' => "select * from contentanalysis.analyze where text='".$text +."'"] ); print $response->content();
When I run this I get:
<?xml version="1.0" encoding="UTF-8"?> <error xmlns:yahoo="" yahoo:lang=" +en-US"><description>Query syntax error(s) [line 1:96 missing EOF at ' +s']</description></error>
So it doesn't like the apostrophe in the query. When I take out the apostrophe, it works like it's supposed to. Since it's a SQL-like query, I tried using double apostrophes, like you would to escape a query string in SQL, and that produced the same error. So I tried URI::Escape on the string. That yields response status of OK, but no contents in the results, i.e.
<?xml version="1.0" encoding="UTF-8"?> <query xmlns:yahoo="" yahoo:count= +"0" yahoo:created="2014-07-19T19:41:26Z" yahoo:lang="en-US"><results/ +></query><!-- total: 177 --> <!-- -->
Anyone know how I can get this working?

Replies are listed 'Best First'.
Re: How to transmit text to Yahoo Content Analysis
by Your Mother (Bishop) on Jul 19, 2014 at 22:51 UTC
    $text =~ s/'/\\'/g; # :P

    How did I do that? Science! Well, no but that is the category returned when you properly escape your quotes.

    <yctCategory score="0.754717">Science</yctCategory>

    Update: I only just noticed you also have a badly balanced " (double quote) in your string. I edited the code before I ran it. :( You will probably need to fix it as well or take a look at the utility I just did and mentioned in another post. It escapes single and double quotes.

Re: How to transmit text to Yahoo Content Analysis
by roboticus (Chancellor) on Jul 19, 2014 at 22:53 UTC


    Your $text string has an apostrophe in it, so if the syntax is expected to be legal SQL, than the problem could easily be that. Try removing the apostrophe to see if the error goes away. If so, figure out what the proper quoting convention needs to be and be sure to quote your strings appropriately.


    When your only tool is a hammer, all problems look like your thumb.

Re: How to transmit text to Yahoo Content Analysis
by thomas895 (Chaplain) on Jul 19, 2014 at 21:48 UTC

    This isn't really a Perl question, you know. Nevertheless, I'll do my best.

    From the page you linked, the "Request parameters" table says the following about "text":

    string (required if url parameter is not used)
    (emphasis added).
    This leads me to conjecture that you could pass your text in the URL as a parameter, escaping that like one would with any other URL.

    Since you're using Perl, perhaps consider a YQL module: there are a few available on CPAN.

    "Excuse me for butting in, but I'm interrupt-driven..."
Re: How to transmit text to Yahoo Content Analysis
by Your Mother (Bishop) on Jul 20, 2014 at 20:35 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1094320]
Approved by AppleFritter
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2018-05-25 21:34 GMT
Find Nodes?
    Voting Booth?