检测推文或消息的情绪

时间:2012-05-17 16:24:12

标签: php machine-learning

是否有任何API可以从字符串中提取情绪(在PHP中使用但可以用任何语言实现)?

如果不存在,我将如何建立一个分类器,大概是与机器学习相关的东西,在那里我提取具有已知正面/负面的单词。

2 个答案:

答案 0 :(得分:2)

我建议AlchemyAPI。它们有非常简单的API(应该不难使用。对于您的具体情况,请查看here

答案 1 :(得分:0)

使用AlchemyapI的上述建议,这是一个基于Facebook状态的非常简单的系统'

  $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 />';
  }