Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Creates rules.py containing logic for the small rule snippets for each entry in the json
- Loading branch information
Showing
3 changed files
with
174 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Copyright 2019 Mycroft AI Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
"""Color definitions for test output.""" | ||
import os | ||
|
||
|
||
class Clr: | ||
PINK = '\033[95m' | ||
BLUE = '\033[94m' | ||
CYAN = '\033[96m' | ||
GREEN = '\033[92m' | ||
YELLOW = '\033[93m' | ||
RED = '\033[91m' | ||
DKGRAY = '\033[90m' | ||
# Classes | ||
USER_UTT = '\033[96m' # cyan | ||
MYCROFT = '\033[33m' # bright yellow | ||
HEADER = '\033[94m' # blue | ||
WARNING = '\033[93m' # yellow | ||
FAIL = '\033[91m' # red | ||
RESET = '\033[0m' | ||
|
||
|
||
class NoClr: | ||
PINK = '' | ||
BLUE = '' | ||
CYAN = '' | ||
GREEN = '' | ||
YELLOW = '' | ||
RED = '' | ||
DKGRAY = '' | ||
USER_UTT = '' | ||
MYCROFT = '' | ||
HEADER = '' | ||
WARNING = '' | ||
FAIL = '' | ||
RESET = '' | ||
|
||
|
||
# MST as in Mycroft Skill Tester | ||
if 'MST_NO_COLOR' not in os.environ: | ||
color = Clr | ||
else: | ||
color = NoClr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# Copyright 2019 Mycroft AI Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
"""A collection of rules for the skill tester.""" | ||
import re | ||
|
||
from mycroft.util.format import expand_options | ||
|
||
from .colors import color | ||
|
||
|
||
def intent_type_check(intent_type): | ||
return (['or'] + | ||
[['endsWith', 'intent_type', intent_type]] + | ||
[['endsWith', '__type__', intent_type]]) | ||
|
||
|
||
def play_query_check(skill, match, phrase): | ||
d = ['and'] | ||
d.append(['equal', '__type__', 'query']) | ||
d.append(['equal', 'skill_id', skill.skill_id]) | ||
d.append(['equal', 'phrase', phrase]) | ||
d.append(['gt', 'conf', match.get('confidence_threshold', 0.5)]) | ||
return d | ||
|
||
|
||
def question_check(skill, question, expected_answer): | ||
d = ['and'] | ||
d.append(['equal', '__type__', 'query.response']) | ||
d.append(['equal', 'skill_id', skill.skill_id]) | ||
d.append(['equal', 'phrase', question]) | ||
d.append(['match', 'answer', expected_answer]) | ||
return d | ||
|
||
|
||
def expected_data_check(expected_items): | ||
d = ['and'] | ||
for item in expected_items: | ||
d.append(['equal', item[0], item[1]]) | ||
return d | ||
|
||
|
||
def load_dialog_list(skill, dialog): | ||
""" Load dialog from files into a single list. | ||
Args: | ||
skill (MycroftSkill): skill to load dialog from | ||
dialog (list): Dialog names (str) to load | ||
Returns: | ||
list: Expanded dialog strings | ||
""" | ||
dialogs = [] | ||
try: | ||
for d in dialog: | ||
for e in skill.dialog_renderer.templates[d]: | ||
dialogs += expand_options(e) | ||
except Exception as template_load_exception: | ||
print(color.FAIL + | ||
"Failed to load dialog template " + | ||
"'dialog/en-us/" + d + ".dialog'" + | ||
color.RESET) | ||
raise Exception("Can't load 'excepted_dialog': " | ||
"file '" + d + ".dialog'") \ | ||
from template_load_exception | ||
return dialogs | ||
|
||
|
||
def expected_dialog_check(expected_dialog, skill): | ||
# Check that expected dialog file is used | ||
if isinstance(expected_dialog, str): | ||
dialog = [expected_dialog] # Make list | ||
else: | ||
dialog = expected_dialog | ||
# Extract dialog texts from skill | ||
dialogs = load_dialog_list(skill, dialog) | ||
# Allow custom fields to be anything | ||
d = [re.sub(r'{.*?\}', r'.*', t) for t in dialogs] | ||
# Merge consequtive .*'s into a single .* | ||
d = [re.sub(r'\.\*( \.\*)+', r'.*', t) for t in d] | ||
|
||
# Create rule allowing any of the sentences for that dialog | ||
return [['match', 'utterance', r] for r in d] | ||
|
||
|
||
def changed_context_check(ctx): | ||
if not isinstance(ctx, list): | ||
ctx = [ctx] | ||
return [['endsWith', 'context', str(c)] for c in ctx] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters