Exemplo n.º 1
0
 def test_template7(self):
     template1 = Template.create("here we have slot {A} and slot {B}")
     fillers = Assignment()
     fillers.add_pair("A", "apple")
     fillers.add_pair("B", "banana")
     assert template1.fill_slots(fillers) == "here we have slot apple and slot banana"
     fillers.remove_pair("B")
     assert Template.create(template1.fill_slots(fillers)).get_slots().pop() == "B"
Exemplo n.º 2
0
    def test_template4(self):
        template1 = Template.create("hi my name is {name}")
        assert str(template1.match("hi my name is Pierre Lison ").get_value("name")) == "Pierre Lison"

        template2 = Template.create("{name} is my name")
        assert str(template2.match("Pierre Lison is my name").get_value("name")) == "Pierre Lison"

        template3 = Template.create("hi my name is {name} and I need coffee")
        assert str(template3.match("hi my name is Pierre and I need coffee ").get_value("name")) == "Pierre"
Exemplo n.º 3
0
    def test_template8(self):
        template = Template.create("here we have a test")
        assert not template.match("here we have a test2").is_matching()
        assert not template.partial_match("here we have a test2").is_matching()
        assert template.partial_match("here we have a test that is working").is_matching()
        assert not template.match("here we have a test that is working").is_matching()

        template2 = Template.create("bla")
        assert not template2.partial_match("bla2").is_matching()
        assert not template2.partial_match("blabla").is_matching()
        assert template2.partial_match("bla bla").is_matching()
        assert not template2.match("bla bla").is_matching()
Exemplo n.º 4
0
 def test_one_char_and_parenthesis(self):
     t = Template.create("?")
     assert t.partial_match("how are you?").is_matching()
     assert t.partial_match("how are you ?").is_matching()
     t = Template.create("Pred1({X})")
     assert t.match("Pred1(FirstTest)").is_matching()
     assert t.match("Pred1(Pred2(Bla))").is_matching()
     assert str(t.match("Pred1(Pred2(Bla))").get_value("X")) == "Pred2(Bla)"
     t = Template.create("Pred2({X},{Y})")
     assert str(t.match("Pred2(Bla,Blo)").get_value("X")) == "Bla"
     assert str(t.match("Pred2(Bla,Blo)").get_value("Y")) == "Blo"
     assert str(t.match("Pred2(Bla(1,2),Blo)").get_value("Y")) == "Blo"
     assert str(t.match("Pred2(Bla,Blo(1,2))").get_value("X")) == "Bla"
Exemplo n.º 5
0
    def test_complex_regex(self):
        t = Template.create("a (pizza)? margherita")
        assert t.match("a margherita").is_matching()
        assert t.match("a pizza margherita").is_matching()
        assert not t.match("a pizza").is_matching()
        assert t.partial_match("I would like a margherita").is_matching()

        t2 = Template.create("a (bottle of)? (beer|wine)")
        assert t2.match("a beer").is_matching()
        assert t2.match("a bottle of wine").is_matching()
        assert not t2.match("a bottle of").is_matching()
        assert not t2.match("a coke").is_matching()
        assert t2.partial_match("I would like a bottle of beer").is_matching()

        t3 = Template.create("move (a (little)? bit)? (to the)? left")
        assert t3.match("move a little bit to the left").is_matching()
        assert t3.match("move a bit to the left").is_matching()
        assert t3.match("move to the left").is_matching()
        assert t3.match("move a little bit left").is_matching()
        assert not t3.match("move a to the left").is_matching()

        t4 = Template.create("I want beer(s)?")
        assert t4.match("I want beer").is_matching()
        assert t4.match("I want beers").is_matching()
        assert not t4.match("I want beer s").is_matching()

        t5 = Template.create("(beer(s)?|wine)")
        assert t5.match("beer").is_matching()
        assert t5.match("beers").is_matching()
        assert t5.match("wine").is_matching()
        assert not t5.match("wines").is_matching()
        assert not t5.match("beer wine").is_matching()
        assert Template.create("* (to the|at the)? left of").match("window to the left of").is_matching()
        assert Template.create("* (to the|at the)? left of").match("window left of").is_matching()
        assert Template.create("* (to the|at the)? left of").match("left of").is_matching()
Exemplo n.º 6
0
    def add_trigger(self, trigger):
        """
        Adds a new trigger to the model, defined by the variable label

        :param trigger: the variable
        """
        self._triggers.append(Template.create(trigger))
Exemplo n.º 7
0
    def ground(self, grounding):
        """
        Ground the slots in the effect variable and value (given the assignment) and
        returns the resulting effect.

        :param grounding: the grounding
        :return: the grounded effect
        """
        new_t = Template.create(self._label_template.fill_slots(grounding))
        new_v = Template.create(self._value_template.fill_slots(grounding))
        if new_t.is_under_specified() or new_v.is_under_specified():
            return TemplateEffect(new_t, new_v, self._priority,
                                  self._exclusive, self._negated)
        else:
            return BasicEffect(str(new_t), ValueFactory.create(str(new_v)),
                               self._priority, self._exclusive, self._negated)
Exemplo n.º 8
0
    def test_template6(self):
        template1 = Template.create("{anything}")
        assert str(template1.match("bla bla bla").get_value("anything")) == "bla bla bla"

        template2 = Template.create("{anything} is good")
        assert str(template2.match("bla bla bla is good").get_value("anything")) == "bla bla bla"
        assert not template2.match("blo blo").is_matching()
        assert not template2.match("bla bla bla is bad").contains_var("anything")
        assert template2.match("blo is good").is_matching()

        template3 = Template.create("this could be {anything}")
        assert str(template3.match("this could be pretty much anything").get_value("anything")) == "pretty much anything"
        assert not template3.match("but not this").is_matching()
        assert not template3.match("this could beA").is_matching()
        assert not template3.partial_match("this could beA").is_matching()
        assert not template3.match("this could be").is_matching()
        assert not template3.partial_match("this could be").is_matching()
Exemplo n.º 9
0
    def parse_effect(str_val):
        """
        Parses the string representing the effect, and returns the corresponding effect.

        :param str_val: the string representing the effect
        :return: the corresponding effect
        """
        if " ^ " in str_val:
            effects = list()
            for split in str_val.split(" ^ "):
                sub_output = Effect.parse_effect(split)
                effects += sub_output.get_sub_effects()
            return Effect(effects)

        else:
            if "Void" in str_val:
                return Effect(list())

            var = ""
            val = ""

            exclusive = True
            negated = False

            if ":=" in str_val:
                var = str_val.split(":=")[0]
                val = str_val.split(":=")[1]
                val = "None" if "{}" in val else val
            elif "!=" in str_val:
                var = str_val.split("!=")[0]
                val = str_val.split("!=")[1]
                negated = True
            elif "+=" in str_val:
                var = str_val.split("+=")[0]
                val = str_val.split("+=")[1]
                exclusive = False

            tvar = Template.create(var)
            tval = Template.create(val)
            if tvar.is_under_specified() or tval.is_under_specified():
                return Effect(TemplateEffect(tvar, tval, 1, exclusive,
                                             negated))
            else:
                return Effect(
                    BasicEffect(var, ValueFactory.create(val), 1, exclusive,
                                negated))
Exemplo n.º 10
0
    def get_input_variables(self):
        """
        Returns the input variables associated with the case

        :return: the set of input variables
        """
        input_vars = set(self._condition.get_input_variables())

        for slot in self._condition.get_slots():
            template = Template.create(slot)
            input_vars.add(template)

        for effect in self.get_effects():
            for input_variable in effect.get_value_slots():
                input_vars.add(Template.create(input_variable))

        return input_vars
Exemplo n.º 11
0
    def _get_sub_effect(node, priority):
        """
        Extracts a basic effect from the XML specification.

        :param node: the XML node
        :param priority: the rule priority
        :return: the corresponding basic effect
        """
        node_keys = node.keys()
        if 'var' not in node_keys:
            raise ValueError()

        variable_name = node.attrib['var']

        if 'value' in node_keys:
            value = node.attrib['value']
        elif 'var2' in node_keys:
            value = '{' + node.attrib['var2'] + '}'
        else:
            value = 'None'
        value = re.sub(r'\s+', ' ', value)

        exclusive = True
        if 'exclusive' in node_keys:
            if node.attrib['exclusive'].lower() == 'false':
                exclusive = False

        negated = 'relation' in node_keys and XMLRuleReader._get_relation(node) == Relation.UNEQUAL

        #  "clear" effect is outdated
        if node.tag.lower() == 'clear':
            value = 'None'

        # checking for other attributes
        for attrib_key in node_keys:
            if attrib_key not in ['var', 'var2', 'value', 'relation', 'exclusive']:
                raise ValueError()

        template_variable = Template.create(variable_name)
        template_value = Template.create(value)

        if template_variable.is_under_specified() or template_value.is_under_specified():
            return TemplateEffect(template_variable, template_value, priority, exclusive, negated)
        else:
            return BasicEffect(variable_name, ValueFactory.create(str(template_value)), priority, exclusive, negated)
Exemplo n.º 12
0
 def test_template2(self):
     template = Template.create("hi my name is {name}")
     utterance1 = "hi my name is Pierre, how are you?"
     assert template.partial_match(utterance1).is_matching()
     utterance2 = "hello how are you?"
     assert not template.partial_match(utterance2).is_matching()
     utterance3 = "hi my name is Pierre"
     assert template.partial_match(utterance3).is_matching()
     assert template.match(utterance3).is_matching()
Exemplo n.º 13
0
    def test_template3(self):
        template = Template.create("hi my name is {name} and I need coffee")
        utterance1 = " hi my name is Pierre and i need coffee "
        utterance2 = "hi my name is Pierre and I need coffee right now"
        assert template.partial_match(utterance1).is_matching()
        assert template.partial_match(utterance2).is_matching()
        utterance3 = "hello how are you?"
        assert not template.partial_match(utterance3).is_matching()

        assert not template.match(utterance3).is_matching()
        assert template.match(utterance1).is_matching()
Exemplo n.º 14
0
    def test_star(self):
        t1 = Template.create("here is * test")
        assert t1.match("here is test").is_matching()
        assert t1.match("here is a test").is_matching()
        assert t1.match("here is a great test").is_matching()
        assert not t1.match("here is a bad nest").is_matching()

        t1 = Template.create("* test")
        assert t1.match("test").is_matching()
        assert t1.match("great test").is_matching()
        assert not t1.match("here is a bad nest").is_matching()

        t1 = Template.create("test *")
        assert t1.match("test").is_matching()
        assert t1.match("test that works").is_matching()
        assert not t1.match("nest that is bad").is_matching()

        t1 = Template.create("this is a * {test}")
        assert t1.match("this is a ball").is_matching()
        assert t1.match("this is a really great ball").is_matching()
        assert not t1.match("this is huge").is_matching()
        assert str(t1.match("this is a ball").get_value("test")) == "ball"
        assert str(t1.match("this is a great blue ball").get_value("test")) == "ball"

        t1 = Template.create("* {test}")
        assert str(t1.match("this is a great ball").get_value("test")) == "ball"
        assert str(t1.match("ball").get_value("test")) == "ball"
        t1 = Template.create("{test} *")
        assert str(t1.match("great ball").get_value("test")) == "great ball"
        assert str(t1.match("ball").get_value("test")) == "ball"
    def get_prob(self, condition):
        """
        Returns the probability of eq=true given the condition

        :param condition: the conditional assignment
        :return: the probability of eq=true
        """
        predicted = None
        actual = None
        for input_var in condition.get_variables():
            if input_var == self._base_var + "^p":
                predicted = condition.get_value(input_var)
            elif input_var == self._base_var + "'":
                actual = condition.get_value(input_var)
            elif input_var == self._base_var:
                actual = condition.get_value(input_var)

        if predicted is None or actual is None:
            raise ValueError()

        if predicted == ValueFactory.none() or actual == ValueFactory.none():
            return EquivalenceDistribution.none_prob
        elif predicted == actual:
            return 1.0
        elif isinstance(predicted, StringVal) and isinstance(actual, StringVal):
            str1 = str(predicted)
            str2 = str(actual)
            if Template.create(str1).match(str2).is_matching() or Template.create(str2).match(str1).is_matching():
                return 1.0
            return 0.0
        elif len(predicted.get_sub_values()) > 0 and len(actual.get_sub_values()) > 0:
            vals0 = predicted.get_sub_values()
            vals1 = actual.get_sub_values()
            intersect = set(vals0)
            intersect.intersection_update(vals1)

            return float(len(intersect) / len(vals0))
        else:
            return 0.0
Exemplo n.º 16
0
    def test_real_function(self):
        # def add(*x):
        #     return ValueFactory.create(sum([float(item) for item in x]))
        #
        # def substract(*x):
        #     return ValueFactory.create(value=float(x[0]) - sum([float(item) for item in x[1:]]))
        #
        Settings.add_function("add", lambda *x: ValueFactory.create(sum([float(item) for item in x])))
        Settings.add_function("substract", lambda *x: ValueFactory.create(float(x[0]) - sum([float(item) for item in x[1:]])))

        t = Template.create("add({X},{Y})")

        assert t.fill_slots(Assignment.create_from_string("X=1 ^ Y=2")) == "3"
        t = Template.create("add(4,{Y},{Z})")
        assert t.fill_slots(Assignment.create_from_string("Z=3 ^ Y=2")) == "9"
        t = Template.create("add(4,2)")
        assert t.fill_slots(Assignment.create_from_string("Z=3 ^ Y=2")) == "6"
        t = Template.create("add(substract({X},{Y}),{Z})")
        assert isinstance(t, FunctionalTemplate)
        assert t.fill_slots(Assignment.create_from_string("X=3 ^ Y=1 ^ Z=2")) == "4"
        t = Template.create("add(substract({X},{Y}),substract({Z}, {A}))")
        assert isinstance(t, FunctionalTemplate)
        assert t.fill_slots(Assignment.create_from_string("X=3 ^ Y=1 ^ Z=4 ^ A=2")) == "4"
Exemplo n.º 17
0
    def __contains__(self, item):
        """
        Returns true if subvalue is a substring of the current StringVal, and false otherwise.

        :param item: the value
        :return: true is subvalue is a substring of the object, false otherwise
        """
        if isinstance(item, StringVal):
            if item._template is None:
                from templates.template import Template
                item._template = Template.create(item._value)

            return item._template.partial_match(self._value).is_matching()
        else:
            # TODO: check bug > subvalue.toString().contains(str): not a proper comparison
            return str(item) in self._value  # corrected version.
Exemplo n.º 18
0
    def get_functions(expression):
        functions = set()

        for matcher in MathExpression.function_pattern.finditer(expression):
            function_s = matcher.captures()[0]
            nb_open_parentheses = 0
            for idx, c in enumerate(expression):
                if c == '(':
                    nb_open_parentheses += 1
                elif c == ')' and nb_open_parentheses > 1:
                    nb_open_parentheses -= 1
                elif c == ')':
                    function_s += expression[matcher.end():idx + 1]
                    if Settings.is_function(function_s):
                        functional_template = Template.create(function_s)
                        functions.add(functional_template)
                    break

        return functions
Exemplo n.º 19
0
    def __init__(self, arg1=None, arg2=None, arg3=None):
        if isinstance(arg1, str) and isinstance(arg2, str) and isinstance(
                arg3, Relation):
            variable, value, relation = arg1, arg2, arg3
            """
            Creates a new basic condition, given a variable label, an expected value, and
            a relation to hold between the variable and its value
    
            :param variable: the variable
            :param value: the value
            :param relation: the relation to hold
            """
            self._variable = Template.create(variable)
            self._template_value = Template.create(value)
            self._ground_value = None if self._template_value.is_under_specified(
            ) else ValueFactory.create(value)
            self._relation = relation

        elif isinstance(arg1, str) and isinstance(arg2, Value) and isinstance(
                arg3, Relation):
            variable, value, relation = arg1, arg2, arg3
            """
            Creates a new basic condition, given a variable label, an expected value, and
            a relation to hold between the variable and its value
    
            :param variable: the variable
            :param value: the value
            :param relation: the relation to hold
            """
            self._variable = Template.create(variable)
            self._template_value = Template.create(str(value))
            self._ground_value = value
            self._relation = relation

        elif isinstance(arg1, BasicConditionWrapper) and isinstance(
                arg2, Assignment) and arg3 is None:
            condition, grounding = arg1, arg2
            """
            Creates a new basic condition that represented the grounding of the provided
            condition together with the value assignment
    
            :param condition: the condition (with free variables)
            :param grounding: the grounding assignment
            """
            self._variable = condition._variable

            if self._variable.is_under_specified():
                self._variable = Template.create(
                    self._variable.fill_slots(grounding))

            self._relation = condition._relation
            self._template_value = condition._template_value
            self._ground_value = condition._ground_value

            if len(self._template_value.get_slots()) > 0:
                self._template_value = Template.create(
                    self._template_value.fill_slots(grounding))

                if not self._template_value.is_under_specified():
                    self._ground_value = ValueFactory.create(
                        str(self._template_value))

        else:
            raise NotImplementedError()
Exemplo n.º 20
0
    def _get_sub_condition(node):
        """
        Extracting a partial condition from a rule specification

        :param node: the XML node
        :return: the corresponding condition
        """
        # extracting a basic condition
        if node.tag == 'if':
            if 'var' not in node.keys():
                raise ValueError()

            variable_name = node.attrib['var']
            template = Template.create(variable_name)

            if template.is_under_specified():
                template = Template.create(str(template).replace('*', '{' + str(random.randint(1, 99)) + '}'))

            value_str = None
            if 'value' in node.keys():
                value_str = node.attrib['value']

            if value_str is not None:
                relation = XMLRuleReader._get_relation(node)
                condition = BasicCondition(variable_name, value_str, relation)
            else:
                if 'var2' not in node.keys():
                    raise ValueError()

                second_variable = node.attrib['var2']
                relation = XMLRuleReader._get_relation(node)
                condition = BasicCondition(variable_name, '{' + second_variable + '}', relation)

            for attrib_key in node.attrib.keys():
                if attrib_key not in ['var', 'var2', 'value', 'relation']:
                    raise ValueError()

            return condition

        # extracting a conjunction or disjunction
        if node.tag == 'or' or node.tag == 'and':
            conditions = list()

            for child_node in node:
                if XMLUtils.has_content(child_node):
                    conditions.append(XMLRuleReader._get_sub_condition(child_node))

            return ComplexCondition(conditions, BinaryOperator.OR if node.tag == 'or' else BinaryOperator.AND)

        # extracting a negated conjunction
        if node.tag == 'neg' or node.tag == 'not':
            conditions = list()

            for child_node in node:
                if XMLUtils.has_content(child_node):
                    conditions.append(XMLRuleReader._get_sub_condition(child_node))

            return conditions[0] if len(conditions) == 1 else ComplexCondition(conditions, BinaryOperator.AND)

        if XMLUtils.has_content(node):
            raise ValueError()

        return VoidCondition()
Exemplo n.º 21
0
 def create_relation(self, str_representation):
     """
     Creates a template from its string representation
     """
     return Template.create(str_representation)
Exemplo n.º 22
0
 def copy_value(self, template):
     """
     Copies a template
     """
     return Template.create(str(template))
Exemplo n.º 23
0
 def test_template5(self):
     template1 = Template.create("hi this is {A} and this is {B}")
     assert str(template1.match("hi this is an apple and this is a banana").get_value("A")) == "an apple"
     assert str(template1.match("hi this is an apple and this is a banana").get_value("B")) == "a banana"
Exemplo n.º 24
0
 def test_template_math(self):
     assert MathExpression("1+2").evaluate() == pytest.approx(3.0, abs=0.001)
     assert MathExpression("-1.2*3").evaluate() == pytest.approx(-3.6, abs=0.001)
     t = Template.create("{X}+2")
     assert str(t.fill_slots(Assignment("X", "3"))) == "5"
Exemplo n.º 25
0
 def test_template1(self):
     template = Template.create("this is a first test")
     utterance = "bla bla this is a first test bla"
     assert template.partial_match(utterance).is_matching()
Exemplo n.º 26
0
 def test_functions(self):
     t = Template.create("{X}+{Y}")
     assert t.fill_slots(Assignment.create_from_string("X=1 ^ Y=2")) == "3"
     assert t.fill_slots(Assignment.create_from_string("X=[1,2] ^ Y=4")) == "[1, 2, 4]"
     t = Template.create("{X}-{Y}")
     assert t.fill_slots(Assignment.create_from_string("X=[1,2] ^ Y=2")) == "[1]"
Exemplo n.º 27
0
 def test_double(self):
     t = Template.create("MakeOrder({Price})")
     assert t.match("MakeOrder(179)").is_matching()
     assert t.match("MakeOrder(179.0)").is_matching()
     assert not t.match("MakkeOrder(179.0)").is_matching()
     assert not t.match("MakkeOrder()").is_matching()
Exemplo n.º 28
0
 def test_match_in_string(self):
     t = Template.create("{X}th of March")
     assert t.match("20th of March").is_matching()
     assert t.partial_match("on the 20th of March").is_matching()
     assert not t.match("20 of March").is_matching()