-
Notifications
You must be signed in to change notification settings - Fork 0
/
glossaries.go
140 lines (124 loc) · 3.32 KB
/
glossaries.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package textra
import (
"encoding/json"
"fmt"
"net/url"
)
// Glossary entries.
const TERM = "term"
type TermSearchRequest struct {
Text string `json:"text"`
MatchType string `json:"match_type"`
Pid string `json:"pid"`
Order string `json:"order"`
Limit string `json:"limit"`
Offset string `json:"offset"`
}
type TermSearchResult struct {
Resultset struct {
Code int `json:"code"`
Message string `json:"message"`
Request struct {
URL string `json:"url"`
Limit int `json:"limit"`
Offset int `json:"offset"`
} `json:"request"`
Result struct {
Term []TermSearchTerm `json:"term"`
} `json:"result"`
} `json:"resultset"`
}
type TermSearchTerm struct {
Pid int `json:"pid"`
Cid int `json:"cid"`
Source string `json:"source"`
Target string `json:"target"`
}
func (tra *TexTra) TermSearch(request *TermSearchRequest) ([]TermSearchTerm, error) {
values := tra.apiValues()
values.Add("api_name", TERM)
values.Add("api_param", "search")
values.Add("pid", request.Pid)
values.Add("text", request.Text)
values.Add("match_type", request.MatchType)
values.Add("order", request.Order)
values.Add("limit", request.Limit)
values.Add("offset", request.Offset)
ret, err := tra.apiPost(values)
if err != nil {
return nil, err
}
result, err := tra.TermSearchDecode(ret)
if err != nil {
return nil, err
}
return result.Resultset.Result.Term, nil
}
func (tra *TexTra) TermSearchDecode(ret []byte) (*TermSearchResult, error) {
result := new(TermSearchResult)
if err := json.Unmarshal(ret, result); err != nil {
return nil, err
}
if result.Resultset.Code != 0 {
return result, fmt.Errorf("%d: %s", result.Resultset.Code, errorText[result.Resultset.Code])
}
return result, nil
}
type TermSetResult struct {
Resultset struct {
Code int `json:"code"`
Message string `json:"message"`
Request struct {
URL string `json:"url"`
} `json:"request"`
Result struct {
Pid int `json:"pid"`
Cid int `json:"cid"`
} `json:"result"`
} `json:"resultset"`
}
func (tra *TexTra) TermSet(pid string, textS string, textT string) (int, int, error) {
values := tra.apiValues()
values.Add("api_name", TERM)
values.Add("api_param", "set")
values.Add("pid", pid)
values.Add("text_s", textS)
values.Add("text_t", textT)
return tra.termSet(values)
}
func (tra *TexTra) TermUpdate(pid string, cid string, textS string, textT string) (int, int, error) {
values := tra.apiValues()
values.Add("api_name", TERM)
values.Add("api_param", "update")
values.Add("pid", pid)
values.Add("cid", cid)
values.Add("text_s", textS)
values.Add("text_t", textT)
return tra.termSet(values)
}
func (tra *TexTra) TermDel(pid string, cid string) (int, int, error) {
values := tra.apiValues()
values.Add("api_name", TERM)
values.Add("api_param", "delete")
values.Add("pid", pid)
values.Add("cid", cid)
return tra.termSet(values)
}
func (tra *TexTra) termSet(values url.Values) (int, int, error) {
ret, err := tra.apiPost(values)
if err != nil {
return 0, 0, err
}
data, err := termSetDecode(ret)
if err != nil {
return 0, 0, err
}
return data.Resultset.Result.Pid, data.Resultset.Result.Cid, nil
}
func termSetDecode(jsonStr []byte) (*TermSetResult, error) {
result := new(TermSetResult)
if err := json.Unmarshal(jsonStr, result); err != nil {
return nil, err
}
return result, nil
}