| 1 | package edu.ucsb.cs156.happiercows.services; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.happiercows.entities.BannedWord; | |
| 4 | import edu.ucsb.cs156.happiercows.models.CensorResult; | |
| 5 | import edu.ucsb.cs156.happiercows.repositories.BannedWordRepository; | |
| 6 | import org.springframework.beans.factory.annotation.Autowired; | |
| 7 | import org.springframework.stereotype.Service; | |
| 8 | ||
| 9 | import java.util.ArrayList; | |
| 10 | import java.util.Comparator; | |
| 11 | import java.util.List; | |
| 12 | import java.util.Locale; | |
| 13 | import java.util.regex.Matcher; | |
| 14 | import java.util.regex.Pattern; | |
| 15 | import java.util.stream.Collectors; | |
| 16 | import java.util.stream.StreamSupport; | |
| 17 | ||
| 18 | /** Replaces banned words in chat messages with asterisks. */ | |
| 19 | @Service | |
| 20 | public class ChatFilterService { | |
| 21 | ||
| 22 | // A match must not have a letter, combining mark, digit, or underscore directly | |
| 23 | // before or after it, so banning "cow" does not censor "cowboy". | |
| 24 | private static final String NOT_WORD_CHAR_BEFORE = "(?<![\\p{L}\\p{M}\\p{N}_])"; | |
| 25 | private static final String NOT_WORD_CHAR_AFTER = "(?![\\p{L}\\p{M}\\p{N}_])"; | |
| 26 | ||
| 27 | @Autowired | |
| 28 | private BannedWordRepository bannedWordRepository; | |
| 29 | ||
| 30 | /** | |
| 31 | * Lowercases each character on its own with Locale.ROOT. Used both when storing a | |
| 32 | * banned word and when matching a message, so the two always agree. Some characters | |
| 33 | * expand when lowercased (e.g. "İ" becomes "i" plus a combining dot). | |
| 34 | */ | |
| 35 | public static String fold(String text) { | |
| 36 | StringBuilder sb = new StringBuilder(); | |
| 37 |
1
1. fold : removed call to java/util/stream/IntStream::forEach → KILLED |
text.codePoints().forEach(cp -> sb.append(lowerCodePoint(cp))); |
| 38 |
1
1. fold : replaced return value with "" for edu/ucsb/cs156/happiercows/services/ChatFilterService::fold → KILLED |
return sb.toString(); |
| 39 | } | |
| 40 | ||
| 41 | /** How a banned word is stored: trimmed, then folded. */ | |
| 42 | public static String normalize(String word) { | |
| 43 |
1
1. normalize : replaced return value with "" for edu/ucsb/cs156/happiercows/services/ChatFilterService::normalize → KILLED |
return fold(word.trim()); |
| 44 | } | |
| 45 | ||
| 46 | private static String lowerCodePoint(int cp) { | |
| 47 |
1
1. lowerCodePoint : replaced return value with "" for edu/ucsb/cs156/happiercows/services/ChatFilterService::lowerCodePoint → KILLED |
return new String(Character.toChars(cp)).toLowerCase(Locale.ROOT); |
| 48 | } | |
| 49 | ||
| 50 | public CensorResult censor(String message) { | |
| 51 | // Longest first, so "c++" is matched before "c" when both are banned | |
| 52 | List<String> words = StreamSupport | |
| 53 | .stream(bannedWordRepository.findAll().spliterator(), false) | |
| 54 | .map(BannedWord::getWord) | |
| 55 | .map(ChatFilterService::normalize) | |
| 56 |
2
1. lambda$censor$0 : negated conditional → KILLED 2. lambda$censor$0 : replaced boolean return with true for edu/ucsb/cs156/happiercows/services/ChatFilterService::lambda$censor$0 → KILLED |
.filter(w -> !w.isEmpty()) |
| 57 | .distinct() | |
| 58 | .sorted(Comparator.comparingInt(String::length).reversed()) | |
| 59 | .collect(Collectors.toList()); | |
| 60 | ||
| 61 |
1
1. censor : negated conditional → KILLED |
if (words.isEmpty()) { |
| 62 |
1
1. censor : replaced return value with null for edu/ucsb/cs156/happiercows/services/ChatFilterService::censor → KILLED |
return new CensorResult(message, false); |
| 63 | } | |
| 64 | ||
| 65 | // Fold the message, remembering which original characters each folded char came from | |
| 66 | StringBuilder folded = new StringBuilder(); | |
| 67 | List<Integer> originalStart = new ArrayList<>(); | |
| 68 | List<Integer> originalEnd = new ArrayList<>(); | |
| 69 | int i = 0; | |
| 70 |
2
1. censor : negated conditional → KILLED 2. censor : changed conditional boundary → KILLED |
while (i < message.length()) { |
| 71 | int cp = message.codePointAt(i); | |
| 72 |
1
1. censor : Replaced integer addition with subtraction → KILLED |
int next = i + Character.charCount(cp); |
| 73 | String lower = lowerCodePoint(cp); | |
| 74 |
2
1. censor : changed conditional boundary → KILLED 2. censor : negated conditional → KILLED |
for (int k = 0; k < lower.length(); k++) { |
| 75 | originalStart.add(i); | |
| 76 | originalEnd.add(next); | |
| 77 | } | |
| 78 | folded.append(lower); | |
| 79 | i = next; | |
| 80 | } | |
| 81 | ||
| 82 | String alternatives = words.stream() | |
| 83 | .map(Pattern::quote) | |
| 84 | .collect(Collectors.joining("|")); | |
| 85 | Pattern pattern = Pattern.compile( | |
| 86 | NOT_WORD_CHAR_BEFORE + "(?:" + alternatives + ")" + NOT_WORD_CHAR_AFTER); | |
| 87 | ||
| 88 | Matcher matcher = pattern.matcher(folded); | |
| 89 | StringBuilder result = new StringBuilder(); | |
| 90 | int last = 0; | |
| 91 | boolean censored = false; | |
| 92 |
1
1. censor : negated conditional → KILLED |
while (matcher.find()) { |
| 93 | int start = originalStart.get(matcher.start()); | |
| 94 |
1
1. censor : Replaced integer subtraction with addition → KILLED |
int end = originalEnd.get(matcher.end() - 1); |
| 95 | result.append(message, last, start) | |
| 96 | .append("*".repeat(message.codePointCount(start, end))); | |
| 97 | last = end; | |
| 98 | censored = true; | |
| 99 | } | |
| 100 | ||
| 101 |
1
1. censor : negated conditional → KILLED |
if (!censored) { |
| 102 |
1
1. censor : replaced return value with null for edu/ucsb/cs156/happiercows/services/ChatFilterService::censor → KILLED |
return new CensorResult(message, false); |
| 103 | } | |
| 104 | result.append(message.substring(last)); | |
| 105 |
1
1. censor : replaced return value with null for edu/ucsb/cs156/happiercows/services/ChatFilterService::censor → KILLED |
return new CensorResult(result.toString(), true); |
| 106 | } | |
| 107 | } | |
Mutations | ||
| 37 |
1.1 |
|
| 38 |
1.1 |
|
| 43 |
1.1 |
|
| 47 |
1.1 |
|
| 56 |
1.1 2.2 |
|
| 61 |
1.1 |
|
| 62 |
1.1 |
|
| 70 |
1.1 2.2 |
|
| 72 |
1.1 |
|
| 74 |
1.1 2.2 |
|
| 92 |
1.1 |
|
| 94 |
1.1 |
|
| 101 |
1.1 |
|
| 102 |
1.1 |
|
| 105 |
1.1 |