用于自然语言处理和翻译的glr解析器
GLRParser的Python项目详细描述
用于自然语言处理和翻译的glr解析器
glrparser不仅仅是一个解析器。是
- 处理不明确语法的自然语言分析器
- 处理功能统一的统一引擎
- 基于语法的自然语言翻译引擎
有关详细信息,请参见wiki页面:https://github.com/mdolgun/GLRParser/wiki
用于解析和翻译的示例代码应该如下:
fromGLRParserimportParser,ParseError,GrammarError,Treetry:parser=Parser()# initialize parser objectparser.load_grammar("GLRParser\grm\simple_trans.grm")# load grammar from a filesent="i saw the man in the house with the telescope"# sentence to parseparser.compile()# constructs parsing tablesparser.parse(sent)# parse the sentencetree=parser.make_tree()# generates parse forestttree=parser.trans_tree(tree)# translate the parse forestprint(ttree.pformatr())# pretty-print the translated parse forestfortransinttree.enum():# enumerate and print all alternative translations in the parse forestprint(trans.replace(" -",""))# concat suffixesexceptGrammarErrorasge:print(ge)exceptParseErroraspe:print(pe))
英语的简单语法->;土耳其语翻译(请参见simple_trans.grm)
S -> NP VP : NP VP S -> S in NP : NP -de S S -> S with NP : NP -la S NP -> i : NP -> the man : adam NP -> the telescope : teleskop NP -> the house : ev NP -> NP-1 in NP-2 : NP-2 -deki NP-1 NP -> NP-1 with NP-2 : NP-2 -lu NP-1 VP -> saw NP : NP -ı gördüm
给定上述语法和输入字符串:
i saw the man in the house with the telescope
它生成一个解析林和5个可选翻译(of 哪两个相同):
1. teleskopla evde adamı gördüm 2. teleskopla evdeki adamı gördüm 3. teleskoplu evde adamı gördüm 4. teleskoplu evdeki adamı gördüm 5. teleskoplu evdeki adamı gördüm
语义解释如下:
1. saw(in the house) saw(with the telescope) 2. man(in the house) saw(with the telescope) 3. saw(in the house) house(with the telescope) 4. man(in the house) man(with the telescope) 5. man(in the house) house(with the telescope)