Skip to content

Commit

Permalink
various fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lunixbochs committed May 23, 2023
1 parent 3f998e0 commit 2b3f774
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 30 deletions.
2 changes: 1 addition & 1 deletion core/file_extension/file_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@

@track_csv_list("file_extensions.csv", headers=("File extension", "Name"), default=_file_extensions_defaults)
def on_update(values):
ctx.lists["self.file_extension"] = file_extensions
ctx.lists["self.file_extension"] = values
55 changes: 31 additions & 24 deletions core/vocabulary/vocabulary.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,7 @@
# This is the opposite ordering to words_to_replace.csv (the latter has the target word first)
}
_word_map_defaults.update({word.lower(): word for word in _capitalize_defaults})


# phrases_to_replace is a spoken form -> written form map, used by our
# implementation of `dictate.replace_words` (at bottom of file) to rewrite words
# and phrases Talon recognized. This does not change the priority with which
# Talon recognizes particular phrases over others.
@track_csv_list("words_to_replace.csv", headers=("Replacement", "Original"), default=_word_map_defaults)
def on_word_map(values):
# "dictate.word_map" is used by Talon's built-in default implementation of
# `dictate.replace_words`, but supports only single-word replacements.
# Multi-word phrases are ignored.
ctx.settings["dictate.word_map"] = values


# "user.vocabulary" is used to explicitly add words/phrases that Talon doesn't
# recognize. Words in user.vocabulary (or other lists and captures) are
# "command-like" and their recognition is prioritized over ordinary words.
@track_csv_list("additional_words.csv", headers=("Word(s)", "Spoken Form (If Different)"))
def on_vocab(values):
ctx.lists["user.vocabulary"] = values

phrases_to_replace = {}

class PhraseReplacer:
"""Utility for replacing phrases by other phrases inside text or word lists.
Expand All @@ -74,7 +54,10 @@ class PhraseReplacer:
- phrase_dict: dictionary mapping recognized/spoken forms to written forms
"""

def __init__(self, phrase_dict: dict[str, str]):
def __init__(self):
self.phrase_index = {}

def update(self, phrase_dict: dict[str, str]):
# Index phrases by first word, then number of subsequent words n_next
phrase_index = dict()
for spoken_form, written_form in phrase_dict.items():
Expand Down Expand Up @@ -124,7 +107,8 @@ def replace_string(self, text: str) -> str:


# Unit tests for PhraseReplacer
rep = PhraseReplacer(
rep = PhraseReplacer()
rep.update(
{
"this": "foo",
"that": "bar",
Expand All @@ -140,7 +124,30 @@ def replace_string(self, text: str) -> str:
assert rep.replace_string("try this is too") == "try stopping early too"
assert rep.replace_string("this is a tricky one") == "stopping early a tricky one"

phrase_replacer = PhraseReplacer(phrases_to_replace)
phrase_replacer = PhraseReplacer()

# phrases_to_replace is a spoken form -> written form map, used by our
# implementation of `dictate.replace_words` (at bottom of file) to rewrite words
# and phrases Talon recognized. This does not change the priority with which
# Talon recognizes particular phrases over others.
@track_csv_list("words_to_replace.csv", headers=("Replacement", "Original"), default=_word_map_defaults)
def on_word_map(values):
global phrases_to_replace
phrases_to_replace = values
phrase_replacer.update(values)

# "dictate.word_map" is used by Talon's built-in default implementation of
# `dictate.replace_words`, but supports only single-word replacements.
# Multi-word phrases are ignored.
ctx.settings["dictate.word_map"] = values


# "user.vocabulary" is used to explicitly add words/phrases that Talon doesn't
# recognize. Words in user.vocabulary (or other lists and captures) are
# "command-like" and their recognition is prioritized over ordinary words.
@track_csv_list("additional_words.csv", headers=("Word(s)", "Spoken Form (If Different)"))
def on_vocab(values):
ctx.lists["user.vocabulary"] = values


@ctx.action_class("dictate")
Expand Down
7 changes: 2 additions & 5 deletions lang/talon/talon.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,8 @@ def on_update_decls(decls):
"modes",
]:
l = getattr(decls, thing)
ctx_talon_lists.lists[
f"user.talon_{thing}"
] = actions.user.create_spoken_forms_from_list(
l.keys(), generate_subsequences=False
)
values = actions.user.create_spoken_forms_from_list(l.keys(), generate_subsequences=False)
ctx_talon_lists.lists[f"user.talon_{thing}"] = values
# print(
# "List: {} \n {}".format(thing, str(ctx_talon_lists.lists[f"user.talon_{thing}"]))
# )
Expand Down

0 comments on commit 2b3f774

Please sign in to comment.