【问题标题】:Using function to count banned words in C#使用函数计算C#中的禁用词
【发布时间】:2016-12-27 15:47:41
【问题描述】:

所以我一直在尝试并试图让这个脚本工作,但它的背后一直很痛苦。基本上我想检查句子,看看它是否包含禁用词。如果一个给定的句子包含超过 3 个单词(等于或更多),那么我希望它告诉我,以便我可以发送警报。

到目前为止我所拥有的:

public bool CheckSentence(string Message)
    {
        var count = 0;

        foreach (WordFilter Filter in this._filteredWords.ToList())
        {
            if (Message.Contains(Filter.Word) && Filter.IsSentence)
            {
                count++;
            }
        }

        return count >= 3;

    }

然后我试图抓住它,如果句子确实包含超过 3 个,它会提醒这个人:

if (PlusEnvironment.GetGame().GetChatManager().GetFilter().CheckSentence(Message.ToLower()))
        {
            Session.SendNotification("Something you've said has made our ears tingle! ");
        }

但我觉得我在某个地方出错了,就像它实际上并没有阅读句子并计算使用了多少次禁用词。

字符串是Message,禁用词列表是Filter.Word,因为我不希望所有词(因为我的过滤器用***替换其他词)都包含在句子过滤器中,我'还向表 Filter.IsSentence(1 或 0)添加了一列。这将检查该词是否在过滤器中,以及它是否也包含在“IsSentence”过滤器中。

从我上面的脚本中,我觉得它实际上并没有计算字符串中有多少被禁止的单词?假设我的字符串是:

Join this website today, free credits and much more! www.site.com 在数据库中,我会添加以下单词:

join, website, free, credits, .com, www.

在这些单词中,只有join, website, free and credits 会被检查是否包含在句子中。由于一个句子中有 4 个禁用词(它们也是 IsSentence),因此要运行第二批代码以向它们发送通知。

【问题讨论】:

  • @PeterDennisBartok 如果您阅读它,您会看到它正在请求函数 VIA:Message.ToLower()

标签: c#


【解决方案1】:

所以你有一个单词列表,这些单词被归类为攻击性/坏词。并且您需要计算给定输入句子中的这些单词,到目前为止,您使用了.Contains(),实际上这对于计算出现次数并不有效。我想将句子分成单词并计算每次匹配的单词。根据您的要求修改的方法签名将如下所示:

public bool CheckSentence(string messageText)
{
    var count = 0;
    string[] wordsInMessage = messageText.Split(new char[]{' ',','}, StringSplitOptions.RemoveEmptyEntries);
    foreach (WordFilter Filter in this._filteredWords.ToList())
    {
       count += wordsInMessage.Count(x=> x == Filter.Word);            
    }

    return count >= 3;
}

【讨论】:

  • 请问Session.SendNotification,我相信return count >= 3;的意思是如果count为3或以上就会返回true?
  • 我认为这实际上已经修复了我的代码,我现在将对其进行测试并通知您。
  • @Liam :是的,如果计数大于3CheckSentence 方法将返回 true,否则返回 false。如果需要,您可以尝试并让我知道更多更新
  • 它工作得很好,虽然它只读取间隔的单词,所以这会工作:JOIN GooGLe | × ,,. ¬ hiring HOteL , it is ++ free ,, community
  • 如果我删除HOteL ,之后的空格,它将不起作用
【解决方案2】:

这是我根据你的例子的实现,它给了我正确的输出,你可以检查输出的 SS。

static void Main(string[] args)
        {
            List<string> list = new List<string>();
            list.Add("join");
            list.Add("website");
            list.Add("free");
            list.Add("credit");
            list.Add("www.");
            list.Add(".com");

            var count = 0;

            string Message = "Join this website today, free credits and much more! www.site.com";

            foreach (var Filter in list)
            {
                if (Message.ToLower().Contains(Filter))
                {
                    Console.WriteLine(Filter);
                    count++;
                }
            }

            if (count>=3)
            {
                Console.WriteLine("\n\n\nTRUE");
            }
            else
            {
                Console.WriteLine("\n\n\nFALSE");
            }

            Console.ReadLine();
        }

【讨论】:

  • 这不是我使用这个应用程序的目的。我正在根据我发布的内容实现代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-02
  • 1970-01-01
  • 1970-01-01
  • 2021-11-25
  • 1970-01-01
相关资源
最近更新 更多