ChatFilterService.java

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
Location : fold
Killed by : edu.ucsb.cs156.happiercows.services.ChatFilterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.services.ChatFilterServiceTests]/[method:normalize_trims_and_lowercases_with_expansion()]
removed call to java/util/stream/IntStream::forEach → KILLED

38

1.1
Location : fold
Killed by : edu.ucsb.cs156.happiercows.services.ChatFilterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.services.ChatFilterServiceTests]/[method:normalize_trims_and_lowercases_with_expansion()]
replaced return value with "" for edu/ucsb/cs156/happiercows/services/ChatFilterService::fold → KILLED

43

1.1
Location : normalize
Killed by : edu.ucsb.cs156.happiercows.services.ChatFilterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.services.ChatFilterServiceTests]/[method:normalize_trims_and_lowercases_with_expansion()]
replaced return value with "" for edu/ucsb/cs156/happiercows/services/ChatFilterService::normalize → KILLED

47

1.1
Location : lowerCodePoint
Killed by : edu.ucsb.cs156.happiercows.services.ChatFilterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.services.ChatFilterServiceTests]/[method:normalize_trims_and_lowercases_with_expansion()]
replaced return value with "" for edu/ucsb/cs156/happiercows/services/ChatFilterService::lowerCodePoint → KILLED

56

1.1
Location : lambda$censor$0
Killed by : edu.ucsb.cs156.happiercows.services.ChatFilterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.services.ChatFilterServiceTests]/[method:only_blank_stored_words_censor_nothing()]
negated conditional → KILLED

2.2
Location : lambda$censor$0
Killed by : edu.ucsb.cs156.happiercows.services.ChatFilterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.services.ChatFilterServiceTests]/[method:only_blank_stored_words_censor_nothing()]
replaced boolean return with true for edu/ucsb/cs156/happiercows/services/ChatFilterService::lambda$censor$0 → KILLED

61

1.1
Location : censor
Killed by : edu.ucsb.cs156.happiercows.services.ChatFilterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.services.ChatFilterServiceTests]/[method:only_blank_stored_words_censor_nothing()]
negated conditional → KILLED

62

1.1
Location : censor
Killed by : edu.ucsb.cs156.happiercows.services.ChatFilterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.services.ChatFilterServiceTests]/[method:no_banned_words_leaves_message_unchanged()]
replaced return value with null for edu/ucsb/cs156/happiercows/services/ChatFilterService::censor → KILLED

70

1.1
Location : censor
Killed by : edu.ucsb.cs156.happiercows.services.ChatFilterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.services.ChatFilterServiceTests]/[method:banned_word_next_to_punctuation_is_censored()]
negated conditional → KILLED

2.2
Location : censor
Killed by : edu.ucsb.cs156.happiercows.services.ChatFilterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.services.ChatFilterServiceTests]/[method:shorter_word_does_not_match_inside_expanded_character()]
changed conditional boundary → KILLED

72

1.1
Location : censor
Killed by : edu.ucsb.cs156.happiercows.services.ChatFilterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.services.ChatFilterServiceTests]/[method:shorter_word_does_not_match_inside_expanded_character()]
Replaced integer addition with subtraction → KILLED

74

1.1
Location : censor
Killed by : edu.ucsb.cs156.happiercows.services.ChatFilterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.services.ChatFilterServiceTests]/[method:banned_word_next_to_punctuation_is_censored()]
changed conditional boundary → KILLED

2.2
Location : censor
Killed by : edu.ucsb.cs156.happiercows.services.ChatFilterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.services.ChatFilterServiceTests]/[method:banned_word_next_to_punctuation_is_censored()]
negated conditional → KILLED

92

1.1
Location : censor
Killed by : edu.ucsb.cs156.happiercows.services.ChatFilterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.services.ChatFilterServiceTests]/[method:shorter_word_does_not_match_inside_expanded_character()]
negated conditional → KILLED

94

1.1
Location : censor
Killed by : edu.ucsb.cs156.happiercows.services.ChatFilterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.services.ChatFilterServiceTests]/[method:banned_word_next_to_punctuation_is_censored()]
Replaced integer subtraction with addition → KILLED

101

1.1
Location : censor
Killed by : edu.ucsb.cs156.happiercows.services.ChatFilterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.services.ChatFilterServiceTests]/[method:shorter_word_does_not_match_inside_expanded_character()]
negated conditional → KILLED

102

1.1
Location : censor
Killed by : edu.ucsb.cs156.happiercows.services.ChatFilterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.services.ChatFilterServiceTests]/[method:shorter_word_does_not_match_inside_expanded_character()]
replaced return value with null for edu/ucsb/cs156/happiercows/services/ChatFilterService::censor → KILLED

105

1.1
Location : censor
Killed by : edu.ucsb.cs156.happiercows.services.ChatFilterServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.services.ChatFilterServiceTests]/[method:banned_word_next_to_punctuation_is_censored()]
replaced return value with null for edu/ucsb/cs156/happiercows/services/ChatFilterService::censor → KILLED

Active mutators

Tests examined


Report generated by PIT 1.22.1