-
Notifications
You must be signed in to change notification settings - Fork 0
/
babelnet.go
202 lines (186 loc) · 5.33 KB
/
babelnet.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package babelnet
import (
"encoding/json"
"fmt"
"github.com/mitchellh/mapstructure"
"io/ioutil"
"net/http"
)
const (
Key = "key"
Lemma = "lemma"
SearchLang = "searchLang"
SynSetId = "id"
TargetLang = "targetLang"
Pos = "pos"
Source = "source"
WnVersion = "wnVersion"
)
// client to connect to the babel REST api server.
type Client struct {
baseUrl string
httpClient *http.Client
key string
}
func NewClient(baseUrl string, key string) (client *Client) {
client = new(Client)
client.key = key
client.baseUrl = baseUrl
client.httpClient = http.DefaultClient
return
}
//https://babelnet.io/v5/getOutgoingEdges?id={synsetId}&key={key}
func (client *Client) GetOutgoingEdges(synSetId string) (resp []BabelEdgeResponse) {
req := &request{
method: "GET",
endpoint: "/v5/getOutgoingEdges",
}
req.setParam(SynSetId, synSetId)
client.constructRequest(req)
request, err := http.NewRequest(req.method, req.fullUrl, req.body)
checkError(err)
fmt.Println(request)
response, err := client.httpClient.Do(request)
checkError(err)
data := client.parseResponse(response)
mapstructure.Decode(data, &resp)
return
}
//https://babelnet.io/v5/getSynsetIdsFromResourceID?id={lemma}&searchLang={searchLang}&pos={pos}&source={source}&key={key}
func (client *Client) GetBabelNetId(idRequest *BabelIdRequest) (resp []SynSetIdResponse) {
req := &request{
method: "GET",
endpoint: "/v5/getSynsetIdsFromResourceID",
}
req.setParam(SynSetId, idRequest.Id)
req.setParam(SearchLang, idRequest.SearchLang)
req.setParam(TargetLang, idRequest.TargetLang)
req.setParam(Source, idRequest.Source)
req.setParam(Pos, idRequest.Pos)
req.setParam(Source, idRequest.Source)
req.setParam(WnVersion, idRequest.WnVersion)
client.constructRequest(req)
request, err := http.NewRequest(req.method, req.fullUrl, req.body)
checkError(err)
fmt.Println(request)
response, err := client.httpClient.Do(request)
checkError(err)
data := client.parseResponse(response)
mapstructure.Decode(data, &resp)
return
}
// https://babelnet.io/v5/getSenses?lemma={lemma}&searchLang={lang}&key={key}
func (client *Client) GetSenses(babelReq *BabelSenseRequest) (resp []BabelSenseResponse) {
req := &request{
method: "GET",
endpoint: "/v5/getSenses",
}
req.setParam(Lemma, babelReq.Lemma)
req.setParam(SearchLang, babelReq.SearchLang)
client.constructRequest(req)
request, err := http.NewRequest(req.method, req.fullUrl, req.body)
checkError(err)
fmt.Println(request)
response, err := client.httpClient.Do(request)
checkError(err)
data := client.parseResponse(response)
mapstructure.Decode(data, &resp)
return
}
func (client *Client) GetSynSet(synSetId string) (resp SynSetInfoResponse) {
req := &request{
method: "GET",
endpoint: "/v5/getSynset",
}
req.setParam(SynSetId, synSetId)
client.constructRequest(req)
request, err := http.NewRequest(req.method, req.fullUrl, req.body)
checkError(err)
fmt.Println(request)
response, err := client.httpClient.Do(request)
checkError(err)
data := client.parseResponse(response)
mapstructure.Decode(data, &resp)
return
}
/**
* Request template : https://babelnet.io/v5/getSynsetIds?lemma={lemma}&searchLang={searchLang}&key={key}
*/
func (client *Client) GetSynSetIds(lemma string, lang string) (resp []SynSetIdResponse) {
req := &request{
method: "GET",
endpoint: "/v5/getSynsetIds",
}
req.setParam(Lemma, lemma)
req.setParam(SearchLang, lang)
client.constructRequest(req)
request, err := http.NewRequest(req.method, req.fullUrl, req.body)
checkError(err)
fmt.Println(request)
response, err := client.httpClient.Do(request)
checkError(err)
data := client.parseResponse(response)
mapstructure.Decode(data, &resp)
return
}
// Get babelNet api version.
func (client *Client) GetVersion() (version string) {
r := &request{
method: "GET",
endpoint: "/v5/getVersion",
}
client.constructRequest(r)
request, err := http.NewRequest(r.method, r.fullUrl, r.body)
checkError(err)
fmt.Println(request)
response, err := client.httpClient.Do(request)
checkError(err)
var resp VersionResponse
data := client.parseResponse(response)
mapstructure.Decode(data, &resp)
version = resp.Version
checkError(err)
fmt.Println(version)
return
}
func checkError(e error) {
if e != nil {
panic(e)
}
}
func (client *Client) constructRequest(req *request) (err error) {
fullURL := fmt.Sprintf("%s%s", client.baseUrl, req.endpoint)
req.setParam(Key, client.key)
queryString := req.query.Encode()
fullURL = fmt.Sprintf("%s?%s", fullURL, queryString)
req.fullUrl = fullURL
return nil
}
/**
Parse Json response to struct>
*/
func (client *Client) parseResponse(response *http.Response) (responseValue interface{}) {
if response.StatusCode != http.StatusOK {
errorMessage := fmt.Sprintf("Error while calling API : %d", response.StatusCode)
fmt.Println(errorMessage)
panic(errorMessage)
}
data, err := ioutil.ReadAll(response.Body)
checkError(err)
defer response.Body.Close()
err = json.Unmarshal(data, &responseValue)
checkApiKeyValid(responseValue)
checkError(err)
return responseValue
}
/**
when api key is not valid or limit requests reached, API sends 200 OK although no concrete response.
*/
func checkApiKeyValid(responseValue interface{}) {
var message BabelErrorMessage
mapstructure.Decode(responseValue, &message)
fmt.Println(message)
if message.Message != "" {
panic(message.Message)
}
}