Merge pull request #202 from velour/sentiment

reaction: add sentiment analysis; remove harass
This commit is contained in:
Chris Sexton 2019-09-27 10:44:01 -04:00 committed by GitHub
commit f53c3e3067
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 26 deletions

1
go.mod
View File

@ -5,6 +5,7 @@ require (
github.com/PuerkitoBio/goquery v1.5.0 github.com/PuerkitoBio/goquery v1.5.0
github.com/armon/go-radix v1.0.0 // indirect github.com/armon/go-radix v1.0.0 // indirect
github.com/chrissexton/leftpad v0.0.0-20181207133115-1e93189d2fff github.com/chrissexton/leftpad v0.0.0-20181207133115-1e93189d2fff
github.com/chrissexton/sentiment v0.0.0-20190927141846-d69c422ba035
github.com/go-sql-driver/mysql v1.4.1 // indirect github.com/go-sql-driver/mysql v1.4.1 // indirect
github.com/gonum/floats v0.0.0-20181209220543-c233463c7e82 // indirect github.com/gonum/floats v0.0.0-20181209220543-c233463c7e82 // indirect
github.com/gonum/internal v0.0.0-20181124074243-f884aa714029 // indirect github.com/gonum/internal v0.0.0-20181124074243-f884aa714029 // indirect

4
go.sum
View File

@ -10,8 +10,12 @@ github.com/andybalholm/cascadia v1.0.0 h1:hOCXnnZ5A+3eVDX8pvgl4kofXv2ELss0bKcqRy
github.com/andybalholm/cascadia v1.0.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y= github.com/andybalholm/cascadia v1.0.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
github.com/armon/go-radix v1.0.0 h1:F4z6KzEeeQIMeLFa97iZU6vupzoecKdU5TX24SNppXI= github.com/armon/go-radix v1.0.0 h1:F4z6KzEeeQIMeLFa97iZU6vupzoecKdU5TX24SNppXI=
github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/cdipaolo/goml v0.0.0-20190412180403-e1f51f713598 h1:j2XRGH5Y5uWtBYXGwmrjKeM/kfu/jh7ZcnrGvyN5Ttk=
github.com/cdipaolo/goml v0.0.0-20190412180403-e1f51f713598/go.mod h1:sduMkaHcXDIWurl/Bd/z0rNEUHw5tr6LUA9IO8E9o0o=
github.com/chrissexton/leftpad v0.0.0-20181207133115-1e93189d2fff h1:+TEqaP0eO1unI7XHHFeMDhsxhLDIb0x8KYuZbqbAmxA= github.com/chrissexton/leftpad v0.0.0-20181207133115-1e93189d2fff h1:+TEqaP0eO1unI7XHHFeMDhsxhLDIb0x8KYuZbqbAmxA=
github.com/chrissexton/leftpad v0.0.0-20181207133115-1e93189d2fff/go.mod h1:QCRjR0b4qiJiNjuP7RFM89bh4UExGJalcWmYeSvlnRc= github.com/chrissexton/leftpad v0.0.0-20181207133115-1e93189d2fff/go.mod h1:QCRjR0b4qiJiNjuP7RFM89bh4UExGJalcWmYeSvlnRc=
github.com/chrissexton/sentiment v0.0.0-20190927141846-d69c422ba035 h1:3+eJGFTbUgOMDCpa8PTmJABs1Z3EDHRrcz6d3oXfZm0=
github.com/chrissexton/sentiment v0.0.0-20190927141846-d69c422ba035/go.mod h1:5V55omeg+mdO+zAi38c3S9I1m5IZgdNPqiSKSXIdo88=
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=

View File

@ -3,8 +3,10 @@
package reaction package reaction
import ( import (
"github.com/rs/zerolog/log"
"math/rand" "math/rand"
"github.com/chrissexton/sentiment"
"github.com/velour/catbase/bot" "github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg" "github.com/velour/catbase/bot/msg"
"github.com/velour/catbase/config" "github.com/velour/catbase/config"
@ -13,51 +15,43 @@ import (
type ReactionPlugin struct { type ReactionPlugin struct {
bot bot.Bot bot bot.Bot
config *config.Config config *config.Config
model sentiment.Models
} }
func New(b bot.Bot) *ReactionPlugin { func New(b bot.Bot) *ReactionPlugin {
model, err := sentiment.Restore()
if err != nil {
log.Fatal().Err(err).Msg("Couldn't restore sentiment model")
}
rp := &ReactionPlugin{ rp := &ReactionPlugin{
bot: b, bot: b,
config: b.Config(), config: b.Config(),
model: model,
} }
b.Register(rp, bot.Message, rp.message) b.Register(rp, bot.Message, rp.message)
return rp return rp
} }
func (p *ReactionPlugin) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool { func (p *ReactionPlugin) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
harrass := false
for _, nick := range p.config.GetArray("Reaction.HarrassList", []string{}) {
if message.User.Name == nick {
harrass = true
break
}
}
chance := p.config.GetFloat64("Reaction.GeneralChance", 0.01) chance := p.config.GetFloat64("Reaction.GeneralChance", 0.01)
negativeWeight := 1
if harrass {
chance = p.config.GetFloat64("Reaction.HarrassChance", 0.05)
negativeWeight = p.config.GetInt("Reaction.NegativeHarrassmentMultiplier", 2)
}
if rand.Float64() < chance { if rand.Float64() < chance {
numPositiveReactions := len(p.config.GetArray("Reaction.PositiveReactions", []string{})) analysis := p.model.SentimentAnalysis(message.Body, sentiment.English)
numNegativeReactions := len(p.config.GetArray("Reaction.NegativeReactions", []string{}))
maxIndex := numPositiveReactions + numNegativeReactions*negativeWeight log.Debug().
Uint8("score", analysis.Score).
Str("body", message.Body).
Msg("sentiment of statement")
index := rand.Intn(maxIndex) var reactions []string
if analysis.Score > 0 {
reaction := "" reactions = p.config.GetArray("Reaction.PositiveReactions", []string{})
if index < numPositiveReactions {
reaction = p.config.GetArray("Reaction.PositiveReactions", []string{})[index]
} else { } else {
index -= numPositiveReactions reactions = p.config.GetArray("Reaction.NegativeReactions", []string{})
index %= numNegativeReactions
reaction = p.config.GetArray("Reaction.NegativeReactions", []string{})[index]
} }
reaction := reactions[rand.Intn(len(reactions))]
p.bot.Send(c, bot.Reaction, message.Channel, reaction, message) p.bot.Send(c, bot.Reaction, message.Channel, reaction, message)
} }