witai

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

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

Go to latest
Published: Oct 18, 2015 License: MIT Imports: 5 Imported by: 0

README

wit.ai GoDoc

Go library for wit.ai Natural Language Processing API. This library integrates with libwit locally and can perform both blocking and non-blocking voice queries using wit.ai service.

Dependencies

In order to start using this library you will need an account on https://wit.ai/. Make sure you complete the Quick Start guide to get yourself all set up.

Other dependencies:

  • Latest libwit (can be downloaded from wit.ai Releases).
  • libsox (brew install sox on OSX).
  • libcurl (brew install curl on OSX).

Usage

package main

import (
	"flag"
	"log"

	"github.com/ianschenck/envflag"
	"github.com/neurodrone/witai"
)

func main() {
	var (
		// This points to the client access token for your account on wit.ai, that you
		// will possibly have stored within an environment variable.
		accessToken = envflag.String("ACCESS_TOKEN", "", "WIT client access token")

		// The recording device you will use for voice queries.
		// Usually, you can leave it be default.
		device = flag.String("device", witai.DefaultDevice, "device name for recording input")
	)
	envflag.Parse()
	flag.Parse()

	// Create a new wit-ai context that will be used for queries.
	ctx, err := witai.NewContext(*device, *accessToken, witai.Error)
	if err != nil {
		log.Fatalln("cannot create new wit-ai context:", err)
	}

	// Always make sure to close the context once you are done.
	defer ctx.Close()

	log.Println("Say something nice now: ...")

	done := make(chan struct{})

	// Query the wit.ai voice service asyncly.
	if err := ctx.VoiceQueryAutoAsync(func(s string) {
		r, err := witai.NewResult(s)
		if err != nil || !r.IsValid() {
			return
		}
		log.Printf("Result: %q\n", r.Outcomes[0].Text)

		// We can exit now that we have the result.
		close(done)
	}); err != nil {
		log.Fatalln("cannot query wit-ai:", err)
	}

	// Wait exiting the process until the async result returns.
	<-done
}

Installation

As simple as:

go get github.com/neurodrone/witai

Documentation

Index

Constants

View Source
const (
	// DefaultDevice points to the default recording device on your machine.
	DefaultDevice = "default"
)

Variables

View Source
var (
	// ErrInvalidResult is returned when the returned wit.ai result
	// does not contain a valid response.
	ErrInvalidResult = errors.New("invalid result")
)

Functions

This section is empty.

Types

type Context

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

Context holds the wit context and the access token and the logic to make text and voice queries to wit.ai.

func NewContext

func NewContext(dev string, access_token string, v Verbosity) (*Context, error)

NewContext returns a *Context given a device, access token and the level of logging verbosity.

func (*Context) Close

func (c *Context) Close() error

Close closes the context that is used for relaying queries and their results back.

func (*Context) TextQuery

func (c *Context) TextQuery(query string) (*Result, *Outcome, error)

TextQuery allows querying wit.ai service with a text query string. It returns the complete result, the first outcome or a non-nil error if one occurs.

The result is returned in a blocking manner.

func (*Context) TextQueryAsync

func (c *Context) TextQueryAsync(query string, cb func(string)) error

TextQueryAsync works similar to TextQuery but is not blocking in nature. It needs callback to process the response once it arrives. If there is an error registering the callback it is returned immediately.

func (*Context) VoiceQueryAuto

func (c *Context) VoiceQueryAuto() (*Result, *Outcome, error)

VoiceQueryAuto is used to query wit.ai service using voice commands via the given input device. It returns the complete result, the first outcome or a non-nil error if one occurs.

This query is run in a blocking manner.

func (*Context) VoiceQueryAutoAsync

func (c *Context) VoiceQueryAutoAsync(cb func(string)) error

VoiceQueryAutoAsync works similar to VoiceQueryAuto but isn't blocking in nature. It registers a callback, which is called once the voice command query returns.

func (*Context) VoiceQueryStart

func (c *Context) VoiceQueryStart() error

VoiceQueryStart starts voice recording and returns immediately. The voice recording is stopped using VoiceQueryStop or VoiceQueryStopAsync command.

func (*Context) VoiceQueryStop

func (c *Context) VoiceQueryStop() (*Result, *Outcome, error)

VoiceQueryStop stops the voice recording and issues the voice query to wit.ai and blockingly waits for the response.

func (*Context) VoiceQueryStopAsync

func (c *Context) VoiceQueryStopAsync(cb func(string)) error

VoiceQueryStopAsync stops the voice recording and issues the voice query to wit.ai in a non-blocking manner.

type Outcome

type Outcome struct {
	Text       string  `json:"_text"`
	Confidence float32 `json:"confidence"`
	Intent     string  `json:"intent"`
}

A Outcome points to each each wit.ai outcome contained within the returned Result for a given text or voice query.

type Result

type Result struct {
	Text     string     `json:"_text"`
	MsgID    string     `json:"msg_id"`
	Outcomes []*Outcome `json:"outcomes"`
}

Result contains the wit.ai result with a list of outcomes.

func NewResult

func NewResult(in string) (*Result, error)

NewResult transforms an input string into a Result object. It returns an error if there's any issue parsing the input.

func (*Result) IsValid

func (r *Result) IsValid() bool

IsValid returns true if the Result struct contains at least one proper outcome with a valid result.

type Verbosity

type Verbosity uint

Verbosity controls the verbosity of wit_* commands' logging levels.

const (
	Error Verbosity // 1
	Warn            // 2
	Info            // 3
	Debug           // 4
)

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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