Skip to content

Latest commit

 

History

History
77 lines (56 loc) · 2.25 KB

File metadata and controls

77 lines (56 loc) · 2.25 KB

Python Minimalist Grammar Parser

Documentation

This repository contains Python bindings for a Minimalist Grammar parser written in Rust. 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.

Installation

The package is made with Maturin and can be build however you'd like using that system. The easiest way to build it or develop with it is by using uv.

git clone https://github.com/MichaelGoodale/python-mg
cd python-mg
uv run example.py

Otherwise, you can also install it with pip or other tools as a wheel by getting it from the GitHub actions page

You can also add it to a uv project like so:

uv add git+https://github.com/MichaelGoodale/python-mg

Usage

The following snippet declares a grammar, parses a sentence and generates a string in the grammar.

from python_mg import Lexicon

grammar = """
::V= C
::V= +W C
knows::C= =D V
says::C= =D V
prefers::D= =D V
drinks::D= =D V
king::N
wine::N
beer::N
queen::N
the::N= D
which::N= D -W
"""
lexicon = Lexicon(grammar)

for p in lexicon.parse("which beer the queen drinks", "C"):
    tree = p.to_tree()
    tree.to_image().show()

for p in lexicon.generate_grammar("C", max_strings=1):
    print(p)
    print(p.latex())
    print(p.to_tree().to_dot())
    p.to_tree().to_image().show()

The parse tree can also be turned into LaTeX code with Forest (see latex-commands.tex) or can be directly turned into a GraphViz Dot file.

Examples

There are some examples of how to apply the code in the examples directory

These include:

Some of the scripts require extra dependencies. You can test them with the following command in uv

uv run --group examples examples/train_llm_from_mg.py