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 ¶
- Variables
- func CrunchSpace(s string) string
- func Get(m map[string]interface{}, keys ...string) interface{}
- func HomeDotDir() string
- func JSON(m map[string]interface{}) string
- func JSONString(st interface{}) string
- func Print(m map[string]interface{})
- func Set(m map[string]interface{}, p ...interface{})
- func SetDef(m map[string]interface{}, p ...interface{})
- func SpaceToRegx(s string) string
- type Regx
- type Responder
- type State
- func (jc *State) ForceSave() error
- func (jc *State) Get(keys ...string) interface{}
- func (jc *State) Path() string
- func (jc *State) Pretty() string
- func (jc *State) Print()
- func (jc *State) Save() error
- func (jc *State) Set(p ...interface{})
- func (jc *State) SetDef(p ...interface{})
- func (jc *State) String() string
- type TextBot
- func (tb *TextBot) Add(responders ...Responder) *TextBot
- func (tb *TextBot) Get(keys ...string) interface{}
- func (tb *TextBot) Keys() []string
- func (tb *TextBot) LockFor(r Responder)
- func (tb *TextBot) Pretty() string
- func (tb *TextBot) Print()
- func (tb *TextBot) PrintResponseTo(text string)
- func (tb *TextBot) Respond()
- func (tb *TextBot) RespondTo(text string) string
- func (tb *TextBot) RespondToREPL()
- func (tb *TextBot) Save()
- func (tb *TextBot) Set(p ...interface{})
- func (tb *TextBot) SetDef(p ...interface{})
- func (tb *TextBot) String() string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var MissingParams = errors.New("Missing one or more parameters.")
var MustBeDataType = errors.New("Must be a Data type.")
Functions ¶
func CrunchSpace ¶
CrunchSpace is the fastest possible method to crunch all unicode spaces into a single space, the first one detected.
func HomeDotDir ¶
func HomeDotDir() 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 SpaceToRegx ¶
SpaceToRegx converts any whitespace between the fields of the string into \s+ suitable for use to compile as a regular expression.
Types ¶
type Regx ¶
func X ¶
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.
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 ¶
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.
type TextBot ¶
type TextBot struct {
// contains filtered or unexported fields
}
func (*TextBot) PrintResponseTo ¶
func (*TextBot) RespondToREPL ¶
func (tb *TextBot) RespondToREPL()