def test_grammar(): rules = Rules(A="B C | D E", B="E | a | b c") lexicon = Lexicon(Article="the | a | an", Pronoun="i | you | he") grammar = Grammar("Simplegram", rules, lexicon) assert grammar.rewrites_for('A') == [['B', 'C'], ['D', 'E']] assert grammar.isa('the', 'Article')
def test_generation(): lexicon = Lexicon(Article="the | a | an", Pronoun="i | you | he") rules = Rules(S="Article | More | Pronoun", More="Article Pronoun | Pronoun Pronoun") grammar = Grammar("Simplegram", rules, lexicon) sentence = grammar.generate_random('S') for token in sentence.split(): found = False for non_terminal, terminals in grammar.lexicon.items(): if token in terminals: found = True assert found
def test_lexicon(): assert Lexicon(Art="the | a | an") == {'Art': ['the', 'a', 'an']}
def test_lexicon(): check = {'Article': ['the', 'a', 'an'], 'Pronoun': ['i', 'you', 'he']} assert Lexicon(Article="the | a | an", Pronoun="i | you | he") == check