小言_互联网的博客

Unity游戏中c#写的敏感词汇检测

401人阅读  评论(0)

本篇敏感词词汇表数据是excel导入为xml,并通过AB加载的(当然可要通过其他方式我前面介绍了 json,scriptsObject方式,不了解的小伙伴可要去看看;), 加载后加入后方一个list里面 ,通过list来建立一个二叉树结构的词库(检索敏感词),创建一个被外部调用的检索的方法。

代码如下:

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System;


public class SensitiveCheck : MonoBehaviour
{
    //InputField input;
    //所有树桩结构

    public Dictionary<string, Node> rootTree = new Dictionary<string, Node>();
    public Dictionary<string, List<string>> sameValueList = new Dictionary<string, List<string>>();
    //key值集合
    public List<string> keyList = new List<string>();
    public string checkString;
    //所有excel表词汇集合
    List<string> dll;
    Stream stream;
    // Use this for initialization
    void Start()
    {
        #region//获取敏感词汇表的数据
        Dictionary<int, System.Xml.XmlNode> NodeXML = GameManager.Instance.allXmlData.GetDataByName("words_shield");
        dll = new List<string>();

        for (int i = 0; i < NodeXML.Count; i++)
        {
            if(NodeXML.ContainsKey(i))
            dll.Add(NodeXML[i].Attributes["chat"].Value);
        }
        #endregion
        System.DateTime bigin = System.DateTime.Now;
        CreateSameCollection(dll);
        //Debug.Log(sameValueList);
        CreateTreeContract();
       // Debug.Log(rootTree);
       // Debug.Log("<color=red>"+"建树结构耗时"+(System.DateTime.Now - bigin).TotalSeconds.ToString());
        // checkString = "法轮功天变态";

    }
    //获取首字母相同的集合
    public void CreateSameCollection(List<string> dll)
    {
        for (int i = 0; i < dll.Count; i++)
        {
            //Debug.Log(i+ dll[i]);
            dll[i].Trim();
            if (string.IsNullOrEmpty(dll[i]))
                continue;
            string key = dll[i].Substring(0, 1);
            //如果不包含当前key值
            if (!sameValueList.ContainsKey(key))
            {
                List<string> list = new List<string>();
                list.Add(dll[i]);
                sameValueList.Add(key, list);
                keyList.Add(key);
            }
            else
            {
                sameValueList[key].Add(dll[i]);
            }
            //dll[i].StartsWith();
        }


    }
    //创建树
    public void CreateTreeContract()
    {
        string collect = null;
        for (int i = 0; i < keyList.Count; i++)
        {
            //创建跟节点
            Node node = new Node();
            node.value = keyList[i];
            rootTree.Add(keyList[i], node);

            //同key的集合
            List<string> collectionsValue = sameValueList[keyList[i]];
            for (int j = 0; j < collectionsValue.Count; j++)
            {
                collect = collectionsValue[j].Remove(0,1);
                AddChrenNode(node, collect);


            }
        }
    }
    //加入子节点
    public void AddChrenNode(Node parentNode, string collections)
    {
       // Debug.Log(collections);
        for (int i = 0; i < collections.Length; i++)
        {
            //if (collections == "6")
            //    Debug.Log(0);
            string key = collections[i].ToString();
            //if (key == parentNode.value)
            //    continue;
            if (!parentNode.chilrenNode.ContainsKey(key))
            {
                Node nodeChild = new Node();
                nodeChild.value = key;
                nodeChild.IsEnd = false;
                if (i < collections.Length - 1)
                {
                    parentNode.chilrenNode.Add(key, nodeChild);
                    string remainingCollection = collections.Remove(0,1);
                    AddChrenNode(nodeChild, remainingCollection);
                }
                else
                {
                    nodeChild.IsEnd = true;
                    parentNode.chilrenNode.Add(key, nodeChild);
                }
            }
            else
            {
                string remainingCollection = collections.Remove(0,1);
                AddChrenNode(parentNode.chilrenNode[key], remainingCollection);
            }
            break;
        }

    }

    //检测输入是否有敏感词
    public string CheckedInput(string inputString)
    {
		//Debug.Log ("输入是否有敏感词");
        string returnString = inputString;
        for (int i = 0; i < inputString.Length; i++)
        {
            try
            {
				//Debug.Log("<Color=Red>输入的词组</Color>"+inputString);
                string key = inputString[i].ToString();
                if (rootTree.ContainsKey(key))
                {
					//Debug.Log("<Color=Red>包含keY</Color>"+key);
                    string stringValue = inputString.Substring(i + 1);
					string checkString = null;
					if(string.IsNullOrEmpty(stringValue))
						checkString = CheckCharIndLL(rootTree[key], key);
					else
                    //检测跟节点子集是否包含
                     checkString = CheckCharIndLL(rootTree[key], stringValue);
                    checkString += key;
					//Debug.Log("checkString--"+checkString);
                    if (!checkString.Contains("tian"))
                    {
                        //重新设置i值跳转到没检测的词位置
                        i += checkString.Length;
                        for (int j = 0; j < checkString.Length; j++)
                        {
                            string s = returnString.Replace(checkString[j], '*');
                            returnString = s;
                        }
                    }
                    //else
                    //{
                    //    continue;
                    //}
                }
                //return returnString;
            }
            catch
            {

                Debug.Log("当前" + inputString[i].ToString() + "不在集合内");
                // return inputString ;
            }

        }
        return returnString;
    }
    //检测字符是否在集合类
    public string CheckCharIndLL(Node parentNode, string charString)
    {
        try
        {
            string returnString = null;
			//Debug.Log("<Color=Red>检测的字符charString</Color>"+charString);
            //if( parentNode.chilrenNode.ContainsKey(charString))
            for (int i = 0; i < charString.Length; i++)
            {

                Node node = parentNode.chilrenNode[charString[i].ToString()];
				Debug.Log("<Color=Red>节点node存在不</Color>"+node);
                if (node != null)
                {
                    returnString = charString[i].ToString();
					Debug.Log("<Color=Red>输入的词是否是词尾</Color>"+charString[i].ToString()+node.IsEnd);
                    //returnString.Replace(returnString[i], '#');
					if (!node.IsEnd)
                    {
                        string compareString = charString.Substring(i + 1);
                        compareString = CheckCharIndLL(node, compareString);
                        if (string.IsNullOrEmpty(compareString))
                            return "tian";
                        else
                        returnString += compareString;
                        
                    }
                    else
                    {
                        return returnString;

                    }
                }
                break;
            }
            return returnString;

        }
        catch
        {
           // Debug.Log("树中并没用该值");
            return "tian";
        }

    }
  
}

public class Node
{
    //是否是结束节点
    public bool IsEnd;
    //当前值
    public string value;
    //子节点
    public Dictionary<string, Node> chilrenNode;
    //父节点
    public Node ParentNode;
    public Node()
    {
        chilrenNode = new Dictionary<string, Node>();

    }
}

 


转载:https://blog.csdn.net/s_xiajunjun1986/article/details/101549277
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场