示例#1
0
    def test_expansion(self):
        """
        test expansions of our grammar
        """
        self.test_gram.add_nonterminal(self.nonterminal)
        a_prod = parse_rule("[[b]], this is a test of expansion")
        self.test_gram.add_rule(self.nonterminal, a_prod)
        self.test_gram.add_nonterminal(a_prod[0])
        b_prod = parse_rule("Wow")
        self.test_gram.add_rule(a_prod[0], b_prod)
        test_string = "Wow, this is a test of expansion"
        test_deriv = IntermediateDeriv(
            set(), TerminalSymbol("Wow, this is a test of expansion"))

        self.assertEqual(self.test_gram.expand(NonterminalSymbol('a')),
                         test_deriv)
示例#2
0
 def test_different_derivations(self):
     """
     Rules with identical symbols but different derivations should not eq
     """
     c_derivation = parse_rule("[[haha]] we're testing it again")
     c_nonterminal = self.a_nonterminal
     c_rule = Rule(c_nonterminal, c_derivation)
     self.assertNotEqual(c_rule, self.a_rule)
示例#3
0
 def setUp(self):
     self.a_derivation = parse_rule("[[Test]] of [rule] {parsing}")
     self.a_nonterminal = NonterminalSymbol('A')
     self.a_rule = Rule(self.a_nonterminal,
                        self.a_derivation,
                        application_rate=1)
     self.b_rule = Rule(self.a_nonterminal,
                        self.a_derivation,
                        application_rate=1)
示例#4
0
 def test_returns_system_vars(self):
     """
     test that our function correctly returns the list of system variables
     defined by the user within the program
     """
     self.test_gram.add_nonterminal(self.nonterminal)
     system_var_prod = parse_rule(
         "[[affimative]], [name], [[I think]] his hair is[hair_color]")
     self.test_gram.add_rule(self.nonterminal, system_var_prod)
     self.assertEqual(2, len(self.test_gram.system_vars))
     system_var_prod_2 = parse_rule("Ah yes, [player_name]")
     self.test_gram.add_rule(system_var_prod[0], system_var_prod_2)
     self.assertEqual(3, len(self.test_gram.system_vars))
     test_system_vars = []
     test_system_vars.append(SystemVar("name"))
     test_system_vars.append(SystemVar("hair_color"))
     test_system_vars.append(SystemVar("player_name"))
     self.assertEqual(test_system_vars, self.test_gram.system_vars)
示例#5
0
 def test_recursive_nt_addition(self):
     """
     add_rule should add all nonterminals present in derivation
     that are not in the grammar to the grammar
     """
     self.test_gram.add_nonterminal(self.nonterminal)
     a_prod = parse_rule("[[b]], this is a test of expansion")
     self.test_gram.add_rule(self.nonterminal, a_prod)
     self.assertEqual(2, len(self.test_gram.nonterminals))
示例#6
0
 def test_empty_expansion(self):
     """
     test that expansions of nonterminals with no productions works correctly
     """
     self.test_gram.add_nonterminal(self.nonterminal)
     a_prod = parse_rule("[[b]], this is a test of expansion")
     self.test_gram.add_rule(self.nonterminal, a_prod)
     self.test_gram.add_nonterminal(a_prod[0])
     test_string = IntermediateDeriv(set(),
                                     "[[b]], this is a test of expansion")
     self.assertEqual(self.test_gram.expand(NonterminalSymbol('a')),
                      test_string)
示例#7
0
def edit_rule():
    data = request.get_json()
    original_rule_head_name = data['original rule head name']
    original_rule_index = int(data['rule id'])
    rule_head_name = data['rule head name']
    rule_body = data['rule body']
    application_rate = int(data['application rate'])
    preconditions_str = data['preconditions']
    effects_str = data['effects']
    if rule_head_name == original_rule_head_name:
        # Simply update the application rate and expansion
        app.grammar.modify_application_rate(
            rule_head=nonterminal_symbol.NonterminalSymbol(rule_head_name),
            rule_index=original_rule_index,
            application_rate=application_rate)
        app.grammar.modify_rule_expansion(
            rule_head=nonterminal_symbol.NonterminalSymbol(rule_head_name),
            rule_index=original_rule_index,
            derivation=grammar.parse_rule(rule_body),
            application_rate=application_rate),
        app.grammar.modify_rule_preconditions_and_effects(
            rule_head=nonterminal_symbol.NonterminalSymbol(rule_head_name),
            rule_index=original_rule_index,
            new_preconditions_str=preconditions_str,
            new_effects_str=effects_str)
    else:
        # Delete the old rule and create a new one that is associated with the new rule head
        app.grammar.remove_rule_by_index(
            nonterminal_symbol.NonterminalSymbol(original_rule_head_name),
            original_rule_index)
        try:
            new_rule_head_object = app.grammar.nonterminals[rule_head_name]
        except KeyError:
            new_rule_head_object = nonterminal_symbol.NonterminalSymbol(
                rule_head_name)
            app.grammar.add_nonterminal(new_rule_head_object)
        app.grammar.add_rule(new_rule_head_object,
                             grammar.parse_rule(rule_body), application_rate)
    return app.grammar.to_json()
示例#8
0
 def test_modify_app_rate(self):
     """
     test that application rates are correctly modified
     """
     self.test_gram.add_nonterminal(self.nonterminal)
     a_prob = parse_rule("test of application_rate")
     self.test_gram.add_rule(self.nonterminal, a_prob)
     old_app = self.test_gram.nonterminals.get(str(
         self.nonterminal.tag)).rules[0].application_rate
     self.test_gram.modify_application_rate(self.nonterminal, 0, 5)
     new_app = self.test_gram.nonterminals.get(str(
         self.nonterminal.tag)).rules[0].application_rate
     self.assertNotEqual(old_app, new_app)
     self.assertEqual(new_app, 5)
示例#9
0
def add_rule():
    data = request.get_json()
    rule_head_name = data['rule head name']
    rule_body = data['rule body']
    application_rate = int(data['application rate'])
    preconditions_str = data['preconditions']
    effects_str = data['effects']
    try:
        # Retrieve the nonterminal symbol named in the rule head
        rule_head_object = app.grammar.nonterminals[rule_head_name]
    except KeyError:
        # No such nonterminal symbol exists yet, so create one
        rule_head_object = nonterminal_symbol.NonterminalSymbol(rule_head_name)
        app.grammar.add_nonterminal(rule_head_object)
    app.grammar.add_rule(rule_head=rule_head_object,
                         rule_body=grammar.parse_rule(rule_body),
                         application_rate=application_rate,
                         preconditions_str=preconditions_str,
                         effects_str=effects_str)
    return app.grammar.to_json()
示例#10
0
from tags import Markup, MarkupSet
from nonterminal_symbol import NonterminalSymbol
from grammar import parse_rule, PCFG

test = PCFG()

ask_day = NonterminalSymbol('ask_day', deep=True)

test.add_nonterminal(ask_day)
ask_day_prod1 = parse_rule("[[greetings]] [[pleasantry]] [[ask]]")
ask_day_prod2 = parse_rule("[[greetings]] [[rude]] [[ask]]")

test.add_rule(ask_day, ask_day_prod1, 10)
test.add_rule(ask_day, ask_day_prod2, 3)

pleasantry_prod1 = parse_rule(
    "This [weather] weather is sure crazy, isn't it?")
pleasantry_prod2 = parse_rule(
    "Did you see the [speaker.sportsTeam] game, it was crazy!")
pleasantry_prod3 = parse_rule(
    "Can you believe what happened on the [news.channel] last night?!")

rude_prod1 = parse_rule("As ugly as ever, I see!")
rude_prod2 = parse_rule(
    "Time has been as unkind to you as it has to [world.celebrity]!")
rude_prod3 = parse_rule("You remain as despicable as ever, it would seem.")

greetings_prod1 = parse_rule("Hey, long time no see, [subject.name].")
greetings_prod2 = parse_rule("Is that you, [subject.name]?")
greetings_prod3 = parse_rule("I haven't seen you in forever, [subject.name]!")
示例#11
0
 def test_different_rules_not_eq(self):
     """Nonterminals with the same tag but different rules should not equal eachother"""
     a_rule = parse_rule("[[Test of]] rule parsing")
     self.a_nonterminal.add_rule(a_rule)
     self.assertEqual(self.a_nonterminal.tag, self.b_nonterminal.tag)
     self.assertNotEqual(self.a_nonterminal, self.b_nonterminal)