Beispiel #1
0
def test_can_generate_all_sentences():
    rule_set = RuleSet()
    rule_set.add(sentence, "I hate you!")
    smell = NonTerminal("SMELL")
    rude_adj = NonTerminal("RUDE_ADJ")
    pet = NonTerminal("PET")
    rule_set.add(sentence, "You smell of ", smell)
    rule_set.add(smell, "elderberries")
    rule_set.add(smell, "dogfood")
    rule_set.add(sentence, "You're as ", rude_adj, " as my ", pet)
    rule_set.add(rude_adj, "ugly")
    rule_set.add(rude_adj, "stupid")
    rule_set.add(pet, "rat")
    rule_set.add(pet, "dog")
    
    assert_equals(["I hate you!", "You smell of elderberries", "You smell of dogfood",
                   "You're as ugly as my rat", "You're as ugly as my dog",
                   "You're as stupid as my rat", "You're as stupid as my dog"], generate_all(rule_set))
Beispiel #2
0
def test_empty_rule_set_generates_no_sentences():
    rule_set = RuleSet()
    
    assert_equals([], generate_all(rule_set))
Beispiel #3
0
def test_can_generate_all_sentences_up_to_maximum_depth():
    rule_set = RuleSet()
    rule_set.add(sentence, "B")
    rule_set.add(sentence, "A ", sentence)
    
    assert_equals(["B", "A B", "A A B"], generate_all(rule_set, 3))
Beispiel #4
0
#!/usr/bin/env python

import sys
import random

from abuse.parse import parse
from abuse.generate import generate
from abuse.generate import generate_all
from abuse.generate import RuleSet

class RandomSelector(object):
    def select(self, lower, upper):
        return random.randint(lower, upper - 1)

rule_set = RuleSet()

if "--all" in sys.argv:
    sys.argv.remove("--all")
    all_abuse = True
else:
    all_abuse = False
    
parse(open(sys.argv[1]).read(), rule_set, [])
if all_abuse:
    print "\n".join(generate_all(rule_set))
else:
    print generate(rule_set, RandomSelector())