Beispiel #1
0
    def fit(self, data: dict) -> None:
        super().fit(data)

        data_lang = data.get('language')

        if data_lang != self.lang:  # pragma: no cover
            self._logger.warning(
                'Training language "%s" and interpreter language "%s" do not match,'\
                'things could go badly',
                data_lang, self.lang)

        self._logger.info('Fitting using "snips v%s"', __version__)

        checksum = compute_checksum(data)
        cached_checksum = None

        # Try to load the used checksum
        if self.cache_directory:
            cached_checksum_path = os.path.join(self.cache_directory,
                                                'trained.checksum')
            cached_checksum = read_file(cached_checksum_path,
                                        ignore_errors=True)

        if not cached_checksum:
            self._logger.debug('Checksum file not found')

        if checksum == cached_checksum:
            self.load_from_cache()
        else:
            config = None

            try:
                self._logger.info(
                    'Importing default configuration for language "%s"',
                    self.lang)
                config = getattr(snips_confs, 'CONFIG_%s' % self.lang.upper())
            except AttributeError:
                self._logger.warning(
                    'Could not import default configuration, it will use the generic one instead'
                )

            resource_pkg_name = self._check_and_install_resources_package()

            self._engine = SnipsNLUEngine(
                config, resources=load_resources(resource_pkg_name))
            self._engine.fit(data)

            if self.cache_directory:  # pragma: no cover
                self._logger.info('Persisting trained engine to "%s"',
                                  self.cache_directory)

                # Make sure the cache directory has been cleaned out
                rmtree(self.cache_directory, ignore_errors=True)

                self._engine.persist(self.cache_directory)

                with open(cached_checksum_path, mode='w') as file:
                    file.write(checksum)

            self._configure()
Beispiel #2
0
 def test_it_should_not_raise_exception_when_errors_ignored(self):
     expect(read_file('something', ignore_errors=True)).to.be.none