🐛 fix ttr

This commit is contained in:
源文雨 2022-12-03 13:40:03 +08:00
parent e75fc83412
commit 6f8c576a44
2 changed files with 10 additions and 37 deletions

2
data

@ -1 +1 @@
Subproject commit 485fc2618e37ee2a4065785cd068df9a90b7d014 Subproject commit e8d06b150b2cf84d9c7dc2f8a9f573da2b2290fd

View File

@ -2,9 +2,8 @@
package thesaurus package thesaurus
import ( import (
"bytes"
"encoding/json" "encoding/json"
"io"
"io/fs"
"math/rand" "math/rand"
"strings" "strings"
@ -82,7 +81,7 @@ func init() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
seg, err := jieba.LoadDictionary(&mockfile{data: data}) seg, err := jieba.LoadDictionary(bytes.NewReader(data))
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -120,13 +119,13 @@ func init() {
} }
logrus.Infoln("[thesaurus]加载", len(chatListD), "条傲娇词库", len(chatListK), "条可爱词库") logrus.Infoln("[thesaurus]加载", len(chatListD), "条傲娇词库", len(chatListK), "条可爱词库")
engine.OnMessage(canmatch(tKIMO), match(chatList, seg, getmsg)). engine.OnMessage(canmatch(tKIMO), match(chatList, seg)).
SetBlock(false). SetBlock(false).
Handle(randreply(kimomap)) Handle(randreply(kimomap))
engine.OnMessage(canmatch(tDERE), match(chatListD, seg, getmsg)). engine.OnMessage(canmatch(tDERE), match(chatListD, seg)).
SetBlock(false). SetBlock(false).
Handle(randreply(sm.D)) Handle(randreply(sm.D))
engine.OnMessage(canmatch(tKAWA), match(chatListK, seg, getmsg)). engine.OnMessage(canmatch(tKAWA), match(chatListK, seg)).
SetBlock(false). SetBlock(false).
Handle(randreply(sm.K)) Handle(randreply(sm.K))
}() }()
@ -139,42 +138,20 @@ type simai struct {
K map[string][]string `yaml:"可爱"` K map[string][]string `yaml:"可爱"`
} }
type mockfile struct {
p uintptr
data []byte
}
func (*mockfile) Stat() (fs.FileInfo, error) {
return nil, nil
}
func (f *mockfile) Read(buf []byte) (int, error) {
if int(f.p) >= len(f.data) {
return 0, io.EOF
}
n := copy(buf, f.data[f.p:])
f.p += uintptr(n)
return n, nil
}
func (f *mockfile) Close() error {
if f.data == nil {
return fs.ErrClosed
}
f.data = nil
return nil
}
const ( const (
tKIMO = iota tKIMO = iota
tDERE tDERE
tKAWA tKAWA
) )
func match(l []string, seg *jieba.Segmenter, getmsg func(*zero.Ctx) string) zero.Rule { func match(l []string, seg *jieba.Segmenter) zero.Rule {
return func(ctx *zero.Ctx) bool { return func(ctx *zero.Ctx) bool {
if zero.FullMatchRule(l...)(ctx) { if zero.FullMatchRule(l...)(ctx) {
return true return true
} }
return ctxext.JiebaFullMatch(seg, getmsg, l...)(ctx) return ctxext.JiebaFullMatch(seg, func(ctx *zero.Ctx) string {
return ctx.ExtractPlainText()
}, l...)(ctx)
} }
} }
@ -196,10 +173,6 @@ func canmatch(typ int64) zero.Rule {
} }
} }
func getmsg(ctx *zero.Ctx) string {
return ctx.MessageString()
}
func randreply(m map[string][]string) zero.Handler { func randreply(m map[string][]string) zero.Handler {
return func(ctx *zero.Ctx) { return func(ctx *zero.Ctx) {
key := ctx.State["matched"].(string) key := ctx.State["matched"].(string)