Skip to content
This repository has been archived by the owner on Sep 8, 2024. It is now read-only.

Commit

Permalink
lets try black
Browse files Browse the repository at this point in the history
  • Loading branch information
ken-mycroft committed Aug 27, 2021
1 parent 00ab7a8 commit 31165ef
Showing 1 changed file with 48 additions and 32 deletions.
80 changes: 48 additions & 32 deletions mycroft/skills/common_query_skill.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@ class CQSMatchLevel(IntEnum):


# Copy of CQSMatchLevel to use if the skill returns visual media
CQSVisualMatchLevel = IntEnum('CQSVisualMatchLevel',
[e.name for e in CQSMatchLevel])
CQSVisualMatchLevel = IntEnum("CQSVisualMatchLevel", [e.name for e in CQSMatchLevel])


def is_CQSVisualMatchLevel(match_level):
return isinstance(match_level, type(CQSVisualMatchLevel.EXACT))


VISUAL_DEVICES = ['mycroft_mark_2']
VISUAL_DEVICES = ["mycroft_mark_2"]


def handles_visuals(platform):
Expand All @@ -52,13 +51,14 @@ class CommonQuerySkill(MycroftSkill, ABC):
This class works in conjunction with skill-query which collects
answers from several skills presenting the best one available.
"""

def __init__(self, name=None, bus=None):
super().__init__(name, bus)

data_dir = Configuration.get().get('data_dir')
data_dir = Configuration.get().get("data_dir")
save_root_dir = self.root_dir
self.root_dir = data_dir + '/mycroft/'
noise_words_filename = self.find_resource('noise_words' + '.list', 'res/text')
self.root_dir = data_dir + "/mycroft/"
noise_words_filename = self.find_resource("noise_words" + ".list", "res/text")
self.root_dir = save_root_dir
self.translated_noise_words = []
try:
Expand All @@ -70,10 +70,10 @@ def __init__(self, name=None, bus=None):

# these should probably be configurable
self.level_confidence = {
CQSMatchLevel.EXACT:0.9,
CQSMatchLevel.CATEGORY:0.6,
CQSMatchLevel.GENERAL:0.5
}
CQSMatchLevel.EXACT: 0.9,
CQSMatchLevel.CATEGORY: 0.6,
CQSMatchLevel.GENERAL: 0.5,
}

def bind(self, bus):
"""Overrides the default bind method of MycroftSkill.
Expand All @@ -83,17 +83,19 @@ def bind(self, bus):
"""
if bus:
super().bind(bus)
self.add_event('question:query', self.__handle_question_query)
self.add_event('question:action', self.__handle_query_action)
self.add_event("question:query", self.__handle_question_query)
self.add_event("question:action", self.__handle_query_action)

def __handle_question_query(self, message):
search_phrase = message.data["phrase"]

# First, notify the requestor that we are attempting to handle
# (this extends a timeout while this skill looks for a match)
self.bus.emit(message.response({"phrase": search_phrase,
"skill_id": self.skill_id,
"searching": True}))
self.bus.emit(
message.response(
{"phrase": search_phrase, "skill_id": self.skill_id, "searching": True}
)
)

# Now invoke the CQS handler to let the skill perform its search
result = self.CQS_match_query_phrase(search_phrase)
Expand All @@ -104,25 +106,37 @@ def __handle_question_query(self, message):
answer = result[2]
callback = result[3] if len(result) > 3 else None
confidence = self.__calc_confidence(match, search_phrase, level, answer)
self.bus.emit(message.response({"phrase": search_phrase,
"skill_id": self.skill_id,
"answer": answer,
"callback_data": callback,
"conf": confidence}))
self.bus.emit(
message.response(
{
"phrase": search_phrase,
"skill_id": self.skill_id,
"answer": answer,
"callback_data": callback,
"conf": confidence,
}
)
)
else:
# Signal we are done (can't handle it)
self.bus.emit(message.response({"phrase": search_phrase,
"skill_id": self.skill_id,
"searching": False}))
self.bus.emit(
message.response(
{
"phrase": search_phrase,
"skill_id": self.skill_id,
"searching": False,
}
)
)

def remove_noise(self, phrase):
# remove noise to produce essence
phrase = ' ' + phrase + ' '
phrase = " " + phrase + " "
for word in self.translated_noise_words:
mtch = ' ' + word + ' '
mtch = " " + word + " "
if phrase.find(mtch) > -1:
phrase = phrase.replace(word,"")
phrase = ' '.join(phrase.split())
phrase = phrase.replace(word, "")
phrase = " ".join(phrase.split())
return phrase.strip()

def __calc_confidence(self, match, phrase, level, answer):
Expand All @@ -133,7 +147,7 @@ def __calc_confidence(self, match, phrase, level, answer):
consumed_pct /= 10

# Add bonus if match has visuals and the device supports them.
platform = self.config_core.get('enclosure', {}).get('platform')
platform = self.config_core.get("enclosure", {}).get("platform")
if is_CQSVisualMatchLevel(level) and handles_visuals(platform):
bonus = 0.1
else:
Expand All @@ -148,15 +162,17 @@ def __calc_confidence(self, match, phrase, level, answer):
if answer.find(word) > -1:
matches += 1

answer_size = len( answer.split(" " ) )
answer_size = len(answer.split(" "))
relevance = 0.0
if answer_size > 0:
relevance = float( float(matches) / float(answer_size) )
relevance = float(float(matches) / float(answer_size))

# extra credit for more words
wc_mod = float( float(answer_size) / float(100) )
wc_mod = float(float(answer_size) / float(100))

confidence = self.level_confidence[level] + consumed_pct + bonus + relevance + wc_mod
confidence = (
self.level_confidence[level] + consumed_pct + bonus + relevance + wc_mod
)

return confidence

Expand Down

0 comments on commit 31165ef

Please sign in to comment.