前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何构建智能反垃圾邮件的WordPress插件

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

作者头像
後來
发布2018-01-29 18:47:55
1.2K0
发布2018-01-29 18:47:55
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文件的代码:

代码语言:js
复制
<?php
if (!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服务分类为垃圾或成人类,则被标记为“垃圾邮件”;如果它被证明是负面的,则被标记为“等待”。一下是此文件的代码:

代码语言:js
复制

<?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 API
 
if (is_admin()) { //if admin include the admin specific functions
    require_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上分享。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 构建WordPress插件的目的
  • 安装插件
  • 构建机器学习反垃圾邮件插件
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档