github.com/wit-ai/wit-go/v2@v2.0.2/utterance.go (about)

     1  // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
     2  
     3  package witai
     4  
     5  import (
     6  	"bytes"
     7  	"encoding/json"
     8  	"fmt"
     9  	"net/http"
    10  )
    11  
    12  // Utterance - https://wit.ai/docs/http/20200513/#get__utterances_link
    13  type Utterance struct {
    14  	Text     string            `json:"text"`
    15  	Intent   UtteranceIntent   `json:"intent"`
    16  	Entities []UtteranceEntity `json:"entities"`
    17  	Traits   []UtteranceTrait  `json:"traits"`
    18  }
    19  
    20  // UtteranceIntent - https://wit.ai/docs/http/20200513/#get__utterances_link
    21  type UtteranceIntent struct {
    22  	ID   string `json:"id"`
    23  	Name string `json:"name"`
    24  }
    25  
    26  // UtteranceEntity - https://wit.ai/docs/http/20200513/#get__utterances_link
    27  type UtteranceEntity struct {
    28  	ID       string            `json:"id"`
    29  	Name     string            `json:"name"`
    30  	Role     string            `json:"role"`
    31  	Start    int               `json:"start"`
    32  	End      int               `json:"end"`
    33  	Body     string            `json:"body"`
    34  	Entities []UtteranceEntity `json:"entities"`
    35  }
    36  
    37  // UtteranceTrait - https://wit.ai/docs/http/20200513/#get__utterances_link
    38  type UtteranceTrait struct {
    39  	ID    string `json:"id"`
    40  	Name  string `json:"name"`
    41  	Value string `json:"value"`
    42  }
    43  
    44  // GetUtterances - Returns an array of utterances.
    45  //
    46  // https://wit.ai/docs/http/20200513/#get__utterances_link
    47  func (c *Client) GetUtterances(limit int, offset int) ([]Utterance, error) {
    48  	if limit <= 0 {
    49  		limit = 0
    50  	}
    51  	if offset <= 0 {
    52  		offset = 0
    53  	}
    54  
    55  	resp, err := c.request(http.MethodGet, fmt.Sprintf("/utterances?limit=%d&offset=%d", limit, offset), "application/json", nil)
    56  	if err != nil {
    57  		return []Utterance{}, err
    58  	}
    59  
    60  	defer resp.Close()
    61  
    62  	var utterances []Utterance
    63  	decoder := json.NewDecoder(resp)
    64  	err = decoder.Decode(&utterances)
    65  	return utterances, err
    66  }
    67  
    68  // DeleteUtterances - Delete validated utterances from your app.
    69  //
    70  // https://wit.ai/docs/http/20200513/#delete__utterances_link
    71  func (c *Client) DeleteUtterances(texts []string) (*TrainingResponse, error) {
    72  	type text struct {
    73  		Text string `json:"text"`
    74  	}
    75  	reqTexts := make([]text, len(texts))
    76  	for i, t := range texts {
    77  		reqTexts[i] = text{Text: t}
    78  	}
    79  
    80  	utterancesJSON, err := json.Marshal(reqTexts)
    81  	if err != nil {
    82  		return nil, err
    83  	}
    84  
    85  	resp, err := c.request(http.MethodDelete, "/utterances", "application/json", bytes.NewBuffer(utterancesJSON))
    86  	if err != nil {
    87  		return nil, err
    88  	}
    89  
    90  	defer resp.Close()
    91  
    92  	var r *TrainingResponse
    93  	decoder := json.NewDecoder(resp)
    94  	err = decoder.Decode(&r)
    95  	return r, err
    96  }
    97  
    98  // TrainingResponse - https://wit.ai/docs/http/20200513/#post__utterances_link
    99  type Training struct {
   100  	Text     string           `json:"text"`
   101  	Intent   string           `json:"intent,omitempty"`
   102  	Entities []TrainingEntity `json:"entities"`
   103  	Traits   []TrainingTrait  `json:"traits"`
   104  }
   105  
   106  // TrainingResponse - https://wit.ai/docs/http/20200513/#post__utterances_link
   107  type TrainingEntity struct {
   108  	Entity   string           `json:"entity"`
   109  	Start    int              `json:"start"`
   110  	End      int              `json:"end"`
   111  	Body     string           `json:"body"`
   112  	Entities []TrainingEntity `json:"entities"`
   113  }
   114  
   115  // TrainingResponse - https://wit.ai/docs/http/20200513/#post__utterances_link
   116  type TrainingTrait struct {
   117  	Trait string `json:"trait"`
   118  	Value string `json:"value"`
   119  }
   120  
   121  // TrainingResponse - https://wit.ai/docs/http/20200513/#post__utterances_link
   122  type TrainingResponse struct {
   123  	Sent bool `json:"sent"`
   124  	N    int  `json:"n"`
   125  }
   126  
   127  // TrainUtterances - Add utterances (sentence + entities annotations) to train your app programmatically.
   128  //
   129  // https://wit.ai/docs/http/20200513/#post__utterances_link
   130  func (c *Client) TrainUtterances(trainings []Training) (*TrainingResponse, error) {
   131  	utterancesJSON, err := json.Marshal(trainings)
   132  	if err != nil {
   133  		return nil, err
   134  	}
   135  
   136  	resp, err := c.request(http.MethodPost, "/utterances", "application/json", bytes.NewBuffer(utterancesJSON))
   137  	if err != nil {
   138  		return nil, err
   139  	}
   140  
   141  	defer resp.Close()
   142  
   143  	var r *TrainingResponse
   144  	decoder := json.NewDecoder(resp)
   145  	err = decoder.Decode(&r)
   146  	return r, err
   147  }