Repository navigation
Expand file tree
/
Copy pathpython.groovy
More file actions
70 lines (50 loc) · 1.99 KB
/
Copy pathpython.groovy
File metadata and controls
70 lines (50 loc) · 1.99 KB
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
/**
* This is an initial prototype of the python integration, intended to be run as a scriptable PR.
* It is very limited and will start a new process for each document. Use the Java PR instead..
*/
import gate.corpora.DocumentJsonUtils
import static groovy.json.JsonOutput.*
import groovy.json.JsonSlurper
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
outputFile = new File(scriptParams.outputFolder + "/" + doc.name)
JsonFactory factory = new JsonFactory()
outputFile.withOutputStream { out ->
allAnnotations = [:]
doc.getAnnotationSetNames().each { annotationSetName ->
annotationSet = doc.getAnnotations(annotationSetName)
allAnnotations.putAll(annotationSet.groupBy {
(annotationSetName ?: '') + ':' + it.type
})
}
JsonGenerator jsonG = factory.createGenerator(out)
try {
DocumentJsonUtils.writeDocument(doc, 0, doc.end(), allAnnotations, null, null,
"annotationID", jsonG)
} finally {
jsonG.close()
}
}
def process = ("/usr/local/bin/python "+ scriptParams.scriptLocation +" "+ outputFile.absolutePath).execute()
output = process.in.getText("UTF-8")
process.waitFor()
changes = new JsonSlurper().parseText(output)
changes.each { change ->
println change
if (change[0] == "ADD_ANNOT") {
doc.getAnnotations(change[1]).add(change[2] as long, change[3] as long, change[4], change[5].toFeatureMap())
}
if (change[0] == "REMOVE_ANNOT") {
annotation = doc.getAnnotations(change[1]).get(change[2])
doc.getAnnotations(change[1]).remove(annotation)
}
if (change[0] == "UPDATE_FEATURE") {
doc.getAnnotations(change[1]).get(change[2]).features.put(change[3], change[4])
}
if (change[0] == "CLEAR_FEATURES") {
doc.getAnnotations(change[1]).get(change[2]).features.clear()
}
if (change[0] == "REMOVE_FEATURE") {
doc.getAnnotations(change[1]).get(change[2]).features.remove(change[3])
}
}