-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPIResponse.go
86 lines (74 loc) · 2.29 KB
/
APIResponse.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
package main
import (
"os"
"strings"
)
type APIResponse struct {
Text string `json:"text"`
Intents []Intent `json:"intents"`
Entities map[string][]Entity `json:"entities"`
}
type Intent struct {
Name string `json:"name"`
Confidence float64 `json:"confidence"`
}
type Entity struct {
Entity string `json:"name"`
Value string `json:"value"`
}
type ModifiedAPIResponse struct {
ID string `json:"id"`
HasCatchWord bool `json:"catchword"`
Intent string `json:"intent"`
Entities []Entity `json:"entities"`
LowConfidence bool `json:"confident"`
}
//catchPhrase
var catchPhrase = strings.ToLower(os.Getenv("CATCHPHRASE"))
//editAPIResponse Changes the structure of the API response to make it ready to be sent to a client.
func modifyAPIResponse(reqType RequestType, prefs UserPreferences, apiRes APIResponse) (ModifiedAPIResponse, error) {
if reqType == ReqTypes.Intent {
modifiedAPIRes, err := modifyForIntent(prefs, apiRes)
if err != nil {
return ModifiedAPIResponse{}, err
}
return modifiedAPIRes, nil
} else if reqType == ReqTypes.CatchPhrase {
return modifyForCatchword(apiRes), nil
}
return ModifiedAPIResponse{}, &unknownReqType{reqType: reqType}
}
func modifyForIntent(prefs UserPreferences, apiRes APIResponse) (ModifiedAPIResponse, error) {
if len(apiRes.Intents) == 0 {
return ModifiedAPIResponse{LowConfidence: true}, nil
}
if apiRes.Intents[0].Confidence <= MinConfidence {
return ModifiedAPIResponse{LowConfidence: true}, nil
}
entities := entityMapToEntityArray(apiRes.Entities)
for i := 0; i < len(entities); i++ {
entity := entities[i]
val, ok := (prefs.Entities[entity.Entity])[entity.Value]
if ok {
entity.Value = val
}
entities[i] = entity
}
return ModifiedAPIResponse{Intent: apiRes.Intents[0].Name, Entities: entities, LowConfidence: false}, nil
}
func modifyForCatchword(apiRes APIResponse) ModifiedAPIResponse {
if strings.Contains(strings.ToLower(apiRes.Text), catchPhrase) {
return ModifiedAPIResponse{HasCatchWord: true}
}
return ModifiedAPIResponse{HasCatchWord: false}
}
func entityMapToEntityArray(m map[string][]Entity) []Entity {
maplen := len(m)
entlist := make([]Entity, maplen, maplen)
i := 0
for _, x := range m {
entlist[i] = x[0]
i++
}
return entlist
}