Repository navigation
Expand file tree
/
Copy pathiterator.py
More file actions
55 lines (45 loc) · 1.75 KB
/
Copy pathiterator.py
File metadata and controls
55 lines (45 loc) · 1.75 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
"""Create an interface by which you can listen for GATE documents with an iterator when your
program starts, and have the iterator quit when GATE gets the appropriate signal to say the pipeline has
ended."""
from document import Document
from corpus import Corpus
from gate_exceptions import InvalidOffsetException
import sys, json, codecs, inspect
class GateIterator(object):
def __init__(self):
self.scriptParams = {}
def __iter__(self):
line = sys.stdin.readline().strip()
while line:
line = codecs.decode(line, "utf8")
if line:
input_line = line
input_json = json.loads(line)
if "command" in input_json:
if input_json["command"] == "BEGIN_EXECUTION":
corpus = Corpus(input_json)
elif input_json["command"] == "ABORT_EXECUTION":
return
elif input_json["command"] == "END_EXECUTION":
return
else:
try:
document = Document.load(input_json)
self.scriptParams = input_json.get("scriptParams")
self.scriptParams = self.scriptParams if self.scriptParams else {}
self.scriptParams["inputAS"] = document.annotationSets[input_json["inputAS"]]
self.scriptParams["outputAS"] = document.annotationSets[input_json["outputAS"]]
inputAS = self.scriptParams["inputAS"]
outputAS = self.scriptParams["outputAS"]
yield document
print json.dumps(document.logger)
except InvalidOffsetException as e:
print >> sys.stderr, "InvalidOffsetException prevented reading a document " + e.message
print json.dumps([])
sys.stdout.flush()
line = sys.stdin.readline().strip()
def iterate():
"""I can't inherit any of this from ProcessingResource because
we need to completely change the flow to support iteration"""
iterator = GateIterator()
return iterator