Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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( 'http://query.yahooapis.com/v1/public/yql', ['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="http://www.yahooapis.com/v1/base.rng" 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="http://www.yahooapis.com/v1/base.rng" yahoo:count= +"0" yahoo:created="2014-07-19T19:41:26Z" yahoo:lang="en-US"><results/ +></query><!-- total: 177 --> <!-- engine4.yql.ne1.yahoo.com -->
Anyone know how I can get this working?

Comment on How to transmit text to Yahoo Content Analysis
Select or Download Code
Re: How to transmit text to Yahoo Content Analysis
by thomas895 (Hermit) 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.

    ~Thomas~ 
    "Excuse me for butting in, but I'm interrupt-driven..."
Re: How to transmit text to Yahoo Content Analysis
by Your Mother (Canon) 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 (Canon) on Jul 19, 2014 at 22:53 UTC

    cormanaz:

    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.

    ...roboticus

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

Re: How to transmit text to Yahoo Content Analysis
by Your Mother (Canon) on Jul 20, 2014 at 20:35 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1094320]
Approved by AppleFritter
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (9)
As of 2014-09-22 18:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (198 votes), past polls