Skip to main content
Join the official 2020 Python Developers SurveyStart the survey!

A set of utilities for training probabilistic context-free grammars and scoring new sentences with them.

Project description

A library for training and applying probabilistic context-free grammars to

* Kasami, T. (1965). An efficient recognition and syntax analysis algorithm
for context-free languages. (No. Scientific-2). Hawaii University, Dept. of
Electrical Engineering.

# Example use

>>> from bllipparser import RerankingParser
>>> from kasami import TreeScorer
>>> from kasami.normalizers import bllip
>>> # Loading WSJ-PTB3 treebank into bllip's RerankingParser
... bllip_rrp = RerankingParser.fetch_and_load('WSJ-PTB3')
>>> bllip_parse = lambda s: bllip.normalize_tree(bllip_rrp.parse(s)[0].ptb_parse)
>>> tree = bllip_parse("I am a little teapot")
>>> print(tree)
(S1 (S (NP (PRP 'I')) (VP (VBP 'am') (NP (DT 'a') (JJ 'little') (NN 'teapot')))))
>>> print(tree.format(depth=1))
(PRP 'I')
(VBP 'am')
(DT 'a')
(JJ 'little')
(NN 'teapot')
>>> for production in tree:
... print(str(production))
(S1 S)
(PRP 'I')
(VBP 'am')
(DT 'a')
(JJ 'little')
(NN 'teapot')
>>> sentences = ["I am a little teapot",
... "Here is my handle",
... "Here is my spout",
... "When I get all steamed up I just shout tip me over and pour me out",
... "I am a very special pot",
... "It is true",
... "Here is an example of what I can do",
... "I can turn my handle into a spout",
... "Tip me over and pour me out"]
>>> teapot_grammar = TreeScorer.from_tree_bank(bllip_parse(s) for s in sentences)
>>> teapot_grammar.score(bllip_parse("Here is a little teapot"))
>>> teapot_grammar.score(bllip_parse("It is my handle"))
>>> teapot_grammar.score(bllip_parse("I am a spout"))
>>> teapot_grammar.score(bllip_parse("Your teapot is gay"))
>>> teapot_grammar.score(bllip_parse("Your mom's teapot is asldasnldansldal"))


# Author
* Aaron Halfaker --

... and substantially informed by

Project details

Download files

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

Files for kasami, version 0.0.7
Filename, size File type Python version Upload date Hashes
Filename, size kasami-0.0.7-py3-none-any.whl (11.5 kB) File type Wheel Python version 3.5 Upload date Hashes View
Filename, size kasami-0.0.7.tar.gz (7.2 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page