| | 1 | | using System.Collections.Generic; |
| | 2 | | using System.Text; |
| | 3 | | using System.Text.RegularExpressions; |
| | 4 | | using Cysharp.Threading.Tasks; |
| | 5 | |
|
| | 6 | | public class RegexProfanityFilter : IProfanityFilter |
| | 7 | | { |
| | 8 | | private readonly Regex regex; |
| | 9 | |
|
| 63 | 10 | | public RegexProfanityFilter(IProfanityWordProvider wordProvider) |
| | 11 | | { |
| 63 | 12 | | var explicitWords = ToRegex(wordProvider.GetExplicitWords()); |
| 63 | 13 | | var nonExplicitWords = ToRegex(wordProvider.GetNonExplicitWords()); |
| 63 | 14 | | regex = new Regex(@$"\b({explicitWords})\b|({nonExplicitWords})", RegexOptions.IgnoreCase); |
| 63 | 15 | | } |
| | 16 | |
|
| | 17 | | public async UniTask<string> Filter(string message) |
| | 18 | | { |
| 79 | 19 | | if (string.IsNullOrEmpty(message)) return message; |
| 73 | 20 | | return regex.Replace(message, |
| 12 | 21 | | match => new StringBuilder().Append('*', match.Value.Length).ToString()); |
| 76 | 22 | | } |
| | 23 | |
|
| 126 | 24 | | private string ToRegex(IEnumerable<string> words) => string.Join("|", words); |
| | 25 | | } |