wit

package module
v0.0.0-...-4dd03e1 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2015 License: MIT Imports: 13 Imported by: 6

README

#go-wit

wercker status

A Go library for the Wit.ai API for Natural Language Processing.

Version

0.3

Installation

go get github.com/jsgoecke/go-wit

Documentation

http://godoc.org/github.com/jsgoecke/go-wit

Usage

package main

import (
	"github.com/jsgoecke/go-wit"
	"encoding/json"
	"log"
	"os"
)

func main() {
	client := wit.NewClient(os.Getenv("WIT_ACCESS_TOKEN"))

	// Process a text message
	request := &wit.MessageRequest{}
	request.Query = "Hello world"
	result, err := client.Message(request)
	if err != nil {
		println(err)
		os.Exit(-1)
	}
	log.Println(result)
	data, _ := json.MarshalIndent(result, "", "    ")
	log.Println(string(data[:]))

	// Process an audio/wav message
	request = &wit.MessageRequest{}
	request.File = "../audio_sample/helloWorld.wav"
	request.ContentType = "audio/wav;rate=8000"
	result, err = client.AudioMessage(request)
	if err != nil {
		println(err)
		os.Exit(-1)
	}
	log.Println(result)
	data, _ = json.MarshalIndent(result, "", "    ")
	log.Println(string(data[:]))
}

// Output:

// structs:
// &{bf699a8f-bc90-4fb4-a715-bd8bd77749db Hello world {hello {{ } []} 0.996}}
// &{54ed4e6d-0653-453e-8c0c-81da57c3846c hello world {hello {{ } []} 0.993}}

// json:
// {
//     "msg_id": "76f1c370-bd92-417f-8cb3-e1419d1a9cb3",
//     "msg_body": "Hello world",
//     "outcome": {
//         "intent": "hello",
//         "entities": {
//             "metric": {},
//             "datetime": null
//         },
//         "confidence": 0.996
//     }
// }
// {
//     "msg_id": "322f9b61-0f75-4953-a392-f8eca058a12f",
//     "msg_body": "hello world",
//     "outcome": {
//         "intent": "hello",
//         "entities": {
//             "metric": {},
//             "datetime": null
//         },
//         "confidence": 0.993
//     }
// }

Testing

Must have the environment variable WIT_ACCESS_TOKEN set to your Wit API token.

cd go-wit
go test

Test Coverage

http://gocover.io/github.com/jsgoecke/go-wit

Lint

http://go-lint.appspot.com/github.com/jsgoecke/go-wit

License

MIT, see LICENSE.txt

Author

Jason Goecke @jsgoecke.

Documentation

Index

Constants

View Source
const (
	// UserAgent is the HTTP Uesr Agent sent on HTTP requests
	UserAgent = "WIT (Go net/http)"
	// APIVersion is the version of the Wit API supported
	APIVersion = "v=20151127"
)

Variables

View Source
var APIKey string

Stores the ApiKey for the Wit API

Functions

This section is empty.

Types

type Client

type Client struct {
	APIBase string
}

Client represents a client for the Wit API (https://wit.ai/docs/api)

func NewClient

func NewClient(apiKey string) *Client

NewClient creates a new client for the Wit API

client := wit.NewClient("<ACCESS-TOKEN>")

func (*Client) AudioMessage

func (client *Client) AudioMessage(request *MessageRequest) (*Message, error)

AudioMessage requests processing of an audio message (https://wit.ai/docs/api#toc_8)

request := &MessageRequest{}
request.File = "./audio_sample/helloWorld.wav"
request.FileContents = data
request.ContentType = "audio/wav;rate=8000"
message, err := client.AudioMessage(request)

func (*Client) CreateEntity

func (client *Client) CreateEntity(entity *Entity) (*Entity, error)

CreateEntity creates a new entity (https://wit.ai/docs/api#toc_19)

result, err := client.CreateEntity(entity)

func (*Client) CreateEntityValue

func (client *Client) CreateEntityValue(id string, entityValue *EntityValue) (*Entity, error)

CreateEntityValue creates a new entity value (https://wit.ai/docs/api#toc_25)

result, err := client.CreateEntityValue("favorite_city, entityValue)

func (*Client) CreateEntityValueExp

func (client *Client) CreateEntityValueExp(id string, value string, exp string) (*Entity, error)

CreateEntityValueExp creates a new entity value expression (https://wit.ai/docs/api#toc_25)

result, err := client.CreateEntityValueExp("favorite_city", "Barcelona", "Paella")

func (*Client) DeleteEntity

func (client *Client) DeleteEntity(id string) error

DeleteEntity deletes an entity (https://wit.ai/docs/api#toc_30)

result, err := client.DeleteEntity("favorite_city")

func (*Client) DeleteEntityValue

func (client *Client) DeleteEntityValue(id string, value string) ([]byte, error)

DeleteEntityValue deletes an entity's value (https://wit.ai/docs/api#toc_25)

result, err := client.DeleteEntityValue("favorite_city", "Paris")

func (*Client) DeleteEntityValueExp

func (client *Client) DeleteEntityValueExp(id string, value string, exp string) ([]byte, error)

DeleteEntityValueExp deletes an entity's value's expression (https://wit.ai/docs/api#toc_35)

result, err := client.DeleteEntityValueExp("favorite_city", "Paris", "")

func (*Client) Entities

func (client *Client) Entities() (*Entities, error)

Entities lists the configured entities (https://wit.ai/docs/api#toc_15)

result, err := client.Entities()

func (*Client) Entity

func (client *Client) Entity(id string) (*Entity, error)

Entity lists a single configured entity (https://wit.ai/docs/api#toc_17)

result, err := client.Entity("wit$temperature")

func (*Client) Intents

func (client *Client) Intents() (*Intents, error)

Intents lists intents configured in the Wit API (https://wit.ai/docs/api#toc_13)

result, err := client.Intents()

func (*Client) Message

func (client *Client) Message(request *MessageRequest) (*Message, error)

Message requests processing of a text message (https://wit.ai/docs/api#toc_3)

result, err := client.Message(request)

func (*Client) Messages

func (client *Client) Messages(id string) (*Message, error)

Messages lists an already existing message (https://wit.ai/docs/api#toc_11)

result, err := client.Messages("ba0fcf60-44d3-4499-877e-c8d65c239730")

func (*Client) UpdateEntity

func (client *Client) UpdateEntity(entity *Entity) ([]byte, error)

UpdateEntity updates and entity (https://wit.ai/docs/api#toc_22)

result, err := client.UpdateEntity(entity)

type Context

type Context struct {
	ReferenceTime string `json:"reference_time"`
	Timezone      string `json:"timezone"`
}

Context represents the context portion of the message request

type DatetimeIntervalEnd

type DatetimeIntervalEnd struct {
	Value string `json:"value"`
	Grain string `json:"grain"`
}

type DatetimeValue

type DatetimeValue struct {
	From DatetimeIntervalEnd `json:"from"`
	To   DatetimeIntervalEnd `json:"to"`
}

DatetimeValue represents the datetime value portion of a Wit message

type Entities

type Entities []string

Entities represents a slice of entites when returend as an array (https://wit.ai/docs/api#toc_15)

type Entity

type Entity struct {
	Builtin bool          `json:"builtin,omitempty"`
	Doc     string        `json:"doc"`
	ID      string        `json:"id"`
	Name    string        `json:"name,omitempty"`
	Values  []EntityValue `json:"values"`
}

Entity represents an Entity for the Wit API (https://wit.ai/docs/api#toc_15)

type EntityValue

type EntityValue struct {
	Value       string   `json:"value"`
	Expressions []string `json:"expressions"`
}

EntityValue represents a Value within an Entity

type Expression

type Expression struct {
	Expression string `json:"expression"`
}

Expression respresents the expression

type HTTPParams

type HTTPParams struct {
	Verb        string
	Resource    string
	ContentType string
	Data        []byte
}

HTTPParams represents the HTTP parameters to pass along to the Wit API

type Intents

type Intents []struct {
	ID       string `json:"id"`
	Name     string `json:"name"`
	Doc      string `json:"doc"`
	Metadata string `json:"metadata"`
}

Intents represents intents in the Wit API (https://wit.ai/docs/api#toc_13)

type Message

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

Message represents a Wit message (https://wit.ai/docs/api#toc_3)

type MessageEntity

type MessageEntity struct {
	Metadata *string              `json:"metadata,omitempty"`
	Value    *interface{}         `json:"value,omitempty"`
	Grain    *string              `json:"grain,omitempty"`
	Type     *string              `json:"type,omitempty"`
	Unit     *string              `json:"unit,omitempty"`
	Body     *string              `json:"body,omitempty"`
	Entity   *string              `json:"entity,omitempty"`
	Start    *int64               `json:"start,omitempty"`
	End      *int64               `json:"end,omitempty"`
	Values   *[]interface{}       `json:"values,omitempty"`
	From     *DatetimeIntervalEnd `json:"from,omitempty"`
	To       *DatetimeIntervalEnd `json:"to,omitempty"`
}

MessageEntity represents the entity portion of a Wit message

type MessageRequest

type MessageRequest struct {
	File         string `json:"file,omitempty"`
	Query        string `json:"query"`
	MsgID        string `json:"msg_id,omitempty"`
	Context      string `json:"context, omitempty"`
	ContentType  string `json:"contentType, omitempty"`
	N            int    `json:"n,omitempty"`
	FileContents []byte `json:"-"`
}

MessageRequest represents a request to process a message

type Outcome

type Outcome struct {
	Text       string                     `json:"_text"`
	Intent     string                     `json:"intent"`
	IntentId   string                     `json:"intent_id"`
	Entities   map[string][]MessageEntity `json:"entities"`
	Confidence float32                    `json:"confidence"`
}

Outcome represents the outcome portion of a Wit message

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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