chatfilter: add config option for stripping accents

This commit is contained in:
Adam
2022-05-06 14:50:10 -04:00
parent 162a2f78cb
commit b5515075ef
3 changed files with 22 additions and 4 deletions

View File

@@ -164,4 +164,15 @@ public interface ChatFilterConfig extends Config
{
return 0;
}
@ConfigItem(
keyName = "stripAccents",
name = "Strip accents",
description = "Remove accents before applying filters",
position = 13
)
default boolean stripAccents()
{
return false;
}
}

View File

@@ -321,7 +321,7 @@ public class ChatFilterPlugin extends Plugin
{
String strippedMessage = jagexPrintableCharMatcher.retainFrom(message)
.replace('\u00A0', ' ');
String strippedAccents = StringUtils.stripAccents(strippedMessage);
String strippedAccents = stripAccents(strippedMessage);
assert strippedMessage.length() == strippedAccents.length();
if (username != null && shouldFilterByName(username))
@@ -377,23 +377,28 @@ public class ChatFilterPlugin extends Plugin
filteredNamePatterns.clear();
Text.fromCSV(config.filteredWords()).stream()
.map(StringUtils::stripAccents)
.map(this::stripAccents)
.map(s -> Pattern.compile(Pattern.quote(s), Pattern.CASE_INSENSITIVE))
.forEach(filteredPatterns::add);
NEWLINE_SPLITTER.splitToList(config.filteredRegex()).stream()
.map(StringUtils::stripAccents)
.map(this::stripAccents)
.map(ChatFilterPlugin::compilePattern)
.filter(Objects::nonNull)
.forEach(filteredPatterns::add);
NEWLINE_SPLITTER.splitToList(config.filteredNames()).stream()
.map(StringUtils::stripAccents)
.map(this::stripAccents)
.map(ChatFilterPlugin::compilePattern)
.filter(Objects::nonNull)
.forEach(filteredNamePatterns::add);
}
private String stripAccents(String input)
{
return config.stripAccents() ? StringUtils.stripAccents(input) : input;
}
private static Pattern compilePattern(String pattern)
{
try

View File

@@ -191,6 +191,7 @@ public class ChatFilterPluginTest
{
when(chatFilterConfig.filterType()).thenReturn(ChatFilterType.CENSOR_WORDS);
when(chatFilterConfig.filteredWords()).thenReturn("filterme");
when(chatFilterConfig.stripAccents()).thenReturn(true);
chatFilterPlugin.updateFilteredPatterns();
assertEquals("plëäsë ******** plügïn", chatFilterPlugin.censorMessage("Blue", "plëäsë fïltërmë plügïn"));
@@ -211,6 +212,7 @@ public class ChatFilterPluginTest
{
when(chatFilterConfig.filterType()).thenReturn(ChatFilterType.CENSOR_WORDS);
when(chatFilterConfig.filteredWords()).thenReturn("plëäsë, filterme");
when(chatFilterConfig.stripAccents()).thenReturn(true);
chatFilterPlugin.updateFilteredPatterns();
assertEquals("****** ******** plügïn", chatFilterPlugin.censorMessage("Blue", "plëäsë fïltërmë plügïn"));