项目中非法字符检测是必须的,聊天系统不屏蔽各种不文明用语

先说说我的原理吧

1.读取非法字符表,把相同的首字符归类到字典,类似新华字典那样

2.然后把输入的字符串,一个个字符找对应的首字符字典,遍历首字符字典,在当前字符后面截取对应的字符长度得到的字符串然后比较,如果字符串相同则认为有非法字符

下面是测试结果

下面为完整代码,有注释应该比较容易看懂


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;/// <summary>
/// 非法关键词过滤(自动忽略汉字数字字母间的其他字符)
/// </summary>
public class FilterWord
{public FilterWord(){TextAsset asset = Resources.Load("dirtywords") as TextAsset;m_AllFilterWord = asset.text;}private string m_AllFilterWord = string.Empty;/// <summary>/// 词库路径/// </summary>public string AllFilterWord{get { return m_AllFilterWord; }set { m_AllFilterWord = value; }}/// <summary>/// 内存词典/// </summary>private WordGroup[] MEMORYLEXICON = new WordGroup[(int)char.MaxValue];private string sourctText = string.Empty;private bool m_IsInitalize = false;/// <summary>/// 检测源/// </summary>public string SourceText{get { return sourctText; }set { sourctText = value; }}/// <summary>/// 检测源游标/// </summary>int cursor = 0;/// <summary>/// 匹配成功后偏移量/// </summary>int wordlenght = 0;/// <summary>/// 检测词游标/// </summary>int nextCursor = 0;private List<string> illegalWords = new List<string>();/// <summary>/// 检测到的非法词集/// </summary>public List<string> IllegalWords{get { return illegalWords; }}/// <summary>/// 判断是否是中文/// </summary>/// <param name="character"></param>/// <returns></returns>private bool isCHS(char character){//  中文表意字符的范围 4E00-9FA5int charVal = (int)character;return (charVal >= 0x4e00 && charVal <= 0x9fa5);}/// <summary>/// 判断是否是数字/// </summary>/// <param name="character"></param>/// <returns></returns>private bool isNum(char character){int charVal = (int)character;return (charVal >= 48 && charVal <= 57);}/// <summary>/// 判断是否是字母/// </summary>/// <param name="character"></param>/// <returns></returns>private bool isAlphabet(char character){int charVal = (int)character;return ((charVal >= 97 && charVal <= 122) || (charVal >= 65 && charVal <= 90));}/// <summary>/// 转半角小写的函数(DBC case)/// </summary>/// <param name="input">任意字符串</param>/// <returns>半角字符串</returns>///<remarks>///全角空格为12288,半角空格为32///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248///</remarks>private string ToDBC(string input){char[] c = input.ToCharArray();for (int i = 0; i < c.Length; i++){if (c[i] == 12288){c[i] = (char)32;continue;}if (c[i] > 65280 && c[i] < 65375)c[i] = (char)(c[i] - 65248);}return new string(c).ToLower();}/// <summary>/// 加载内存词库/// </summary>public void LoadDictionary(){if (m_IsInitalize){return;}m_IsInitalize = true;List<string> wordList = new List<string>();Array.Clear(MEMORYLEXICON, 0, MEMORYLEXICON.Length);string[] words = AllFilterWord.Split('\n');foreach (string word in words){string str = word.Replace("\r", "");string key = this.ToDBC(str);wordList.Add(key);}Comparison<string> cmp = delegate (string key1, string key2){return key1.CompareTo(key2);};wordList.Sort(cmp);for (int i = wordList.Count - 1; i > 0; i--){if (wordList[i].ToString() == wordList[i - 1].ToString()){wordList.RemoveAt(i);}}foreach (var word in wordList){if (string.IsNullOrEmpty(word)){continue;}WordGroup group = MEMORYLEXICON[word[0]];if (group == null){group = new WordGroup();MEMORYLEXICON[(int)word[0]] = group;}group.Add(word.Substring(1));}}/// <summary>/// 检测/// </summary>/// <param name="blackWord"></param>/// <returns></returns>private bool Check(string blackWord){wordlenght = 0;//检测源下一位游标nextCursor = cursor + 1;bool found = false;string tempStr = ToDBC(sourctText);//遍历词的每一位做匹配for (int i = 0; i < blackWord.Length; i++){//特殊字符偏移游标int offset = 0;if (nextCursor >= tempStr.Length){break;}else{if (i >= blackWord.Length|| nextCursor + offset >= tempStr.Length){found = false;break;}if ((int)blackWord[i] == (int)tempStr[nextCursor + offset]){if (isAlphabet(tempStr[nextCursor + offset])){if(tempStr.Length < blackWord.Length){found = false;break;}if (i >= blackWord.Length - 1){int temp = nextCursor + offset + 1;if(tempStr.Length > temp){if(isAlphabet(tempStr[temp])){found = false;break;}else{found = true;}}else{found = true;}}}else{if (i >= blackWord.Length - 1){found = true;}}}else{found = false;break;}}nextCursor = nextCursor + 1 + offset;wordlenght++;}return found;}/// <summary>/// 查找并替换/// </summary>/// <param name="replaceChar"></param>public string Filter(char replaceChar){cursor = 0;nextCursor = 0;LoadDictionary();if (sourctText != string.Empty){//sourctText = sourctText.Replace("\n", "");//sourctText = sourctText.Trim();char[] tempString = sourctText.ToCharArray();for (int i = 0; i < SourceText.Length; i++){//查询以该字为首字符的词组WordGroup group = MEMORYLEXICON[(int)ToDBC(SourceText)[i]];if (group != null){for (int z = 0; z < group.Count(); z++){string word = group.GetWord(z);if (word.Length == 0 || Check(word)){string blackword = string.Empty;for (int pos = 0; pos < wordlenght + 1; pos++){blackword += tempString[pos + cursor].ToString();tempString[pos + cursor] = replaceChar;}illegalWords.Add(blackword);cursor = cursor + wordlenght;i = i + wordlenght;}}}cursor++;}return new string(tempString);}else{return string.Empty;}}
}/// <summary>
/// 具有相同首字符的词组集合
/// </summary>
class WordGroup
{/// <summary>/// 集合/// </summary>private List<string> groupList;public WordGroup(){groupList = new List<string>();}/// <summary>/// 添加词/// </summary>/// <param name="word"></param>public void Add(string word){groupList.Add(word);}/// <summary>/// 获取总数/// </summary>/// <returns></returns>public int Count(){return groupList.Count;}/// <summary>/// 根据下标获取词/// </summary>/// <param name="index"></param>/// <returns></returns>public string GetWord(int index){return groupList[index];}
}

下面是抽出一个统一方法来调用检测

主要两个方法

1.检测是否有非法字符,返回bool

2.把非法字符转成*号,返回string

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class SystemUtil
{/// <summary>/// 判断是否非法字符/// </summary>/// <param name="str"></param>/// <returns></returns>public static bool IsInvaild(string str){string source = Filter(str);return str != source;}/// <summary>/// 把非法字符变成*号/// </summary>/// <param name="str"></param>/// <returns></returns>public static string Filter(string str){filterWord.SourceText = str;return filterWord.Filter('*');}public static FilterWord filterWord{get{if (null == m_FilterWord){m_FilterWord = new FilterWord();}return m_FilterWord;}}private static FilterWord m_FilterWord;
}

下面是工程下载地址

https://gitee.com/PieKen/FilterWord

unity c#非法字符(脏词)检测相关推荐

  1. PHP敏感词匹配算法,敏感词检测算法小结

    序 本文简单介绍下敏感词或者脏词检测算法. 经典AC算法 经典的AC算法由三部分构成,goto表,fail表和output表,共包含四种具体的算法,分别是计算三张查找表的算法以及AC算法本身. got ...

  2. c语言遇到非法字符,98行的四则计算器.(支持括号)加入了非法字符的检测

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 #include #include #include double s2n(char **tem)//字符串转为数字 { double tem_s=ato ...

  3. php检测非法字符的一种方法

    //php检测非法字符的一种方法 $illegal_character="#['!`~\/\\\%^&*()+=\$\#:;<>\]\[{}]#"; $tnam ...

  4. c语言非法字符有哪些,98行的四则计算器.(支持括号)加入了非法字符的检测

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 #include #include #include double s2n(char **tem)//字符串转为数字 { double tem_s=ato ...

  5. c语言非法字符判别,98行的四则计算器.(支持括号)加入了非法字符的检测

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 #include #include #include double s2n(char **tem)//字符串转为数字 { double tem_s=ato ...

  6. c语言非法字符空格,98行的四则计算器.(支持括号)加入了非法字符的检测

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 #include #include #include double s2n(char **tem)//字符串转为数字 { double tem_s=ato ...

  7. windows文件名非法字符过滤检测-正则表达式

    过滤文件名非法字符 windows现在已知的文件名非法字符有 \ / : * ? " < > | var reg = new RegExp('[\\\\/:*?\"&l ...

  8. java非法字符检测_Java Web 一些特殊字符的过滤(appscan检查的安全问题)

    适用于出现以下问题: 1.SQL盲注 2.存储的跨站点脚本编制 或 跨站点脚本编制 import java.io.IOException; import java.util.Enumeration; ...

  9. 写一个高性能的敏感词检测组件

    最近写了一个高性能的敏感词检测组件[ToolGood.Words]. 一.高性能,它的效率到底有多快? 如果将正则表达式的算法效率设为1,高性能可达到正则表达式的1.5万倍. 二.选一个巧妙的算法: ...

最新文章

  1. DNS隧道之DNS2TCP使用心得教程——是可以用来穿透qiang的,ubuntu下直接apt install dns2tcp...
  2. ST17H26对接RC522读IC卡
  3. 高性能MySQL——查询性能优化
  4. 《微软开源跨平台移动开发实践》团购通知
  5. java 无法找到ant_Java-Ant需要tools.jar并且无法找到我
  6. ERROR manager.SqlManager: Error reading from database: java.sql.SQLException: Streaming result set
  7. 设计模式—结构型模式概述(思维导图)
  8. linux ln链接命令
  9. 数据存储与容灾(第2版)主编 鲁先志 武春岭综合训练答案
  10. BM3D 算法原理详细解析 按过程步骤讲解(附C++实现代码)
  11. Idea 破解版下载指南
  12. html5 新增input类型,html5新增的input类型
  13. python牛顿法算立方根_Exercise 1.8 牛顿法求立方根
  14. css3 移动端video视频全屏,横屏展示,适配微信/打包成app
  15. 字节Scala面试题(2) --- 伴生对象
  16. vue-element-admin 花裤衩 模板 ,中文版,运行报错解决方案
  17. 生物信息-related
  18. 来到fsb的第24天
  19. 代刷网html统计代码,【QQ代刷网】前台代码大全
  20. Java游戏编程---第一章 2D图形和动画

热门文章

  1. 部署IIS PHP SQL server环境
  2. Google/Baidu的搜索技巧
  3. python实战-京东签到及登入滑块验证
  4. 6S大气校正模型说明
  5. matlab进行系统校正,MATLAB校正系统程序
  6. 2017年光伏行业可以预见到的三个分水岭
  7. Android 数据库框架ormlite 使用
  8. 诸神之眼-nmap详细使用介绍3!Nmap高阶操作(防火墙规避和脚本使用)! (*╹▽╹*) 信息收集 ~ 其四
  9. View绘制之ScrollBar绘制
  10. 深度测试和Z缓存专场