package main import ( "log" "os" "strings" "unicode" "io/ioutil" "path/filepath" "git.fromouter.space/hamcha/tg" gomarkov "github.com/simon-weber/gomarkov" ) var orogen *gomarkov.Chain func oroscopo_init() { 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_message(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 } // Find end idx := strings.LastIndexByte(txt, '.') if idx < 10 { tries++ continue } txt = txt[:idx] // Find beginning idx = strings.IndexFunc(txt, unicode.IsUpper) if idx < 0 { tries++ continue } txt = txt[idx:] break } broker.SendTextMessage(update.Chat, "Ecco cosa dicono le stelle:\n"+txt+".", &tg.MessageOptions{ ReplyID: &update.MessageID, }) } }