.net 如何在C#中添加脏话过滤器

yws3nbqq  于 2023-01-18  发布在  .NET
关注(0)|答案(2)|浏览(119)

我有一个评论列表。客户端要求我们在评论发布之前使用一个亵渎语言过滤器来检查评论。我有一个亵渎语言过滤器。cs类。唯一的问题是我不知道如何集成这两个类。
简单地说,我如何让评论在发布之前进入过滤器。
先谢了!
下面是形成注解列表的代码:

public void ProcessRequest (HttpContext context) 
{

    // ****************************************
    if (context.Request["postform"] == "1")
    {

        videomessage myVideoMessage = new videomessage();

        myVideoMessage.video_id = context.Request["video_id"];
        myVideoMessage.first_name_submitter = context.Request["first_name_submitter"];
        myVideoMessage.last_initial_submitter = context.Request["last_initial_submitter"];
        myVideoMessage.message = context.Request["message"];
        myVideoMessage.status = "0";

        myVideoMessage.Save();
    }
    // ****************************************

    // ****************************************
    StringBuilder myStringBuilder = new StringBuilder();

    // PULL VIDEOMESSAGES FOR VIDEO_ID
    videomessage[] myCommentsList = new videomessage().Listing("video_id", context.Request["video_id"], "entry_date" , "DESC");

    // FORM COMMENTS IF MORE THAN ONE COMMENT EXISTS
    foreach (videomessage tmpMessage in myCommentsList)
    {
        if (tmpMessage.status == "0" || tmpMessage.status == "1")
        {
            myStringBuilder.Append("<div class=\"comment_box\">");
            myStringBuilder.Append("<p class=\"comment_date\">");
            myStringBuilder.Append(Utility.FormatShortDate(tmpMessage.entry_date) + " " + tmpMessage.first_name_submitter + " " + tmpMessage.last_initial_submitter + "." + "</p>");

            if (!String.IsNullOrEmpty(tmpMessage.message))
            {
                myStringBuilder.Append("<p>" + tmpMessage.message + "</p>");
                myStringBuilder.Append("</div>");
            }
        }
    }
    string return_str = myStringBuilder.ToString();

    // IF NO COMMENTS RETURN THIS
    if( String.IsNullOrEmpty(return_str) )  return_str = "<p>No comments currently exist for this video.</p>";
    // ****************************************

    // RETURN STRING        
    context.Response.ContentType = "text/plain";
    context.Response.Write(return_str);
}

下面是过滤器:

public class ProfanityFilter
{        
    // METHOD: containsProfanity
    public bool containsProfanity(string checkStr)
    {
        bool badwordpresent = false;

        string[] inStrArray = checkStr.Split(new char[] { ' ' });

        string[] words = this.profanityArray();

        // LOOP THROUGH WORDS IN MESSAGE
        for (int x = 0; x < inStrArray.Length; x++)
        {
            // LOOP THROUGH PROFANITY WORDS
            for (int i = 0; i < words.Length; i++)
            {
                // IF WORD IS PROFANITY, SET FLAG AND BREAK OUT OF LOOP
                //if (inStrArray[x].toString().toLowerCase().equals(words[i]))
                if( inStrArray[x].ToLower() == words[i].ToLower() )
                {
                    badwordpresent = true;
                    break;
                }
            }
            // IF FLAG IS SET, BREAK OUT OF OUTER LOOP
            if (badwordpresent == true) break;
        }

        return badwordpresent;
    }
    // ************************************************************************



    // ************************************************************************
    // METHOD: profanityArray()
    // METHOD OF PROFANITY WORDS
    private string[] profanityArray()
    {

        // THESE WERE UPDATED TO USE THE SAME BADWORDS FROM FACESOFMBCFBAPP
        string[] words = {//words in the array}
}
return words;
}
i2byvkas

i2byvkas1#

检查以下问题:

我建议阅读最后一个问题的公认答案和CodingHorror博客文章。虽然OP要求PHP解决方案,但在阅读这些内容后,您可能需要考虑替代方法。

rt4zxlrg

rt4zxlrg2#

string return_str = myStringBuilder.ToString();之后添加:

ProfanityFilter filter = new ProfanityFilter();

if (filter.ContainsProfanity(return_str)
{
    // Do action when message contains profanity
}
else
{
    // Do action when message does not contains profanity     
}

还要记住,你的代码没有考虑到换行符(我不知道注解是否不能有换行符,只是以防万一)。

相关问题