Skip to content

Instantly share code, notes, and snippets.

@birdinforest
Created April 19, 2016 07:52
Show Gist options
  • Save birdinforest/696617fb35dc2ebcb0d61e9cdeee07f1 to your computer and use it in GitHub Desktop.
Save birdinforest/696617fb35dc2ebcb0d61e9cdeee07f1 to your computer and use it in GitHub Desktop.
Sensitive words check.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
public class SensitiveWordCheck : MonoBehaviour {
#if CHS
private const string FILENAME_SENSITIVE_DB = "SensitiveWordsDatabaseCHS";
#else
private const string FILENAME_SENSITIVE_DB = "SensitiveWordsDatabase";
#endif
public string inputFiled;
private List<string> sensitiveWordsList = new List<string>();
private void Awake() {
LoadSensitiveWordsDatabase();
}
private void CheckSensitiveWorld(string value){
if(IsSensitiveOf(value)) {
// Find sensitive word.
}
}
private void LoadSensitiveWordsDatabase ()
{
if (sensitiveWordsList.Count > 0)
return;
TextAsset csv = Resources.Load (FILENAME_SENSITIVE_DB) as TextAsset;
string[] rows = CSVHelper.SplitCsvToRows (csv, false); // Remove comment lines if there are comments, not encrypted
for (int i = 0; i < rows.Length; i++) {
string word = rows[i];
if (word.Length >= Constants.PLAYER_NAME_LIMIT_MIN || word.Length <= Constants.PLAYER_NAME_LIMIT_MAX && word != string.Empty) // Don't include it if over limit
sensitiveWordsList.Add (word);
}
}
private bool IsSensitiveOf (string name)
{
if (sensitiveWordsList.Count == 0) {
Debug.LogWarning ("UIInputBox:IsSensitiveOf - List sensitiveDatabase is empty.");
return false;
}
int count = 0;
for (int i = 0; i < sensitiveWordsList.Count; i++) {
Regex r = new Regex(sensitiveWordsList[i], RegexOptions.IgnoreCase);
if (sensitiveWordsList[i].Length > name.Length)
continue;
if (r.IsMatch(name))
count++;
}
return count > 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment