prose

package module
v0.0.0-...-ff4efc8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 25, 2019 License: MIT Imports: 5 Imported by: 0

README

Prose

Introduction

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.

Summary

Get Started

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.

Entity

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.

Action

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)

AI

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())

Example

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

Author

Click my awesome GitHub profile, the 1000000th visitor will be offered a special gift.

Documentation

Overview

Package prose is used to perform language-processing by the use of entity-matching.

GitHub: http://github.com/snwfdhmp/prose Developer: snwfdhmp

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegexpWords

func RegexpWords(words ...string) *regexp.Regexp

RegexpWords returns a regexp matching if one word in params is present in string (case insensitive)

Types

type AI

type AI struct {
	Name    string      //Name of the AI. Will be used for example in conversations
	Actions []*Action   //Actions the AI can handle
	Logger  *log.Logger //Logger to log to
}

AI represents a Bot

func NewAI

func NewAI(name string, logger *log.Logger) *AI

NewAI creates an AI with name and logger in params, and will initialize Actions to an empty []*Action

func (*AI) Handle

func (ai *AI) Handle(actions ...*Action)

Handle will add actions in params to actions that AI can handle

func (*AI) NewCommandAction

func (ai *AI) NewCommandAction(command string) *Action

NewCommandAction is a shortcut used to provide shell-command-running actions

func (*AI) NewCommandActionFunc

func (ai *AI) NewCommandActionFunc(command string) func(a *Action) error

NewCommandActionFunc is a shortcut used to render a Run function executing a shell command

func (*AI) Process

func (ai *AI) Process(input string) []*Action

Process will return a slice of every Action needing to be run according to input and action triggers

func (*AI) Run

func (ai *AI) Run(actions []*Action, w io.Writer, stopIfError bool) []error

Run will run a slice of Action and output to w, it will stop at the first error if stopIfError=true

type Action

type Action struct {
	Writer  io.Writer
	Trigger [][]*Entity
	Run     func(*Action) error
}

Action represents an Action

func NewAction

func NewAction(run func(*Action) error) *Action

NewAction returns a new action that runs the function in params if triggered The functions have to be func(*Action) error, in Action you can use Writer that can change through every execution of Run command if needed (for example for API, chat bot, SMTP, ...)

func (*Action) On

func (a *Action) On(entities ...*Entity)

On adds a trigger to the action, that will be triggered if every entity in params match the tested string

func (*Action) Write

func (a *Action) Write(str string)

type Entity

type Entity struct {
	Name      string
	IsPresent *regexp.Regexp
}

Entity represents an entity

func NewEntity

func NewEntity(name string, regex *regexp.Regexp) *Entity

NewEntity returns a new entity with name and regexp in params The regexp will be used to know if the entity is present or not in a string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL