Earley Bird is a small program that, given a well-formatted context-free grammar, and an input sentence that has already been passed through a part-of-speech tagger program, can tell you whether or not the sentence is valid in this grammar, and if so, output all possible parse trees in a nice format.
This program was written as a final project for a TAU seminar "Advanced Computational Linguistics" [0627.2223] taken at spring 2011.
It is based on the algorithm of the Earley Parser as described on Speech and Language Processing (Jurafsky, Martin).
The input forms has been chosen and designed so the program would integrate with the Apertium open-source machine translation framework.
To run it, just call:
python earley.py <grammar.cfg> <sentence> [--debug]
See 'Input' for instructions on input formats.
Use --debug
to get all kinds of secret messages during the process.
To use Earley Bird, one needs two things.
Context-free grammar: A set of production rules in the form of V -> w
, where V
is a single nonterminal symbol, and w
is a list of strings of terminals and/or nonterminals.
At least one rule with left-hand side S
(for sentence) must exist in order for sentences to be valid.
An item in w
can be empty, to represent epsilon productions.
For info, see CFG description in Wikipedia.
Earley bird reads a grammar from a text file where every line comprises of such rule. Comments are supported, prefixed by #
.
Example:
S -> NP VP | VP
NP -> D N | N | N NP
VP -> V | V NP | V PP | V NP PP
PP -> P NP
For other examples of CFG, see sample.cfg
in the project directory.
Sentence: Earley Bird expects an input of a sentence string with words already parsed for possible parts of speech.
The input should be formatted using the Apertium stream format, with each word followed by groups of a lemma and a list of tags, e.g.
time/time<N> flies/fly<N>/fly<V> like/like<V>/like<P> an/a<D> arrow/arrow<N>
The first line in the standard output would be the result of the validity check. i.e. either ==> Sentence is valid.
, or ==> Sentence is invalid.
If the sentence was found invalid using the provided grammar, tough beans, that's it.
If it's valid, you'll also get a list of valid parse trees.
==> Sentence is valid.
Valid parse trees: