Exemplo n.º 1
0
    def run(self, key, ctx: Context):
        from sagas.nlu.rasa_procs import invoke_nlu

        lang = ctx.meta['lang']
        if lang not in default_projects:
            return False
        # proj=default_projects[lang]
        proj = lang

        def proc(cnt: Text) -> bool:
            succ = False
            logger.debug('query with rasa-nlu: %s', cnt)
            # print(('query with rasa-nlu: %s'%cnt))
            resp = invoke_nlu(self.endpoint, proj, "current", cnt)
            if resp is not None:
                intent = resp["intent"]
                entities = resp['entities']

                ent_names = {ent['entity'] for ent in entities}
                intent_name = intent['name']
                intent_confidence = float(intent['confidence'])
                self._result = intent_confidence
                logger.info('%s(%s) -> %f, with entities %s' %
                            (cnt, intent_name, intent_confidence,
                             ', '.join(ent_names)))
                # print(f'{self.intent}, {self.confidence}')
                if self.intent == intent_name and intent_confidence > self.confidence:
                    # print('... matched intent and confidence')
                    ctx.add_result(self.name(), 'default', key, {
                        'intent': intent_name,
                        'confidence': intent_confidence
                    })
                    if self.contains_entity is None:
                        succ = True
                    elif self.contains_entity is not None and ent_names.issubset(
                            self.contains_entity):
                        succ = True
            return succ

        if self.entire:
            # print('proc -> %s'%key)
            return proc(key)
        else:
            for cnt in ctx.stem_pieces(key):
                result = proc(cnt)
                if result:
                    return True

        return False
Exemplo n.º 2
0
def check_interr(key: Text, ctx: Context, check_fn, lang='pt') -> bool:
    for stem in ctx.stem_pieces(key):
        interr = get_interrogative(stem, lang)
        if interr and check_fn(interr):
            return True
    return False