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
2017-08-07 15:33:58 +02:00

58 lines
1,002 B
Go

package main
import (
"log"
"os"
"strings"
"io/ioutil"
"path/filepath"
"github.com/hamcha/clessy/tg"
"github.com/zgiber/mctext"
)
var orogen *mctext.MChain
func initoroscopo() {
counter := 0
corpus := ""
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
}
corpus += string(data)
counter++
return nil
})
orogen = mctext.New()
orogen.Parse(strings.NewReader(corpus))
log.Printf("[oroscopo] Loaded corpus from %d files\n", counter)
}
func oroscopo(broker *tg.Broker, update tg.APIMessage) {
if isCommand(update, "oroscopo") {
txt := ""
tries := 0
for tries < 10 {
txt = orogen.Generate("", 100)
idx := strings.LastIndexByte(txt, '.')
if idx < 10 {
tries++
continue
}
txt = txt[:idx]
break
}
broker.SendTextMessage(update.Chat, "<b>Ecco cosa dicono le stelle:</b>\n"+txt+".", &update.MessageID)
}
}