slu

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2021 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AudioContext

type AudioContext struct {
	ID          uuid.UUID `json:"id"`
	Segments    Segments  `json:"segments"`
	IsFinalised bool      `json:"is_finalised"`
}

AudioContext represents a single SLU audio context, which can have multiple segments. See Speechly documentation for more information about audio contexts.

func NewAudioContext

func NewAudioContext() AudioContext

NewAudioContext returns a new AudioContext.

func (*AudioContext) AddEntity

func (c *AudioContext) AddEntity(segmentID int32, e Entity) error

AddEntity adds an entity to the specific segment of the context.

func (*AudioContext) AddTranscript

func (c *AudioContext) AddTranscript(segmentID int32, t Transcript) error

AddTranscript adds a transcript to the specific segment of the context.

func (AudioContext) CheckID

func (c AudioContext) CheckID(id string) error

CheckID checks if provided id matches current context.

func (*AudioContext) Finalise

func (c *AudioContext) Finalise() error

Finalise finalises the audio context by finalising all segments in it.

func (*AudioContext) FinaliseSegment

func (c *AudioContext) FinaliseSegment(segmentID int32) error

FinaliseSegment finalises specific segment in the context.

func (*AudioContext) SetID

func (c *AudioContext) SetID(id string) error

SetID sets the ID to the context.

func (*AudioContext) SetIntent

func (c *AudioContext) SetIntent(segmentID int32, i Intent) error

SetIntent sets the intent to the specific segment of the context.

type AudioContextHandler

type AudioContextHandler interface {
	// Read reads the next AudioContext state from the handler.
	// If the context has been stopped, this will return io.EOF.
	// If any error has happened while handling the context, this will return it.
	Read() (AudioContext, error)

	// Close closes the handler by signalling it to send the StopContext event and exit the loop.
	Close() error
}

AudioContextHandler is a handler for a single AudioContext stream. It handles the sending of audio data by starting a new audio context, sending audio data and stopping the context. It handles the receiving of SLU responses by processing them into an AudioContext state and exposing an API to access the snapshots of that state.

An example of such state progression is something like this:

* First state:

{
  "id": "ae275a56-66b6-49a3-addb-90df03edb946",
  "segments": [],
  "is_finalised": false
}

* Second state:

{
  "id": "ae275a56-66b6-49a3-addb-90df03edb946",
  "segments": [
    {
      "id": 0,
      "is_finalised": false,
      "transcripts": [
        {
          "word": "TURN",
          "index": 2,
          "start_time": 480,
          "end_time": 720,
          "is_finalised": false
        }
      ],
      "entities": [],
      "intent": {
        "value": "",
        "is_finalised": false
      }
    }
  ],
  "is_finalised": false
}

* Third state:

{
  "id": "ae275a56-66b6-49a3-addb-90df03edb946",
  "segments": [
    {
      "id": 0,
      "is_finalised": false,
      "transcripts": [
        {
          "word": "TURN",
          "index": 2,
          "start_time": 480,
          "end_time": 900,
          "is_finalised": false
        },
        {
          "word": "OFF",
          "index": 3,
          "start_time": 900,
          "end_time": 1020,
          "is_finalised": false
        }
      ],
      "entities": [],
      "intent": {
        "value": "turn_off",
        "is_finalised": true
      }
    }
  ],
  "is_finalised": false
}

And so forth, until eventually the context is finalised by the API.

type AudioSource

type AudioSource interface {
	io.WriterTo
	io.Closer
}

AudioSource is the interface that represents the audio data source that is sent to SLU API within a particular audio context.

type Client

type Client struct {
	*pgrpc.Client
	// contains filtered or unexported fields
}

Client is a client for Speechly SLU API.

func NewClient

func NewClient(u url.URL, t speechly.AccessToken, log logger.Logger) (*Client, error)

NewClient returns a new Client that will access provided URL with provided access token.

func (*Client) StreamingRecognise

func (c *Client) StreamingRecognise(ctx context.Context, fmt Config) (RecogniseStream, error)

StreamingRecognise starts a new SLU recognition stream with specified Config.

type Config

type Config struct {
	NumChannels     int32
	SampleRateHertz int32
	LanguageCode    language.Tag
}

Config is the configuration of an SLU recognition stream. It is used by the client to send Config requests when starting new streams. For more information, check the Speechly SLU API documentation.

type Entities added in v1.0.2

type Entities map[EntityIndex]Entity

Entities is a map of SLU entities.

func NewEntities added in v1.0.2

func NewEntities(e []Entity) Entities

NewEntities returns new Entities constructed from e.

func (Entities) MarshalJSON added in v1.0.2

func (e Entities) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

type Entity

type Entity struct {
	Type        string `json:"type"`
	Value       string `json:"value"`
	StartIndex  int32  `json:"start_index"`
	EndIndex    int32  `json:"end_index"`
	IsFinalised bool   `json:"is_finalised"`
}

Entity is the entity detected by SLU API.

func (*Entity) Parse

func (e *Entity) Parse(v *sluv1.SLUEntity, isTentative bool) error

Parse parses response from API into Entity.

type EntityIndex added in v1.0.2

type EntityIndex struct {
	StartIndex, EndIndex int32
}

EntityIndex is a struct used for indexing entities.

type EntityIndexList added in v1.0.4

type EntityIndexList []EntityIndex

EntityIndexList is a list of entity indices. It implements the sort.Interface, so it can be used for obtaining sorted list of entity indices.

func SortedEntityIndexList added in v1.0.4

func SortedEntityIndexList(e Entities) EntityIndexList

SortedEntityIndexList returns a sorted version of EntityIndexList constructed from Entities.

func (EntityIndexList) Len added in v1.0.4

func (e EntityIndexList) Len() int

func (EntityIndexList) Less added in v1.0.4

func (e EntityIndexList) Less(i, j int) bool

func (EntityIndexList) Swap added in v1.0.4

func (e EntityIndexList) Swap(i, j int)

type Intent

type Intent struct {
	Value       string `json:"value"`
	IsFinalised bool   `json:"is_finalised"`
}

Intent is the intent detected by SLU API.

func (*Intent) Parse

func (i *Intent) Parse(v *sluv1.SLUIntent, isTentative bool) error

Parse parses response from API into Intent.

type RecogniseStream

type RecogniseStream interface {
	// NewAudioContext starts a new audio context by sending a START even to SLU API.
	// If there is already an audio context running,
	// this will block until the running context is stopped, or the stream closed.
	NewAudioContext(context.Context, AudioSource, int) (AudioContextHandler, error)

	// Close closes the stream by closing the sending part of gRPC stream.
	// It will wait for current audio context (if any) to be stopped, before closing the stream.
	Close() error
}

RecogniseStream is a single SLU recognition stream, which maps to a gRPC stream. Since one stream can have multiple contexts RecogniseStream provides an API for launching these. However, only a single audio context can be active at a time, which is controlled and guaranteed by the stream.

type Segment

type Segment struct {
	ID          int32       `json:"id"`
	IsFinalised bool        `json:"is_finalised"`
	Transcripts Transcripts `json:"transcripts"`
	Entities    Entities    `json:"entities"`
	Intent      Intent      `json:"intent"`
}

Segment represents a single SLU segment, which is bounded by a single SLU intent. See Speechly documentation for more information about segments.

func NewSegment

func NewSegment(id int32) Segment

NewSegment returns a new Segment with specified ID.

func (*Segment) AddEntity

func (s *Segment) AddEntity(e Entity) error

AddEntity adds an entity to a segment.

func (*Segment) AddTranscript

func (s *Segment) AddTranscript(t Transcript) error

AddTranscript adds a transcript to a segment. This cannot be called after segment was finalised.

func (*Segment) Finalise

func (s *Segment) Finalise() error

Finalise finalises the segment, by setting the IsFinalised flag to true and removing all tentative intents and transcripts from the segment. If the segment does not have at least one finalised transcript, an error is returned.

func (*Segment) SetIntent

func (s *Segment) SetIntent(i Intent) error

SetIntent sets the intent of a segment.

type Segments added in v1.0.2

type Segments map[int32]Segment

Segments is a map of SLU segments.

func NewSegments added in v1.0.2

func NewSegments(s []Segment) Segments

NewSegments returns a new Segments constructed from s.

func (Segments) Get added in v1.0.2

func (s Segments) Get(id int32) Segment

Get retrieves a Segment by id. If a Segment with given id is not present, it will instead be added to s and returned.

func (Segments) MarshalJSON added in v1.0.2

func (s Segments) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

type Transcript

type Transcript struct {
	Word        string `json:"word"`
	Index       int32  `json:"index"`
	StartTime   int32  `json:"start_time"`
	EndTime     int32  `json:"end_time"`
	IsFinalised bool   `json:"is_finalised"`
}

Transcript is a transcript of a word, detected by Speechly SLU API.

func (*Transcript) Parse

func (t *Transcript) Parse(v *sluv1.SLUTranscript, isTentative bool) error

Parse parses response from API into Transcript.

type Transcripts added in v1.0.2

type Transcripts map[int32]Transcript

Transcripts is a map of SLU transcripts.

func NewTranscripts added in v1.0.2

func NewTranscripts(t []Transcript) Transcripts

NewTranscripts returns a new Transcripts constructed from t.

func (Transcripts) MarshalJSON added in v1.0.2

func (t Transcripts) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

Jump to

Keyboard shortcuts

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