-
Notifications
You must be signed in to change notification settings - Fork 19
/
service.go
68 lines (56 loc) · 1.95 KB
/
service.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
package bothub
import (
"net/http"
"strings"
"github.com/nyaruka/gocommon/httpx"
"github.com/nyaruka/gocommon/stringsx"
"github.com/nyaruka/goflow/envs"
"github.com/nyaruka/goflow/flows"
)
// a classification service implementation for a bothub.it bot
type service struct {
client *Client
classifier *flows.Classifier
redactor stringsx.Redactor
}
// NewService creates a new classification service
func NewService(httpClient *http.Client, httpRetries *httpx.RetryConfig, classifier *flows.Classifier, accessToken string) flows.ClassificationService {
return &service{
client: NewClient(httpClient, httpRetries, accessToken),
classifier: classifier,
redactor: stringsx.NewRedactor(flows.RedactionMask, accessToken),
}
}
func (s *service) Classify(env envs.Environment, input string, logHTTP flows.HTTPLogCallback) (*flows.Classification, error) {
// eng-US -> en_us
lang := env.DefaultLanguage().ISO639_1()
if lang == "" {
lang = "en"
}
country := strings.ToLower(string(env.DefaultCountry()))
if country != "" {
lang += ("_" + country)
}
response, trace, err := s.client.Parse(input, lang)
if trace != nil {
logHTTP(flows.NewHTTPLog(trace, flows.HTTPStatusFromCode, s.redactor))
}
if err != nil {
return nil, err
}
result := &flows.Classification{
Intents: make([]flows.ExtractedIntent, len(response.IntentRanking)),
Entities: make(map[string][]flows.ExtractedEntity, len(response.LabelsList)),
}
for i, intent := range response.IntentRanking {
result.Intents[i] = flows.ExtractedIntent{Name: intent.Name, Confidence: intent.Confidence}
}
for label, entities := range response.Entities {
result.Entities[label] = make([]flows.ExtractedEntity, 0, len(response.Entities))
for _, entity := range entities {
result.Entities[label] = append(result.Entities[label], flows.ExtractedEntity{Value: entity.Entity, Confidence: entity.Confidence})
}
}
return result, nil
}
var _ flows.ClassificationService = (*service)(nil)