mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nlp.git
synced 2024-12-26 08:34:17 +00:00
optimizations
This commit is contained in:
parent
0ba0c14b72
commit
29ccfac4f6
@ -2,12 +2,26 @@ from xml.sax.saxutils import escape
|
|||||||
|
|
||||||
|
|
||||||
class StandOffData:
|
class StandOffData:
|
||||||
def __init__(self, attrs):
|
def __init__(self, attrs={}):
|
||||||
self.meta = attrs.get('meta', {})
|
self.meta = attrs.get('meta', {})
|
||||||
self.lookup = {tag_definition.id: tag_definition for tag_definition in
|
self.lookup = {}
|
||||||
[TagDefinition(x) for x in attrs.get('tags', [])]}
|
for x in attrs.get('tags', []):
|
||||||
self.annotations = [TagAnnotation(x, self.lookup) for x in
|
self.add_tag_definition(x)
|
||||||
attrs.get('annotations', [])]
|
self.annotations = [TagAnnotation(x, self.lookup)
|
||||||
|
for x in attrs.get('annotations', [])]
|
||||||
|
|
||||||
|
def add_tag_definition(self, attrs):
|
||||||
|
tag_definition = TagDefinition(attrs)
|
||||||
|
if tag_definition.id in self.lookup:
|
||||||
|
raise Exception('Tag id already in use: {}'.format(self.to_dict()))
|
||||||
|
self.lookup[tag_definition.id] = tag_definition
|
||||||
|
|
||||||
|
def to_dict(self):
|
||||||
|
return {
|
||||||
|
'meta': self.meta,
|
||||||
|
'lookup': {k: v.to_dict() for k, v in self.lookup.items()},
|
||||||
|
'annotations': [x.to_dict() for x in self.annotations]
|
||||||
|
}
|
||||||
|
|
||||||
def to_vrt(self, text):
|
def to_vrt(self, text):
|
||||||
# Devide annotations into CWB's verticalized text format (.vrt) logic
|
# Devide annotations into CWB's verticalized text format (.vrt) logic
|
||||||
@ -88,21 +102,23 @@ class TagAnnotation:
|
|||||||
def __init__(self, attrs, lookup):
|
def __init__(self, attrs, lookup):
|
||||||
self.lookup = lookup
|
self.lookup = lookup
|
||||||
self.tag_id = attrs['tag_id']
|
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.start = attrs['start']
|
||||||
self.end = attrs['end']
|
self.end = attrs['end']
|
||||||
if self.start >= self.end:
|
|
||||||
raise Exception('start must be lower then end')
|
|
||||||
self.properties = [
|
self.properties = [
|
||||||
PropertyAnnotation({**x, 'tag_id': self.tag_id}, self.lookup)
|
PropertyAnnotation(x, self.lookup[self.tag_id].properties)
|
||||||
for x in attrs.get('properties', [])
|
for x in attrs.get('properties', [])
|
||||||
]
|
]
|
||||||
|
''' Sanity checks '''
|
||||||
|
if self.tag_id not in self.lookup:
|
||||||
|
raise Exception('Unknown tag: {}'.format(self.to_dict()))
|
||||||
|
if self.end < self.start:
|
||||||
|
raise Exception('Annotation end less then start: '
|
||||||
|
'{}'.format(self.to_dict()))
|
||||||
property_ids = [x.property_id for x in self.properties]
|
property_ids = [x.property_id for x in self.properties]
|
||||||
for required_property_id in self.lookup[self.tag_id].required_properties:
|
for required_property_id, required_property in self.lookup[self.tag_id].required_properties.items(): # noqa
|
||||||
if required_property_id not in property_ids:
|
if required_property_id not in property_ids:
|
||||||
raise Exception(
|
raise Exception('Missing required property: '
|
||||||
'Missing required property: {}'.format(required_property_id))
|
'{}'.format(required_property.to_dict()))
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
@ -151,15 +167,15 @@ class PropertyAnnotation:
|
|||||||
def __init__(self, attrs, lookup):
|
def __init__(self, attrs, lookup):
|
||||||
self.lookup = lookup
|
self.lookup = lookup
|
||||||
self.property_id = attrs['property_id']
|
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']
|
self.value = attrs['value']
|
||||||
# TODO: Process attrs['possibleValues'] as self.labels (no id?)
|
# TODO: Process attrs['possibleValues'] as self.labels (no id?)
|
||||||
|
''' Sanity checks '''
|
||||||
|
if self.property_id not in self.lookup:
|
||||||
|
raise Exception('Unknown property: {}'.format(self.to_dict()))
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
return self.lookup[self.tag_id].properties[self.property_id].name
|
return self.lookup[self.property_id].name
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
return {
|
return {
|
||||||
@ -174,18 +190,30 @@ class TagDefinition:
|
|||||||
self.id = attrs['id']
|
self.id = attrs['id']
|
||||||
self.name = attrs['name']
|
self.name = attrs['name']
|
||||||
self.description = attrs.get('description', '')
|
self.description = attrs.get('description', '')
|
||||||
self.properties = {
|
self.properties = {}
|
||||||
property_definition.id: property_definition
|
for x in attrs.get('properties', []):
|
||||||
for property_definition in [
|
self.add_property_definition(x)
|
||||||
PropertyDefinition(x) for x in attrs.get('properties', [])
|
|
||||||
]
|
def add_property_definition(self, attrs):
|
||||||
}
|
property_definition = PropertyDefinition(attrs)
|
||||||
|
if property_definition.id in self.properties:
|
||||||
|
raise Exception('Property id already in use: '
|
||||||
|
'{}'.format(property_definition.to_dict()))
|
||||||
|
self.properties[property_definition.id] = property_definition
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def required_properties(self):
|
def required_properties(self):
|
||||||
return {property.id: property for property in self.properties.values()
|
return {property.id: property for property in self.properties.values()
|
||||||
if property.is_required}
|
if property.is_required}
|
||||||
|
|
||||||
|
def to_dict(self):
|
||||||
|
return {
|
||||||
|
'id': self.id,
|
||||||
|
'name': self.name,
|
||||||
|
'description': self.description,
|
||||||
|
'properties': {k: v.to_dict() for k, v in self.properties.items()}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class PropertyDefinition:
|
class PropertyDefinition:
|
||||||
def __init__(self, attrs):
|
def __init__(self, attrs):
|
||||||
@ -202,3 +230,12 @@ class PropertyDefinition:
|
|||||||
@property
|
@property
|
||||||
def has_multiple_values(self):
|
def has_multiple_values(self):
|
||||||
return 'multiple' in self.flags
|
return 'multiple' in self.flags
|
||||||
|
|
||||||
|
def to_dict(self):
|
||||||
|
return {
|
||||||
|
'id': self.id,
|
||||||
|
'name': self.name,
|
||||||
|
'description': self.description,
|
||||||
|
'flags': self.flags,
|
||||||
|
'labels': self.labels
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user