Merge pull request #35 from velour/BabblerSeed

stk5's wish come true
This commit is contained in:
Chris Sexton 2016-10-20 09:36:53 -04:00 committed by GitHub
commit 2991331ccb
2 changed files with 91 additions and 11 deletions

View File

@ -78,13 +78,25 @@ func (p *BabblerPlugin) Message(message msg.Message) bool {
lowercase := strings.ToLower(message.Body) lowercase := strings.ToLower(message.Body)
tokens := strings.Fields(lowercase) tokens := strings.Fields(lowercase)
numTokens := len(tokens)
if len(tokens) == 2 && tokens[1] == "says" { if numTokens >= 2 && tokens[1] == "says" {
saying := p.babble(tokens[0]) if numTokens > 3 {
p.Bot.SendMessage(message.Channel, "try seabass says [seed-token]")
return true
}
var saying string
if len(tokens) == 2 {
saying = p.babble(tokens[0])
} else {
saying = p.babbleSeed(tokens[0], tokens[2])
}
if saying == "" { if saying == "" {
p.Bot.SendMessage(message.Channel, "Ze ain't said nothin'") p.Bot.SendMessage(message.Channel, "Ze ain't said nothin'")
} } else {
p.Bot.SendMessage(message.Channel, saying) p.Bot.SendMessage(message.Channel, saying)
}
return true return true
} else if len(tokens) == 4 && strings.Index(lowercase, "initialize babbler for ") == 0 { } else if len(tokens) == 4 && strings.Index(lowercase, "initialize babbler for ") == 0 {
who := tokens[3] who := tokens[3]
@ -223,16 +235,17 @@ func addToMarkovChain(babble *babbler, phrase string) {
} }
func newBabbler() *babbler { func newBabbler() *babbler {
return &babbler{ start := &node{
start: &node{
wordFrequency: 0, wordFrequency: 0,
arcs: map[string]*arc{}, arcs: map[string]*arc{},
}, }
return &babbler{
start: start,
end: &node{ end: &node{
wordFrequency: 0, wordFrequency: 0,
arcs: map[string]*arc{}, arcs: map[string]*arc{},
}, },
lookup: map[string]*node{}, lookup: map[string]*node{"": start},
} }
} }
@ -260,12 +273,19 @@ func getMarkovChain(db *sqlx.DB, who string) (*babbler, error) {
} }
func (p *BabblerPlugin) babble(who string) string { func (p *BabblerPlugin) babble(who string) string {
return p.babbleSeed(who, "")
}
func (p *BabblerPlugin) babbleSeed(who, seed string) string {
if babbler, ok := p.babblers[who]; ok { if babbler, ok := p.babblers[who]; ok {
if len(babbler.start.arcs) == 0 { if len(babbler.start.arcs) == 0 {
return "" return ""
} }
words := []string{} words := []string{seed}
cur := babbler.start var cur *node
if cur, ok = babbler.lookup[seed]; !ok {
return fmt.Sprintf("%s hasn't used the word '%s'", who, seed)
}
for cur != babbler.end { for cur != babbler.end {
which := rand.Intn(cur.wordFrequency) which := rand.Intn(cur.wordFrequency)
sum := 0 sum := 0
@ -279,7 +299,7 @@ func (p *BabblerPlugin) babble(who string) string {
} }
} }
return strings.Join(words, " ") return strings.TrimSpace(strings.Join(words, " "))
} }
return fmt.Sprintf("could not find babbler: %s", who) return fmt.Sprintf("could not find babbler: %s", who)

View File

@ -25,6 +25,28 @@ func makeMessage(payload string) msg.Message {
} }
} }
func TestBabblerNoBabbler(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
c.config.Babbler.DefaultUsers = []string{"seabass"}
assert.NotNil(t, c)
res := c.Message(makeMessage("!seabass2 says"))
assert.Len(t, mb.Messages, 1)
assert.True(t, res)
assert.Contains(t, mb.Messages[0], "could not find babbler: seabass2")
}
func TestBabblerNothingSaid(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
c.config.Babbler.DefaultUsers = []string{"seabass"}
assert.NotNil(t, c)
res := c.Message(makeMessage("!seabass says"))
assert.Len(t, mb.Messages, 1)
assert.True(t, res)
assert.Contains(t, mb.Messages[0], "Ze ain't said nothin")
}
func TestBabbler(t *testing.T) { func TestBabbler(t *testing.T) {
mb := bot.NewMockBot() mb := bot.NewMockBot()
c := New(mb) c := New(mb)
@ -45,6 +67,44 @@ func TestBabbler(t *testing.T) {
assert.Contains(t, mb.Messages[0], "message") assert.Contains(t, mb.Messages[0], "message")
} }
func TestBabblerSeed(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
c.config.Babbler.DefaultUsers = []string{"seabass"}
assert.NotNil(t, c)
seabass := makeMessage("This is a message")
seabass.User = &user.User{Name: "seabass"}
res := c.Message(seabass)
assert.Len(t, c.babblers, 1)
seabass.Body = "This is another message"
res = c.Message(seabass)
seabass.Body = "This is a long message"
res = c.Message(seabass)
res = c.Message(makeMessage("!seabass says long"))
assert.Len(t, mb.Messages, 1)
assert.True(t, res)
assert.Contains(t, mb.Messages[0], "long message")
}
func TestBabblerBadSeed(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
c.config.Babbler.DefaultUsers = []string{"seabass"}
assert.NotNil(t, c)
seabass := makeMessage("This is a message")
seabass.User = &user.User{Name: "seabass"}
res := c.Message(seabass)
assert.Len(t, c.babblers, 1)
seabass.Body = "This is another message"
res = c.Message(seabass)
seabass.Body = "This is a long message"
res = c.Message(seabass)
res = c.Message(makeMessage("!seabass says long message"))
assert.Len(t, mb.Messages, 1)
assert.True(t, res)
assert.Contains(t, mb.Messages[0], "try seabass says [seed-token]")
}
func TestBabblerBatch(t *testing.T) { func TestBabblerBatch(t *testing.T) {
mb := bot.NewMockBot() mb := bot.NewMockBot()
c := New(mb) c := New(mb)