catbase/plugins/babbler/commands.go

158 lines
3.4 KiB
Go
Raw Permalink Normal View History

package babbler
import (
"fmt"
"strings"
)
2021-02-01 15:45:41 +00:00
func (p *BabblerPlugin) initializeBabbler(who string) string {
_, err := p.getOrCreateBabbler(who)
if err != nil {
2021-02-01 15:45:41 +00:00
return "babbler initialization failed."
}
2021-02-01 15:45:41 +00:00
return "okay."
}
2021-02-01 15:45:41 +00:00
func (p *BabblerPlugin) addToBabbler(babblerName, whatWasSaid string) {
babblerID, err := p.getOrCreateBabbler(babblerName)
if err == nil {
if p.WithGoRoutines {
2021-02-01 15:45:41 +00:00
go p.addToMarkovChain(babblerID, whatWasSaid)
} else {
2021-02-01 15:45:41 +00:00
p.addToMarkovChain(babblerID, whatWasSaid)
}
}
}
2021-02-01 15:45:41 +00:00
func (p *BabblerPlugin) getBabble(who string, tokens []string) string {
_, err := p.getBabbler(who)
if err != nil {
if err == NO_BABBLER {
2017-06-16 19:02:28 +00:00
// return fmt.Sprintf("%s babbler not found.", who), true
2021-02-01 15:45:41 +00:00
return ""
}
} else {
var saying string
2021-02-01 15:45:41 +00:00
if len(tokens) == 0 {
saying, err = p.babble(who)
} else {
2021-02-01 15:45:41 +00:00
saying, err = p.babbleSeed(who, tokens)
}
if err != nil {
if err == SAID_NOTHING {
2021-02-01 15:45:41 +00:00
return fmt.Sprintf("%s hasn't said anything yet.", who)
} else if err == NEVER_SAID {
2021-02-01 15:45:41 +00:00
return fmt.Sprintf("%s never said '%s'", who, strings.Join(tokens, " "))
}
} else if saying != "" {
2021-02-01 15:45:41 +00:00
return saying
}
}
2021-02-01 15:45:41 +00:00
return ""
}
2021-02-01 15:45:41 +00:00
func (p *BabblerPlugin) getBabbleWithSuffix(who string, tokens []string) string {
_, err := p.getBabbler(who)
if err != nil {
if err == NO_BABBLER {
2017-06-16 19:02:28 +00:00
// return fmt.Sprintf("%s babbler not found.", who), true
2021-02-01 15:45:41 +00:00
return ""
}
} else {
2021-02-01 15:45:41 +00:00
saying, err := p.babbleSeedSuffix(who, tokens)
if err != nil {
if err == SAID_NOTHING {
2021-02-01 15:45:41 +00:00
return fmt.Sprintf("%s hasn't said anything yet.", who)
} else if err == NEVER_SAID {
2021-02-01 15:45:41 +00:00
return fmt.Sprintf("%s never said '%s'", who, strings.Join(tokens, " "))
}
} else if saying != "" {
2021-02-01 15:45:41 +00:00
return saying
}
}
2021-02-01 15:45:41 +00:00
return ""
}
2021-02-01 15:45:41 +00:00
func (p *BabblerPlugin) getBabbleWithBookends(who string, start, end []string) string {
_, err := p.getBabbler(who)
if err != nil {
if err == NO_BABBLER {
2017-06-16 19:02:28 +00:00
// return fmt.Sprintf("%s babbler not found.", who), true
2021-02-01 15:45:41 +00:00
return ""
}
} else {
2021-02-01 15:45:41 +00:00
saying, err := p.babbleSeedBookends(who, start, end)
if err != nil {
if err == SAID_NOTHING {
2021-02-01 15:45:41 +00:00
return fmt.Sprintf("%s hasn't said anything yet.", who)
} else if err == NEVER_SAID {
2021-02-01 15:45:41 +00:00
seeds := append(start[1:], "...")
seeds = append(seeds, end...)
2021-02-01 15:45:41 +00:00
return fmt.Sprintf("%s never said '%s'", who, strings.Join(seeds, " "))
}
} else if saying != "" {
2021-02-01 15:45:41 +00:00
return saying
}
}
2021-02-01 15:45:41 +00:00
return ""
}
2021-02-01 15:45:41 +00:00
func (p *BabblerPlugin) batchLearn(tokens []string) string {
who := tokens[3]
babblerId, err := p.getOrCreateBabbler(who)
if err != nil {
2021-02-01 15:45:41 +00:00
return "batch learn failed."
}
body := strings.Join(tokens[4:], " ")
body = strings.ToLower(body)
for _, a := range strings.Split(body, ".") {
for _, b := range strings.Split(a, "!") {
for _, c := range strings.Split(b, "?") {
for _, d := range strings.Split(c, "\n") {
trimmed := strings.TrimSpace(d)
if trimmed != "" {
p.addToMarkovChain(babblerId, trimmed)
}
}
}
}
}
2021-02-01 15:45:41 +00:00
return "phew that was tiring."
}
2021-02-01 15:45:41 +00:00
func (p *BabblerPlugin) merge(who, into string) string {
if who == into {
2021-02-01 15:45:41 +00:00
return "that's annoying. stop it."
}
whoBabbler, err := p.getBabbler(who)
if err != nil {
if err == NO_BABBLER {
2021-02-01 15:45:41 +00:00
return fmt.Sprintf("%s babbler not found.", who)
}
2021-02-01 15:45:41 +00:00
return "merge failed."
}
intoBabbler, err := p.getOrCreateBabbler(into)
if err != nil {
2021-02-01 15:45:41 +00:00
return "merge failed."
}
err = p.mergeBabblers(intoBabbler, whoBabbler, into, who)
if err != nil {
2021-02-01 15:45:41 +00:00
return "merge failed."
}
2021-02-01 15:45:41 +00:00
return "mooooiggged"
}