From 6284b7c2ab4ae4c3655049fa5a82dd8250320f4a Mon Sep 17 00:00:00 2001 From: Mathieu Date: Tue, 14 Sep 2021 10:49:09 +0200 Subject: [PATCH] Added wolfram-alpha fallback --- jarvis/skills/intent_manager.py | 13 +++++++++-- jarvis/skills/research/wikipedia/__init__.py | 9 ++++---- jarvis/utils/fallbacks/__init__.py | 0 .../utils/fallbacks/wolframalpha/wa_client.py | 22 +++++++++++++++++++ 4 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 jarvis/utils/fallbacks/__init__.py create mode 100644 jarvis/utils/fallbacks/wolframalpha/wa_client.py diff --git a/jarvis/skills/intent_manager.py b/jarvis/skills/intent_manager.py index d79b85e..cadadb4 100644 --- a/jarvis/skills/intent_manager.py +++ b/jarvis/skills/intent_manager.py @@ -3,7 +3,8 @@ import json from adapt.engine import DomainIntentDeterminationEngine from padatious import IntentContainer -from jarvis.utils import utils +from jarvis.utils import utils, client_utils +from jarvis.utils.fallbacks.wolframalpha import wa_client adapt_engine = DomainIntentDeterminationEngine() padatious_intents_container = IntentContainer('intent_cache') @@ -104,7 +105,15 @@ def recognise(sentence, client_ip=None, client_port=None): confidence_padatious = get_confidence(best_intent_padatious) if confidence_adapt < 0.2 and confidence_padatious < 0.2: - return "I didn't understand..." + + # Wolfram-Alpha Fallback + wolfram_response = wa_client.ask(data['utterance']) + if wolfram_response is not None: + client_utils.speak(wolfram_response, client_ip, client_port) + return wolfram_response + else: + # Nothing found at all + return "I didn't understand..." else: return handle_intent(data, best_intent_adapt if confidence_adapt > confidence_padatious else best_intent_padatious) diff --git a/jarvis/skills/research/wikipedia/__init__.py b/jarvis/skills/research/wikipedia/__init__.py index c0388c6..dad4a6e 100644 --- a/jarvis/skills/research/wikipedia/__init__.py +++ b/jarvis/skills/research/wikipedia/__init__.py @@ -1,6 +1,7 @@ from jarvis.skills import Skill, SkillRegistering from jarvis.skills.decorators import intent_file_handler from jarvis.skills.research.wikipedia import wikipedia +from jarvis.utils.fallbacks.wolframalpha import wa_client class WikipediaSkill(Skill, metaclass=SkillRegistering): @@ -10,11 +11,11 @@ class WikipediaSkill(Skill, metaclass=SkillRegistering): @intent_file_handler("search.wikipedia.intent", "WikipediaQueryIntent") def handle_wikipedia_query_intent(self, data): if 'query' in data: - self.speak_dialog_threaded("wikipedia_searching") - print("[INTENT RESULT] : " + wikipedia.page_summary(query=data['query'])) + self.speak(wikipedia.page_summary(query=data['query'])) else: - # TODO: fallback to duckduckgo or wolfram alpha - pass + wolfram_response = wa_client.ask(data['utterance']) + if wolfram_response is not None: + self.speak(wolfram_response) def create_skill(data): diff --git a/jarvis/utils/fallbacks/__init__.py b/jarvis/utils/fallbacks/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/jarvis/utils/fallbacks/wolframalpha/wa_client.py b/jarvis/utils/fallbacks/wolframalpha/wa_client.py new file mode 100644 index 0000000..b2a810f --- /dev/null +++ b/jarvis/utils/fallbacks/wolframalpha/wa_client.py @@ -0,0 +1,22 @@ +import urllib.parse + +import requests + +from jarvis.utils import config_utils + + +def ask(sentence): + query = urllib.parse.quote_plus(sentence) + query_url = f"https://api.wolframalpha.com/v2/query?" \ + f"appid={config_utils.get_in_secret('WOLFRAM_APP_ID')}" \ + f"&input={query}" \ + f"&format=plaintext" \ + f"&output=json" + + r = requests.get(query_url).json() + + if r['queryresult']['success']: + data = r["queryresult"]["pods"][1]["subpods"][0] + return data['plaintext'] + else: + return None