Skip to content

Commit

Permalink
feat: 基于dfa算法检测违禁词
Browse files Browse the repository at this point in the history
  • Loading branch information
rehiy committed Mar 16, 2024
1 parent 32ccaa1 commit 7294127
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 27 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/clbanning/mxj v1.8.4
github.com/gin-gonic/gin v1.9.1
github.com/gorilla/websocket v1.5.1
github.com/importcjj/sensitive v0.0.0-20200106142752-42d1c505be7b
github.com/mitchellh/mapstructure v1.5.0
github.com/opentdp/go-helper v0.7.1-0.20240314081626-c3e3805521f2
github.com/rehiy/one-llm v0.1.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
github.com/importcjj/sensitive v0.0.0-20200106142752-42d1c505be7b h1:9hudrgWUhyfR4FRMOfL9KB1uYw48DUdHkkgr9ODOw7Y=
github.com/importcjj/sensitive v0.0.0-20200106142752-42d1c505be7b/go.mod h1:zLVdX6Ed2SvCbEamKmve16U0E03UkdJo4ls1TBfmc8Q=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
Expand Down
67 changes: 40 additions & 27 deletions wclient/robot/handler_bad.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ package robot

import (
"fmt"
"strings"

"github.com/importcjj/sensitive"

"github.com/opentdp/wechat-rest/dbase/keyword"
"github.com/opentdp/wechat-rest/dbase/profile"
"github.com/opentdp/wechat-rest/dbase/tables"
"github.com/opentdp/wechat-rest/wcferry"
)

var badMember = map[string]int{}
var badwordList = []*tables.Keyword{}
var badFilter *sensitive.Filter

var roomMemberAlias = map[string]string{}

func badHandler() []*Handler {

updateBadWord()
updateBadWordFilter()

cmds := []*Handler{}

Expand All @@ -32,7 +32,7 @@ func badHandler() []*Handler {
Group: "badword", Roomid: prid(msg), Phrase: msg.Content, Level: 1,
})
if err == nil {
updateBadWord()
badFilter.AddWord(msg.Content)
return "违禁词添加成功"
}
return "违禁词已存在"
Expand All @@ -51,7 +51,7 @@ func badHandler() []*Handler {
Group: "badword", Roomid: prid(msg), Phrase: msg.Content,
})
if err == nil {
updateBadWord()
badFilter.DelWord(msg.Content)
return "违禁词删除成功"
}
return "违禁词删除失败"
Expand All @@ -75,28 +75,33 @@ func badPreCheck(msg *wcferry.WxMsg) string {
return ""
}

// 遍历关键词
// 查找违禁词
text := roomMemberName(msg.Sender, msg.Roomid) + msg.Content
for _, v := range badwordList {
if msg.IsGroup {
if v.Roomid != "*" && v.Roomid != "+" && v.Roomid != msg.Roomid {
continue // 忽略
}
} else {
if v.Roomid != "*" && v.Roomid != "-" {
continue // 忽略
}
keys := badFilter.FindAll(text)
if len(keys) == 0 {
return ""
}

// 判断违禁级别
level := 0
for _, k := range keys {
v, _ := keyword.Fetch(&keyword.FetchParam{Group: "badword", Phrase: k})
if v.Level > 0 && (v.Roomid == msg.Roomid || v.Roomid == "*" || v.Roomid == "+") {
level += int(v.Level)
}
if v.Level > 0 && strings.Contains(text, v.Phrase) {
badMember[msg.Sender] += int(v.Level)
if badMember[msg.Sender] > 10 {
defer delete(badMember, msg.Sender)
defer wc.CmdClient.DelChatRoomMembers(msg.Roomid, msg.Sender)
return "我送你离开,天涯之外你是否还在"
}
str := "违规风险 +%d,当前累计:%d,大于 10 将被请出群聊"
return fmt.Sprintf(str, v.Level, badMember[msg.Sender])
}

// 等级违规积分
if level > 0 {
badMember[msg.Sender] += level
if badMember[msg.Sender] > 10 {
defer delete(badMember, msg.Sender)
defer wc.CmdClient.DelChatRoomMembers(msg.Roomid, msg.Sender)
str := "违规累计 %d;送你离开,天涯之外你是否还在"
return fmt.Sprintf(str, badMember[msg.Sender])
}
str := "违规风险 +%d,当前累计:%d,大于 10 将被请出群聊"
return fmt.Sprintf(str, level, badMember[msg.Sender])
}

return ""
Expand All @@ -110,14 +115,22 @@ func roomMemberName(wxid, roomid string) string {
if roomMemberAlias[k] == "" {
roomMemberAlias[k] = wc.CmdClient.GetAliasInChatRoom(wxid, roomid)
}

return roomMemberAlias[k]

}

func updateBadWord() {
func updateBadWordFilter() {

badwordList, _ = keyword.FetchAll(&keyword.FetchAllParam{
items, _ := keyword.FetchAll(&keyword.FetchAllParam{
Group: "badword",
})

filter := sensitive.New()
for _, v := range items {
filter.AddWord(v.Phrase)
}

badFilter = filter

}

0 comments on commit 7294127

Please sign in to comment.