|
| 1 | +.. Python Minimalist Grammars documentation master file, created by |
| 2 | + sphinx-quickstart on Wed Oct 15 11:30:51 2025. |
| 3 | + You can adapt this file completely to your liking, but it should at least |
| 4 | + contain the root `toctree` directive. |
| 5 | +
|
| 6 | +Python Minimalist Grammars documentation |
| 7 | +======================================== |
| 8 | + |
| 9 | +This documents the `python-mg <github.com/MichaelGoodale/python-mg>`_ package which contains Python bindings for `a Minimalist Grammar parser written in Rust <https://github.com/MichaelGoodale/minimalist-grammar-parser>`_. |
| 10 | +It provides the tools necessary to generate strings from a Minimalist Grammar and parse sentences in a Minimalist Grammar, as well as inspecting their parse tree. |
| 11 | + |
| 12 | +.. toctree:: |
| 13 | + :maxdepth: 2 |
| 14 | + :caption: Contents: |
| 15 | + |
| 16 | + lexicon |
| 17 | + graphing |
| 18 | + metrics |
| 19 | + |
| 20 | + |
| 21 | +Installation |
| 22 | +------------ |
| 23 | + |
| 24 | +The package is made with `Maturin <https://github.com/PyO3/maturin>`_ and can be build however you'd like using that system. |
| 25 | +The easiest way to build it or develop with it is by using `uv <https://github.com/astral-sh/uv>`_. |
| 26 | + |
| 27 | +.. code-block:: bash |
| 28 | +
|
| 29 | + git clone https://github.com/MichaelGoodale/python-mg |
| 30 | + cd python-mg |
| 31 | + uv run example.py |
| 32 | +
|
| 33 | +Otherwise, you can also install it with `pip` or other tools as a wheel by getting it from `the GitHub actions page <https://github.com/MichaelGoodale/python-mg/actions>`_ |
| 34 | + |
| 35 | +You can also add it to a uv project like so: |
| 36 | + |
| 37 | +.. code-block:: bash |
| 38 | +
|
| 39 | + uv add git+https://github.com/MichaelGoodale/python-mg |
| 40 | +
|
| 41 | +
|
| 42 | +
|
| 43 | +Usage |
| 44 | +----- |
| 45 | + |
| 46 | +The following snippet declares a grammar, parses a sentence and generates a string in the grammar. |
| 47 | + |
| 48 | +.. code-block:: python |
| 49 | +
|
| 50 | + from python_mg import Lexicon |
| 51 | +
|
| 52 | + grammar = """ |
| 53 | + ::V= C |
| 54 | + ::V= +W C |
| 55 | + knows::C= =D V |
| 56 | + says::C= =D V |
| 57 | + prefers::D= =D V |
| 58 | + drinks::D= =D V |
| 59 | + king::N |
| 60 | + wine::N |
| 61 | + beer::N |
| 62 | + queen::N |
| 63 | + the::N= D |
| 64 | + which::N= D -W |
| 65 | + """ |
| 66 | + lexicon = Lexicon(grammar) |
| 67 | +
|
| 68 | + for p in lexicon.parse("which beer the queen drinks", "C"): |
| 69 | + tree = p.to_tree() |
| 70 | + tree.to_image().show() |
| 71 | +
|
| 72 | + for p in lexicon.generate_grammar("C", max_strings=1): |
| 73 | + print(p) |
| 74 | + print(p.latex()) |
| 75 | + print(p.to_tree().to_dot()) |
| 76 | + p.to_tree().to_image().show() |
| 77 | +
|
| 78 | +The parse tree can also be turned into LaTeX code with Forest (see `latex-commands.tex <https://github.com/MichaelGoodale/python-mg/blob/master/latex-commands.tex>`_) or can be directly turned into a GraphViz Dot file. |
0 commit comments