From 258a3370d781b43b7b1ad7f011202d0659916596 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Fri, 27 Sep 2019 10:39:43 -0400 Subject: [PATCH] reaction: add sentiment analysis; remove harass The harass stuff may come back later. --- go.mod | 1 + go.sum | 4 ++++ plugins/reaction/reaction.go | 46 ++++++++++++++++-------------------- 3 files changed, 25 insertions(+), 26 deletions(-) diff --git a/go.mod b/go.mod index 78e1799..5ae6707 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ require ( github.com/PuerkitoBio/goquery v1.5.0 github.com/armon/go-radix v1.0.0 // indirect 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/gonum/floats v0.0.0-20181209220543-c233463c7e82 // indirect github.com/gonum/internal v0.0.0-20181124074243-f884aa714029 // indirect diff --git a/go.sum b/go.sum index 8ebbfb2..4184663 100644 --- a/go.sum +++ b/go.sum @@ -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/armon/go-radix v1.0.0 h1:F4z6KzEeeQIMeLFa97iZU6vupzoecKdU5TX24SNppXI= 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/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/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= diff --git a/plugins/reaction/reaction.go b/plugins/reaction/reaction.go index a4a5f55..b2fddae 100644 --- a/plugins/reaction/reaction.go +++ b/plugins/reaction/reaction.go @@ -3,8 +3,10 @@ package reaction import ( + "github.com/rs/zerolog/log" "math/rand" + "github.com/chrissexton/sentiment" "github.com/velour/catbase/bot" "github.com/velour/catbase/bot/msg" "github.com/velour/catbase/config" @@ -13,51 +15,43 @@ import ( type ReactionPlugin struct { bot bot.Bot config *config.Config + + model sentiment.Models } 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{ bot: b, config: b.Config(), + model: model, } b.Register(rp, bot.Message, rp.message) return rp } 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) - negativeWeight := 1 - if harrass { - chance = p.config.GetFloat64("Reaction.HarrassChance", 0.05) - negativeWeight = p.config.GetInt("Reaction.NegativeHarrassmentMultiplier", 2) - } - if rand.Float64() < chance { - numPositiveReactions := len(p.config.GetArray("Reaction.PositiveReactions", []string{})) - numNegativeReactions := len(p.config.GetArray("Reaction.NegativeReactions", []string{})) + analysis := p.model.SentimentAnalysis(message.Body, sentiment.English) - maxIndex := numPositiveReactions + numNegativeReactions*negativeWeight + log.Debug(). + Uint8("score", analysis.Score). + Str("body", message.Body). + Msg("sentiment of statement") - index := rand.Intn(maxIndex) - - reaction := "" - - if index < numPositiveReactions { - reaction = p.config.GetArray("Reaction.PositiveReactions", []string{})[index] + var reactions []string + if analysis.Score > 0 { + reactions = p.config.GetArray("Reaction.PositiveReactions", []string{}) } else { - index -= numPositiveReactions - index %= numNegativeReactions - reaction = p.config.GetArray("Reaction.NegativeReactions", []string{})[index] + reactions = p.config.GetArray("Reaction.NegativeReactions", []string{}) } + reaction := reactions[rand.Intn(len(reactions))] + p.bot.Send(c, bot.Reaction, message.Channel, reaction, message) }