Prose implements simple language-processing by using simple regexps to recognize certain "meaning entities" inside a message.
It's good at :
- Understanding a command request and its parameters out of a human message
It's not good at :
- Understanding the meaning of a message outside pre-defined scopes
Extended documentation can be found here.
You can install package simply by
go get github.com/snwfdhmp/prose
Then you can use it simply by importing github.com/snwfdhmp/prose
.
Full documentation can be found here.
An entity object contains a compiled regexp that will match strings if the entity exists in it. You can consider an entity as a named-regexp.
Example:
An entity can check words ...
what := prose.NewEntity("what", prose.RegexpWords("what"))
... it can also detect topics or abstract concepts ...
sport := prose.NewEntity("sport", prose.RegexpWords("football", "basketball", "hockey"))
love := prose.NewEntity("love", prose.RegexpWords("love", "romance", "wife"))
... or even character types.
number := prose.NewEntity("number", "[1-9]")
All that you can track with regexps, you can track it with prose.
An action is an object containing a function that will be run in a particular context.
Example :
An action can be a shell command ...
ai := prose.NewAI("Jarvis", logrus.New())
action := ai.NewCommandAction("echo 'Ran at $(date +%H:%M:%S)'")
... or a custom function of your choice.
ai := prose.NewAI("Jarvis", logrus.New())
run := func (a *prose.Action) error {
ai.Logger.Println("I'm an awesome func.")
io.WriteString(a.Writer, "And I can also write to streams.")
}
action := prose.NewAction(run)
An AI is an object containing the executive part of the scheme. It will reference actions to run, and will process the input string from (the user|an http request|a slack message|whatever)
They are simply created by :
ai := prose.NewAI("Jarvis", logrus.New())
package main
import (
"fmt"
"os"
gotime "time"
"github.com/sirupsen/logrus"
"github.com/snwfdhmp/prose"
)
var (
log = logrus.New()
)
func main() {
ai := prose.NewAI("Jarvis", log) // We create an AI named Jarvis
// Let's create an entity matching 'what'
what := prose.NewEntity("what", prose.RegexpWords("what"))
// ...and another matching 'time'
time := prose.NewEntity("time", prose.RegexpWords("time"))
// Now we create an action : writing time
sendTime := prose.NewAction(func(a *prose.Action) error {
msg := fmt.Sprintf("It's %s\n", gotime.Now().Format("15:04"))
a.Write(msg)
return nil
})
sendTime.On(what, time) // it'll be run if 'what' and 'time' are in the tested strign
ai.Handle(sendTime) // it'll be run by ai
input := "Hey ! What time is it ?" //Let's say that's our user input
actions := ai.Process(input) // Our AI processes the input and decide which actions to run
// Now we tell the AI to runs the actions, to output to w, and to stop at first error
errs := ai.Run(actions, os.Stdout, true) // note that we receive a slice of errors
if len(errs) > 0 {
log.Errorln("Errors:", errs)
return
}
}
Let's run the example above :
$ go run example.go
It's 01:53
Click my awesome GitHub profile, the 1000000th visitor will be offered a special gift.