From 9d73770a4e1858a2122ebd2d01018310ef9a6168 Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Mon, 6 Mar 2023 15:07:10 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E6=9B=B4?= =?UTF-8?q?=E6=8D=A2=E6=95=8F=E6=84=9F=E8=AF=8D=E7=9A=84=E6=8E=A9=E7=9B=96?= =?UTF-8?q?=E5=AD=97=E7=AC=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/qqbot/filter.py | 6 ++++-- pkg/qqbot/manager.py | 6 +++++- sensitive-template.json | 1 + 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/pkg/qqbot/filter.py b/pkg/qqbot/filter.py index 6ed83329..7166d8b4 100644 --- a/pkg/qqbot/filter.py +++ b/pkg/qqbot/filter.py @@ -7,6 +7,7 @@ import logging class ReplyFilter: sensitive_words = [] + mask = "*" # 默认值( 兼容性考虑 ) baidu_check = False @@ -14,8 +15,9 @@ class ReplyFilter: baidu_secret_key = "" inappropriate_message_tips = "[百度云]请珍惜机器人,当前返回内容不合规" - def __init__(self, sensitive_words: list): + def __init__(self, sensitive_words: list, mask: str = "*"): self.sensitive_words = sensitive_words + self.mask = mask import config if hasattr(config, 'baidu_check') and hasattr(config, 'baidu_api_key') and hasattr(config, 'baidu_secret_key'): self.baidu_check = config.baidu_check @@ -36,7 +38,7 @@ class ReplyFilter: match = re.findall(word, message) if len(match) > 0: for i in range(len(match)): - message = message.replace(match[i], "*" * len(match[i])) + message = message.replace(match[i], self.mask * len(match[i])) # 百度云审核 if self.baidu_check: diff --git a/pkg/qqbot/manager.py b/pkg/qqbot/manager.py index a2973d5f..6535cac3 100644 --- a/pkg/qqbot/manager.py +++ b/pkg/qqbot/manager.py @@ -82,7 +82,11 @@ class QQBotManager: and config.sensitive_word_filter is not None \ and config.sensitive_word_filter: with open("sensitive.json", "r", encoding="utf-8") as f: - self.reply_filter = pkg.qqbot.filter.ReplyFilter(json.load(f)['words']) + sensitive_json = json.load(f) + self.reply_filter = pkg.qqbot.filter.ReplyFilter( + sensitive_words=sensitive_json['words'], + mask=sensitive_json['mask'] if 'mask' in sensitive_json else '*' + ) else: self.reply_filter = pkg.qqbot.filter.ReplyFilter([]) diff --git a/sensitive-template.json b/sensitive-template.json index 5ee7ee77..11e85353 100644 --- a/sensitive-template.json +++ b/sensitive-template.json @@ -1,4 +1,5 @@ { + "mask": "*", "words": [ "习近平", "胡锦涛", From d993852de7049d4261724998c4ac4134506251ba Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Mon, 6 Mar 2023 15:26:06 +0800 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E5=B0=86?= =?UTF-8?q?=E6=95=8F=E6=84=9F=E8=AF=8D=E6=9B=BF=E6=8D=A2=E6=88=90=E6=95=B4?= =?UTF-8?q?=E4=B8=AA=E5=AD=97=E7=AC=A6=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/qqbot/filter.py | 9 +++++++-- pkg/qqbot/manager.py | 3 ++- sensitive-template.json | 2 ++ 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/pkg/qqbot/filter.py b/pkg/qqbot/filter.py index 7166d8b4..f0efeda9 100644 --- a/pkg/qqbot/filter.py +++ b/pkg/qqbot/filter.py @@ -8,6 +8,7 @@ import logging class ReplyFilter: sensitive_words = [] mask = "*" + mask_word = "" # 默认值( 兼容性考虑 ) baidu_check = False @@ -15,9 +16,10 @@ class ReplyFilter: baidu_secret_key = "" inappropriate_message_tips = "[百度云]请珍惜机器人,当前返回内容不合规" - def __init__(self, sensitive_words: list, mask: str = "*"): + def __init__(self, sensitive_words: list, mask: str = "*", mask_word: str = ""): self.sensitive_words = sensitive_words self.mask = mask + self.mask_word = mask_word import config if hasattr(config, 'baidu_check') and hasattr(config, 'baidu_api_key') and hasattr(config, 'baidu_secret_key'): self.baidu_check = config.baidu_check @@ -38,7 +40,10 @@ class ReplyFilter: match = re.findall(word, message) if len(match) > 0: for i in range(len(match)): - message = message.replace(match[i], self.mask * len(match[i])) + if self.mask_word == "": + message = message.replace(match[i], self.mask * len(match[i])) + else: + message = message.replace(match[i], self.mask_word) # 百度云审核 if self.baidu_check: diff --git a/pkg/qqbot/manager.py b/pkg/qqbot/manager.py index 6535cac3..b9733991 100644 --- a/pkg/qqbot/manager.py +++ b/pkg/qqbot/manager.py @@ -85,7 +85,8 @@ class QQBotManager: sensitive_json = json.load(f) self.reply_filter = pkg.qqbot.filter.ReplyFilter( sensitive_words=sensitive_json['words'], - mask=sensitive_json['mask'] if 'mask' in sensitive_json else '*' + mask=sensitive_json['mask'] if 'mask' in sensitive_json else '*', + mask_word=sensitive_json['mask_word'] if 'mask_word' in sensitive_json else '' ) else: self.reply_filter = pkg.qqbot.filter.ReplyFilter([]) diff --git a/sensitive-template.json b/sensitive-template.json index 11e85353..61d15ff9 100644 --- a/sensitive-template.json +++ b/sensitive-template.json @@ -1,5 +1,7 @@ { + "说明": "mask将替换敏感词中的每一个字,若mask_word值不为空,则将敏感词整个替换为mask_word的值", "mask": "*", + "mask_word": "", "words": [ "习近平", "胡锦涛",