上传铭感词系统

This commit is contained in:
liwei 2023-06-20 15:36:06 +08:00
parent 5cc19eae0d
commit 67f1d979e0
4 changed files with 16912 additions and 9 deletions

16878
bin/wordfilter.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@ -9,7 +9,6 @@ import (
) )
// 世界任务完成 // 世界任务完成
func (this *apiComp) FinishCheck(session comm.IUserSession, req *pb.WorldtaskFinishReq) (errdata *pb.ErrorData) { func (this *apiComp) FinishCheck(session comm.IUserSession, req *pb.WorldtaskFinishReq) (errdata *pb.ErrorData) {
if req.TaskId == 0 { if req.TaskId == 0 {
this.module.Error("世界任务完成参数错误", log.Field{Key: "uid", Value: session.GetUserId()}, log.Field{Key: "params", Value: req.String()}) this.module.Error("世界任务完成参数错误", log.Field{Key: "uid", Value: session.GetUserId()}, log.Field{Key: "params", Value: req.String()})

View File

@ -7,12 +7,12 @@ import (
type Option func(*Options) type Option func(*Options)
type Options struct { type Options struct {
WorldFile string //词组文件 WorldFile []string //词组文件
Debug bool //日志是否开启 Debug bool //日志是否开启
Log log.Ilogf Log log.Ilogf
} }
func SetWorldFile(v string) Option { func SetWorldFile(v []string) Option {
return func(o *Options) { return func(o *Options) {
o.WorldFile = v o.WorldFile = v
} }

View File

@ -3,15 +3,16 @@ package wordfilter
import ( import (
"bufio" "bufio"
"fmt" "fmt"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/lego/utils/codec/json"
"io" "io"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os" "os"
"path/filepath"
"regexp" "regexp"
"strings" "strings"
"time" "time"
jsoniter "github.com/json-iterator/go"
) )
func newSys(options *Options) (sys *Sys, err error) { func newSys(options *Options) (sys *Sys, err error) {
@ -20,8 +21,21 @@ func newSys(options *Options) (sys *Sys, err error) {
trie: NewTrie(), trie: NewTrie(),
noise: regexp.MustCompile(`[\|\s&%$@*]+`), noise: regexp.MustCompile(`[\|\s&%$@*]+`),
} }
if options.WorldFile != "" { if len(options.WorldFile) > 0 {
err = sys.Loadjson(options.WorldFile) for _, v := range options.WorldFile {
ext := filepath.Ext(v)
switch ext {
case ".json":
err = sys.Loadjson(v)
case ".txt", ".text":
err = sys.Loadtext(v)
default:
log.Warn("不支持的文件类型", log.Field{Key: "ext", Value: ext})
continue
}
}
} }
return return
} }
@ -78,6 +92,18 @@ func (Sys *Sys) Load(rd io.Reader) error {
return nil return nil
} }
func (Sys *Sys) Loadtext(fliepath string) (err error) {
var (
file *os.File
)
if file, err = os.Open(fliepath); err != nil {
err = fmt.Errorf("no found file:%s", fliepath)
return
}
defer file.Close()
return Sys.Load(file)
}
func (Sys *Sys) Loadjson(fliepath string) (err error) { func (Sys *Sys) Loadjson(fliepath string) (err error) {
var ( var (
file *os.File file *os.File
@ -95,7 +121,7 @@ func (Sys *Sys) Loadjson(fliepath string) (err error) {
return return
} }
word = &WordJson{} word = &WordJson{}
if err = jsoniter.Unmarshal(bytes, word); err != nil { if err = json.Unmarshal(bytes, word); err != nil {
err = fmt.Errorf("read file:%s json.Unmarshal err:%v", fliepath, err) err = fmt.Errorf("read file:%s json.Unmarshal err:%v", fliepath, err)
return return
} }