Skip to content

Instantly share code, notes, and snippets.

@mawillcockson
Last active July 13, 2021 07:42
Show Gist options
  • Select an option

  • Save mawillcockson/d0115697796b1276f49686f53eb01ea7 to your computer and use it in GitHub Desktop.

Select an option

Save mawillcockson/d0115697796b1276f49686f53eb01ea7 to your computer and use it in GitHub Desktop.
Searching Plover's dictionaries
from pathlib import Path
from orjson import loads
import re
TRIM = 50
plover_dir = Path("~/.config/plover/").expanduser()
dictionary = {}
for path in plover_dir.glob("*.json"):
if not path.is_file():
continue
dictionary.update(
loads(
path.read_text()
)
)
biggest_mismatch = sorted(
dictionary.items(),
key=lambda pair: abs(len(pair[0]) - len(pair[1])),
reverse=True,
)
max_len = max(map(lambda pair: len(pair[0]), biggest_mismatch[:TRIM + 1]))
print("\n".join(f"{pair[0]: <{max_len}} -> {pair[1]}" for pair in biggest_mismatch[:TRIM + 1]))
from pathlib import Path
from orjson import loads
import re
TRIM = 50
left_hand_only = re.compile(r"(?i)^[STKPWHRAO*/1-50]+$")
right_hand_only = re.compile(r"(?i)^[*EUFRPBLGTSDZ/6-9-]+$")
single_stroke = re.compile(r"(?i)^[STKPWHRAO*EUFRPBLGTSDZ0-9-]+$")
pattern = single_stroke
dictionary = loads(
Path("~/.config/plover/main.json").expanduser().read_text()
)
longest_matching = sorted(
filter(lambda pair: pattern.match(pair[0]), dictionary.items()),
key=lambda pair: len(pair[0]),
reverse=True,
)
max_len = max(map(lambda pair: len(pair[0]), longest_matching[:TRIM + 1]))
print("\n".join(f"{pair[0]: <{max_len}} -> {pair[1]}" for pair in longest_matching[:TRIM + 1]))
@mawillcockson
Copy link
Author

The regular expressions don't quite do what they're supposed to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment