package main import ( "log" "os" "strings" "io/ioutil" "path/filepath" "github.com/hamcha/clessy/tg" gomarkov "github.com/simon-weber/gomarkov" ) var orogen *gomarkov.Chain func initoroscopo() { counter := 0 orogen = gomarkov.NewChain() filepath.Walk(*oropath, func(path string, info os.FileInfo, err error) error { if info.IsDir() { return nil } data, err := ioutil.ReadFile(path) if err != nil { return err } orogen.Update(string(data)) counter++ return nil }) log.Printf("[oroscopo] Loaded corpus from %d files\n", counter) } func oroscopo(broker *tg.Broker, update tg.APIMessage) { if isCommand(update, "oroscopo") { var err error var txt string tries := 0 for tries < 10 { txt, err = orogen.Respond("", 20, 100) if err != nil { txt = err.Error() tries++ continue } idx := strings.LastIndexByte(txt, '.') if idx < 10 { tries++ continue } txt = txt[:idx] break } broker.SendTextMessage(update.Chat, "Ecco cosa dicono le stelle:\n"+txt+".", &update.MessageID) } }