-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.go
463 lines (433 loc) · 10 KB
/
template.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
package main
import (
"bytes"
"encoding/xml"
"fmt"
"regexp"
"strings"
"text/template"
"github.com/rug-compling/alpinods"
"github.com/rug-compling/alud/v2"
)
type Fields struct {
Corpusname string
Filename string
CorpusFilename string
Body string
ID int
IDs string
Sentid string
Sentence string
MarkedSentence string
Comments string
Match string
Tree string
Words string
Lemmas string
Pts string
Postags string
Metadata string
UD string
}
var (
re = regexp.MustCompile(`%[- +.#0-9]*[a-zA-Z%]`)
reBS = regexp.MustCompile(`\\.`)
)
/*
%% %
%c corpusname
%f filename
%F if corpusname then corpusname::filename else filename
%b file body
%i *id
%j all ids
%I sentid
%s sentence
%S *sentence marked
%o comments
%m *match
%M *match tree
%w *match, words only
%l *match, lemmas only
%p *match, pts only
%P *match, postags only
%d metadata
%u UD
*/
func transformTemplate(chIn <-chan Item, chOut chan<- Item, tmpl string) {
var needAlpino, needMeta, multi, needID, needIDs, needMatch, needMarked, needWords, needTree, needUD bool
format := reBS.ReplaceAllStringFunc(tmpl, func(s string) string {
if s == `\n` {
return "\n"
}
if s == `\t` {
return "\t"
}
if s == `\\` {
return "\\"
}
return s
})
format = re.ReplaceAllStringFunc(format, func(s string) string {
if s == "%%" {
return "%"
}
toS := ` | printf "` + s[:len(s)-1] + `s"}}`
toD := ` | printf "` + s[:len(s)-1] + `d"}}`
switch s[len(s)-1] {
case 'c':
return "{{.Corpusname" + toS
case 'f':
return "{{.Filename" + toS
case 'F':
return "{{.CorpusFilename" + toS
case 'b':
return "{{.Body" + toS
case 'i':
needAlpino = true
needID = true
multi = true
return "{{.ID" + toD
case 'j':
needAlpino = true
needIDs = true
return "{{.IDs}}"
case 'I':
needAlpino = true
return "{{.Sentid" + toS
case 's':
needAlpino = true
return "{{.Sentence" + toS
case 'S':
needAlpino = true
needMarked = true
multi = true
return "{{.MarkedSentence" + toS
case 'm':
needMatch = true
multi = true
return "{{.Match" + toS
case 'M':
needAlpino = true
needTree = true
multi = true
return "{{.Tree" + toS
case 'o':
needAlpino = true
return "{{.Comments" + toS
case 'w':
needAlpino = true
needWords = true
multi = true
return "{{.Words" + toS
case 'l':
needAlpino = true
needWords = true
multi = true
return "{{.Lemmas" + toS
case 'p':
needAlpino = true
needWords = true
multi = true
return "{{.Pts" + toS
case 'P':
needAlpino = true
needWords = true
multi = true
return "{{.Postags" + toS
case 'd':
needAlpino = true
needMeta = true
return "{{.Metadata}}"
case 'u':
needAlpino = true
needUD = true
return "{{.UD}}"
default:
x(fmt.Errorf("Unknown flag %q", s))
}
return ""
})
if !(strings.HasSuffix(format, "\n") || strings.HasSuffix(format, "{{.UD}}")) {
format += "\n"
}
myTemplate, err := template.New("tmpl").Parse(format)
x(err)
for item := range chIn {
if !item.transformed {
item.transformed = true
item.name = trimXML(item.name) + ".t"
}
var out bytes.Buffer
var data Fields
data.Corpusname = item.arch
data.Filename = item.oriname
if item.arch == "" {
data.CorpusFilename = item.oriname
} else {
data.CorpusFilename = item.arch + "::" + item.oriname
}
data.Body = item.data
var alpino alpinods.AlpinoDS
if needAlpino {
if w(xml.Unmarshal([]byte(item.data), &alpino)) == nil {
data.Sentid = alpino.Sentence.SentID
data.Sentence = alpino.Sentence.Sentence
if alpino.Comments != nil && alpino.Comments.Comment != nil {
data.Comments = strings.Join(alpino.Comments.Comment, "\n\t")
}
if needMeta && alpino.Metadata != nil && alpino.Metadata.Meta != nil {
metas := make([]string, len(alpino.Metadata.Meta))
for i, meta := range alpino.Metadata.Meta {
metas[i] = fmt.Sprintf("%s: %q", meta.Name, meta.Value)
}
data.Metadata = strings.Join(metas, ", ")
}
if needUD {
if alpino.Conllu == nil {
if item.arch != "" {
data.UD = fmt.Sprintf("# archive = %s\n", item.arch)
}
ud, err := alud.Ud([]byte(item.data), item.oriname, alpino.Sentence.SentID, alud.OPT_DUMMY_OUTPUT)
if err == nil {
data.UD += ud
} else {
e := err.Error()
i := strings.Index(e, "\n")
if i > 0 {
e = e[:i]
}
data.UD += fmt.Sprintf("# source = %s\n# error = %s\n\n", item.oriname, e)
}
} else if alpino.Conllu.Error != "" {
if item.arch != "" {
data.UD = fmt.Sprintf("# archive = %s\n", item.arch)
}
data.UD += fmt.Sprintf("# source = %s\n# error = %s\n\n", item.oriname, alpino.Conllu.Error)
} else {
data.UD = strings.TrimLeft(alpino.Conllu.Conllu, " \t\r\n")
if item.arch != "" {
if strings.Contains(data.UD, "# source =") && !strings.Contains(data.UD, "# archive") {
data.UD = fmt.Sprintf("# archive = %s\n%s", item.arch, data.UD)
}
}
if !strings.HasSuffix(data.UD, "\n") {
data.UD += "\n\n"
} else if !strings.HasSuffix(data.UD, "\n\n") {
data.UD += "\n"
}
}
}
}
}
var i int
for {
if multi {
var node alpinods.Node
ok := false
if needAlpino {
if w(xml.Unmarshal([]byte(item.match[i]), &node)) == nil {
ok = true
}
}
if needID {
if ok {
data.ID = node.ID
} else {
data.ID = -1
}
}
if needWords || needMarked {
data.Words, data.Lemmas, data.Pts, data.Postags, data.MarkedSentence = doWords(&alpino, &node)
}
if needTree {
data.Tree = doTree(&alpino, &node)
}
if needMatch {
data.Match = item.match[i]
}
}
if needIDs {
idlist := make([]string, 0)
for _, match := range item.match {
var node alpinods.Node
if w(xml.Unmarshal([]byte(match), &node)) == nil {
idlist = append(idlist, fmt.Sprint(node.ID))
} else {
idlist = []string{"NA"}
}
}
data.IDs = strings.Join(idlist, " ")
}
x(myTemplate.Execute(&out, data))
i++
if i == len(item.match) || !multi {
break
}
}
item.data = out.String()
item.match = make([]string, 0)
item.original = false
chOut <- item
} // for item
close(chOut)
}
func doTree(alpino *alpinods.AlpinoDS, node *alpinods.Node) string {
var out bytes.Buffer
first := true
nodelist := make([]*alpinods.Node, 1)
nodelist[0] = node
seen := make(map[int]bool)
handled := make(map[int]bool)
var f func(*alpinods.Node, string, bool)
f = func(node *alpinods.Node, indent string, doRel bool) {
if node == nil {
return
}
p := indent
if doRel {
fmt.Fprint(&out, p, node.Rel)
p = " "
}
if node.Index != 0 {
seen[node.Index] = true
if node.Word != "" || node.Node != nil && len(node.Node) > 0 {
handled[node.Index] = true
}
fmt.Fprintf(&out, "%s[%d]", p, node.Index)
p = " "
}
if node.Cat != "" {
fmt.Fprint(&out, p, node.Cat)
} else if node.Pt != "" {
fmt.Fprintf(&out, "%s%s %q", p, node.Pt, node.Word)
}
fmt.Fprintln(&out)
if node.Node != nil {
indent += " "
for _, n := range node.Node {
f(n, indent, true)
}
}
for n := range seen {
if !handled[n] {
nodelist = append(nodelist, findNodeByIndex(alpino.Node, n))
handled[n] = true
}
}
}
for len(nodelist) > 0 {
current := nodelist[0]
nodelist = nodelist[1:]
f(current, "", first)
first = false
}
return out.String()
}
func doWords(alpino *alpinods.AlpinoDS, node *alpinods.Node) (words, lemmas, pts, postags, sentence string) {
if alpino == nil || alpino.Node == nil {
return
}
nwords := alpino.Node.End
wordslist := make([]string, nwords)
use := make([]bool, nwords)
slemmas := make([]string, nwords)
spts := make([]string, nwords)
spostags := make([]string, nwords)
var f func(*alpinods.Node)
f = func(node *alpinods.Node) {
if node.Word != "" {
use[node.Begin] = true
slemmas[node.Begin] = node.Lemma
spts[node.Begin] = node.Pt
spostags[node.Begin] = node.Postag
}
if node.Node != nil {
for _, n := range node.Node {
f(n)
}
}
if node.Index > 0 && node.Word == "" && (node.Node == nil || len(node.Node) == 0) {
f(findNodeByIndex(alpino.Node, node.Index))
}
}
f(node)
first := nwords
last := 0
inUse := false
swords := strings.Fields(alpino.Sentence.Sentence)
for i, w := range swords {
if use[i] {
if i < first {
first = i
}
if i > last {
last = i
}
if !inUse {
inUse = true
wordslist[i] = "\x1B[7m" + w
} else {
wordslist[i] = w
}
} else {
if inUse {
inUse = false
wordslist[i-1] += "\x1B[0m"
}
wordslist[i] = w
}
}
if inUse {
wordslist[nwords-1] += "\x1B[0m"
}
sentence = strings.Join(wordslist, " ")
if last >= first {
wlist := make([]string, 0, last-first+1)
llist := make([]string, 0, last-first+1)
plist := make([]string, 0, last-first+1)
Plist := make([]string, 0, last-first+1)
inUse = true
for i := first; i <= last; i++ {
if use[i] {
inUse = true
wlist = append(wlist, swords[i])
llist = append(llist, slemmas[i])
plist = append(plist, spts[i])
Plist = append(Plist, spostags[i])
} else {
if inUse {
wlist = append(wlist, "[...]")
llist = append(llist, "[...]")
plist = append(plist, "[...]")
Plist = append(Plist, "[...]")
inUse = false
}
}
}
words = strings.Join(wlist, " ")
lemmas = strings.Join(llist, " ")
pts = strings.Join(plist, " ")
postags = strings.Join(Plist, " ")
}
return words, lemmas, pts, postags, sentence
}
func findNodeByIndex(node *alpinods.Node, index int) *alpinods.Node {
if node.Index == index {
if node.Word != "" {
return node
}
if node.Node != nil && len(node.Node) > 0 {
return node
}
}
if node.Node != nil {
for _, n := range node.Node {
nn := findNodeByIndex(n, index)
if nn != nil {
return nn
}
}
}
return nil
}