本文整理汇总了Python中grammar.Grammar.print_grammar方法的典型用法代码示例。如果您正苦于以下问题:Python Grammar.print_grammar方法的具体用法?Python Grammar.print_grammar怎么用?Python Grammar.print_grammar使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类grammar.Grammar
的用法示例。
在下文中一共展示了Grammar.print_grammar方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_grammar
# 需要导入模块: from grammar import Grammar [as 别名]
# 或者: from grammar.Grammar import print_grammar [as 别名]
def get_grammar(self):
print '----------------- BEGIN regex_to_grammar -----------------\n'
binary_op = ['|', '.']
terminals = set(list(self.re))
if '+' in terminals:
terminals.remove('+')
if '*' in terminals:
terminals.remove('*')
if '|' in terminals:
terminals.remove('|')
if '.' in terminals:
terminals.remove('.')
for i in self.re:
if i in terminals:
# print 'Stack=',self.stack ,'char', i
self.stack.append(i)
else:
if i not in binary_op:
# print 'Stack=',self.stack ,'char', i
self.stack.append(
self.create_new_rules(i, self.stack.pop()))
else:
# print 'Stack=',self.stack ,'char', i
dummy = self.create_new_rules(i, self.stack[-2:])
a = self.stack.pop()
b = self.stack.pop()
self.stack.append(dummy)
self.dictionary['S'] = [('S', ''.join(self.stack.pop()))]
# print "Dictionary/Grammar:", self.dictionary
# print 'Terminals:', list(terminals)
# print 'Non Terminals:', self.dictionary.keys()
# print 'Number of rules:', len(self.dictionary)
grammar_ = Grammar(
set(self.dictionary.keys()), terminals, self.dictionary, 'S')
print "Grammar: ",
grammar_.print_grammar()
print grammar_.grammar[2]
print '\n----------------- END regex_to_grammar -----------------\n\n'
return grammar_
示例2: test_sequitur
# 需要导入模块: from grammar import Grammar [as 别名]
# 或者: from grammar.Grammar import print_grammar [as 别名]
def test_sequitur(self):
"""docstring for test_sequitur"""
g = Grammar()
g.train_string("Hello, world!")
self.assertEqual("0 --(0)--> H e l l o , _ w o r l d ! \n", g.print_grammar())
示例3: test_sequitur_base
# 需要导入模块: from grammar import Grammar [as 别名]
# 或者: from grammar.Grammar import print_grammar [as 别名]
def test_sequitur_base(self):
"""docstring for test_sequitur_base"""
g = Grammar()
g.train_string("abcabdabcabd")
self.assertEqual("0 --(0)--> 1 1 \n1 --(2)--> 2 c 2 d abcabd\n2 --(2)--> a b ab\n", g.print_grammar())