An implementation of the a Context Free Grammar (CFG) using the Earley algorithm in Node.JS
Earley 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 project is a direct port of Earley Bird, origionally written in Python by Sagie Maoz.
npm install earley-node
To use Earley Bird, one needs two things.
Context-free grammar: A set of production rules in the form of
V -> w, where
Vis a single nonterminal symbol, and
wis 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
wcan 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
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.cfgin 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:
Parse tree #1:
[.GAMMA [.S [.NP [.N [.time ] ] ] [.VP [.V [.flies ] ] [.PP [.P [.like ] ] [.NP [.D [.an ] ] [.N [.arrow ] ] ] ] ] ] ]
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
- Sentence input should be inside
^...$to comply with the Apertium stream format.
- There is a bug in creating the trees list in ParseTrees, causing all possible parse trees creates as branches of the same tree, instead of duplicating the head to different alternatives.
- Some ambiguities are not represented, but this should be checked only after the trees creation is fixed.
- Generated trees should be filtered and only ones that are long enough (as long as the sentence) should be returned.
Copyright (c) 2013 Chloi Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.