user202729 / plover-python-dictionary-lib

Library for writing Python dictionary for Plover, and generating JSON dictionary file from Python dictionary.
GNU General Public License v3.0
2 stars 1 forks source link
plover plover-plugins

plover-python-dictionary-lib

PyPI

Library for writing Python dictionary for Plover, and generating JSON dictionary file from Python dictionary.

A Python dictionary is a Plover dictionary that is written in Python. Refer to documentation of the plover-python-dictionary package to see what is the advantage of a Python dictionary.

This library provides some convenient helper tools to write a dictionary.

Installation

This package is available on PyPI. To install it, run the command

pip install plover-python-dictionary-lib

This is required to use/run the Python dictionaries that use this library.

Example & Usage

Getting started

This is a minimal example of a Python dictionary. You can save it as helloworld.py and load it into Plover, provided plover-python-dictionary package is installed.

#!/bin/python3
from plover.system import english_stenotype as e
from plover_python_dictionary_lib import get_context_from_system
context=get_context_from_system(e)
s=context.SingleDictionary
stroke=context.stroke
translation=context.translation

dictionary=s({
    "S": "hello world"
    })

lookup = lambda strokes: dictionary.lookup_tuple(strokes)
LONGEST_KEY = dictionary.longest_key

if __name__=="__main__":
    dictionary.print_items()

When loaded into Plover, it will define a dictionary with a single translation, as suggested by the dictionary variable.

It can also be run as a standalone Python script to print out the JSON dictionary it would corresponds to. Refer to "Generate JSON" section for details.

Dictionary Operations

The power of the package comes from the variety of built-in functions that allows manipulating the components easily to build up a whole dictionary.

When you have built up the desired dictionary, simply assign it to the dictionary variable, and set lookup and LONGEST_KEY correspondingly.

You can experiment with the operators simply by running the necessary imports in a Python shell; alternatively, just run the Python file standalone to print out the content of the dictionary.

map() method

Allows you to modify the content of an existing dictionary.

>>> dict1 = s({"S": "is", "K": "can"})
>>> dict1.map(lambda x: x*2)
MappedDictionary({(S,): 'isis', (K,): 'cancan'})

You can also map over the keys provided the arguments are specially named as strokes and result:

>>> dict1.map(lambda strokes, result: f"{result} ({strokes})")
MappedDictionary({(S,): 'is ((S,))', (K,): 'can ((K,))'})

You can also customize the argument names:

def applyMods(mods, characters):
    for mod in mods:
        characters = f"{mod}({characters})"
    return characters
mods = s({"-R": ["shift"], "": []}).named("mods") 
characters = s({"A": "a"}).named("characters")
dict = (mods * characters).map(applyMods)
dict.print_items()
# {"AR": "shift(a)", "A": "a"}

In this case, named("characters") marks that the translation of the characters dictionary is to be passed to the argument named characters in applyMods.

Extra

Generate JSON

The Python dictionary must be written with this plugin.

Call .print_items() on the main Dictionary object. (see also the example dictionaries above)

For example: if this code is included at the end of the Python dictionary file named dictionary.py

if __name__=="__main__":
    dictionary.print_items()

(assuming that the main dictionary object is named dictionary) then running python dictionary.py will print the dictionary as JSON to the standard output.

Note: If you get the error:

ModuleNotFoundError: No module named 'plover'

it means Plover was installed in a different Python environment from the environment that you ran the script in.

It depends on the operating-system and specific installation method how to run it in the correct environment. See https://github.com/user202729/plover-python-dictionary-lib/issues/4 for an example.

Note (fixed bug, affects old version only): because of an incompatibility between Plover and the plover_stroke library, sometimes the JSON dictionary may not work in Plover.