Added wolfram-alpha fallback

This commit is contained in:
Mathieu 2021-09-14 10:49:09 +02:00
parent 3c95978fc9
commit 6284b7c2ab
4 changed files with 38 additions and 6 deletions

View File

@ -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,6 +105,14 @@ 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:
# 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,

View File

@ -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):

View File

View File

@ -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