From b4b09e74c675d552be1c4864738b8710d724f7a0 Mon Sep 17 00:00:00 2001 From: skiesel Date: Thu, 21 Mar 2019 21:36:11 -0400 Subject: [PATCH 1/2] Track who said what --- plugins/tldr/tldr.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/plugins/tldr/tldr.go b/plugins/tldr/tldr.go index efaa037..ed82150 100644 --- a/plugins/tldr/tldr.go +++ b/plugins/tldr/tldr.go @@ -19,6 +19,7 @@ var ( type TLDRPlugin struct { Bot bot.Bot History []string + Users []string Index int } @@ -26,6 +27,7 @@ func New(b bot.Bot) *TLDRPlugin { plugin := &TLDRPlugin{ Bot: b, History: []string{}, + Users: []string{}, Index: 0, } b.Register(plugin, bot.Message, plugin.message) @@ -54,6 +56,7 @@ func (p *TLDRPlugin) message(kind bot.Kind, message msg.Message, args ...interfa bestScores := make([]float64, nTopics) bestDocs := make([]string, nTopics) + bestUsers := make([]string, nTopics) dr, dc := docsOverTopics.Dims() for doc := 0; doc < dc; doc++ { @@ -62,6 +65,7 @@ func (p *TLDRPlugin) message(kind bot.Kind, message msg.Message, args ...interfa if score > bestScores[topic] { bestScores[topic] = score bestDocs[topic] = p.History[doc] + bestUsers[topic] = p.Users[doc] } } } @@ -77,17 +81,17 @@ func (p *TLDRPlugin) message(kind bot.Kind, message msg.Message, args ...interfa response := "Here you go captain 'too good to read backlog':\n" for topic := 0; topic < tr; topic++ { - max := -1. - best := "" + bestScore := -1. + bestTopic := "" for word := 0; word < tc; word++ { score := topicsOverWords.At(topic, word) - if score > max { - max = score - best = vocab[word] + if score > bestScore { + bestScore = score + bestTopic = vocab[word] } } - response += fmt.Sprintf("Topic #%d : %s\n", topic, best) - response += fmt.Sprintf("\t%s\n", bestDocs[topic]) + response += fmt.Sprintf("Topic #%d : %s\n", topic, bestTopic) + response += fmt.Sprintf("\t<%s>%s\n", bestUsers[topic], bestDocs[topic]) } p.Bot.Send(bot.Message, message.Channel, response) @@ -100,6 +104,7 @@ func (p *TLDRPlugin) message(kind bot.Kind, message msg.Message, args ...interfa maxHistorySize := p.Bot.Config().GetInt("TLDR.HistorySize", 1000) if currentHistorySize < maxHistorySize { p.History = append(p.History, lowercaseMessage) + p.Users = append(p.Users, message.User.Name) p.Index = 0 } else { if currentHistorySize > maxHistorySize { @@ -112,6 +117,7 @@ func (p *TLDRPlugin) message(kind bot.Kind, message msg.Message, args ...interfa } p.History[p.Index] = lowercaseMessage + p.Users[p.Index] = message.User.Name p.Index++ } } From dbc99bfe7a6ce564448d049c2e17102c5925a7c1 Mon Sep 17 00:00:00 2001 From: skiesel Date: Thu, 21 Mar 2019 21:46:28 -0400 Subject: [PATCH 2/2] Add supporting docs size --- plugins/tldr/tldr.go | 48 +++++++++++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/plugins/tldr/tldr.go b/plugins/tldr/tldr.go index ed82150..e1919be 100644 --- a/plugins/tldr/tldr.go +++ b/plugins/tldr/tldr.go @@ -19,7 +19,7 @@ var ( type TLDRPlugin struct { Bot bot.Bot History []string - Users []string + Users []string Index int } @@ -27,7 +27,7 @@ func New(b bot.Bot) *TLDRPlugin { plugin := &TLDRPlugin{ Bot: b, History: []string{}, - Users: []string{}, + Users: []string{}, Index: 0, } b.Register(plugin, bot.Message, plugin.message) @@ -54,18 +54,28 @@ func (p *TLDRPlugin) message(kind bot.Kind, message msg.Message, args ...interfa return false } - bestScores := make([]float64, nTopics) - bestDocs := make([]string, nTopics) - bestUsers := make([]string, nTopics) + bestScores := make([][]float64, nTopics) + bestDocs := make([][]string, nTopics) + bestUsers := make([][]string, nTopics) + + supportingDocs := p.Bot.Config().GetInt("TLDR.Support", 3) + for i := 0; i < supportingDocs; i++ { + bestScores[i] = make([]float64, supportingDocs) + bestDocs[i] = make([]string, supportingDocs) + bestUsers[i] = make([]string, supportingDocs) + } dr, dc := docsOverTopics.Dims() - for doc := 0; doc < dc; doc++ { - for topic := 0; topic < dr; topic++ { + for topic := 0; topic < dr; topic++ { + minScore, minIndex := min(bestScores[topic]) + + for doc := 0; doc < dc; doc++ { score := docsOverTopics.At(topic, doc) - if score > bestScores[topic] { - bestScores[topic] = score - bestDocs[topic] = p.History[doc] - bestUsers[topic] = p.Users[doc] + if score > minScore { + bestScores[topic][minIndex] = score + bestDocs[topic][minIndex] = p.History[doc] + bestUsers[topic][minIndex] = p.Users[doc] + minScore, minIndex = min(bestScores[topic]) } } } @@ -91,7 +101,9 @@ func (p *TLDRPlugin) message(kind bot.Kind, message msg.Message, args ...interfa } } response += fmt.Sprintf("Topic #%d : %s\n", topic, bestTopic) - response += fmt.Sprintf("\t<%s>%s\n", bestUsers[topic], bestDocs[topic]) + for i := range bestDocs[topic] { + response += fmt.Sprintf("\t<%s>%s\n", bestUsers[topic][i], bestDocs[topic][i]) + } } p.Bot.Send(bot.Message, message.Channel, response) @@ -133,3 +145,15 @@ func (p *TLDRPlugin) help(kind bot.Kind, message msg.Message, args ...interface{ func shouldKeepMessage(message string) bool { return true } + +func min(slice []float64) (float64, int) { + minVal := 1. + minIndex := -1 + for index, val := range slice { + if val < minVal { + minVal = val + minIndex = index + } + } + return minVal, minIndex +}