|
| 1 | +import re |
| 2 | +import typing |
| 3 | +from collections import defaultdict |
| 4 | +from typing import Iterator, Mapping |
| 5 | +from uu import Error |
| 6 | + |
| 7 | +from talon import app, registry |
| 8 | + |
| 9 | +from .spoken_forms_output import SpokenFormOutputEntry |
| 10 | + |
| 11 | +grapheme_capture_name = "user.any_alphanumeric_key" |
| 12 | + |
| 13 | + |
| 14 | +def get_grapheme_spoken_form_entries() -> list[SpokenFormOutputEntry]: |
| 15 | + return [ |
| 16 | + { |
| 17 | + "type": "grapheme", |
| 18 | + "id": id, |
| 19 | + "spokenForms": spoken_forms, |
| 20 | + } |
| 21 | + for symbol_list in generate_lists_from_capture(grapheme_capture_name) |
| 22 | + for id, spoken_forms in get_id_to_spoken_form_map(symbol_list).items() |
| 23 | + ] |
| 24 | + |
| 25 | + |
| 26 | +def generate_lists_from_capture(capture_name) -> Iterator[str]: |
| 27 | + """ |
| 28 | + Given the name of a capture, yield the names of each list that the capture |
| 29 | + expands to. Note that we are somewhat strict about the format of the |
| 30 | + capture rule, and will not handle all possible cases. |
| 31 | + """ |
| 32 | + if capture_name.startswith("self."): |
| 33 | + capture_name = "user." + capture_name[5:] |
| 34 | + try: |
| 35 | + rule = registry.captures[capture_name][0].rule.rule |
| 36 | + except Error: |
| 37 | + app.notify("Error constructing spoken forms for graphemes") |
| 38 | + print(f"Error getting rule for capture {capture_name}") |
| 39 | + return |
| 40 | + rule = rule.strip() |
| 41 | + if rule.startswith("(") and rule.endswith(")"): |
| 42 | + rule = rule[1:-1] |
| 43 | + rule = rule.strip() |
| 44 | + components = re.split(r"\s*\|\s*", rule) |
| 45 | + for component in components: |
| 46 | + if component.startswith("<") and component.endswith(">"): |
| 47 | + yield from generate_lists_from_capture(component[1:-1]) |
| 48 | + elif component.startswith("{") and component.endswith("}"): |
| 49 | + component = component[1:-1] |
| 50 | + if component.startswith("self."): |
| 51 | + component = "user." + component[5:] |
| 52 | + yield component |
| 53 | + else: |
| 54 | + app.notify("Error constructing spoken forms for graphemes") |
| 55 | + print( |
| 56 | + f"Unexpected component {component} while processing rule {rule} for capture {capture_name}" |
| 57 | + ) |
| 58 | + |
| 59 | + |
| 60 | +def get_id_to_spoken_form_map(list_name: str) -> Mapping[str, list[str]]: |
| 61 | + """ |
| 62 | + Given the name of a Talon list, return a mapping from the values in that |
| 63 | + list to the list of spoken forms that map to the given value. |
| 64 | + """ |
| 65 | + try: |
| 66 | + raw_list = typing.cast(dict[str, str], registry.lists[list_name][0]).copy() |
| 67 | + except Error: |
| 68 | + app.notify(f"Error getting list {list_name}") |
| 69 | + return {} |
| 70 | + |
| 71 | + inverted_list: defaultdict[str, list[str]] = defaultdict(list) |
| 72 | + for key, value in raw_list.items(): |
| 73 | + inverted_list[value].append(key) |
| 74 | + |
| 75 | + return inverted_list |
0 commit comments