Beispiel #1
0
    def from_byte_array(cls, engine_bytes):
        """Load a :class:`SnipsNLUEngine` instance from a bytearray

        Args:
            engine_bytes (bytearray): A bytearray representing a zipped nlu
                engine.
        """
        with temp_dir() as tmp_dir:
            archive_path = tmp_dir / "trained_engine.zip"
            with archive_path.open(mode="wb") as f:
                f.write(engine_bytes)
            unzip_archive(archive_path, str(tmp_dir))
            engine = cls.from_path(tmp_dir / "trained_engine")
        return engine
Beispiel #2
0
    def from_byte_array(cls, unit_bytes, **shared):
        """Load a :class:`ProcessingUnit` instance from a bytearray

        Args:
            unit_bytes (bytearray): A bytearray representing a zipped
                processing unit.
        """
        cleaned_unit_name = _sanitize_unit_name(cls.unit_name)
        with temp_dir() as tmp_dir:
            file_io = io.BytesIO(unit_bytes)
            unzip_archive(file_io, str(tmp_dir))
            processing_unit = cls.from_path(tmp_dir / cleaned_unit_name,
                                            **shared)
        return processing_unit
Beispiel #3
0
    def from_byte_array(cls, unit_bytes):
        """Load a :class:`ProcessingUnit` instance from a bytearray

        Args:
            unit_bytes (bytearray): A bytearray representing a zipped
                processing unit.
        """
        cleaned_unit_name = _sanitize_unit_name(cls.unit_name)
        with temp_dir() as tmp_dir:
            archive_path = (tmp_dir / cleaned_unit_name).with_suffix(".zip")
            with archive_path.open(mode="wb") as f:
                f.write(unit_bytes)
            unzip_archive(archive_path, str(tmp_dir))
            processing_unit = cls.from_path(tmp_dir / cleaned_unit_name)
        return processing_unit
Beispiel #4
0
def _build_builtin_parser(language, gazetteer_entities):
    with temp_dir() as serialization_dir:
        gazetteer_entity_parser = None
        if gazetteer_entities:
            gazetteer_entity_parser = _build_gazetteer_parser(
                serialization_dir, gazetteer_entities, language)

        metadata = {
            "language": language.upper(),
            "gazetteer_parser": gazetteer_entity_parser
        }
        metadata_path = serialization_dir / "metadata.json"
        with metadata_path.open("w", encoding="utf-8") as f:
            f.write(json_string(metadata))
        parser = _BuiltinEntityParser.from_path(serialization_dir)
        return BuiltinEntityParser(parser)
Beispiel #5
0
    def to_byte_array(self):
        """Serialize the :class:`SnipsNLUEngine` instance into a bytearray

        This method persists the engine in a temporary directory, zip the
        directory and return the zipped file as binary data.

        Returns:
            bytearray: the engine as bytearray data
        """

        with temp_dir() as tmp_dir:
            engine_dir = tmp_dir / "trained_engine"
            self.persist(engine_dir)
            archive_base_name = tmp_dir / "trained_engine"
            archive_name = archive_base_name.with_suffix(".zip")
            shutil.make_archive(base_name=str(archive_base_name),
                                format="zip",
                                root_dir=str(tmp_dir),
                                base_dir="trained_engine")
            with archive_name.open(mode="rb") as f:
                engine_bytes = f.read()
        return engine_bytes
Beispiel #6
0
    def to_byte_array(self):
        """Serialize the :class:`ProcessingUnit` instance into a bytearray

        This method persists the processing unit in a temporary directory, zip
        the directory and return the zipped file as binary data.

        Returns:
            bytearray: the processing unit as bytearray data
        """
        cleaned_unit_name = _sanitize_unit_name(self.unit_name)
        with temp_dir() as tmp_dir:
            processing_unit_dir = tmp_dir / cleaned_unit_name
            self.persist(processing_unit_dir)
            archive_base_name = tmp_dir / cleaned_unit_name
            archive_name = archive_base_name.with_suffix(".zip")
            shutil.make_archive(base_name=str(archive_base_name),
                                format="zip",
                                root_dir=str(tmp_dir),
                                base_dir=cleaned_unit_name)
            with archive_name.open(mode="rb") as f:
                processing_unit_bytes = bytearray(f.read())
        return processing_unit_bytes