def test_prob_lexicon(): check = { 'Article': [('the', 0.5), ('a', 0.25), ('an', 0.25)], 'Pronoun': [('i', 0.4), ('you', 0.3), ('he', 0.3)] } lexicon = ProbLexicon(Article="the [0.5] | a [0.25] | an [0.25]", Pronoun="i [0.4] | you [0.3] | he [0.3]") assert lexicon == check
def test_prob_grammar(): rules = ProbRules(A="B C [0.3] | D E [0.7]", B="E [0.1] | a [0.2] | b c [0.7]") lexicon = ProbLexicon(Article="the [0.5] | a [0.25] | an [0.25]", Pronoun="i [0.4] | you [0.3] | he [0.3]") grammar = ProbGrammar("Simplegram", rules, lexicon) assert grammar.rewrites_for('A') == [(['B', 'C'], 0.3), (['D', 'E'], 0.7)] assert grammar.isa('the', 'Article')
def test_prob_generation(): lexicon = ProbLexicon(Verb="am [0.5] | are [0.25] | is [0.25]", Pronoun="i [0.4] | you [0.3] | he [0.3]") rules = ProbRules( S="Verb [0.5] | More [0.3] | Pronoun [0.1] | nobody is here [0.1]", More="Pronoun Verb [0.7] | Pronoun Pronoun [0.3]") grammar = ProbGrammar("Simplegram", rules, lexicon) sentence = grammar.generate_random('S') assert len(sentence) == 2