/
nlp.go
93 lines (73 loc) · 1.75 KB
/
nlp.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package wildlink
import (
"sync"
"github.com/dghubble/sling"
"golang.org/x/xerrors"
)
type NLPService struct {
c *Client
sling *sling.Sling
}
func newNLPService(c *Client, sling *sling.Sling) *NLPService {
return &NLPService{c: c, sling: sling.Path("v2/nlp/")}
}
type AnalyzeParams struct {
Content string
}
type AnalyzeResult struct {
ContentPart []string `json:"ContentPart,omitempty"`
URL string `json:"URL,omitempty"`
}
type AnalyzeResultIterator interface {
Next() bool
Scan() (*AnalyzeResult, error)
Err() error
Close() error
}
type analyzeResultIter struct {
sync.Mutex
results *[]AnalyzeResult
last bool
err error
}
func (c *analyzeResultIter) Close() error {
c.Lock()
defer c.Unlock()
c.results = nil
c.last = true
return nil
}
func (c *analyzeResultIter) Err() error {
return c.err
}
func (c *analyzeResultIter) Scan() (*AnalyzeResult, error) {
c.Lock()
defer c.Unlock()
if c.results != nil && len(*c.results) > 0 {
item, buffer := (*c.results)[0], (*c.results)[1:]
c.results = &buffer
return &item, nil
}
return nil, xerrors.New("Nothing left and Next should have stopped you")
}
func (c *analyzeResultIter) Next() bool {
c.Lock()
defer c.Unlock()
if c.results != nil && len(*c.results) > 0 {
return true
}
if c.last {
return false
}
return len(*c.results) > 0
}
func (s *NLPService) ListAnalysis(params *AnalyzeParams) (AnalyzeResultIterator, error) {
apiError := new(APIError)
slingReq := s.c.SetAuthHeaders(s.sling.New().Path("analyze"))
results := new([]AnalyzeResult)
_, err := slingReq.Post("").BodyJSON(params).Receive(results, apiError)
if relevantError(err, *apiError) != nil {
return nil, relevantError(err, *apiError)
}
return &analyzeResultIter{results: results}, nil
}