Repository navigation
Expand file tree
/
Copy pathdocument.py
More file actions
65 lines (48 loc) · 1.6 KB
/
Copy pathdocument.py
File metadata and controls
65 lines (48 loc) · 1.6 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
from collections import defaultdict
from annotation_set import AnnotationSet
from sourcedstring import SourcedString
import HTMLParser, re
class _AnnotationSetsDict(defaultdict):
def __init__(self, doc, logger):
self.logger = logger
self.doc = doc
def __missing__(self, key):
annotationSet = AnnotationSet(self.doc, name = key, logger = self.logger)
self[key] = annotationSet
# self.logger.append(("CREATE_AS", key))
return annotationSet
class Document(object):
def __init__(self, logger, text, features, src=None):
self.logger = logger
self.annotationSets = _AnnotationSetsDict(self, self.logger)
self._text = text
self.src = src
self.features = features
@staticmethod
def load(json, src=None):
"""Loads the document from a dictionary that results from GATE json,
returns a document and a change logger"""
logger = []
text = json["text"]
features = json["documentFeatures"]
doc = Document(logger, text, features, src)
if "entities" in json and len(json["entities"]):
for entity_key, instances in json["entities"].iteritems():
annotation_set, annotation_name = entity_key.split(":")
for entity in instances:
start, end = entity.pop("indices")
_id = entity.pop("annotationID")
doc.annotationSets[annotation_set].add(start, end,
annotation_name, entity, _id)
del logger[:]
return doc
@property
def text(self):
return self._text
@text.setter
def text(self, value):
raise NotImplementedError("Text cannot be modified in a Python processing resource")
def size(self):
return int(len(self.text))
def __len__(self):
return self.size()