| 1 | package edu.ucsb.cs156.happiercows.services; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.happiercows.entities.ChatFilterWord; | |
| 4 | import edu.ucsb.cs156.happiercows.repositories.ChatFilterWordRepository; | |
| 5 | import org.springframework.beans.factory.annotation.Autowired; | |
| 6 | import org.springframework.stereotype.Service; | |
| 7 | ||
| 8 | import java.util.Arrays; | |
| 9 | import java.util.Comparator; | |
| 10 | import java.util.List; | |
| 11 | import java.util.regex.Pattern; | |
| 12 | import java.util.stream.Collectors; | |
| 13 | import java.util.stream.StreamSupport; | |
| 14 | ||
| 15 | /** | |
| 16 | * Censors words and phrases on the admin-maintained filter list | |
| 17 | * (see {@link ChatFilterWord}) in chat messages. See issue #19. | |
| 18 | * | |
| 19 | * <ul> | |
| 20 | * <li>Matching ignores case.</li> | |
| 21 | * <li>Only whole words and phrases match: filtering "ass" does not | |
| 22 | * censor "class".</li> | |
| 23 | * <li>Each censored character becomes an asterisk, so the censored | |
| 24 | * text keeps its length ("darn" becomes "****"). Whitespace inside a | |
| 25 | * censored phrase is kept.</li> | |
| 26 | * <li>A phrase matches even if the message has extra whitespace | |
| 27 | * between its words.</li> | |
| 28 | * </ul> | |
| 29 | */ | |
| 30 | @Service | |
| 31 | public class ChatFilterService { | |
| 32 | ||
| 33 | /** Letters, digits, and underscore count as part of a word. */ | |
| 34 | private static final String WORD_CHAR = "[\\p{L}\\p{N}_]"; | |
| 35 | ||
| 36 | @Autowired | |
| 37 | private ChatFilterWordRepository chatFilterWordRepository; | |
| 38 | ||
| 39 | /** | |
| 40 | * Returns the message with every filtered word or phrase replaced by | |
| 41 | * asterisks. A message with no filtered words is returned unchanged. | |
| 42 | */ | |
| 43 | public String censor(String message) { | |
| 44 | // Longest first, so a phrase like "bad word" is matched before | |
| 45 | // a shorter entry like "bad" can claim part of it. | |
| 46 | List<String> words = StreamSupport | |
| 47 | .stream(chatFilterWordRepository.findAll().spliterator(), false) | |
| 48 | .map(ChatFilterWord::getWord) | |
| 49 | .sorted(Comparator.comparingInt(String::length).reversed()) | |
| 50 | .toList(); | |
| 51 | ||
| 52 |
1
1. censor : negated conditional → KILLED |
if (words.isEmpty()) { |
| 53 |
1
1. censor : replaced return value with "" for edu/ucsb/cs156/happiercows/services/ChatFilterService::censor → KILLED |
return message; |
| 54 | } | |
| 55 | ||
| 56 | String alternatives = words.stream() | |
| 57 | .map(ChatFilterService::wordToRegex) | |
| 58 | .collect(Collectors.joining("|")); | |
| 59 | ||
| 60 | Pattern pattern = Pattern.compile( | |
| 61 | "(?<!" + WORD_CHAR + ")(?:" + alternatives + ")(?!" + WORD_CHAR + ")", | |
| 62 | Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE); | |
| 63 | ||
| 64 |
1
1. censor : replaced return value with "" for edu/ucsb/cs156/happiercows/services/ChatFilterService::censor → KILLED |
return pattern.matcher(message) |
| 65 |
1
1. lambda$censor$0 : replaced return value with "" for edu/ucsb/cs156/happiercows/services/ChatFilterService::lambda$censor$0 → KILLED |
.replaceAll(match -> match.group().replaceAll("\\S", "*")); |
| 66 | } | |
| 67 | ||
| 68 | /** | |
| 69 | * Converts a stored word or phrase into a regex that matches it | |
| 70 | * literally, allowing any run of whitespace between its words. | |
| 71 | * Stored words are already trimmed and single-spaced. | |
| 72 | */ | |
| 73 | static String wordToRegex(String word) { | |
| 74 |
1
1. wordToRegex : replaced return value with "" for edu/ucsb/cs156/happiercows/services/ChatFilterService::wordToRegex → KILLED |
return Arrays.stream(word.split(" ")) |
| 75 | .map(Pattern::quote) | |
| 76 | .collect(Collectors.joining("\\s+")); | |
| 77 | } | |
| 78 | } | |
Mutations | ||
| 52 |
1.1 |
|
| 53 |
1.1 |
|
| 64 |
1.1 |
|
| 65 |
1.1 |
|
| 74 |
1.1 |