mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nlp.git
synced 2024-09-21 13:52:52 +00:00
116 lines
3.8 KiB
Python
116 lines
3.8 KiB
Python
from .export import ExportMixin
|
|
|
|
|
|
class StandOffData(ExportMixin):
|
|
def __init__(self, attrs):
|
|
self.meta = attrs.get('meta', {})
|
|
self.lookup = {tag_definition.id: tag_definition for tag_definition in
|
|
[TagDefinition(x) for x in attrs.get('tags', [])]}
|
|
self.annotations = [TagAnnotation(x, self.lookup) for x in
|
|
attrs.get('annotations', [])]
|
|
|
|
|
|
class TagAnnotation:
|
|
def __init__(self, attrs, lookup):
|
|
self.lookup = lookup
|
|
self.tag_id = attrs['tag_id']
|
|
if self.tag_id not in self.lookup:
|
|
raise Exception('Unknown tag id: {}'.format(self.tag_id))
|
|
self.start = attrs['start']
|
|
self.end = attrs['end']
|
|
if self.start >= self.end:
|
|
raise Exception('start must be lower then end')
|
|
self.description = attrs.get('description', '')
|
|
self.properties = [
|
|
PropertyAnnotation({**x, 'tag_id': self.tag_id}, self.lookup)
|
|
for x in attrs.get('properties', [])
|
|
]
|
|
for required_property_id in self.lookup[self.tag_id].required_properties:
|
|
if required_property_id not in self.properties:
|
|
raise Exception(
|
|
'Missing required property: {}'.format(required_property_id))
|
|
|
|
@property
|
|
def name(self):
|
|
return self.lookup[self.tag_id].name
|
|
|
|
def __lt__(self, other):
|
|
if self.start == other.start:
|
|
return self.name == 'token' and other.name != 'token'
|
|
else:
|
|
return self.start < other.start
|
|
|
|
def __le__(self, other):
|
|
if self.start == other.start:
|
|
return self.name == 'token' or other.name != 'token'
|
|
else:
|
|
return self.start < other.start
|
|
|
|
def __eq__(self, other):
|
|
return self.start == other.start and self.name == other.name
|
|
|
|
def __ne__(self, other):
|
|
return self.start != other.start and self.name != other.name
|
|
|
|
def __gt__(self, other):
|
|
if self.start == other.start:
|
|
return self.name != 'token' and other.name == 'token'
|
|
else:
|
|
return self.start > other.start
|
|
|
|
def __ge__(self, other):
|
|
if self.start == other.start:
|
|
return self.name != 'token' or other.name == 'token'
|
|
else:
|
|
return self.start > other.start
|
|
|
|
|
|
class PropertyAnnotation:
|
|
def __init__(self, attrs, lookup):
|
|
self.lookup = lookup
|
|
self.property_id = attrs['property_id']
|
|
self.tag_id = attrs['tag_id']
|
|
if self.property_id not in self.lookup[self.tag_id].properties:
|
|
raise Exception('Unknown property id: {}'.format(self.property_id))
|
|
self.value = attrs['value']
|
|
# TODO: Process attrs['possibleValues'] as self.labels (no id?)
|
|
|
|
@property
|
|
def name(self):
|
|
return self.lookup[self.tag_id].properties[self.property_id].name
|
|
|
|
|
|
class TagDefinition:
|
|
def __init__(self, attrs):
|
|
self.id = attrs['id']
|
|
self.name = attrs['name']
|
|
self.description = attrs.get('description', '')
|
|
self.properties = {
|
|
property_definition.id: property_definition
|
|
for property_definition in [
|
|
PropertyDefinition(x) for x in attrs.get('properties', [])
|
|
]
|
|
}
|
|
|
|
@property
|
|
def required_properties(self):
|
|
return {property.id: property for property in self.properties.values()
|
|
if property.is_required}
|
|
|
|
|
|
class PropertyDefinition:
|
|
def __init__(self, attrs):
|
|
self.id = attrs['id']
|
|
self.name = attrs['name']
|
|
self.description = attrs.get('description', '')
|
|
self.flags = attrs.get('flags', [])
|
|
self.labels = attrs.get('labels', [])
|
|
|
|
@property
|
|
def is_required(self):
|
|
return 'required' in self.flags
|
|
|
|
@property
|
|
def has_multiple_values(self):
|
|
return 'multiple' in self.flags
|