Beispiel #1
0
    def parse(self, query):
        with string_pointer(c_char_p()) as ptr:
            lib.ffi_snips_nlu_engine_run_parse_into_json(
                self._engine, query.encode("utf-8"), byref(ptr))
            result = string_at(ptr)

        return json.loads(result.decode("utf-8"))
Beispiel #2
0
    def get_intents(self, query):
        """Returns all intents sorted by decreasing confidence scores

        Args:
            query (str): input to process

        Returns:
            A list of intents along with their probability. See
            https://snips-nlu.readthedocs.io/en/latest/tutorial.html#parsing for details about the
            format.
        """
        with string_pointer(c_char_p()) as ptr:
            lib.ffi_snips_nlu_engine_run_get_intents_into_json(
                self._engine, query.encode("utf8"), byref(ptr))
            result = string_at(ptr)
        return json.loads(result.decode("utf8"))
Beispiel #3
0
    def get_slots(self, query, intent):
        """Extracts slots from the input when the intent is known

        Args:
            query (str): input to process
            intent (str): intent which the input corresponds to

        Returns:
            A list of slots. See
            https://snips-nlu.readthedocs.io/en/latest/tutorial.html#parsing for details about the
            format.
        """
        with string_pointer(c_char_p()) as ptr:
            lib.ffi_snips_nlu_engine_run_get_slots_into_json(
                self._engine, query.encode("utf8"), intent.encode("utf8"), byref(ptr))
            result = string_at(ptr)
        return json.loads(result.decode("utf8"))
    def parse(self, query, intents_whitelist=None, intents_blacklist=None):
        """Extracts intent and slots from an input query

        Args:
            query (str): input to process
            intents_whitelist (list of str, optional): if defined, this will
                restrict the scope of intent parsing to the provided intents
            intents_blacklist (list of str, optional): if defined, these
                intents will be excluded from the scope of intent parsing

        Returns:
            A python dict containing data about intent and slots. See
            https://snips-nlu.readthedocs.io/en/latest/tutorial.html#parsing
            for details about the format.
        """
        if intents_whitelist is not None:
            if not all(
                    isinstance(intent, str) for intent in intents_whitelist):
                raise TypeError("Expected 'intents_whitelist' to contain "
                                "objects of type 'str'")
            intents = [intent.encode("utf8") for intent in intents_whitelist]
            arr = CStringArray()
            arr.size = c_int(len(intents))
            arr.data = (c_char_p * len(intents))(*intents)
            intents_whitelist = byref(arr)
        if intents_blacklist is not None:
            if not all(
                    isinstance(intent, str) for intent in intents_blacklist):
                raise TypeError("Expected 'intents_blacklist' to contain "
                                "objects of type 'str'")
            intents = [intent.encode("utf8") for intent in intents_blacklist]
            arr = CStringArray()
            arr.size = c_int(len(intents))
            arr.data = (c_char_p * len(intents))(*intents)
            intents_blacklist = byref(arr)
        with string_pointer(c_char_p()) as ptr:
            exit_code = lib.ffi_snips_nlu_engine_run_parse_into_json(
                self._engine, query.encode("utf8"), intents_whitelist,
                intents_blacklist, byref(ptr))
            msg = "Something went wrong when parsing query '%s'" % query
            check_ffi_error(exit_code, msg)
            result = string_at(ptr)

        return json.loads(result.decode("utf8"))