-
Notifications
You must be signed in to change notification settings - Fork 30
/
language_detection.go
39 lines (32 loc) · 922 Bytes
/
language_detection.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
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
package witai
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
)
// Locales - https://wit.ai/docs/http#get__language_link
type Locales struct {
DetectedLocales []Locale `json:"detected_locales"`
}
// Locale - https://wit.ai/docs/http#get__language_link
type Locale struct {
Locale string `json:"locale"`
Confidence float64 `json:"confidence"`
}
// Detect - returns the detected languages from query - https://wit.ai/docs/http#get__language_link
func (c *Client) Detect(text string) (*Locales, error) {
resp, err := c.request(http.MethodGet, fmt.Sprintf("/language?q=%s", url.PathEscape(text)), "application/json", nil)
if err != nil {
return nil, err
}
defer resp.Close()
var locales *Locales
decoder := json.NewDecoder(resp)
err = decoder.Decode(&locales)
if err != nil {
return nil, err
}
return locales, nil
}