From 44d3a4d18ffd228e2a0390212b863a47b33b04f8 Mon Sep 17 00:00:00 2001 From: Hamcha Date: Mon, 7 Aug 2017 15:19:14 +0200 Subject: [PATCH] oroscopo: DOES THIS WORK? GEE I WONDER --- mods/main.go | 6 ++++++ mods/oroscopo.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 mods/oroscopo.go diff --git a/mods/main.go b/mods/main.go index 3e57090..c9f10fc 100644 --- a/mods/main.go +++ b/mods/main.go @@ -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() diff --git a/mods/oroscopo.go b/mods/oroscopo.go new file mode 100644 index 0000000..992a393 --- /dev/null +++ b/mods/oroscopo.go @@ -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, "Ecco cosa dicono le stelle:\n"+txt, &update.MessageID) + return + } + } +}