Skip to content

MichaelGoodale/python-mg

Repository files navigation

Python Minimalist Grammar Parser

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.

About

Python bindings to parse and generate strings using a Minimalist Grammar (MG)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published