def fit(self, data): data_lang = data.get('language') if data_lang != self.lang: 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' ) self._engine = SnipsNLUEngine(config, resources=load_resources( 'snips_nlu_%s' % self.lang)) self._engine.fit(data) if self.cache_directory: self._logger.info('Persisting trained engine to "%s"' % self.cache_directory) rmtree(self.cache_directory, ignore_errors=True) self._engine.persist(self.cache_directory) with open(cached_checksum_path, mode='w') as f: f.write(checksum) self._configure()
def test_it_should_read_the_file_relative_to_another_one(self): with patch('builtins.open', mock_open(read_data='some content')) as mopen: expect( read_file( 'somepath', relative_to_file='/home/julien/pytlas/a.file')).to.equal( 'some content') mopen.assert_called_once_with('/home/julien/pytlas%ssomepath' % os.path.sep, encoding='utf-8')
from pytlas import register_training, register_translations from pytlas.utils import read_file from .skill import * import json register_translations( 'fr', lambda: json.loads(read_file('lights.fr.json', relative_to_file=__file__))) register_training( 'en', lambda: read_file('lights.en.dsl', relative_to_file=__file__)) register_training( 'fr', lambda: read_file('lights.fr.dsl', relative_to_file=__file__))
def test_it_should_read_the_file_correctly(self): with patch('builtins.open', mock_open(read_data='some content')) as mopen: expect(read_file('somepath')).to.equal('some content') mopen.assert_called_once_with('somepath', encoding='utf-8')
def test_it_should_not_raise_exception_when_errors_ignored(self): expect(read_file('something', ignore_errors=True)).to.be.none
def test_it_should_raise_an_exception_when_errors_not_ignored(self): expect(lambda: read_file('something')).to.throw(Exception)