def test_can_use_intermediate_rules(): rule_set = RuleSet() smell = NonTerminal("SMELL") rule_set.add(sentence, "You smell of ", smell) rule_set.add(smell, "elderberries") rule_set.add(smell, "dogfood") assert_equals("You smell of elderberries", generate(rule_set, StaticSelector(0, 0))) assert_equals("You smell of dogfood", generate(rule_set, StaticSelector(0, 1)))
def test_can_generate_empty_sentence(): rule_set = RuleSet() rule_set.add(sentence) assert_equals("", generate(rule_set, StaticSelector(0)))
def test_resorts_to_picking_sentence_randomly_from_all_sentences_if_non_terminal_cannot_be_expanded(): rule_set = RuleSet() rule_set.add(sentence, NonTerminal("RUDE_WORD")) rule_set.add(sentence, "Go away!") assert_equals("Go away!", generate(rule_set, StaticSelector(0, 0)));
def test_resorts_to_picking_sentence_randomly_from_all_sentences_if_maximum_depth_is_reached(): rule_set = RuleSet() rule_set.add(sentence, "B") rule_set.add(sentence, "A ", sentence) assert_equals("A A B", generate(rule_set, StaticSelector(1, 1, 1, 2), 3));
def test_selector_is_used_to_pick_rule_when_there_are_many_possible_rules(): rule_set = RuleSet() rule_set.add(sentence, "I hate you!") rule_set.add(sentence, "Go to hell!") assert_equals("I hate you!", generate(rule_set, StaticSelector(0))) assert_equals("Go to hell!", generate(rule_set, StaticSelector(1)))
def test_adding_rule_to_convert_sentence_to_terminal(): rule_set = RuleSet() rule_set.add(sentence, "I hate you!") assert_equals("I hate you!", generate(rule_set, StaticSelector(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())