rust/chat_sanitizer/src/bad_words.rs
author Wuzzy <Wuzzy2@mail.ru>
Tue, 09 Jun 2020 07:56:59 +0200
changeset 15616 e4abca26f215
parent 14505 ba29aa03db87
permissions -rw-r--r--
Teach AI to use cleavers
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14503
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
     1
use crate::{normalized_message, MessageChecker, Severity};
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
     2
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
     3
use std::marker::PhantomData;
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
     4
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
     5
struct BadWordsChecker<T> {
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
     6
    blacklist: Vec<String>,
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
     7
    whitelist: Vec<String>,
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
     8
    player_id_type: PhantomData<T>,
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
     9
}
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    10
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    11
impl<T> BadWordsChecker<T> {
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    12
    pub fn new(blacklist: &[&str], whitelist: &[&str]) -> Self {
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    13
        Self {
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    14
            blacklist: blacklist.iter().map(|s| normalized_message(*s)).collect(),
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    15
            whitelist: whitelist.iter().map(|s| normalized_message(*s)).collect(),
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    16
            player_id_type: PhantomData,
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    17
        }
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    18
    }
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    19
}
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    20
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    21
impl<T> MessageChecker<T> for BadWordsChecker<T> {
14505
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14503
diff changeset
    22
    fn check(&self, _player_id: T, message: &str) -> Severity {
14503
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    23
        let msg = normalized_message(message);
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    24
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    25
        // silly implementation, allows bad messages with a single good word
14505
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14503
diff changeset
    26
        for bad_word in &self.blacklist {
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14503
diff changeset
    27
            if msg.contains(bad_word) {
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14503
diff changeset
    28
                if !self
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14503
diff changeset
    29
                    .whitelist
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14503
diff changeset
    30
                    .iter()
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14503
diff changeset
    31
                    .any(|good_word| msg.contains(good_word))
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14503
diff changeset
    32
                {
14503
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    33
                    return Severity::Warn;
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    34
                }
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    35
            }
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    36
        }
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    37
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    38
        Severity::Pass
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    39
    }
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    40
}
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    41
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    42
#[cfg(test)]
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    43
mod tests {
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    44
    use super::*;
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    45
    #[test]
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    46
    fn it_works() {
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    47
        let checker = BadWordsChecker::new(&["fsck", "poop"], &["fsck -y"]);
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    48
        assert_eq!(checker.check(0, "group hug"), Severity::Pass);
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    49
        assert_eq!(checker.check(0, "fpoopf"), Severity::Warn);
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    50
        assert_eq!(checker.check(0, "PooP"), Severity::Warn);
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    51
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    52
        // this one fails
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    53
        //assert_eq!(checker.check(0, "poop 'fsck -y' poop"), Severity::Warn);
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    54
14505
ba29aa03db87 Implement letter repeat check
unc0rr
parents: 14503
diff changeset
    55
        // ideally this one shouldn't fail, need a better confusables check
14503
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    56
        // assert_eq!(checker.check(0, "P00P"), Severity::Warn);
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    57
    }
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    58
}