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

Commit

Permalink
Prepare for multi-lang support
Browse files Browse the repository at this point in the history
  • Loading branch information
forslund committed Mar 23, 2020
1 parent 90e7161 commit 75b5a6b
Showing 1 changed file with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@
SLEEP_LENGTH = 0.25


def find_dialog(skill_path, dialog):
def find_dialog(skill_path, dialog, lang):
"""Check the usual location for dialogs.
TODO: subfolders
"""
if exists(join(skill_path, 'dialog')):
return join(skill_path, 'dialog', 'en-us', dialog)
return join(skill_path, 'dialog', lang, dialog)
else:
return join(skill_path, 'locale', 'en-us', dialog)
return join(skill_path, 'locale', lang, dialog)


def load_dialog_file(dialog_path):
Expand All @@ -68,9 +68,17 @@ def load_dialog_list(skill_path, dialog):
return load_dialog_file(dialog_path), debug


def dialog_from_sentence(sentence, skill_path):
"""Find dialog file from example sentence."""
dialog_paths = join(skill_path, 'dialog', 'en-us', '*.dialog')
def dialog_from_sentence(sentence, skill_path, lang):
"""Find dialog file from example sentence.
Arguments:
sentence (str): Text to match
skill_path (str): path to skill directory
lang (str): language code to use
Returns (str): Dialog file best matching the sentence.
"""
dialog_paths = join(skill_path, 'dialog', lang, '*.dialog')
best = (None, 0)
for path in glob(dialog_paths):
patterns = load_dialog_file(path)
Expand Down Expand Up @@ -118,7 +126,7 @@ def given_english(context):
def when_user_says(context, text):
context.bus.emit(Message('recognizer_loop:utterance',
data={'utterances': [text],
'lang': 'en-us',
'lang': context.lang,
'session': '',
'ident': time.time()},
context={'client_name': 'mycroft_listener'}))
Expand All @@ -141,7 +149,7 @@ def check_dialog(message):
@then('"{skill}" should reply with "{example}"')
def then_example(context, skill, example):
skill_path = context.msm.find_skill(skill).path
dialog = dialog_from_sentence(example, skill_path)
dialog = dialog_from_sentence(example, skill_path, context.lang)
print('Matching with the dialog file: {}'.format(dialog))
assert dialog is not None, 'No matching dialog...'
then_dialog(context, skill, dialog)
Expand Down Expand Up @@ -194,7 +202,7 @@ def then_user_follow_up(context, text):
wait_while_speaking()
context.bus.emit(Message('recognizer_loop:utterance',
data={'utterances': [text],
'lang': 'en-us',
'lang': context.lang,
'session': '',
'ident': time.time()},
context={'client_name': 'mycroft_listener'}))
Expand Down

0 comments on commit 75b5a6b

Please sign in to comment.