Skip to content

Commit

Permalink
update_opm (#50)
Browse files Browse the repository at this point in the history
solver class kwargs changed
  • Loading branch information
JarbasAl authored Aug 4, 2024
1 parent bebcea7 commit 34999ce
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
37 changes: 23 additions & 14 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import os.path
import re
from os.path import dirname, join
from typing import Optional

import requests
from ovos_bus_client.session import SessionManager, Session
Expand Down Expand Up @@ -63,22 +64,23 @@ def extract_keyword(cls, utterance: str, lang: str):
return kw

# abstract Solver methods to implement
def get_data(self, query, context=None):
def get_data(self, query: str,
lang: Optional[str] = None,
units: Optional[str] = None):
"""
query assured to be in self.default_lang
return a dict response
"""
LOG.debug(f"WikiSolver query: {query}")
context = context or {}
lang = context.get("lang") or self.default_lang
lang = lang or self.default_lang
lang = lang.split("-")[0]
url = f"https://{lang}.wikipedia.org/w/api.php?action=query&list=search&srsearch={query}&format=json"
res = requests.get(url).json()["query"]["search"]
if not res:
q2 = self.extract_keyword(query, lang)
if q2 and q2 != query:
LOG.debug(f"WikiSolver Fallback, new query: {q2}")
return self.get_data(q2, context)
return self.get_data(q2, lang=lang, units=units)

for r in res:
title = r["title"]
Expand All @@ -100,15 +102,21 @@ def get_data(self, query, context=None):
return {"title": title, "short_answer": ans[0], "summary": summary, "img": img}
return {}

def get_spoken_answer(self, query, context=None):
data = self.get_data(query, context)
def get_spoken_answer(self, query: str,
lang: Optional[str] = None,
units: Optional[str] = None):
data = self.get_data(query, lang=lang, units=units)
return data.get("short_answer", "")

def get_image(self, query, context=None):
data = self.get_data(query, context)
def get_image(self, query: str,
lang: Optional[str] = None,
units: Optional[str] = None):
data = self.get_data(query, lang=lang, units=units)
return data.get("img", "")

def get_expanded_answer(self, query, context=None):
def get_expanded_answer(self, query: str,
lang: Optional[str] = None,
units: Optional[str] = None):
"""
return a list of ordered steps to expand the answer, eg, "tell me more"
{
Expand All @@ -117,7 +125,7 @@ def get_expanded_answer(self, query, context=None):
"img": "optional/path/or/url
}
"""
data = self.get_data(query, context)
data = self.get_data(query, lang=lang, units=units)
ans = flatten_list([sentence_tokenize(s) for s in data["summary"].split("\n")])
steps = [{
"title": data.get("title", query).title(),
Expand Down Expand Up @@ -150,7 +158,7 @@ def register_kw_xtract(self):
samples += expand_parentheses(l)
else:
samples.append(l)
self.wiki.register_kw_extractors(samples, lang)
self.wiki.register_kw_extractors(samples, lang=lang)

@classproperty
def runtime_requirements(self):
Expand Down Expand Up @@ -215,7 +223,7 @@ def handle_tell_more(self, message):
# common query
def CQS_match_query_phrase(self, phrase):
sess = SessionManager.get()
query = self.wiki.extract_keyword(phrase, sess.lang)
query = self.wiki.extract_keyword(phrase, lang=sess.lang)
if not query:
# doesnt look like a question we can answer at all
return None
Expand Down Expand Up @@ -259,7 +267,7 @@ def ask_the_wiki(self, sess: Session):
query = self.session_results[sess.session_id]["query"]

try:
results = self.wiki.long_answer(query, context={"lang": sess.lang})
results = self.wiki.long_answer(query, lang=sess.lang, units=sess.system_unit)
except Exception as err: # handle solver plugin failures, happens in some queries
self.log.error(err)
results = None
Expand All @@ -279,7 +287,8 @@ def display_wiki_entry(self):
return
sess = SessionManager.get()
image = self.session_results[sess.session_id].get("image") or \
self.wiki.get_image(self.session_results[sess.session_id]["query"])
self.wiki.get_image(self.session_results[sess.session_id]["query"],
lang=sess.lang, units=sess.system_unit)
title = self.session_results[sess.session_id].get("title") or "Wikipedia"
if image:
self.session_results[sess.session_id]["image"] = image
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
ovos-utils~=0.0, >=0.0.38
ovos_workshop~=0.0, >=0.0.15
ovos_workshop~=0.0, >=0.0.15
ovos-plugin-manager>=0.0.26a38

0 comments on commit 34999ce

Please sign in to comment.