Files
昔念 685069fded feat(cool): 添加敏感词过滤功能
- 引入 go-sensitive-word 敏感词过滤库
- 在全局初始化中加载敏感词库并配置过滤器
- 在创建玩家时应用敏感词过滤,替换不合适的昵称内容
2025-09-09 01:11:10 +08:00

43 lines
1.2 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package go_sensitive_word
import (
"errors"
"github.com/zmexing/go-sensitive-word/filter"
"github.com/zmexing/go-sensitive-word/store"
)
// Manager 是敏感词过滤系统的核心结构,整合了词库存储和过滤算法
type Manager struct {
store.Store // // 词库存储接口(支持内存、本地文件、远程等)
filter.Filter // // 敏感词匹配算法接口(如 DFA
}
// NewFilter 初始化过滤器和词库存储
// 参数storeOption 指定存储方式filterOption 指定过滤算法
func NewFilter(storeOption StoreOption, filterOption FilterOption) (*Manager, error) {
var filterStore store.Store
var myFilter filter.Filter
switch storeOption.Type {
case StoreMemory: // 使用内存词库
filterStore = store.NewMemoryModel()
default:
return nil, errors.New("invalid store type")
}
switch filterOption.Type {
case FilterDfa: // 使用 DFA 算法
dfaModel := filter.NewDfaModel()
// 启动监听协程,实时接收新增/删除词的通知
go dfaModel.Listen(filterStore.GetAddChan(), filterStore.GetDelChan())
myFilter = dfaModel
default:
return nil, errors.New("invalid filter type")
}
return &Manager{
Store: filterStore,
Filter: myFilter,
}, nil
}