yum/FastTextPager

Compressed text paging over OSC.

git clone https://git.yummers.dev/yum/FastTextPager

yumMore stuff7fb9c57

master
1.5 KiB43 linesraw
1#!/usr/bin/env python3
2
3class ProfanityFilter:
4    def __init__(self, en_path: str):
5        self.en_path = en_path
6        self.en_profanity = set()
7
8    def load(self):
9        with open(self.en_path, 'r') as f:
10            for line in f:
11                self.en_profanity.add(line.strip())
12
13    def filter(self, line: str, language_code: str = "en") -> str:
14        filtered = ""
15
16        if language_code not in {"en"}:
17            raise ValueError(f"Language code \"{language_code}\" is " +
18                    "unsupported by the profanity filter")
19
20        # Translation table converting vowels to asterisks.
21        vowel_to_asterisk = str.maketrans('aeiouAEIOU', '**********')
22
23        result = []
24        for word in line.split():
25            word_clean = word.lower()
26            # Filter out non-alphabet characters from the word.
27            word_clean = ''.join([char for char in word_clean if char.isalpha()])
28            if word_clean in self.en_profanity:
29                result.append(word.translate(vowel_to_asterisk))
30            else:
31                result.append(word)
32
33        return " ".join(result)
34
35if __name__ == "__main__":
36    en_path = "/mnt/d/vrc/TaSTT/GUI/Profanity/Profanity/en"
37    p = ProfanityFilter(en_path)
38    p.load()
39    assert(p.filter("fuck") == "f*ck")
40    assert(p.filter("fuck!") == "f*ck!")
41    assert(p.filter("fuck shit") == "f*ck sh*t")
42    assert(p.filter("fuck shit this should not be filtered") == "f*ck sh*t this should not be filtered")
43    assert(p.filter("ASS") == "*SS")