本文整理汇总了Python中grammar.Grammar.left_for_right方法的典型用法代码示例。如果您正苦于以下问题:Python Grammar.left_for_right方法的具体用法?Python Grammar.left_for_right怎么用?Python Grammar.left_for_right使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类grammar.Grammar
的用法示例。
在下文中一共展示了Grammar.left_for_right方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: GrammarTest
# 需要导入模块: from grammar import Grammar [as 别名]
# 或者: from grammar.Grammar import left_for_right [as 别名]
class GrammarTest(unittest.TestCase):
def setUp(self):
rules = [
'S -> VP | NP VP',
'VP -> V',
'NP -> Det N | N',
'V -> walk | fly | book',
'N -> I | you | cows | book',
'Det -> the',
'Det -> a'
]
self.grammar = Grammar(rules)
def test_init(self):
self.assertItemsEqual([['VP'], ['NP', 'VP']], self.grammar.get('S'))
self.assertItemsEqual([['the'], ['a']], self.grammar.get('Det'))
def test_get(self):
self.assertItemsEqual([['VP'], ['NP', 'VP']], self.grammar.get('S'))
self.assertItemsEqual([['V']], self.grammar.get('VP'))
self.assertItemsEqual([], self.grammar.get('NotThere'))
def test_is_pos(self):
self.assertFalse(self.grammar.is_pos('S'))
self.assertFalse(self.grammar.is_pos('VP'))
self.assertFalse(self.grammar.is_pos('NotThere'))
self.assertTrue(self.grammar.is_pos('Det'))
def test_pos_for(self):
self.assertEqual(self.grammar.pos_for('the'), ['Det'])
self.assertEqual(self.grammar.pos_for('book'), ['N', 'V']) # 'book' has two POS
self.assertEqual(self.grammar.pos_for('NotThere'), []) # 'book' has two POS
def test_terminals(self):
self.assertItemsEqual(set(['walk', 'fly', 'book', 'I', 'you', 'cows', 'book', 'the', 'a']), self.grammar.terminals)
def test_left_for_right(self):
self.assertItemsEqual(set(['Det']), self.grammar.left_for_right(['the']))
self.assertItemsEqual(set(['N', 'V']), self.grammar.left_for_right(['book']))
self.assertItemsEqual(set(['V']), self.grammar.left_for_right(['walk']))
self.assertItemsEqual(set(['NP']), self.grammar.left_for_right(['Det', 'N']))