- 2014 年 2 月 3 日
- 瓦西利斯·弗里尼奥提斯(Vasilis Vryniotis)
- 。 7条留言
在本文中,我们将讨论如何轻松构建简单的Facebook情绪分析工具,该工具能够将公共帖子(来自用户和页面)分类为肯定,否定和中立。 我们将使用Facebook的Graph API Search和Datumbox API 1.0v。 类似于 Twitter情绪分析工具 尽管我们是几个月前构建的,但是该实现是用PHP编写的,因此您可以使用所选的计算机语言轻松地构建自己的工具。
更新:Datumbox机器学习框架现在是开源的,免费提供给 下载。 如果要构建情感分析分类器而不遇到API限制,请使用com.datumbox.applications.nlp.TextClassifier类。
该工具的完整PHP代码可在上找到 Github上.
Facebook情绪分析如何工作?
正如我们在之前的文章中讨论的那样, 情感分析 需要使用先进的机器学习和自然语言处理技术。 在之前的文章中,我们详细介绍了几种文本分类器,例如 朴素贝叶斯是, Softmax回归 和 最大熵,我们讨论了使用 功能选择 在文本分类问题中,最后我们看到了如何开发一种实现 JAVA中的多项式朴素贝叶斯分类器.
在Facebook上执行情感分析与我们之前讨论的内容没有太大不同。 简而言之,我们需要获取Facebook帖子并提取其内容,然后对它们进行标记化以提取其关键字组合。 然后,我们执行特征选择以仅保留对分类问题重要的n-gram,然后训练分类器以识别正面,负面和中立的职位。
使用Datumbox可以大大简化上述过程 机器学习API。 在Facebook上执行情感分析所需要做的全部工作就是调用Graph API搜索以提取感兴趣的帖子,提取其文本并调用Datumbox Sentiment Analysis API进行分类。
构建Facebook情绪分析工具
为了构建Facebook情绪分析工具,您需要做两件事:使用Facebook API来获取公共帖子,并根据其关键字评估帖子的极性。 对于第一个任务,我们将使用Facebook的Graph API搜索,第二个任务将使用Datumbox API 1.0v。
我们将通过使用2个类来加快工具的开发: 脸书 PHP SDK 这将使我们能够轻松访问“图形”搜索和“基准”框 PHP-API-客户端。 在此过程中,最复杂的任务再次是创建一个Facebook应用程序,这将使我们能够从Facebook获取帖子; Datumbox集成是小菜一碟。
创建自己的Facebook应用程序
不幸的是,Facebook强制要求在访问之前进行身份验证 他们的图搜索API。 值得庆幸的是,它们提供了非常易于使用的 软件开发套件(SDK) 它负责集成的大多数技术细节。 仍然在使用它之前,您必须使用您的Facebook帐户创建一个新的Facebook应用程序。
这个过程很简单。 去 Facebook开发人员页面 (如果您以前从未编写过Facebook应用程序,则需要注册)。 单击菜单上的应用程序,然后选择“创建新应用程序”。
在弹出窗口中,填写应用程序的“显示名称”,“名称空间”,选择一个类别,然后单击“创建应用程序”。 创建应用程序后,转到应用程序的主页,然后选择仪表板。 这是您获取AppID和App Secret值的地方。 将这些值复制到一个安全的地方,因为稍后我们将需要它们。
接下来转到应用程序的“设置”,然后单击页面底部的“+ 应用程序平台”。在弹出窗口中选择“网站”,然后在站点 URL 地址上输入您要上传工具的位置的 URL(例如:https://localhost/)。单击“保存更改”即可完成!
获取您的Datumbox API密钥
访问Datumbox API 注册账户 免费帐户并访问您的 API凭据面板 获取您的API密钥。
开发Facebook情绪分析类
最后,我们要做的就是编写一个简单的类,将两个API集成在一起。 首先调用Facebook Graph Search,进行身份验证,获取帖子,然后将其传递给Datumbox API以获取其极性。
这是该类的代码以及必要的注释。
<?php include_once(dirname(__FILE__).'/DatumboxAPI.php'); include_once(dirname(__FILE__).'/facebook-php-sdk/src/facebook.php'); class FacebookSentimentAnalysis { protected $datumbox_api_key; //Your Datumbox API Key. Get it from https://www.datumbox.com/apikeys/view/ protected $app_id; //Your Facebook APP Id. Get it from https://developers.facebook.com/ protected $app_secret; //Your Facebook APP Id. Get it from https://developers.facebook.com/ /** * The constructor of the class * * @param string $datumbox_api_key Your Datumbox API Key * @param string $app_id Your Facebook App Id * @param string $app_secret Your Facebook App Secret * * @return FacebookSentimentAnalysis */ public function __construct($datumbox_api_key, $app_id, $app_secret){ $this->datumbox_api_key=$datumbox_api_key; $this->app_id=$app_id; $this->app_secret=$app_secret; } /** * This function fetches the fb posts list and evaluates their sentiment * * @param array $facebookSearchParams The Facebook Search Parameters that are passed to Facebook API. Read more here https://developers.facebook.com/docs/reference/api/search/ * * @return array */ public function sentimentAnalysis($facebookSearchParams) { $posts=$this->getPosts($facebookSearchParams); return $this->findSentiment($posts); } /** * Calls the Open Graph Search method of the Facebook API for particular Graph API Search Parameters and returns the list of posts that match the search criteria. * * @param mixed $facebookSearchParams The Facebook Search Parameters that are passed to Facebook API. Read more here https://developers.facebook.com/docs/reference/api/search/ * * @return array $posts */ protected function getPosts($facebookSearchParams) { //Use the Facebook SDK Client $Client = new Facebook(array( 'appId' => $this->app_id, 'secret' => $this->app_secret, )); // Get User ID $user = $Client->getUser(); //if Use is not set, redirect to login page if(!$user) { header('Location: '.$Client->getLoginUrl()); die(); } $posts = $Client->api('/search', 'GET', $facebookSearchParams); //call the service and get the list of posts unset($Client); return $posts; } /** * Finds the Sentiment for a list of Facebook posts. * * @param array $posts List of posts coming from Facebook's API * * @param array $posts */ protected function findSentiment($posts) { $DatumboxAPI = new DatumboxAPI($this->datumbox_api_key); //initialize the DatumboxAPI client $results=array(); if(!isset($posts['data'])) { return $results; } foreach($posts['data'] as $post) { //foreach of the posts that we received $message=isset($post['message'])?$post['message']:''; if(isset($post['caption'])) { $message.=("nn".$post['caption']); } if(isset($post['description'])) { $message.=("nn".$post['description']); } if(isset($post['link'])) { $message.=("nn".$post['link']); } $message=trim($message); if($message!='') { $sentiment=$DatumboxAPI->SentimentAnalysis(strip_tags($message)); //call Datumbox service to get the sentiment if($sentiment!=false) { //if the sentiment is not false, the API call was successful. $tmp = explode('_',$post['id']); if(!isset($tmp[1])) { $tmp[1]=''; } $results[]=array( //add the post message in the results 'id'=>$post['id'], 'user'=>$post['from']['name'], 'text'=>$message, 'url'=>'https://www.facebook.com/'.$tmp[0].'/posts/'.$tmp[1], 'sentiment'=>$sentiment, ); } } } unset($posts); unset($DatumboxAPI); return $results; } }
如您在上面的构造函数中所见,我们传递了访问2个API所需的密钥。 在公共方法sendimentAnalysis()上,我们初始化Facebook客户端,进行身份验证并检索帖子列表。 请注意,如果您尚未授权您的应用程序,或者您尚未使用帐户登录Facebook,您将被重定向到Facebook.com以登录并授权该应用程序(这是您的应用程序,无需担心隐私问题)。 一旦检索到帖子列表,它们将被传递到Datumbox API以获取其极性。
你已准备好出发! 您已经准备好使用此类在Facebook上执行情感分析。 您可以 下载 Github的Facebook情绪分析工具的完整PHP代码。
使用和扩展实施
要使用提供的工具,您需要如上所述创建Facebook应用程序,然后通过修改config.php文件对其进行配置。 在此文件中,您将需要放置Datumbox API密钥,之前复制的Facebook App ID和Secret。
最后,在上一篇文章中,我们建立了一个独立的 Twitter情绪分析工具。 合并这两个实现并创建一个能够从Facebook和Twitter上获取帖子并将结果显示在单个报告中的工具,不会花费超过10分钟的时间。
如果您喜欢这篇文章,请花一点时间在Facebook或Twitter上分享! 🙂