mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-09-04 08:37:14 +00:00
feat: 支持将敏感词替换成整个字符串
This commit is contained in:
+7
-2
@@ -8,6 +8,7 @@ import logging
|
|||||||
class ReplyFilter:
|
class ReplyFilter:
|
||||||
sensitive_words = []
|
sensitive_words = []
|
||||||
mask = "*"
|
mask = "*"
|
||||||
|
mask_word = ""
|
||||||
|
|
||||||
# 默认值( 兼容性考虑 )
|
# 默认值( 兼容性考虑 )
|
||||||
baidu_check = False
|
baidu_check = False
|
||||||
@@ -15,9 +16,10 @@ class ReplyFilter:
|
|||||||
baidu_secret_key = ""
|
baidu_secret_key = ""
|
||||||
inappropriate_message_tips = "[百度云]请珍惜机器人,当前返回内容不合规"
|
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.sensitive_words = sensitive_words
|
||||||
self.mask = mask
|
self.mask = mask
|
||||||
|
self.mask_word = mask_word
|
||||||
import config
|
import config
|
||||||
if hasattr(config, 'baidu_check') and hasattr(config, 'baidu_api_key') and hasattr(config, 'baidu_secret_key'):
|
if hasattr(config, 'baidu_check') and hasattr(config, 'baidu_api_key') and hasattr(config, 'baidu_secret_key'):
|
||||||
self.baidu_check = config.baidu_check
|
self.baidu_check = config.baidu_check
|
||||||
@@ -38,7 +40,10 @@ class ReplyFilter:
|
|||||||
match = re.findall(word, message)
|
match = re.findall(word, message)
|
||||||
if len(match) > 0:
|
if len(match) > 0:
|
||||||
for i in range(len(match)):
|
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:
|
if self.baidu_check:
|
||||||
|
|||||||
@@ -85,7 +85,8 @@ class QQBotManager:
|
|||||||
sensitive_json = json.load(f)
|
sensitive_json = json.load(f)
|
||||||
self.reply_filter = pkg.qqbot.filter.ReplyFilter(
|
self.reply_filter = pkg.qqbot.filter.ReplyFilter(
|
||||||
sensitive_words=sensitive_json['words'],
|
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:
|
else:
|
||||||
self.reply_filter = pkg.qqbot.filter.ReplyFilter([])
|
self.reply_filter = pkg.qqbot.filter.ReplyFilter([])
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
{
|
{
|
||||||
|
"说明": "mask将替换敏感词中的每一个字,若mask_word值不为空,则将敏感词整个替换为mask_word的值",
|
||||||
"mask": "*",
|
"mask": "*",
|
||||||
|
"mask_word": "",
|
||||||
"words": [
|
"words": [
|
||||||
"习近平",
|
"习近平",
|
||||||
"胡锦涛",
|
"胡锦涛",
|
||||||
|
|||||||
Reference in New Issue
Block a user