| 1 | package edu.ucsb.cs156.happiercows.services; | |
| 2 | ||
| 3 | import java.util.ArrayList; | |
| 4 | import java.util.Comparator; | |
| 5 | import java.util.List; | |
| 6 | import java.util.regex.Matcher; | |
| 7 | import java.util.regex.Pattern; | |
| 8 | ||
| 9 | import org.springframework.beans.factory.annotation.Autowired; | |
| 10 | import org.springframework.stereotype.Service; | |
| 11 | ||
| 12 | import edu.ucsb.cs156.happiercows.entities.BannedWord; | |
| 13 | import edu.ucsb.cs156.happiercows.repositories.BannedWordRepository; | |
| 14 | ||
| 15 | @Service("ProfanityFilterService") | |
| 16 | public class ProfanityFilterService { | |
| 17 | ||
| 18 | @Autowired | |
| 19 | BannedWordRepository bannedWordRepository; | |
| 20 | ||
| 21 | public String censor(String message) { | |
| 22 |
2
1. censor : negated conditional → KILLED 2. censor : negated conditional → KILLED |
if (message == null || message.isEmpty()) { |
| 23 |
1
1. censor : replaced return value with "" for edu/ucsb/cs156/happiercows/services/ProfanityFilterService::censor → KILLED |
return message; |
| 24 | } | |
| 25 | ||
| 26 | List<String> bannedWords = loadBannedWords(); | |
| 27 |
1
1. censor : negated conditional → KILLED |
if (bannedWords.isEmpty()) { |
| 28 |
1
1. censor : replaced return value with "" for edu/ucsb/cs156/happiercows/services/ProfanityFilterService::censor → KILLED |
return message; |
| 29 | } | |
| 30 | ||
| 31 | String censored = message; | |
| 32 | for (String bannedWord : bannedWords) { | |
| 33 | censored = mask(censored, bannedWord); | |
| 34 | } | |
| 35 | ||
| 36 |
1
1. censor : replaced return value with "" for edu/ucsb/cs156/happiercows/services/ProfanityFilterService::censor → KILLED |
return censored; |
| 37 | } | |
| 38 | ||
| 39 | // Banned words are stored lowercase (see BannedWord) and matching is case | |
| 40 | // insensitive. Longer entries are applied first so that a banned phrase | |
| 41 | // such as "darn it" is masked before a shorter banned word inside it. | |
| 42 | private List<String> loadBannedWords() { | |
| 43 | List<String> bannedWords = new ArrayList<>(); | |
| 44 | for (BannedWord bannedWord : bannedWordRepository.findAll()) { | |
| 45 | bannedWords.add(bannedWord.getWord()); | |
| 46 | } | |
| 47 |
1
1. loadBannedWords : removed call to java/util/List::sort → KILLED |
bannedWords.sort(Comparator.comparingInt(String::length).reversed()); |
| 48 |
1
1. loadBannedWords : replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/services/ProfanityFilterService::loadBannedWords → KILLED |
return bannedWords; |
| 49 | } | |
| 50 | ||
| 51 | // Only the characters belonging to a banned word are replaced with '*'. | |
| 52 | // Surrounding words and punctuation are untouched, and any whitespace | |
| 53 | // inside a banned phrase keeps its original position. | |
| 54 | private String mask(String message, String bannedWord) { | |
| 55 | Pattern pattern = Pattern.compile( | |
| 56 | "(?<!\\w)" + Pattern.quote(bannedWord) + "(?!\\w)", | |
| 57 | Pattern.CASE_INSENSITIVE); | |
| 58 | ||
| 59 | Matcher matcher = pattern.matcher(message); | |
| 60 | StringBuilder censored = new StringBuilder(message); | |
| 61 |
1
1. mask : negated conditional → KILLED |
while (matcher.find()) { |
| 62 |
2
1. mask : negated conditional → KILLED 2. mask : changed conditional boundary → KILLED |
for (int i = matcher.start(); i < matcher.end(); i++) { |
| 63 | char c = message.charAt(i); | |
| 64 |
2
1. mask : negated conditional → KILLED 2. mask : removed call to java/lang/StringBuilder::setCharAt → KILLED |
censored.setCharAt(i, Character.isWhitespace(c) ? c : '*'); |
| 65 | } | |
| 66 | } | |
| 67 | ||
| 68 |
1
1. mask : replaced return value with "" for edu/ucsb/cs156/happiercows/services/ProfanityFilterService::mask → KILLED |
return censored.toString(); |
| 69 | } | |
| 70 | } | |
Mutations | ||
| 22 |
1.1 2.2 |
|
| 23 |
1.1 |
|
| 27 |
1.1 |
|
| 28 |
1.1 |
|
| 36 |
1.1 |
|
| 47 |
1.1 |
|
| 48 |
1.1 |
|
| 61 |
1.1 |
|
| 62 |
1.1 2.2 |
|
| 64 |
1.1 2.2 |
|
| 68 |
1.1 |