Ejemplo n.º 1
0
 def test_add_nonterminal(self):
     """
     test adding a nonterminal to a PCFG
     """
     nonterminal = NonterminalSymbol('a')
     self.test_gram.add_nonterminal(nonterminal)
     test_nonterminals = {'a': NonterminalSymbol('a')}
     self.assertEqual(self.test_gram.nonterminals, test_nonterminals)
Ejemplo n.º 2
0
 def test_different_symbols(self):
     """
     Rules with identical derivations but different symbols should not eq
     """
     c_nonterminal = NonterminalSymbol('c')
     c_rule = Rule(c_nonterminal, self.a_derivation)
     self.assertNotEqual(c_rule, self.a_rule)
Ejemplo n.º 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)
Ejemplo n.º 4
0
 def test_remove_rule(self):
     """
     test that it successfully removes an implemented rule
     """
     self.test_gram.add_nonterminal(self.nonterminal)
     test_derivation = [NonterminalSymbol('b'), "aaaaade"]
     self.test_gram.remove_rule(self.nonterminal, test_derivation)
     self.assertEqual(
         self.test_gram.nonterminals.get(str(self.nonterminal.tag)).rules,
         [])
Ejemplo n.º 5
0
 def test_add_rule(self):
     """
     test adding a rule to an existing nonterminal in PCFG
     """
     self.test_gram.add_nonterminal(self.nonterminal)
     test_derivation = [NonterminalSymbol('b'), "aaaaade"]
     self.test_gram.add_rule(self.nonterminal, test_derivation)
     test_rules = [Rule(self.nonterminal, test_derivation)]
     self.assertEqual(
         self.test_gram.nonterminals.get(str(self.nonterminal.tag)).rules,
         test_rules)
Ejemplo n.º 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)
Ejemplo n.º 7
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)
Ejemplo n.º 8
0
def parse_rule(rule_string):
    """
    function parses a string and returns the generation represented by that string
    :param rule_string: the string representing the rule to be parsed
    :type rule_string: string
    :returns: list[symbols]
    """
    if rule_string == '':
        return [TerminalSymbol('')]
    # this regex is a pain but it matches strings of either [[...]] or [...]
    split_list = re.split('(\[{2}[^\]\[]+\]{2})', rule_string)
    # remove all empty strings
    split_list = filter(None, split_list)
    derivation = []
    for token in split_list:
        if token[:2] == '[[':
            derivation.append(NonterminalSymbol(token.lstrip("[").rstrip("]")))
        else:
            derivation.append(TerminalSymbol(token))
    return derivation
Ejemplo n.º 9
0
def from_json(json_in):
    gram_res = PCFG()
    dict_rep = json.loads(json_in)
    nonterminals = dict_rep.get('nonterminals')
    for tag, nonterminal in nonterminals.iteritems():
        rules = nonterminal['rules']
        markup = nonterminal['markup']

        # translate UI markup rep into data markup rep
        tmp_markups = []
        for markup_set, tags in markup.iteritems():
            tmp_set = MarkupSet(markup_set)
            for i in tags:
                new_mark = Markup(i, tmp_set)
                tmp_markups.append(new_mark)

        temp_nonterm = NonterminalSymbol(tag, markup=set(tmp_markups), deep=nonterminal['deep'])
        gram_res.add_nonterminal(temp_nonterm)

        for ruleindex, rule in enumerate(rules):
            # rule is an object
            expansion = parse_rule(''.join(rule['expansion']))
            application_rate = rule['app_rate']
            preconditions_str = rule['preconditionsStr']
            effects_str = rule['effectsStr']
            gram_res.add_rule(
                rule_head=temp_nonterm,
                rule_body=expansion,
                application_rate=application_rate,
                preconditions_str=preconditions_str,
                effects_str=effects_str
            )

    for markupSet in dict_rep.get('markups'):
        x = MarkupSet(markupSet)
        gram_res.add_new_markup_set(MarkupSet(markupSet))
        for tags in dict_rep['markups'][markupSet]:
            gram_res.add_unused_markup(Markup(tags, tagset=x))

    return gram_res
Ejemplo n.º 10
0
class TestNonterminalEquivalency(unittest.TestCase):
    """
    Testing Equivalency of NonterminalSymbols
    """
    def setUp(self):
        self.a_nonterminal = NonterminalSymbol('A')
        self.b_nonterminal = NonterminalSymbol('A')
        self.aaa = NonterminalSymbol('AAA')
        self.bbb = NonterminalSymbol('BBB')

    def test_should_eq_each_other(self):
        """identical nonterminals should equal eachother"""
        self.assertEqual(self.a_nonterminal, self.b_nonterminal)

    def test_different_not_eq(self):
        """nonidentical nonterminals should not equal eachother"""
        self.assertNotEqual(self.aaa, self.bbb)

    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)

    def test_different_markup_not_eq(self):
        """
        Heaven forbid you had two nonterminals with the same tag but different markup, they should
        not equal eachother
        """

        MRK_SET1 = MarkupSet('A_MARKUP')
        MRK_SET2 = MarkupSet('B_MARKUP')
        MRK_1 = Markup('AAA', MRK_SET1)
        MRK_2 = Markup('BBB', MRK_SET2)

        self.a_nonterminal.add_markup(MRK_1)
        self.b_nonterminal.add_markup(MRK_2)

        self.assertIn(MRK_1, self.a_nonterminal.markup)
        self.assertIn(MRK_1, self.a_nonterminal.markup)
        self.assertNotEqual(self.a_nonterminal, self.b_nonterminal)
Ejemplo n.º 11
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]!")
Ejemplo n.º 12
0
class TestPcfgOperations(unittest.TestCase):
    """
    Testing operations on a PCFG
    """
    def setUp(self):
        self.test_gram = PCFG()
        self.nonterminal = NonterminalSymbol('a')
        self.markup_class = MarkupSet('TEST_MARKUP')
        self.markup = Markup("MARKUP", self.markup_class)

    def test_add_nonterminal(self):
        """
        test adding a nonterminal to a PCFG
        """
        nonterminal = NonterminalSymbol('a')
        self.test_gram.add_nonterminal(nonterminal)
        test_nonterminals = {'a': NonterminalSymbol('a')}
        self.assertEqual(self.test_gram.nonterminals, test_nonterminals)

    def test_add_rule(self):
        """
        test adding a rule to an existing nonterminal in PCFG
        """
        self.test_gram.add_nonterminal(self.nonterminal)
        test_derivation = [NonterminalSymbol('b'), "aaaaade"]
        self.test_gram.add_rule(self.nonterminal, test_derivation)
        test_rules = [Rule(self.nonterminal, test_derivation)]
        self.assertEqual(
            self.test_gram.nonterminals.get(str(self.nonterminal.tag)).rules,
            test_rules)

    def test_remove_rule(self):
        """
        test that it successfully removes an implemented rule
        """
        self.test_gram.add_nonterminal(self.nonterminal)
        test_derivation = [NonterminalSymbol('b'), "aaaaade"]
        self.test_gram.remove_rule(self.nonterminal, test_derivation)
        self.assertEqual(
            self.test_gram.nonterminals.get(str(self.nonterminal.tag)).rules,
            [])

    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)

    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))

    def test_markup_class_addition(self):
        """
        tests to ensure that if we add a markup to a nonterminal, and that markup class does not already
        exist within our PCFG markup class list, we add it to the markup class list
        """
        self.nonterminal.add_markup(self.markup)
        self.test_gram.add_nonterminal(self.nonterminal)
        test = set()
        test.add(self.markup)
        self.assertEqual(
            self.test_gram.markup_class[self.markup.tagset.__str__()], test)

    def test_expansion_returns_markup(self):
        """make sure our expansions return markup correctly"""

        self.nonterminal.add_markup(self.markup)
        self.test_gram.add_nonterminal(self.nonterminal)

    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)

    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)

    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)
Ejemplo n.º 13
0
 def setUp(self):
     self.a_nonterminal = NonterminalSymbol('A')
     self.b_nonterminal = NonterminalSymbol('A')
     self.aaa = NonterminalSymbol('AAA')
     self.bbb = NonterminalSymbol('BBB')
Ejemplo n.º 14
0
 def setUp(self):
     self.test_gram = PCFG()
     self.nonterminal = NonterminalSymbol('a')
     self.markup_class = MarkupSet('TEST_MARKUP')
     self.markup = Markup("MARKUP", self.markup_class)