Skip to main content

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

Project description

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.

  • The | operator
    • Compute the union of two dictionaries together (basically updating one dictionary with another as like a normal python dictionary)
you = s({"KPWR": "you"})
they = s({"TWH": "they"})
dict1 = you | they
dict1.print_items()
# {"KPWR": "you", "TWH": "they"}
  • The * operator
    • Compute the Cartesian product of two dictionaries such that:
      • Adjacent strokes are merged as according to steno order
      • Adjacent translations are merged using the + operator
    • Example:
dict1 = s({
		"KPWR": "you",
		"TWH": "they"
	})
dict2 = s({
		"-R": " are"
	})
dict = dict1 * dict2
dict.print_items()
# {"KPWR-R": "you are", "TWH-R": "they are"}

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

plover_python_dictionary_lib-0.5.0.tar.gz (27.6 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file plover_python_dictionary_lib-0.5.0.tar.gz.

File metadata

File hashes

Hashes for plover_python_dictionary_lib-0.5.0.tar.gz
Algorithm Hash digest
SHA256 56bd3453000c71108f18b125d337f2f3e43be6c3a5c18f26066542505b2fc679
MD5 b2888ae72cdb71186b1c24cea788a8c2
BLAKE2b-256 512ffcb3122d7410d6da72024f2f63828d0df6e19f4036a6653ad2b2f07b353b

See more details on using hashes here.

File details

Details for the file plover_python_dictionary_lib-0.5.0-py3-none-any.whl.

File metadata

File hashes

Hashes for plover_python_dictionary_lib-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8bd2d0eb6128ed1657dd2f72f26b1f92b88879673e2b948544ec8ed6931589fb
MD5 8af835980a45a980c1dd1fc562e47c73
BLAKE2b-256 40967893af7ae4ed88ca22f957b73390af2e5dd3ffe67e702e819c809c029d2f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page