in output_formats.py [0:0]
def run_inference(self, example: InputExample, output_sentence: str,
entity_types: Dict[str, EntityType] = None, relation_types: Dict[str, RelationType] = None) \
-> Tuple[set, set, bool]:
"""
Process an output sentence to extract arguments, given as entities and relations.
"""
entity_types = set(entity_type.natural for entity_type in entity_types.values())
relation_types = set(relation_type.natural for relation_type in relation_types.values()) \
if relation_types is not None else {}
triggers = example.triggers
assert len(triggers) <= 1
if len(triggers) == 0:
# we do not have triggers
return set(), set(), False
trigger = triggers[0]
# parse output sentence
raw_predicted_entities, wrong_reconstruction = self.parse_output_sentence(example, output_sentence)
# update predicted entities with the positions in the original sentence
predicted_entities = set()
predicted_relations = set()
# process and filter entities
for entity_name, tags, start, end in raw_predicted_entities:
if len(tags) == 0 or len(tags[0]) > 1:
# we do not have a tag for the entity type
continue
entity_type = tags[0][0]
if entity_type in entity_types:
entity_tuple = (entity_type, start, end)
predicted_entities.add(entity_tuple)
# process tags to get relations
for tag in tags[1:]:
if len(tag) == 2:
relation_type, related_entity = tag
if relation_type in relation_types:
predicted_relations.add(
(relation_type, entity_tuple, (trigger.type.natural, trigger.start, trigger.end))
)
return predicted_entities, predicted_relations, wrong_reconstruction