-
Notifications
You must be signed in to change notification settings - Fork 3
/
detect.go
78 lines (61 loc) · 2.14 KB
/
detect.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
package detectlanguage
// DetectRequest contains language detection request params
type DetectRequest struct {
Query string `json:"q"`
}
// DetectResponse is a resource containing language detection response
type DetectResponse struct {
Data *DetectResponseData `json:"data"`
}
// DetectResponseData contains language detection response data
type DetectResponseData struct {
Detections []*DetectionResult `json:"detections"`
}
// DetectionResult is single language detection result
type DetectionResult struct {
Language string `json:"language"`
Reliable bool `json:"isReliable"`
Confidence float32 `json:"confidence"`
}
// DetectBatchRequest contains batch language detection request params
type DetectBatchRequest struct {
Query []string `json:"q"`
}
// DetectBatchResponse is a resource batch containing language detection response
type DetectBatchResponse struct {
Data *DetectBatchResponseData `json:"data"`
}
// DetectBatchResponseData contains batch language detection response data
type DetectBatchResponseData struct {
Detections [][]*DetectionResult `json:"detections"`
}
// Detect executes language detection for a single text
func (c *Client) Detect(in string) (out []*DetectionResult, err error) {
var response DetectResponse
err = c.post(nil, "detect", &DetectRequest{Query: in}, &response)
if err != nil {
return nil, err
}
return response.Data.Detections, err
}
// DetectCode executes language detection for a single text and returns detected language code
func (c *Client) DetectCode(in string) (out string, err error) {
detections, err := c.Detect(in)
if err != nil {
return "", err
}
if len(detections) == 0 {
return "", &DetectionError{"Language not detected"}
}
return detections[0].Language, err
}
// DetectBatch executes language detection with multiple texts.
// It is significantly faster than doing a separate request for each text indivdually.
func (c *Client) DetectBatch(in []string) (out [][]*DetectionResult, err error) {
var response DetectBatchResponse
err = c.post(nil, "detect", &DetectBatchRequest{Query: in}, &response)
if err != nil {
return nil, err
}
return response.Data.Detections, err
}