This repository has been archived on 2023-07-05. You can view files and clone it, but cannot push or open issues or pull requests.
clessy/mods/oroscopo.go

74 lines
1.3 KiB
Go
Raw Permalink Normal View History

2017-08-07 13:19:14 +00:00
package main
import (
"log"
"os"
"strings"
2017-08-07 13:42:39 +00:00
"unicode"
2017-08-07 13:19:14 +00:00
"io/ioutil"
"path/filepath"
2018-11-08 18:55:34 +00:00
"git.fromouter.space/hamcha/tg"
2017-08-07 13:39:48 +00:00
gomarkov "github.com/simon-weber/gomarkov"
2017-08-07 13:19:14 +00:00
)
2017-08-07 13:39:48 +00:00
var orogen *gomarkov.Chain
2017-08-07 13:19:14 +00:00
2018-05-24 15:48:35 +00:00
func oroscopo_init() {
2017-08-07 13:19:14 +00:00
counter := 0
2017-08-07 13:39:48 +00:00
orogen = gomarkov.NewChain()
2017-08-07 13:19:14 +00:00
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
}
2017-08-07 13:39:48 +00:00
orogen.Update(string(data))
2017-08-07 13:19:14 +00:00
counter++
return nil
})
log.Printf("[oroscopo] Loaded corpus from %d files\n", counter)
}
2018-05-24 15:48:35 +00:00
func oroscopo_message(broker *tg.Broker, update tg.APIMessage) {
2017-08-07 13:19:14 +00:00
if isCommand(update, "oroscopo") {
2017-08-07 13:39:48 +00:00
var err error
var txt string
2017-08-07 13:33:58 +00:00
tries := 0
for tries < 10 {
2017-08-07 13:39:48 +00:00
txt, err = orogen.Respond("", 20, 100)
if err != nil {
txt = err.Error()
tries++
continue
}
2017-08-07 13:42:39 +00:00
// Find end
2017-08-07 13:19:14 +00:00
idx := strings.LastIndexByte(txt, '.')
if idx < 10 {
2017-08-07 13:33:58 +00:00
tries++
2017-08-07 13:19:14 +00:00
continue
}
txt = txt[:idx]
2017-08-07 13:42:39 +00:00
// Find beginning
idx = strings.IndexFunc(txt, unicode.IsUpper)
if idx < 0 {
tries++
continue
}
txt = txt[idx:]
2017-08-07 13:33:58 +00:00
break
2017-08-07 13:19:14 +00:00
}
2018-12-13 10:29:45 +00:00
broker.SendTextMessage(update.Chat, "<b>Ecco cosa dicono le stelle:</b>\n"+txt+".", &tg.MessageOptions{
ReplyID: &update.MessageID,
})
2017-08-07 13:19:14 +00:00
}
}