forked from an/flokati
markov.go: learning after answering (prevents repition of input)
This commit is contained in:
parent
0f080a2946
commit
5f9238a433
2
main.go
2
main.go
|
@ -45,7 +45,7 @@ var (
|
||||||
func main() {
|
func main() {
|
||||||
sayCh = make(chan string, 1024)
|
sayCh = make(chan string, 1024)
|
||||||
|
|
||||||
xlog.Init(xlog.INFO)
|
xlog.Init(xlog.DEBUG)
|
||||||
|
|
||||||
//bot := ircx.Classic(*server, *name)
|
//bot := ircx.Classic(*server, *name)
|
||||||
cfg := ircx.Config{User: *name, MaxRetries: 1000}
|
cfg := ircx.Config{User: *name, MaxRetries: 1000}
|
||||||
|
|
|
@ -37,11 +37,8 @@ func markovHandleMessage(m *irc.Message) {
|
||||||
if text == "" {
|
if text == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
text = markovParseText(text)
|
text = markovParseText(text)
|
||||||
|
|
||||||
markovChain.Write(text)
|
|
||||||
|
|
||||||
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.Index(text, strings.ToLower(ModParams["_nick"])) != -1 {
|
if rand.Intn(100) <= respChance || strings.Index(text, strings.ToLower(ModParams["_nick"])) != -1 {
|
||||||
|
@ -53,6 +50,8 @@ func markovHandleMessage(m *irc.Message) {
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
markovChain.Write(text)
|
||||||
}
|
}
|
||||||
|
|
||||||
func markovRun() {
|
func markovRun() {
|
||||||
|
@ -132,7 +131,7 @@ type MarkovPrefix []string
|
||||||
|
|
||||||
// String returns the Prefix as a string (for use as a map key).
|
// String returns the Prefix as a string (for use as a map key).
|
||||||
func (p MarkovPrefix) String() string {
|
func (p MarkovPrefix) String() string {
|
||||||
return strings.Join(p, " ")
|
return strings.Trim(strings.Join(p, " "), " ")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Shift removes the first word from the Prefix and appends the given word.
|
// Shift removes the first word from the Prefix and appends the given word.
|
||||||
|
@ -163,7 +162,7 @@ func (c *MarkovChain) Write(in string) (int, error) {
|
||||||
in = strings.ToLower(in)
|
in = strings.ToLower(in)
|
||||||
if strings.HasPrefix(in, strings.ToLower(ModParams["_nick"])) {
|
if strings.HasPrefix(in, strings.ToLower(ModParams["_nick"])) {
|
||||||
tok := strings.Split(in, " ")
|
tok := strings.Split(in, " ")
|
||||||
in = strings.Replace(in, tok[0]+" ", "", 0)
|
in = strings.Replace(in, tok[0]+" ", "", 1)
|
||||||
}
|
}
|
||||||
sr := strings.NewReader(in)
|
sr := strings.NewReader(in)
|
||||||
p := make(MarkovPrefix, c.prefixLen)
|
p := make(MarkovPrefix, c.prefixLen)
|
||||||
|
@ -176,6 +175,7 @@ func (c *MarkovChain) Write(in string) (int, error) {
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
c.MarkovChain[key] = append(c.MarkovChain[key], s)
|
c.MarkovChain[key] = append(c.MarkovChain[key], s)
|
||||||
c.mu.Unlock()
|
c.mu.Unlock()
|
||||||
|
xlog.Debug("Chain len: %d, learned [%s] [%s]", len(c.MarkovChain), key, s)
|
||||||
p.Shift(s)
|
p.Shift(s)
|
||||||
}
|
}
|
||||||
return len(in), nil
|
return len(in), nil
|
||||||
|
@ -186,20 +186,18 @@ 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"])) {
|
if strings.HasPrefix(in, strings.ToLower(ModParams["_nick"])) {
|
||||||
tok := strings.Split(in, " ")
|
tok := strings.Split(in, " ")
|
||||||
in = strings.Replace(in, tok[0]+" ", "", 0)
|
in = strings.Replace(in, tok[0]+" ", "", 1)
|
||||||
}
|
}
|
||||||
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, " ")
|
||||||
if len(p) < c.prefixLen {
|
|
||||||
p = append(p, "")
|
|
||||||
}
|
|
||||||
if len(p) > c.prefixLen {
|
if len(p) > c.prefixLen {
|
||||||
i := rand.Intn(len(p) - 1)
|
i := rand.Intn(len(p) - 1)
|
||||||
p = p[i : i+c.prefixLen]
|
p = p[i : i+c.prefixLen]
|
||||||
}
|
}
|
||||||
prefix := p.String()
|
prefix := p.String()
|
||||||
|
xlog.Debug("Looking for answer on [%s]", prefix)
|
||||||
var words []string
|
var words []string
|
||||||
for i := 0; i < n; i++ {
|
for i := 0; i < n; i++ {
|
||||||
choices := c.MarkovChain[p.String()]
|
choices := c.MarkovChain[p.String()]
|
||||||
|
@ -213,9 +211,12 @@ func (c *MarkovChain) Generate(n int, in string) string {
|
||||||
}
|
}
|
||||||
p.Shift(next)
|
p.Shift(next)
|
||||||
}
|
}
|
||||||
|
prefix = strings.Trim(prefix, " ")
|
||||||
if len(words) == 0 {
|
if len(words) == 0 {
|
||||||
return ""
|
xlog.Debug("No answer found")
|
||||||
|
return prefix + " ... pfrrrz"
|
||||||
} else {
|
} else {
|
||||||
|
xlog.Debug("Found words: [%s]", strings.Join(words, " "))
|
||||||
return prefix + " " + strings.Join(words, " ")
|
return prefix + " " + strings.Join(words, " ")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue