wangkaiyue 2 жил өмнө
parent
commit
ffb5ec27c3

+ 0 - 6
api/v1/aiChatApi.go

@@ -45,12 +45,6 @@ type QuestionRes struct {
 	Data      []string `json:"data" dc:"true:成功 false:失败"`
 }
 
-// GuessQuestionReq 猜你想问
-type GuessQuestionReq struct {
-	g.Meta `path:"/guessQuestion" tags:"AiChat" method:"post" summary:"获取场景下的猜你想问"`
-	Href   string `json:"href" v:"url"`
-}
-
 // UsuallyProblemReq 常见问题
 type UsuallyProblemReq struct {
 	g.Meta `path:"/usuallyProblem" tags:"AiChat" method:"post" summary:"获取场景下的常见问题"`

+ 0 - 1
internal/cmd/cmd.go

@@ -23,7 +23,6 @@ var (
 				group.Bind(
 					controller.ChatHistory,    //历史记录
 					controller.Evaluate,       //评价
-					controller.GuessQuestion,  //猜你想问
 					controller.UsuallyProblem, //常见问题
 				)
 			})

+ 0 - 25
internal/controller/guessQuestion.go

@@ -1,25 +0,0 @@
-package controller
-
-import (
-	v1 "aiChat/api/v1"
-	"aiChat/internal/model"
-	"context"
-	"fmt"
-	"github.com/gogf/gf/v2/frame/g"
-)
-
-var (
-	GuessQuestion = cGuessQuestion{}
-)
-
-type cGuessQuestion struct{}
-
-func (c *cGuessQuestion) Method(ctx context.Context, req *v1.GuessQuestionReq) (res *v1.QuestionRes, err error) {
-	session := model.SessionCtx.Get(ctx).JSession
-	if session.AccountId <= 0 {
-		return nil, fmt.Errorf("无用户身份")
-	}
-	var list []string
-	list, err = model.Question.GetGuessQuestion(ctx, g.Config().MustGet(ctx, "chat.usuallyProblem", 5).Int())
-	return &v1.QuestionRes{Data: list}, nil
-}

+ 0 - 14
internal/model/question.go

@@ -35,20 +35,6 @@ type QuestionReq struct {
 	Href string `json:"href"` //咨询页面
 }
 
-// GetGuessQuestion 猜你想问
-func (q *cQuestion) GetGuessQuestion(ctx context.Context, limit int) (list []string, err error) {
-	list = make([]string, 0, limit)
-	res, err := g.Model("ai_question_list").Ctx(ctx).Fields("question").
-		Where("status = 1 and question_type = ?", consts.QUESTION_TYPE_GUESS).OrderDesc("id").Limit(limit).All()
-	if err != nil {
-		return nil, err
-	}
-	for _, m := range res.List() {
-		list = append(list, gconv.String(m["question"]))
-	}
-	return
-}
-
 // GetUsuallyProblem 获取常见问题
 func (q *cQuestion) GetUsuallyProblem(ctx context.Context, scenario, limit int) (list []string, err error) {
 	list = make([]string, 0, limit)

+ 1 - 1
main.go

@@ -10,6 +10,6 @@ import (
 )
 
 func main() {
-	fsw.ReadFswDict("./manifest/fsw/baidu.dict", "./manifest/fsw/器械.dict", "./manifest/fsw/广告.dict", "./manifest/fsw/政治.dict", "./manifest/fsw/网站.dict", "./manifest/fsw/色情.dict")
+	fsw.ReadFswDirDict("./manifest/fsw/")
 	cmd.Main.Run(gctx.New())
 }

+ 30 - 7
utility/fsw/readdict.go

@@ -10,9 +10,8 @@ import (
 // 敏感词库
 var link map[string]interface{}
 
-func getWords(path string) (words []string, err error) {
-	var fi *os.File
-	fi, err = os.Open(path)
+func getWordsByFile(filePath string) (words []string) {
+	fi, err := os.Open(filePath)
 	if err != nil {
 		return
 	}
@@ -28,15 +27,40 @@ func getWords(path string) (words []string, err error) {
 	return
 }
 
-// 读取字段
+func GetWordsByDir(dir string) (words []string) {
+	files, err := os.ReadDir(dir)
+	if err != nil {
+		return
+	}
+	for _, file := range files {
+		if file.IsDir() {
+			words = append(words, GetWordsByDir(dir+file.Name()+"/")...)
+		} else {
+			words = append(words, getWordsByFile(dir+file.Name())...)
+		}
+	}
+	return
+}
+
+// ReadFswDirDict 读取文件夹获取敏感词
+func ReadFswDirDict(dir string) {
+	initList(GetWordsByDir(dir))
+}
+
+// ReadFswDict 读取多文件获取敏感词
 func ReadFswDict(config ...string) {
 	var AllWords []string
 	for _, path := range config {
-		fswWords, _ := getWords(path)
-		AllWords = append(AllWords, fswWords...)
+		AllWords = append(AllWords, getWordsByFile(path)...)
 	}
 	log.Println(len(AllWords))
 	//要组装关键字
+	initList(AllWords)
+}
+
+func initList(AllWords []string) {
+	log.Printf("初始化敏感词%d个\n", len(AllWords))
+	//要组装关键字
 	link = make(map[string]interface{})
 	var nowMap *map[string]interface{}
 	for _, key := range AllWords {
@@ -55,7 +79,6 @@ func ReadFswDict(config ...string) {
 			if i == len(key)-1 {
 				(*nowMap)["YN"] = "Y"
 			}
-
 		}
 	}
 }