Overview
com.111percent.percent-banword
작명, 채팅 등의 상황에서 금지단어 처리를 위한 기능을 제공합니다.
내부적으로 Aho–Corasick 알고리즘을 사용합니다.
초기화
금지단어 처리를 위해 BanWordProcessor
객체를 생성해야 합니다.
방법1. 공용 단어 목록 사용
111% 공용 단어 목록을 다운로드하여 초기화시키는 기본 사용법입니다.
var banWordProcessor = await new BanWordProcessor.Builder() .UseCommonWords(localCacheDurationHour: 24) .BuildAsync();
방법2. 유저 커스텀 단어 목록 사용
임의의 단어들로 초기화 시키는 방식입니다.
List<string> deniedWords /*= load...*/; List<string> allowedWords /*= load...*/; var banWordProcessor = await new BanWordProcessor.Builder() // 111% 공용 단어 목록에 추가하려면 이 주석을 해제하세요 //.UseCommonWords(localCacheDurationHour: 24) .AddDeniedWords(deniedWords) .AddAllowedWords(allowedWords) .BuildAsync();
방법3. 바이너리 데이터로 serialize된 유저 커스텀 단어 목록 사용
- 단어의 수가 수만개 이상으로 많아지면 초기화에 오랜 시간이 걸립니다.
- 단어들을 바이너리 데이터로 serialize 하여 배포하면 초기화 시간을 절약 가능합니다.
- serialize
// banWordProcessor를 원하는 단어들로 초기화 var serializedWords = BanWordProcessor.BinaryConvert.Serialize(banWordProcessor); // TODO: serializedWords를 파일로 저장하여 배포
- deserialize
// TODO: serializedWords 로드 var banWordProcessor = await new BanWordProcessor.Builder() .UseCustomWords(serializedWords) .BuildAsync();
사용법
HasBanWord
: 문자열에 금지단어가 포함되었는지 여부만 간단히 확인bool hasBanWord = banWordProcessor.HasBanWord(text);
ExtractBanWords
: 문자열 내의 금지단어와 그 위치(index)를 확인foreach (var detected in banWordProcessor.ExtractBanWords(text)) Console.WriteLine($"[{detected.Index}] {detected.Word}");
ReplaceBanWord
: 금지단어를 다른 문자열로 치환// text의 금지단어를 동일한 글자 수의 * 으로 치환하는 예시입니다. var replacedText = banWordProcessor.ReplaceBanWord( text, word => new string('*', word.Length));
허용단어
- 금지단어가 포함되지만 비속어로 취급하지 않도록 하기 위한 화이트리스트 단어 입니다.
- 예시: 금지단어
조또
, 허용단어리조또
- 예시: 금지단어
- 111% 공용 단어 목록에 포함된 특정 단어를 허용시키기 위한 기능으로도 활용 가능합니다.
Editor Tool
유니티 에디터에서 Percent
- BanWord
메뉴로 기능 테스트를 해볼 수 있습니다.
Perbase 서버에서 참조하는 방법
Server/Packages/Perbase.V2.PreBuildJob/packages.json
파일의Projects
에 다음과 같이com.111percent.percent-banword
패키지를 추가합니다. (참고)"com.111percent.percent-banword": { "Package": "com.111percent.percent-banword", "Dependencies": "auto" },
- 사용자 csproj에
com.111percent.percent-banword.csproj
프로젝트 참조를 추가합니다.<ProjectReference Include="..\Packages\com.111percent.percent-banword\com.111percent.percent-banword.csproj" />