class CommandDetector: def __init__(self, ): self.container = IntentContainer('intent_cache') self.commands = Commands() def add_command(self, commands): for c in self.commands.family_list: for intent, filename in c.items(): self.container.load_file(intent, filename) def train_commands(self): for c in self.commands.family_list: self.add_command(c) self.container.train() def calc_intent(self, text): return self.container.calc_intent(text)
#!/usr/bin/env python3 # Sample Padatious program used for testing import sys from glob import glob from os.path import basename from padatious import IntentContainer reload_cache = len(sys.argv) > 1 and sys.argv[1] == '-r' container = IntentContainer('intent_cache') for file_name in glob('data/*.intent'): name = basename(file_name).replace('.intent', '') container.load_file(name, file_name, reload_cache=reload_cache) for file_name in glob('data/*.entity'): name = basename(file_name).replace('.entity', '') container.load_entity(name, file_name, reload_cache=reload_cache) container.train() query = None while query != 'q': query = input('> ') data = container.calc_intent(query) print(data.name + ': ' + str(data.conf)) for key, val in data.matches.items(): print('\t' + key + ': ' + val)
class PadatiousService(FallbackSkill): def __init__(self, emitter): FallbackSkill.__init__(self) self.config = ConfigurationManager.get()['padatious'] intent_cache = expanduser(self.config['intent_cache']) try: from padatious import IntentContainer except ImportError: LOG.error('Padatious not installed. Please re-run dev_setup.sh') try: call(['notify-send', 'Padatious not installed', 'Please run build_host_setup and dev_setup again']) except OSError: pass return ver = get_distribution('padatious').version LOG.warning('VERSION: ' + ver) if ver != PADATIOUS_VERSION: LOG.warning('Using Padatious v' + ver + '. Please re-run ' + 'dev_setup.sh to install ' + PADATIOUS_VERSION) self.container = IntentContainer(intent_cache) self.emitter = emitter self.emitter.on('padatious:register_intent', self.register_intent) self.register_fallback(self.handle_fallback, 5) self.finished_training_event = Event() self.train_delay = self.config['train_delay'] self.train_time = get_time() + self.train_delay self.wait_and_train() def wait_and_train(self): sleep(self.train_delay) if self.train_time < 0.0: return if self.train_time <= get_time() + 0.01: self.train_time = -1.0 self.finished_training_event.clear() LOG.info('Training...') self.container.train(print_updates=False) LOG.info('Training complete.') self.finished_training_event.set() def register_intent(self, message): LOG.debug('Registering Padatious intent: ' + message.data['intent_name']) file_name = message.data['file_name'] intent_name = message.data['intent_name'] if not isfile(file_name): return self.container.load_file(intent_name, file_name) self.train_time = get_time() + self.train_delay self.wait_and_train() def handle_fallback(self, message): utt = message.data.get('utterance') LOG.debug("Padatious fallback attempt: " + utt) utt = normalize(utt, message.data.get('lang', 'en-us')) if not self.finished_training_event.is_set(): LOG.debug('Waiting for training to finish...') self.finished_training_event.wait() data = self.container.calc_intent(utt) if data.conf < 0.5: return False self.emitter.emit(Message(data.name, data=data.matches)) return True