Skip to content

Commit cf69b5c

Browse files
➕ Add lookup token to output object array.
1 parent 0eb90ef commit cf69b5c

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

plover_websocket_server/lookup.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Find the steno strokes for the given text
22
import re
33

4+
from plover import log
45
from plover.engine import StenoEngine
56

67

@@ -16,6 +17,7 @@ def lookup(engine: StenoEngine, text_to_lookup: str) -> list:
1617
Starts from the beginning of the string and then solving for the remainder.
1718
"""
1819
memo = {}
20+
log.debug(f"Starting lookup for: '{text_to_lookup}'")
1921

2022
def get_steno_for_phrase(phrase: str) -> list | None:
2123
"""Finds steno for a phrase.
@@ -24,12 +26,15 @@ def get_steno_for_phrase(phrase: str) -> list | None:
2426
to lowercase and prepending the capitalization stroke.
2527
"""
2628
# 1. Try the phrase as-is (respecting capitalization)
29+
log.debug(f" - get_steno_for_phrase('{phrase}')")
2730
steno_capitalized: set = engine.reverse_lookup(phrase)
2831

2932
# If the phrase is a single non-word character (like '!'),
3033
# also try looking it up as a Plover command (e.g., '{!}').
3134
if len(phrase) == 1 and not phrase.isalnum():
35+
# Handle characters that are special within Plover's command syntax
3236
command_phrase = f"{{{phrase}}}"
37+
log.debug(f" - Trying command lookup for '{command_phrase}'")
3338
steno_from_command = engine.reverse_lookup(command_phrase)
3439
if steno_from_command:
3540
steno_capitalized.update(steno_from_command)
@@ -55,17 +60,19 @@ def solve(words_tuple: tuple) -> list[list[tuple]]:
5560
return [[]] # Base case: one valid solution, which is empty.
5661
if words_tuple in memo:
5762
return memo[words_tuple]
63+
log.debug(f"--> solve({words_tuple})")
5864

5965
def get_steno_options(i):
6066
return get_steno_for_phrase(" ".join(words_tuple[:i]))
6167

6268
def process_i(i, best_steno_for_prefix):
6369
# Recursively find all solutions for the rest of the phrase
70+
prefix_phrase = " ".join(words_tuple[:i])
6471
suffix_tuple = words_tuple[i:]
6572
suffix_solutions = solve(suffix_tuple)
6673

6774
# Combine the prefix's steno with each suffix solution
68-
return [[best_steno_for_prefix] + suffix_solution for suffix_solution in suffix_solutions]
75+
return [[{"text": prefix_phrase, "steno": best_steno_for_prefix}] + suffix_solution for suffix_solution in suffix_solutions]
6976

7077
all_solutions = [
7178
solution
@@ -83,14 +90,21 @@ def process_i(i, best_steno_for_prefix):
8390

8491
all_possible_sequences = solve(tuple(words))
8592

93+
log.debug(f"All possible sequences: {all_possible_sequences}")
94+
8695
if not all_possible_sequences:
8796
return []
8897

8998
# Sort the collected sequences by overall efficiency
9099
# 1. Total number of strokes in the sequence
91100
# 2. Total number of keys pressed in the sequence
92101
sorted_sequences = sorted(
93-
all_possible_sequences, key=lambda seq: (sum(len(stroke) for stroke in seq), sum(sum(len(p) for p in stroke) for stroke in seq))
102+
all_possible_sequences,
103+
key=lambda seq: (
104+
sum(len(item["steno"]) for item in seq),
105+
sum(sum(len(p) for p in item["steno"]) for item in seq),
106+
),
94107
)
95108

109+
log.debug(f"Lookup finished. Returning {sorted_sequences}")
96110
return sorted_sequences

0 commit comments

Comments
 (0)