Package: plover-python-dictionary-lib
plover-python-dictionary-libLibrary 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 todocumentation of theplover-python-dictionarypackageto see what is the advantage of a Python dictionary.This library provides some convenient helper tools to write a dictionary.InstallationThis package is available onPyPI.
To install it, run the commandpipinstallplover-python-dictionary-libThis is required to use/run the Python dictionaries that use this library.Example & UsageGetting startedThis is a minimal example of a Python dictionary. You can save it ashelloworld.pyand load it into Plover, providedplover-python-dictionarypackage is installed.#!/bin/python3fromplover.systemimportenglish_stenotypeasefromplover_python_dictionary_libimportget_context_from_systemcontext=get_context_from_system(e)s=context.SingleDictionarystroke=context.stroketranslation=context.translationdictionary=s({"S":"hello world"})lookup=lambdastrokes:dictionary.lookup_tuple(strokes)LONGEST_KEY=dictionary.longest_keyif__name__=="__main__":dictionary.print_items()When loaded into Plover, it will define a dictionary with a single translation, as suggested by thedictionaryvariable.It can also be run as a standalone Python script to print out the JSON dictionary it would corresponds to.
Refer to"Generate JSON" sectionfor details.Dictionary OperationsThe 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 thedictionaryvariable, and setlookupandLONGEST_KEYcorrespondingly.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|operatorCompute 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|theydict1.print_items()# {"KPWR": "you", "TWH": "they"}The*operatorCompute the Cartesian product of two dictionaries such that:Adjacent strokes are merged as according to steno orderAdjacent translations are merged using the+operatorExample:dict1=s({"KPWR":"you","TWH":"they"})dict2=s({"-R":" are"})dict=dict1*dict2dict.print_items()# {"KPWR-R": "you are", "TWH-R": "they are"}map()methodAllows you to modify the content of an existing dictionary.>>>dict1=s({"S":"is","K":"can"})>>>dict1.map(lambdax:x*2)MappedDictionary({(S,):'isis',(K,):'cancan'})You can also map over the keys provided the arguments are specially named asstrokesandresult:>>>dict1.map(lambdastrokes,result:f"{result}({strokes})")MappedDictionary({(S,):'is ((S,))',(K,):'can ((K,))'})You can also customize the argument names:defapplyMods(mods,characters):formodinmods:characters=f"{mod}({characters})"returncharactersmods=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 thecharactersdictionary is
to be passed to the argument namedcharactersinapplyMods.ExtraYou can read00_two_letter_fingerspelling_exampleexample dictionary file(GitHub link) for an example (this one is the most well-documented example file, with common patterns and explanation),the rest of the files in theexample/folder,and the documentation (as Python docstrings) in the source code,Useful resources:Frequently used dictionary components(feel free to edit the wiki)Generate JSONThe Python dictionary must be written with this plugin.Call.print_items()on the mainDictionaryobject. (see also the example dictionaries above)For example: if this code
is included at the end of the Python dictionary file nameddictionary.pyif__name__=="__main__":dictionary.print_items()(assuming that the main dictionary object is nameddictionary) then runningpython dictionary.pywill 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 Pythonenvironmentfrom 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. Seehttps://github.com/user202729/plover-python-dictionary-lib/issues/4for an example.Note(fixed bug, affects old version only): because ofan incompatibility between Plover and theplover_strokelibrary,
sometimes the JSON dictionary may not work in Plover.