-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdetect.go
More file actions
59 lines (45 loc) · 1.54 KB
/
detect.go
File metadata and controls
59 lines (45 loc) · 1.54 KB
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
package detectlanguage
import "context"
// DetectRequest contains language detection request params
type DetectRequest struct {
Query string `json:"q"`
}
// DetectionResult is single language detection result
type DetectionResult struct {
Language string `json:"language"`
Score float64 `json:"score"`
}
// DetectBatchRequest contains batch language detection request params
type DetectBatchRequest struct {
Query []string `json:"q"`
}
// Detect executes language detection for a single text
func (c *Client) Detect(ctx context.Context, in string) (out []*DetectionResult, err error) {
var response []*DetectionResult
err = c.post(ctx, "detect", &DetectRequest{Query: in}, &response)
if err != nil {
return nil, err
}
return response, err
}
// DetectCode executes language detection for a single text and returns detected language code
func (c *Client) DetectCode(ctx context.Context, in string) (out string, err error) {
detections, err := c.Detect(ctx, 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(ctx context.Context, in []string) (out [][]*DetectionResult, err error) {
var response [][]*DetectionResult
err = c.post(ctx, "detect-batch", &DetectBatchRequest{Query: in}, &response)
if err != nil {
return nil, err
}
return response, err
}