From dd503789e0ac84376144006b3934debdf627a9c5 Mon Sep 17 00:00:00 2001 From: Amber Date: Tue, 22 Mar 2022 17:03:50 -0500 Subject: [PATCH] Added word filtering from grumbulon's fork. Plan to eventually make this regenerate the post instead of just replacing the text --- .gitignore | 1 + config.def.json | 1 + filter.txt.example | 5 +++++ functions.py | 11 ++++++++++- main.py | 3 ++- 5 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 filter.txt.example diff --git a/.gitignore b/.gitignore index c25599c..491d923 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ __pycache__/ config.json venv/ *.log +filter.txt \ No newline at end of file diff --git a/config.def.json b/config.def.json index 908b063..651f93d 100644 --- a/config.def.json +++ b/config.def.json @@ -11,5 +11,6 @@ "length_upper_limit": 50, "overlap_ratio_enabled": false, "overlap_ratio": 0.7, + "word_filter": 0, "website": "https://git.nixnet.services/amber/amber-ebooks" } diff --git a/filter.txt.example b/filter.txt.example new file mode 100644 index 0000000..58f8fdf --- /dev/null +++ b/filter.txt.example @@ -0,0 +1,5 @@ +put +bad +words +in +filter.txt diff --git a/functions.py b/functions.py index dad8b1a..f4daa6b 100755 --- a/functions.py +++ b/functions.py @@ -56,6 +56,15 @@ def make_sentence(output, cfg): elif cfg['mention_handling'] == 0: sentence = re.sub(r"\S*@\u200B\S*\s?", "", sentence) + # optionally remove filtered words + if cfg['word_filter'] == 1: + try: + fp = open('./filter.txt') + for word in fp: + if word in sentence: + sentence = sentence.replace(word, "[REDACTED]") + finally: + fp.close() output.send(sentence) @@ -64,7 +73,7 @@ def make_toot(cfg): pin, pout = multiprocessing.Pipe(False) p = multiprocessing.Process(target=make_sentence, args=[pout, cfg]) p.start() - p.join(300) # wait 5 seconds to get something + p.join(5) # wait 5 seconds to get something if p.is_alive(): # if it's still trying to make a toot after 5 seconds p.terminate() p.join() diff --git a/main.py b/main.py index 6847e88..f139229 100755 --- a/main.py +++ b/main.py @@ -30,7 +30,8 @@ cfg = { "length_lower_limit": 5, "length_upper_limit": 50, "overlap_ratio_enabled": False, - "overlap_ratio": 0.7 + "overlap_ratio": 0.7, + "word_filter": 0 } try: