Well, I was not successful in easily using this Curl idea as the website suggested...they seem to have some admin/volunteer problems at the moment, although the concept sounds good. I just could not easily get Curl installed on my Windows machine.

This is different enough that I thought a new response was warranted rather than just an update to the previous response. I'm not sure if the "sentiment values" actually correspond to the graph on the website, but these results seem reasonable and seem to "jive" with the "sentiment meter" readings.

Here is some LWP code for you.

Figuring out (a) what URL to send the POST back to can be frustrating as well as (b) figuring out the parameters that it wants to have - with LWP you have to decide this from looking at the page source code that is sent to you (that may include Javascript, and this one does). Usually you don't have to run the Javascript yourself, just reply with what that script would do.

#!/usr/bin/perl -w use strict; use LWP::UserAgent; $|=1; my $DEBUG =0; my $url = "http://sentimentanalyzer.appspot.com/"; my $ua = LWP::UserAgent->new or die "New UserAgent Failed"; # get the main page ... this works ... # my $response = $ua->get($url) or die "Problem getting $url\n"; $response->is_success or die "Failed to GET '$url': ", $response->status_line; my $html_page = $response->content( ); print $html_page if $DEBUG; # The trouble starts with figuring out the correct stuff to # POST back to the website... # You have to know what URL to post back to - (not necessarily # exactly where it came from) and also what to send in the POST! # This site does not appear to use cookies # The URL to POST back to is slightly different than the # main URL - here is not "dynamic" and probably this works # even without retrieving the main site - When SSL and cookies # get involved, it can be more complicated. foreach my $content ("A dog not like a cat.", "This is bogus!", "I really love my grandmother.", ) { $response = $ua->post( 'http://sentimentanalyzer.appspot.com/api/classify', [ 'content' => "$content", 'value' => "Submit", 'lang' => 'en', ], ); $response->is_success or die "Error: ", $response->status_line; $html_page = $response->content(); print $html_page if $DEBUG; #This is "Very Ugly", but appears to work my ($score) = $html_page =~ /\{\"score\":(.*)\s*\}/; print "$score $content\n"; } __END__ 0.003720114371614687 A dog not like a cat. 0.81868251185499441 This is bogus! 0.90770799032922367 I really love my grandmother.
Update:

Firefox has moved some of this stuff around. To view the page source sent to the browser: Tools|Web Developer|Page Source. There are other tools like HTTPfox and others that I don't know how to use well yet, but other Monks will!


In reply to Re: Passing Values to a webpage and retrieving an answer by Marshall
in thread Passing Values to a webpage and retrieving an answer by nateg

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.