def parse(self, text, scope=None):
        """Extract builtin entities from *text*

        Args:
            text (str): Input
            scope (list of str, optional): List of builtin entity labels. If
                defined, the parser will extract entities using the provided
                scope instead of the entire scope of all available entities.
                This allows to look for specifics builtin entity kinds.

        Returns:
            list of dict: The list of extracted entities
        """
        if not isinstance(text, str):
            raise TypeError("Expected language to be of type 'str' but found: "
                            "%s" % type(text))
        if scope is not None:
            if not all(isinstance(e, str) for e in scope):
                raise TypeError(
                    "Expected scope to contain objects of type 'str'")
            scope = [e.encode("utf8") for e in scope]
            arr = CStringArray()
            arr.size = c_int(len(scope))
            arr.data = (c_char_p * len(scope))(*scope)
            scope = byref(arr)

        with string_pointer(c_char_p()) as ptr:
            exit_code = lib.snips_nlu_ontology_extract_builtin_entities_json(
                self._parser, text.encode("utf8"), scope, byref(ptr))
            check_ffi_error(
                exit_code, "Something went wrong when extracting "
                "builtin entities")
            result = string_at(ptr)
            return json.loads(result.decode("utf8"))
Esempio n. 2
0
def get_builtin_entity_shortname(entity):
    """Get the short name of the entity

    Examples:

    >>> get_builtin_entity_shortname(u"snips/amountOfMoney")
    'AmountOfMoney'
    """
    global _BUILTIN_ENTITIES_SHORTNAMES
    if entity not in _BUILTIN_ENTITIES_SHORTNAMES:
        with string_pointer(c_char_p()) as ptr:
            exit_code = lib.snips_nlu_ontology_entity_shortname(
                entity.encode("utf8"), byref(ptr))
            check_ffi_error(
                exit_code, "Something went wrong when retrieving "
                "builtin entity shortname")
            result = string_at(ptr)
            _BUILTIN_ENTITIES_SHORTNAMES[entity] = result.decode("utf8")
    return _BUILTIN_ENTITIES_SHORTNAMES[entity]