def _extract_entity(self, chunk, current_offset): """Extract an entity from a chunk if present.""" entity = None if "meta" in chunk or "alias" in chunk: start = current_offset text = chunk['text'] end = start + len(text) entity_type = chunk.get("alias", chunk["meta"]) if entity_type != u'@sys.ignore': entity = utils.build_entity(start, end, text, entity_type) return entity
def _find_entities_in_training_example(self, example): """Extracts entities from a markdown intent example.""" entities = [] offset = 0 for match in re.finditer(ent_regex, example): entity_text = match.groupdict()['entity_text'] entity_type = match.groupdict()['entity'] entity_value = match.groupdict()['value'] if match.groupdict()['value'] else entity_text start_index = match.start() - offset end_index = start_index + len(entity_text) offset += len(match.group(0)) - len(entity_text) entity = build_entity(start_index, end_index, entity_value, entity_type) entities.append(entity) return entities