textbot

package module
v0.0.0-...-1017c3c Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2020 License: AGPL-3.0 Imports: 13 Imported by: 0

README

Natural language framework for creating text-based bots leveraging regular expressions and using a parallel responder implementation.

Documentation

Overview

Example
package main

import (
	"fmt"

	tb "github.com/robmuh/go-textbot"
)

var greet1 = tb.X(`How's it goin there, Mr. Rob?`)
var greet2 = tb.X(`(?i)how's it goin there, mr. rob?`)
var greet3 = tb.X(`(?i)how('|\si)s it goin(g|')? there,? mr.? rob`)

func main() {
	text := "How's   it goin  there, Mr. Rob?"

	fmt.Println(greet1.MatchString(text))
	fmt.Println(greet1.Is(text))
	fmt.Println(greet2.Is(text))
	fmt.Println(greet3.Is(text))

	fmt.Println()

	text = "how's it going there mr rob"
	fmt.Println(greet1.Is(text))
	fmt.Println(greet2.Is(text))
	fmt.Println(greet3.Is(text))

	fmt.Println()

	text = "There is   a lot   of space here."
	fmt.Println(tb.CrunchSpace(text))
	fmt.Println(tb.SpaceToRegx(text))

}
Output:

true
true
true
true

false
false
true

There is a lot of space here.
There\s+is\s+a\s+lot\s+of\s+space\s+here.

Index

Examples

Constants

This section is empty.

Variables

View Source
var MissingParams = errors.New("Missing one or more parameters.")
View Source
var MustBeDataType = errors.New("Must be a Data type.")

Functions

func CrunchSpace

func CrunchSpace(s string) string

CrunchSpace is the fastest possible method to crunch all unicode spaces into a single space, the first one detected.

func Get

func Get(m map[string]interface{}, keys ...string) interface{}

func HomeDotDir

func HomeDotDir() string

func JSON

func JSON(m map[string]interface{}) string

func JSONString

func JSONString(st interface{}) string

JSONString can be used to easily fulfill the String() method of the Responder interface:

func (r *Responder) String() string { return tb.JSONString(r) }

Returns the marshalled JSON of the structure (st) or a JSON map with only ERROR set to the error encountered during marshalling.

func Print

func Print(m map[string]interface{})
Example
d := map[string]interface{}{}
Set(d, "org", "people", "robmuh", "name", "Mr. Rob")
Set(d, "org", "people", "robmuh", "age", 50)
Set(d, "org", "people", "robmuh", "local", true)
Set(d, "org", "people", "robmuh", "empty", nil)
Set(d, "org", "people", "betropper", "name", "Ben")
Set(d, "org", "people", "betropper", "local", false)

Print(d)
Output:

{
  "org": {
    "people": {
      "betropper": {
        "local": false,
        "name": "Ben"
      },
      "robmuh": {
        "age": 50,
        "empty": null,
        "local": true,
        "name": "Mr. Rob"
      }
    }
  }
}

func Set

func Set(m map[string]interface{}, p ...interface{})

func SetDef

func SetDef(m map[string]interface{}, p ...interface{})

func SpaceToRegx

func SpaceToRegx(s string) string

SpaceToRegx converts any whitespace between the fields of the string into \s+ suitable for use to compile as a regular expression.

Types

type Regx

type Regx struct {
	*re.Regexp
}

func X

func X(s string) *Regx

Regx is just shorthand for regexp.MustCompile. It's worth nothing, however, that all text input (args or repl) has white space trimmed before passed to any comparison, which simplifies the expressions needed. Case, however, has to be explicitly ignored if that is what you want (to preserve names and such in some responders).

By the way, you REALLY want to use/learn regular expressions for creating the most "natural" responders because they allow so much more variation in your interactions.

func (*Regx) Has

func (r *Regx) Has(s string) []string

func (*Regx) Is

func (r *Regx) Is(s string) bool

type Responder

type Responder interface {

	// Consistently returns the same UUID for this specific
	// responder. Usually this is a hard-coded constant. The UUID is
	// always associated with this specific responder in this
	// specific package. If the package location or name changes in
	// any way the UUID needs to be reset.
	UUID() string

	// Keys are the names of the context keys used in the RespondTo
	// context State. This provides visibility into the keys for
	// other responder classes or textbots when managing the context
	// state. In some sense, this allows context garbage collection.
	// When a responder is removed from a bot the bot can clear the
	// context state keys for that bot so long as no other responder
	// also needs those keys.
	Keys() []string

	// RespondTo processes a natural language text input in a given
	// context represented by the State (which is usually the state
	// of the TextBot itself). It returns a blank string unless
	// there is something significant to say/respond.
	RespondTo(text string, context *State) string

	// Must return a JSON representation of the current responder
	// state (not to be confused with the context of the RespondTo()
	// method). Can be empty ({}).
	String() string
}

type State

type State struct {

	// Dir defaults to a full path to a home directory named after
	// the running executable with a dot in front ("~/.myproggy").
	Dir string `json:"dir,omitempty"`

	// File name defaults to "cache.json", which will be placed into the
	// Dir directory.
	File string `json:"file,omitempty"`

	// Data is the actual key/value data.
	Data map[string]interface{} `json:"data"`

	// Every indicates the duration interval for automatic Saves (if any).
	Every string `json:"every,omitempty"`
	// contains filtered or unexported fields
}

func NewState

func NewState(args ...string) *State

NewState accepts up to three variadic arguments and returns a new State pointer. First argument is the Dir path string. Second is File name. Third is a parsable time.Duration string for the interval of Every automatic Save.

func (*State) ForceSave

func (jc *State) ForceSave() error

func (*State) Get

func (jc *State) Get(keys ...string) interface{}

func (*State) Path

func (jc *State) Path() string

func (*State) Pretty

func (jc *State) Pretty() string

func (*State) Print

func (jc *State) Print()

func (*State) Save

func (jc *State) Save() error

Save saves the file if it needs saving. No attempt to check if something else has modified the file is made.

func (*State) Set

func (jc *State) Set(p ...interface{})

func (*State) SetDef

func (jc *State) SetDef(p ...interface{})

func (*State) String

func (jc *State) String() string

type TextBot

type TextBot struct {
	// contains filtered or unexported fields
}

func New

func New(responders ...Responder) *TextBot

func (*TextBot) Add

func (tb *TextBot) Add(responders ...Responder) *TextBot

func (*TextBot) Get

func (tb *TextBot) Get(keys ...string) interface{}

func (*TextBot) Keys

func (tb *TextBot) Keys() []string

func (*TextBot) LockFor

func (tb *TextBot) LockFor(r Responder)

func (*TextBot) Pretty

func (tb *TextBot) Pretty() string

func (*TextBot) Print

func (tb *TextBot) Print()

func (*TextBot) PrintResponseTo

func (tb *TextBot) PrintResponseTo(text string)

func (*TextBot) Respond

func (tb *TextBot) Respond()

func (*TextBot) RespondTo

func (tb *TextBot) RespondTo(text string) string

func (*TextBot) RespondToREPL

func (tb *TextBot) RespondToREPL()

func (*TextBot) Save

func (tb *TextBot) Save()

func (*TextBot) Set

func (tb *TextBot) Set(p ...interface{})

func (*TextBot) SetDef

func (tb *TextBot) SetDef(p ...interface{})

func (*TextBot) String

func (tb *TextBot) String() string

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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