如何构建智能反垃圾邮件WordPress插件

时间:2022-04-27
本文章向大家介绍如何构建智能反垃圾邮件WordPress插件,主要内容包括WordPress插件的目标、安装插件、构建机器学习反垃圾邮件插件、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

在本文中,我们将看到我们如何构建一个使用机器学习来阻止垃圾邮件,成人邮件,甚至是负面评论的WordPress插件。该插件与WordPress 3.6v或更高版本兼容,并使用Datumbox API 1.0v。尽管本文讨论了WordPress插件的开发,但我们应该注意,通过使用Datumbox API,可以非常容易地保护任何类型的在线社区免受垃圾邮件,攻击性或不适当的内容侵害。阅读下面,你会看到如何达成目的。

更新:Datumbox机器学习框架现在是开源的,可以免费下载。如果要构建反垃圾邮件分类器而不触及API限制,请使用com.datumbox.applications.nlp.TextClassifier类。

您可以从WordPressGithub下载机器学习反垃圾邮件WordPress插件的完整代码。

WordPress插件的目标

我们的目标是建立一个插件,每当有人提交新的评论时就会触发。它应该使博客所有者能够选择他/她希望阻止的内容的类型。为了使插件变得智能化并利用机器学习,我们将使用Datumbox的3个可用API函数:垃圾邮件检测,成人内容检测和情感分析。

安装插件

安装插件是非常容易的:

  1. 只需下载它,解压缩zip文件,并将包含的“machine-learning-antispam”文件夹移动到WordPress插件文件夹中。
  2. 转到您的管理区域,点击插件菜单并激活插件。
  3. 最后进入左侧菜单并选择设置=>机器学习反垃圾邮件。您只需添加您的Datumbox API密钥,然后选择您要过滤的评论类型(垃圾邮件,成人或负面评论)。

使用插件需要你有一个Datumbox API密钥。您可以通过注册 Datumbox账户免费获得一个。注册号之后,到您的API证书区域复制您的API密钥并将其粘贴到上述配置页面中。

构建机器学习反垃圾邮件插件

首先,我们创建一个名为“机器学习反垃圾邮件”的文件夹。这个文件夹将包含我们的插件的所有文件。为了能够轻松地调用Datumbox API,我们下载了PHP Datumbox API客户端,并在之前的文件夹中复制了DatumboxAPI.php文件。我们这样做是因为DatumboxAPI类为我们提供了一个非常简单的接口来调用Datumbox API。同样,您将在稍后看到,本教程最简单的部分是将机器学习功能集成到您的软件中。这是因为Datumbox API非常易于使用,并且提供了多种已经实现的各种语言的API客户端。

第二步是创建一个“options.php”文件,其中将包含管理插件所需的所有配置功能和管理页面。这是我们放置添加我们的插件在设置菜单中的代码并打印配置页面的地方。要了解更多信息,我强烈建议您阅读官方WordPress指南“ 创建选项页面 ”。这里是options.php文件的代码:

<?phpif (!function_exists('add_action')) {    die();} 
add_action('admin_menu', 'machinelearningantispam_admin_menu'); 
function machinelearningantispam_admin_menu() {
    add_submenu_page('options-general.php', __('Machine Learning Antispam'), __('Machine Learning Antispam'), 'manage_options', 'machine-learning-antispam-config', 'machinelearningantispam_conf_page');         //call register settings function
    add_action( 'admin_init', 'machinelearningantispam_settings' );} 
function machinelearningantispam_settings() {
    register_setting( 'machinelearningantispam-settings-group', 'datumbox_api_key');
    register_setting( 'machinelearningantispam-settings-group', 'machinelearningantispam_filterspam');
    register_setting( 'machinelearningantispam-settings-group', 'machinelearningantispam_filteradult');
    register_setting( 'machinelearningantispam-settings-group', 'machinelearningantispam_filternegative');} 
function machinelearningantispam_conf_page() {
        ?>    <div class="wrap">    
        <h2><?php echo __('Machine Learning Antispam'); ?></h2>     
        <?php        
        if(get_option('datumbox_api_key')=='') {    
        ?>        
        <p><b><?php 
        echo __('In order to use this plugin you must have a Datumbox API key. Sign up for a Free Datumbox Account:'); ?></b></p>        
        <button onclick="window.location='http://www.datumbox.com/users/register/';" class="button button-primary"><?php echo __('Register Now'); 
        ?></button>        
        <br/>       
        <br/>        
        <hr/><br/>    
        <?php        
        }    
        ?>         
        <form method="post" action="options.php">       
         <?php settings_fields( 'machinelearningantispam-settings-group' ); 
        ?>        
        <?php //do_settings( 'machinelearningantispam-settings-group' ); 
        ?>        
        <table class="form-table">            
        <tr valign="top">            
        <th scope="row"><?php echo __('Datumbox API Key'); ?></th>            
        <td><input type="text" name="datumbox_api_key" value="<?php echo get_option('datumbox_api_key'); ?>" /></td>            
        </tr>            <tr valign="top">           
        <th scope="row"><?php echo __('Filter Spam Comments'); ?></th>            
        <td><input type="checkbox" name="machinelearningantispam_filterspam" value="1" <?php 
        echo (get_option('machinelearningantispam_filterspam'))?'checked="checked"':''; ?> /></td>            
        </tr>            
        <tr valign="top">            
        <th scope="row"><?php echo __('Filter Adult Comments'); ?></th>            
        <td><input type="checkbox" name="machinelearningantispam_filteradult" value="1" <?php echo (get_option('machinelearningantispam_filteradult'))?'checked="checked"':''; ?> /></td>            
        </tr>            
        <tr valign="top">            
        <th scope="row"><?php echo __('Filter Negative Comments'); ?></th>            
        <td><input type="checkbox" name="machinelearningantispam_filternegative" value="1" <?php echo (get_option('machinelearningantispam_filternegative'))?'checked="checked"':''; ?> /></td>            
        </tr>        
        </table>                 
        <?php submit_button(); ?>    
        </form>    
        </div>   
<?php } ?>

第三步,我们继续开发我们插件的核心文件。我们创建一个名为machine-learning-antispam.php的文件,并在其中放入每次提交新评论时运行的machinelearningant_pam_check_comment()函数。该功能检查选项并调用DatumboxAPI服务,以验证评论是垃圾邮件,成人邮件还是负面邮件。如果评论被Datumbox服务分类为垃圾邮件或成人评论被标记为“垃圾邮件”,而如果它被证明是负面的,则被标记为“等待”。这里是文件的代码:

<?php
/**
* Plugin Name: Machine Learning Antispam
* Plugin URI: http://www.datumbox.com
* Description: This WordPress Plugin uses Machine Learning to detect spam and adult content comments and mark them as spam. Additionally it allows you to filter negative comments and keep them pending for approval.
* Version: 1.0
* Author: Vasilis Vryniotis
* Author URI: http://www.datumbox.com
* License: GPL2
*/
if (!function_exists('add_action')) {
   die(); //block direct web requests
}
require_once(dirname( __FILE__ ).'/DatumboxAPI.php'); //require the DatumboxAPI client to easily call Datumbox 
APIif (is_admin()) { //if admin include the admin specific 
functionsrequire_once(dirname( __FILE__ ).'/options.php');}
function machinelearningantispam_get_key() {
   return get_option('datumbox_api_key'); //return the api key of datumbox}
function machinelearningantispam_call_datumbox($commentText,$type_of_check) {
   $apiKey=machinelearningantispam_get_key(); //fetch the API key
   if($apiKey==false || $apiKey=='') {
       return true; //don't block the comment if the plugin is not well configured}
   $DatumboxAPI = new DatumboxAPI($apiKey); //initialize DatumboxAPI Client
   if($type_of_check=='spam') {
       $response=$DatumboxAPI->SpamDetection($commentText); //Call Spam Detection service
       if($response=='spam') { //if spam return false 
       return false;}}
   else if($type_of_check=='adult') {
       $response=$DatumboxAPI->AdultContentDetection($commentText); //Call Adult Content Detection service
       if($response=='adult') { //if adult return false
       return false;}}
   else if($type_of_check=='negative') {
       $response=$DatumboxAPI->SentimentAnalysis($commentText); //Call Sentiment Analysis service
       if($response=='negative') { //if negative return false
       return false;}}
   unset($DatumboxAPI);
   return true;}
function machinelearningantispam_check_comment($commentdata) {
   if(get_option('machinelearningantispam_filterspam') && machinelearningantispam_call_datumbox($commentdata['comment_content'],'spam')==false) {//if Spam filtering is on and the Datumbox Service considers it spam then mark it as spam
   add_filter('pre_comment_approved', 'machinelearningantispam_result_spam');}
   else if(get_option('machinelearningantispam_filteradult') && machinelearningantispam_call_datumbox($commentdata['comment_content'],'adult')==false) {//if Adult filtering is on and the Datumbox Service considers it adult then mark it as spam
   add_filter('pre_comment_approved', 'machinelearningantispam_result_spam');}
   else if(get_option('machinelearningantispam_filternegative') && machinelearningantispam_call_datumbox($commentdata['comment_content'],'negative')==false) {//if Negative filtering is on and the Datumbox Service considers it negative then mark it as pending
   add_filter('pre_comment_approved', 'machinelearningantispam_result_pending');}
   return $commentdata;}
function machinelearningantispam_result_spam() {
   return 'spam';}
function machinelearningantispam_result_pending() {
   return 0;}
add_action( 'preprocess_comment' , 'machinelearningantispam_check_comment' );
?>

正如我们上面看到的,插件的2个主要功能是machinelearningantpam_call_datumbox()和machinelearningantpam_check_comment()。第一个函数使用Datumbox PHP API客户端来调用API函数。第二个函数检查插件是否被配置为阻止垃圾邮件,成人和负面评论,如果这些启用它调用的API。如果API将评论标记为不合适,我们将评论的状态更新为垃圾邮件或未决。

就这样!你现在有一个插件,能够打击机器学习的力量垃圾邮件!

你喜欢这篇文章吗?请花一点时间在Twitter上分享。