8

Are there any APIs that can extract moods from a string (for use in PHP but can be implemented in any language)?

If one doesn't exist, how would I go about building a classifier, presumably something related to machine learning, where I extract words with known positive/negativity.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Pez Cuckow
  • 14,048
  • 16
  • 80
  • 130
  • 3
    Is this useful? http://stackoverflow.com/a/959162/995958 – lorenzo-s May 17 '12 at 16:27
  • 3
    All those upvotes are either seeing something I don't, or really want this, but did you do any research about this? Where are you in your plans to build one (for the second part of your question) etc? (remember that the tooltip on downvoting a question starts with "This question does not show any research effort;" ) – Nanne May 17 '12 at 16:29
  • @Nanne speaking for myself; it would interesting to see this implemented. – 1321941 May 17 '12 at 16:32
  • 4
    @Nanne: I just thought it was an awesome question. :) – Elliot Bonneville May 17 '12 at 16:33
  • What about sarcasm? Most people can't even pick that up in text. – Gary May 17 '12 at 16:33
  • 1
    I was just amazed where all these upvotes come from on this -what seems to me- "do my research for me" question. Well, the community-vote has it it seems :) – Nanne May 17 '12 at 16:34
  • 1
    It was less do my research for me and more ask the experts, at least that's why I posted it here! – Pez Cuckow May 17 '12 at 20:25
  • 1
    A few years back, The Guardian did something with Twitter during a Tony Blair speech. They just used Mechanical Turk, which worked out cheaper and more accurate than anything a developer could do. It seems just too difficult to handle sarcasm, passive-aggressive, typos, slang, etc. Plus, if you build something good, people will learn how to game it. – Dan Blows May 20 '12 at 14:52

2 Answers2

2

I would suggest AlchemyAPI. They have pretty simple APIs ( which shouldn't be difficult to use. For your specific case, look into here

UltraInstinct
  • 43,308
  • 12
  • 81
  • 104
0

Using the above suggestion of AlchemyapI, here is a really simple system based on Facebook statuses'

  $id = CURRENT USER ID;
  $message = array(); //the users posts with scores
  $status = $fb->fql("SELECT status_id, message FROM status WHERE uid=$id LIMIT 10");
  foreach($status as $stat) {
    $message = file_get_contents("http://access.alchemyapi.com/calls/text/TextGetTextSentiment"
                      ."?outputMode=json&apikey=MYAPIKEY"
                      ."&text=".urlencode($stat['message']));
    $data = json_decode($message); //get reply
    $messages[] = array("status"=>$stat['message'], "score"=>($data->docSentiment->type!="neutral") ? $data->docSentiment->score : 0); //save reply
  }

  $user = $fb->api("/".$id); //query the user

  $content .= "<h3>".$user['name']."</h3>";

  $total = 0;
  $count = 0;
  foreach($messages as $message) {
    $total += $message['score'];
    if($message['score']!=0) $count++;
  }

  $content .= 'Has an average rating of '.$total/$count.' <meter min="-1" max="1" value="'.$total/$count.'"></meter><br /><br />';

  foreach($messages as $message) {
    $content .= '<b>'.$message['status'].'</b> '.$message['score'].'</br>'
               .'<meter ' //class="'.($message['score'] == 0 ? "yellow" : $message['score'] < 0 ? "red" : "green").'" '
               .'value="'.$message['score'].'" min="-0.5" max="0.5" optimum="0">'.$message['score'].' out of -1 to 1</meter><br /><br />';
  }
Pez Cuckow
  • 14,048
  • 16
  • 80
  • 130