敏感词检查

作者:追风剑情 发布于:2019-11-27 15:46 分类:Unity3d

下载敏感词库
百度网盘 提取码 06o6

示例

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text.RegularExpressions;
  5. using UnityEngine;
  6.  
  7. public static class Badwords
  8. {
  9. private static string[] words;
  10.  
  11. //content: 一行一个单词的敏感词库文件内容
  12. public static void SetContent(string content)
  13. {
  14. words = content.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
  15. Debug.LogFormat("badword count: {0}", words.Length);
  16. }
  17.  
  18. //判断input中是否包含敏感词
  19. public static bool IsContainBadword(string input, out string badword)
  20. {
  21. badword = string.Empty;
  22. string pattern;
  23. bool match = false;
  24. for (int i=0; i<words.Length; i++)
  25. {
  26. pattern = string.Format(".*{0}.*", words[i]);
  27. try
  28. {
  29. match = Regex.IsMatch(input, pattern);
  30. }
  31. catch(ArgumentException ex)
  32. {
  33. Debug.LogErrorFormat("参数异常 input={0}, pattern={1}", input, pattern);
  34. }
  35. if (match)
  36. {
  37. badword = pattern;
  38. return true;
  39. }
  40. }
  41. return false;
  42. }
  43. }


标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号