-
Notifications
You must be signed in to change notification settings - Fork 31
/
message.go
139 lines (117 loc) · 3.87 KB
/
message.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
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
package witai
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
)
// MessageResponse - https://wit.ai/docs/http/#get__message_link
type MessageResponse struct {
ID string `json:"msg_id"`
Text string `json:"text"`
Intents []MessageIntent `json:"intents"`
Entities map[string][]MessageEntity `json:"entities"`
Traits map[string][]MessageTrait `json:"traits"`
}
// MessageEntity - https://wit.ai/docs/http/#get__message_link
type MessageEntity 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"`
Value string `json:"value"`
Confidence float64 `json:"confidence"`
Entities []MessageEntity `json:"entities"`
Extra map[string]interface{} `json:"-"`
}
// MessageTrait - https://wit.ai/docs/http/#get__message_link
type MessageTrait struct {
ID string `json:"id"`
Value string `json:"value"`
Confidence float64 `json:"confidence"`
Extra map[string]interface{} `json:"-"`
}
// MessageIntent - https://wit.ai/docs/http/#get__message_link
type MessageIntent struct {
ID string `json:"id"`
Name string `json:"name"`
Confidence float64 `json:"confidence"`
}
// MessageRequest - https://wit.ai/docs/http/#get__message_link
type MessageRequest struct {
Query string `json:"q"`
Tag string `json:"tag"`
N int `json:"n"`
Context *MessageContext `json:"context"`
Speech *Speech `json:"-"`
}
// Speech - https://wit.ai/docs/http/20170307#post__speech_link
type Speech struct {
File io.Reader `json:"file"`
ContentType string `json:"content_type"` // Example: audio/raw;encoding=unsigned-integer;bits=16;rate=8000;endian=big
}
// MessageContext - https://wit.ai/docs/http/20170307#context_link
type MessageContext struct {
ReferenceTime string `json:"reference_time"` // "2014-10-30T12:18:45-07:00"
Timezone string `json:"timezone"`
Locale string `json:"locale"`
Coords MessageCoords `json:"coords"`
}
// MessageCoords - https://wit.ai/docs/http/20170307#context_link
type MessageCoords struct {
Lat float32 `json:"lat"`
Long float32 `json:"long"`
}
// Parse - parses text and returns entities
func (c *Client) Parse(req *MessageRequest) (*MessageResponse, error) {
if req == nil {
return nil, errors.New("invalid request")
}
q := buildParseQuery(req)
resp, err := c.request(http.MethodGet, "/message"+q, "application/json", nil)
if err != nil {
return nil, err
}
defer resp.Close()
var msgResp *MessageResponse
decoder := json.NewDecoder(resp)
err = decoder.Decode(&msgResp)
return msgResp, err
}
// Speech - sends audio file for parsing
func (c *Client) Speech(req *MessageRequest) (*MessageResponse, error) {
if req == nil || req.Speech == nil {
return nil, errors.New("invalid request")
}
q := buildParseQuery(req)
resp, err := c.request(http.MethodPost, "/speech"+q, req.Speech.ContentType, req.Speech.File)
if err != nil {
return nil, err
}
defer resp.Close()
var msgResp *MessageResponse
decoder := json.NewDecoder(resp)
err = decoder.Decode(&msgResp)
return msgResp, err
}
func buildParseQuery(req *MessageRequest) string {
q := fmt.Sprintf("?q=%s", url.QueryEscape(req.Query))
if req.N != 0 {
q += fmt.Sprintf("&n=%d", req.N)
}
if req.Tag != "" {
q += fmt.Sprintf("&tag=%s", req.Tag)
}
if req.Context != nil {
b, _ := json.Marshal(req.Context)
if b != nil {
q += fmt.Sprintf("&context=%s", url.QueryEscape(string(b)))
}
}
return q
}