コード例 #1
0
    def parse(self, message, project_name):
        from rasa_nlu.project import Project
        self.logger.info('Parsing message with %s: %s' %
                         (project_name, message))

        project = self._projects.get(project_name)
        if project is None:
            project = Project(project=project_name,
                              project_dir=self._project_dir)

            self._projects[project_name] = project

        # --------------------------
        # RasaNLU intent JSON format
        # --------------------------
        # {
        #   "intent": {
        #     "name": "...",
        #     "confidence": 1.0
        #   },
        #   "entities": [
        #     {
        #       "value": "...",
        #       "entity": "..."
        #     }
        #   ],
        #   "text": "..."
        # }
        result = project.parse(message)
        self.logger.debug(str(result))

        return result
コード例 #2
0
ファイル: app.py プロジェクト: brBart/rhasspy-hassio-addon
def get_intent(profile, text):
    system = profile.intent.get('system', 'fuzzywuzzy')

    if system == 'rasa':
        rasa_config = profile.intent[system]

        # Use rasaNLU
        global intent_projects

        project = intent_projects.get(profile.name, None)
        if project is None:
            import rasa_nlu
            from rasa_nlu.project import Project
            project_dir = profile.read_path(rasa_config['project_dir'])
            project_name = rasa_config['project_name']

            project = Project(project=project_name, project_dir=project_dir)

            intent_projects[profile.name] = project

        return project.parse(text)
    elif system == 'remote':
        remote_url = profile.intent[system]['url']
        headers = {'Content-Type': 'text/plain'}

        # Pass profile name through
        params = {'profile': profile.name, 'nohass': True}
        response = requests.post(remote_url,
                                 headers=headers,
                                 data=text,
                                 params=params)

        response.raise_for_status()

        # Return intent directly
        return response.json()
    else:
        fuzzy_config = profile.intent[system]

        # Use fuzzywuzzy
        global intent_examples

        if not profile.name in intent_examples:
            examples_path = profile.read_path(fuzzy_config['examples_json'])
            with open(examples_path, 'r') as examples_file:
                intent_examples[profile.name] = json.load(examples_file)

        text, intent_name, slots = best_intent(intent_examples[profile.name],
                                               text)

        # Try to match RasaNLU format for future compatibility
        intent = {
            'text':
            text,
            'intent': {
                'name': intent_name,
            },
            'entities': [{
                'entity': name,
                'value': values[0]
            } for name, values in slots.items()]
        }

        return intent