#language #cnf #nlp #cyk

rusty_grammar

修改后的CYK算法,适用于语言

2个版本

0.1.2 2020年9月13日
0.1.1 2020年9月13日

#1925算法

GPL-3.0+

17KB
238

rusty_grammar − 最新版本 API rusty_grammar: rustc 1.46.0+

rusty_grammar是一个库,它利用修改后的CYK算法来定义语法并理解语言。

示例

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());
}

依赖项

~520KB
~10K SLoC