1
1
package detectlanguage
2
2
3
+ import "context"
4
+
3
5
// DetectRequest contains language detection request params
4
6
type DetectRequest struct {
5
7
Query string `json:"q"`
6
8
}
7
9
8
- // DetectResponse is a resource containing language detection response
9
- type DetectResponse struct {
10
- Data * DetectResponseData `json:"data"`
11
- }
12
-
13
- // DetectResponseData contains language detection response data
14
- type DetectResponseData struct {
15
- Detections []* DetectionResult `json:"detections"`
16
- }
17
-
18
10
// DetectionResult is single language detection result
19
11
type DetectionResult struct {
20
- Language string `json:"language"`
21
- Reliable bool `json:"isReliable"`
22
- Confidence float32 `json:"confidence"`
12
+ Language string `json:"language"`
13
+ Score float64 `json:"score"`
23
14
}
24
15
25
16
// DetectBatchRequest contains batch language detection request params
26
17
type DetectBatchRequest struct {
27
18
Query []string `json:"q"`
28
19
}
29
20
30
- // DetectBatchResponse is a resource batch containing language detection response
31
- type DetectBatchResponse struct {
32
- Data * DetectBatchResponseData `json:"data"`
33
- }
34
-
35
- // DetectBatchResponseData contains batch language detection response data
36
- type DetectBatchResponseData struct {
37
- Detections [][]* DetectionResult `json:"detections"`
38
- }
39
-
40
21
// Detect executes language detection for a single text
41
22
func (c * Client ) Detect (in string ) (out []* DetectionResult , err error ) {
42
- var response DetectResponse
43
- err = c .post (nil , "detect" , & DetectRequest {Query : in }, & response )
23
+ var response [] * DetectionResult
24
+ err = c .post (context . TODO () , "detect" , & DetectRequest {Query : in }, & response )
44
25
45
26
if err != nil {
46
27
return nil , err
47
28
}
48
29
49
- return response . Data . Detections , err
30
+ return response , err
50
31
}
51
32
52
33
// DetectCode executes language detection for a single text and returns detected language code
@@ -67,12 +48,12 @@ func (c *Client) DetectCode(in string) (out string, err error) {
67
48
// DetectBatch executes language detection with multiple texts.
68
49
// It is significantly faster than doing a separate request for each text indivdually.
69
50
func (c * Client ) DetectBatch (in []string ) (out [][]* DetectionResult , err error ) {
70
- var response DetectBatchResponse
71
- err = c .post (nil , "detect" , & DetectBatchRequest {Query : in }, & response )
51
+ var response [][] * DetectionResult
52
+ err = c .post (nil , "detect-batch " , & DetectBatchRequest {Query : in }, & response )
72
53
73
54
if err != nil {
74
55
return nil , err
75
56
}
76
57
77
- return response . Data . Detections , err
58
+ return response , err
78
59
}
0 commit comments