[][src]Crate rusty_grammar

rusty_grammar

rusty_grammar is a library that makes use of a modified CYK algorithm to define grammars and understand language.

About

The rusty_grammar library leverages the CYK algorithm. It modifies it to work for words and parts of speech/writing. For example adjectives, nouns, prepositional phrases and etc. Exposes traits to define a WordBank and Grammar set. Of course grammars must be defined in CNF form. For more information on that find it at Chomsky Normal Form.

See the rusty_grammar website for additional documentation and usage examples.

Quick Example

This example is not tested
struct G {}
impl<'grammar> Grammar<'grammar> for G {
    fn convert(&self) -> Vec<GrammarRule<'grammar>> {
	let mut rules = Vec::new();
	rules.push(GrammarRule{ left_symbol: "ActionSentence", right_symbol: "Verb NounClause | Verb NounClause PrepClause" });
	rules.push(GrammarRule{ left_symbol: "NounClause", right_symbol: "Count ANoun | Adjective Noun" });
	rules.push(GrammarRule{ left_symbol: "PrepClause", right_symbol: "Prep NounClause" });
	rules.push(GrammarRule{ left_symbol: "ANoun", right_symbol: "Adjective Noun" });
	rules.push(GrammarRule{ left_symbol: "Adjective", right_symbol: "adjective" });
	rules.push(GrammarRule{ left_symbol: "Prep", right_symbol: "prep" });
	rules.push(GrammarRule{ left_symbol: "Verb", right_symbol: "verb" });
	rules.push(GrammarRule{ left_symbol: "Noun", right_symbol: "noun" });
	rules.push(GrammarRule{ left_symbol: "Count", right_symbol: "definiteArticle | indefiniteArticle | number" });
	rules
    }
}

struct WB {}
impl WordBank for WB {
    fn lookup(&self, word: &str) -> &str {
	match word {
	    "examine" => "verb",
	    "sword" => "noun",
	    "rusty" => "adjective",
	    _ => "dne"
	}
    }
}

fn main() {
    let g = G{};
    let wb = WB{};
    let input = "examine rusty sword";
    let cyk: CYK<WB> = CYK::new(g, wb);
    let res = cyk.memoized_parse(input);
    println!("{}", res);
    println!("final_res: {:?}", res.get_final());
}

Structs

CYK

The struct for the CYK algorithm.

GrammarRule

The Struct to define a Grammar Rule.

MatrixResult

A struct to store the result of the CYK algorithm.

Traits

Grammar

The trait for a grammar. For a struct to be a grammar it must implement these methods.

WordBank

The trait for a wordbank. For a struct to be a wordbank it must implment these methods.