oroscopo: DOES THIS WORK? GEE I WONDER

This commit is contained in:
Hamcha 2017-08-07 15:19:14 +02:00
parent 48ff3fe479
commit 44d3a4d18f
2 changed files with 61 additions and 0 deletions

View file

@ -60,6 +60,10 @@ var mods = map[string]Mod{
OnInit: initsearch,
OnMessage: search,
},
"oroscopo": {
OnInit: initoroscopo,
OnMessage: oroscopo,
},
}
func initmods() {
@ -104,6 +108,7 @@ var talktoken *string
var gapifile *string
var gapikey *string
var gapiCtx context.Context
var oropath *string
func main() {
brokerAddr := flag.String("broker", "localhost:7314", "Broker address:port")
@ -117,6 +122,7 @@ func main() {
talktoken = flag.String("apiai", "@apiai.token", "api.ai token")
gapifile = flag.String("gapifile", "gapi.json", "Google API Service Credentials file (for STT)")
gapikey = flag.String("gapikey", "@gapi.key", "Google API key (for search/KG)")
oropath = flag.String("oropath", "/astri", "Path to oroscopo corpus directory")
disable := flag.String("disable", "", "Blacklist mods (separated by comma)")
enable := flag.String("enable", "", "Whitelist mods (separated by comma)")
flag.Parse()

55
mods/oroscopo.go Normal file
View file

@ -0,0 +1,55 @@
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") {
for {
txt := orogen.Generate("", 100)
idx := strings.LastIndexByte(txt, '.')
if idx < 10 {
continue
}
txt = txt[:idx]
broker.SendTextMessage(update.Chat, "<b>Ecco cosa dicono le stelle:</b>\n"+txt, &update.MessageID)
return
}
}
}