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