#!/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.