Several changes in answer generation in markov module

This commit is contained in:
Andreas Neue 2016-11-03 18:04:40 +01:00
parent 835f3349d6
commit 450abd2ced
1 changed files with 18 additions and 15 deletions

View File

@ -44,7 +44,7 @@ func markovHandleMessage(m *irc.Message) {
answerLen, _ := strconv.Atoi(ModParams["markov-answer-len"]) answerLen, _ := strconv.Atoi(ModParams["markov-answer-len"])
respChance, _ := strconv.Atoi(ModParams["markov-response-chance"]) respChance, _ := strconv.Atoi(ModParams["markov-response-chance"])
if rand.Intn(100) <= respChance || strings.HasPrefix(text, ModParams["_nick"]) { if rand.Intn(100) <= respChance || strings.Index(text, ModParams["_nick"]) != -1 {
responseText := markovChain.Generate(answerLen, text) responseText := markovChain.Generate(answerLen, text)
if responseText != "" { if responseText != "" {
go func() { go func() {
@ -161,6 +161,10 @@ func markovNewChain(prefixLen int) *MarkovChain {
// Write parses the bytes into prefixes and suffixes that are stored in MarkovChain. // Write parses the bytes into prefixes and suffixes that are stored in MarkovChain.
func (c *MarkovChain) Write(in string) (int, error) { func (c *MarkovChain) Write(in string) (int, error) {
in = strings.ToLower(in) in = strings.ToLower(in)
if strings.HasPrefix(in, strings.ToLower(ModParams["_nick"])) {
tok := strings.Split(in, " ")
in = strings.Replace(in, tok[0]+" ", "", 0)
}
sr := strings.NewReader(in) sr := strings.NewReader(in)
p := make(MarkovPrefix, c.prefixLen) p := make(MarkovPrefix, c.prefixLen)
for { for {
@ -180,24 +184,20 @@ func (c *MarkovChain) Write(in string) (int, error) {
// Generate returns a string of at most n words generated from MarkovChain. // Generate returns a string of at most n words generated from MarkovChain.
func (c *MarkovChain) Generate(n int, in string) string { func (c *MarkovChain) Generate(n int, in string) string {
in = strings.ToLower(in) in = strings.ToLower(in)
if strings.HasPrefix(in, strings.ToLower(ModParams["_nick"])) {
tok := strings.Split(in, " ")
in = strings.Replace(in, tok[0]+" ", "", 0)
}
c.mu.Lock() c.mu.Lock()
defer c.mu.Unlock() defer c.mu.Unlock()
p := make(MarkovPrefix, c.prefixLen) p := make(MarkovPrefix, c.prefixLen)
p = strings.Split(in, " ") p = strings.Split(in, " ")
for { if len(p) < c.prefixLen {
if len(p) == c.prefixLen { p = append(p, "")
break }
} if len(p) > c.prefixLen {
if len(p) < c.prefixLen { i := rand.Intn(len(p) - 1)
p = append(p, "") p = p[i : i+c.prefixLen]
}
if len(p) > c.prefixLen {
if rand.Intn(2) == 0 {
p = p[1:]
} else {
p = p[0 : len(p)-1]
}
}
} }
prefix := p.String() prefix := p.String()
var words []string var words []string
@ -208,6 +208,9 @@ func (c *MarkovChain) Generate(n int, in string) string {
} }
next := choices[rand.Intn(len(choices))] next := choices[rand.Intn(len(choices))]
words = append(words, next) words = append(words, next)
if strings.HasSuffix(next, ".") || strings.HasSuffix(next, "!") || strings.HasSuffix(next, "?") {
break
}
p.Shift(next) p.Shift(next)
} }
if len(words) == 0 { if len(words) == 0 {