-
Notifications
You must be signed in to change notification settings - Fork 0
/
local_api_gretel_results.go
331 lines (286 loc) · 9.97 KB
/
local_api_gretel_results.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// +build !nodbxml
package main
import (
"errors"
"net/http"
"github.com/pebbe/dbxml"
"encoding/json"
"encoding/xml"
"fmt"
"io/ioutil"
"strconv"
"strings"
)
func mini(a int, b int) int {
if a < b {
return a
}
return b
}
func maxi(a int, b int) int {
if a > b {
return a
}
return b
}
func unpack(s []string, vars ...*string) {
for i, str := range s {
*vars[i] = str
}
}
type xPathVariable struct {
// skip variable declaration in xquery if name == $node, that variable is already defined
Name string `json:"name"`
Path string `json:"path"`
}
type gretelResultsPayload struct {
// Corpus to search
Corpus string `json:"corpus"`
// Is this an analysis request?, meaning the limit on the results returned is higher.
Analysis bool `json:"isAnalysis"`
// the page that's being requested, maps to a set of results at a specific offset
Page int `json:"iteration"`
// Set of components to search - pingponged between client and server - when null, all components are put in the array and searched)
// The component being searched this request is at index 0
RemainingDactFileIDs *[]string `json:"remainingComponents"`
// Retrieve the preceding and following sentences.
// This appears to be UNSUPPORTED in dbxml
Context bool `json:"retrieveContext"`
// variables to also output
Variables []xPathVariable `json:"variables"`
// The xpath expression to query the db
XPath string `json:"xpath"`
// Ignored parameters:
// needregulargrinded
// remainingDatabases
// seachlimit
}
type innerXML struct {
InnerXML string `xml:",innerxml"`
}
type gretelXqueryResult struct {
SentenceID string `xml:"sentence-id"`
Sentence innerXML `xml:"sentence"`
NodeBegins []string `xml:"node-begins>id"`
NodeIDs []string `xml:"node-ids>id"`
Result innerXML `xml:"result"`
Meta innerXML `xml:"meta"`
Variables innerXML `xml:"vars"`
PreviousSentence string `xml:"prevous-sentence"`
NextSentence string `xml:"next-sentence"`
}
// from gretel4 config.php
const searchLimit = 500
const analysisLimit = 50000
const searchPageSize = 50
const analysisPageSize = 50000
func api_gretel_results(q *Context) {
q.w.Header().Set("Access-Control-Allow-Origin", "*")
requestBody, err := ioutil.ReadAll(q.r.Body)
if gretelSendErr("Error reading request body", q, err) {
return
}
var payload gretelResultsPayload
err = json.Unmarshal(requestBody, &payload)
if gretelSendErr("Invalid JSON in request body", q, err) {
return
}
if !mayAccess(q, payload.Corpus) {
http.Error(q.w, "", 403)
return
}
pageSize, searchLimit := func() (int, int) {
if payload.Analysis {
return analysisPageSize, analysisLimit
}
return searchPageSize, searchLimit
}()
// This is a bit dumb, but whatever.
// If the client didn't supply files to search, search them all.
var remainingDactFileIDs = payload.RemainingDactFileIDs
var remainingDactFiles = make([]dactfile, 0)
if remainingDactFileIDs == nil {
remainingDactFiles, err = getDactFiles(payload.Corpus)
if gretelSendErr("", q, err) {
return
}
} else {
for _, id := range *remainingDactFileIDs {
var file dactfile
file, err = getDactFileById(payload.Corpus, id)
if gretelSendErr("Invalid dact file id "+id, q, err) {
return
}
remainingDactFiles = append(remainingDactFiles, file)
}
}
resultJSON, err := getResults(q, remainingDactFiles, payload.Page, pageSize, searchLimit, payload.XPath, payload.Context, payload.Corpus, payload.Variables)
if gretelSendErr("Error retrieving results", q, err) {
return
}
q.w.Header().Set("Content-Type", "application/json; charset=utf-8")
q.w.Header().Set("Cache-Control", "no-cache")
q.w.Header().Add("Pragma", "no-cache")
fmt.Fprint(q.w, resultJSON)
}
func getResults(q *Context, remainingDactFiles []dactfile, page int, pageSize int, resultLimit int, xpath string, context bool, corpus string, variables []xPathVariable) (string, error) {
startIndex := (pageSize * page) + 1
endIndex := mini(pageSize*(page+1), resultLimit)
if startIndex >= endIndex || len(remainingDactFiles) == 0 {
return "", errors.New("Out of bounds or no remaining databases to search")
}
dactFile := remainingDactFiles[0]
db, errval := dbxml.OpenRead(dactFile.path)
if errval != nil {
// technically leaking path to file here, but this is an internal server error and should never happen.
// Mostly it just helps with debugging should it ever happen
return "", errors.New("Cannot open database " + dactFile.path)
}
xquery := xquery_gretel_results(startIndex, endIndex, xpath, context, variables)
var qu *dbxml.Query
qu, errval = db.PrepareRaw(xquery)
if errval != nil {
return "", errors.New("Invalid query: " + xquery)
}
docs, errval := qu.Run()
if errval != nil {
return "", errors.New("Cannot execute query: " + xquery)
}
sentenceMap := make(map[string]string)
nodeIDMap := make(map[string]string)
beginsMap := make(map[string]string)
xmlSentencesMap := make(map[string]string)
metaMap := make(map[string]string)
variablesMap := make(map[string]string)
originMap := make(map[string]string) // database where the sentence originated - we store the id of the dactfile here
// read results
i := 0
for docs.Next() {
var result gretelXqueryResult
resultString := docs.Match()
errval = xml.Unmarshal([]byte(resultString), &result)
if errval != nil {
return "", errval
}
sentenceID := result.SentenceID
sentenceMap[sentenceID] = strings.TrimSpace(strings.Join([]string{result.PreviousSentence, result.Sentence.InnerXML, result.NextSentence}, " "))
nodeIDMap[sentenceID] = strings.Join(result.NodeIDs, "-")
beginsMap[sentenceID] = strings.Join(result.NodeBegins, "-")
xmlSentencesMap[sentenceID] = result.Result.InnerXML
metaMap[sentenceID] = result.Meta.InnerXML
if result.Variables.InnerXML != "" {
variablesMap[sentenceID] = "<vars>" + result.Variables.InnerXML + "</vars>" // client expects a certain data structure
} else {
variablesMap[sentenceID] = ""
}
originMap[sentenceID] = dactFile.id
i++
}
// less results returned by query than requested, i.e. we got all of them:
// done with this dact file, remove it from the remaining files
// and reset the page (it will be applied to the next dact file in the next request).
if i < pageSize {
remainingDactFiles = remainingDactFiles[1:]
page = -1 // We always increment current page by 1 so set to -1 to return page 0 to client
}
// Map the files back to their ids to send back to the client
var remainingDactFileIds = make([]string, 0)
for _, file := range remainingDactFiles {
remainingDactFileIds = append(remainingDactFileIds, file.id)
}
// TODO we should search across multiple dact files if we haven't found enough results to fill a page yet
result := map[string]interface{}{
"beginlist": beginsMap,
"endPosIteration": page + 1,
"idlist": nodeIDMap,
"metalist": metaMap,
"needRegularGrinded": false,
"remainingComponents": remainingDactFileIds,
"remainingDatabases": remainingDactFileIds,
"searchLimit": resultLimit,
"sentenceDatabases": originMap,
"sentences": sentenceMap,
"success": true,
"tblist": false,
"varlist": variablesMap,
"xmllist": xmlSentencesMap,
"xquery": xquery}
rbyte, errval := json.Marshal(result)
if errval != nil {
return "", errors.New("Cannot marshal response")
}
return string(json.RawMessage(rbyte)[:]), nil
}
/*
Example query from gretel4
--------------------------
(
for $node in db:open("LASSY_ID_WRPE")/treebank//node[
@cat="smain"
and node[@rel="su" and @pt="vnw"]
and node[@rel="hd" and @pt="ww"]
and node[
@rel="predc"
and @cat="np"
and node[@rel="det" and @pt="lid"]
and node[@rel="hd" and @pt="n"]
]
]
let $tree := ($node/ancestor::alpino_ds)
let $sentid := ($tree/@id)
let $sentence := ($tree/sentence)
let $ids := ($node//@id)
let $begins := ($node//@begin)
let $beginlist := (distinct-values($begins))
let $meta := ($tree/metadata/meta)
return <match>{data($sentid)}||{data($sentence)}||{string-join($ids, '-')}||{string-join($beginlist, '-')}||{$node}||{$meta}||</match>
)
[position() = 51 to 100]
*/
func xquery_gretel_results(startIndex int, endIndex int, xpath string, context bool, variables []xPathVariable) string {
var optContextDeclaration string
var optContextResults string
if context {
optContextDeclaration = `
let $prevs := ($tree/preceding-sibling::alpino_ds[1]/sentence)
let $nexts := ($tree/following-sibling::alpino_ds[1]/sentence)
`
optContextResults = `
<previous-sentence>{data($prevs)}</previous-sentence>
<next-sentence>{data($nexts)}</next-sentence>
`
}
var optVariableDeclaration string
var optVariableResults string
for _, variable := range variables {
optVariableResults += `<var name="` + variable.Name + `">{` + variable.Name + `/@*}</var>`
if variable.Name != "$node" { // variable $node already exists in query
optVariableDeclaration += "let " + variable.Name + " := (" + variable.Path + ")[1]\n"
}
}
return `(
for $node in collection()` + xpath + `
let $tree := ($node/ancestor::alpino_ds)
let $sentid := dbxml:metadata('dbxml:name', $tree)
let $sentence := ($tree/sentence)
let $begins := ($node//@begin)
let $ids := (distinct-values($begins))
let $meta := ($tree/metadata/meta)
` + optVariableDeclaration + optContextDeclaration + `
return
<match>
<sentence-id>{data($sentid)}</sentence-id>
<sentence><em>{data($sentence)}</em></sentence>
<node-begins>
{for $nodeId in $begins return <id>{data($nodeId)}</id>}
</node-begins>
<node-ids>
{for $nodeId in $ids return <id>{data($nodeId)}</id>}
</node-ids>
<result>{$node}</result>
<meta>{$meta}</meta>
<vars>` + optVariableResults + `</vars>
` + optContextResults + `
</match>
)[position() = ` + strconv.Itoa(startIndex) + ` to ` + strconv.Itoa(endIndex) + `]`
}