Example #1
0
 def setUp(self):
     self.a = Expression()
     self.a.add_data(None, "1")
     self.a.add_data(None, "2")
     self.b = Expression()
     self.b.add_data(None, "2")
     self.b.add_data(None, "1")
def random_expression(only_ascii=False):
    """
    operations: new node, add data, decapitate, merge
    """
    mean_size = 20
    xs = [40.0, 30.0, 50.0, 20.0]
    xs = [x * (1.0 - random.random()) for x in xs]
    assert all(x != 0 for x in xs)
    new_node, add_data, decapitate, _ = [x / sum(xs) for x in xs]
    expressions = [Expression(), Expression(), Expression(), Expression()]
    while len(expressions) != 1:
        if (1.0 / mean_size) < random.random():
            # Will start to merge more and will not create new nodes
            new_node = 0.0
        # Choose action
        r = random.random()
        if r < new_node:
            # New expression
            expressions.append(Expression())
        elif r < add_data + new_node:
            # Add data
            e = random.choice(expressions)
            e.add_data(random_relation(only_ascii), random_data(only_ascii))
        elif r < decapitate + add_data + new_node:
            # Decapitate
            e = random.choice(expressions)
            e.decapitate(random_relation(only_ascii),
                         reverse=(0.25 < random.random()))
        elif len(expressions) != 1:
            # Merge
            random.shuffle(expressions)
            e2 = expressions.pop()
            e1 = expressions[-1]
            e1 += e2
    return expressions[0]
Example #3
0
    def setUp(self):
        self.a = Expression()
        self.a.add_data(0, "data")
        self.a.decapitate("onelevel")

        self.b = Expression()
        self.b.add_data(0, "data")
        self.b.decapitate("onelevel", reverse=True)
Example #4
0
 def setUp(self):
     A = Expression()
     A.add_data("bla", "somedata")
     A.decapitate("hier")
     B = Expression()
     B.add_data("ble", "otherdata")
     B.decapitate("hier")
     self.a = A + B
     self.b = B + A
Example #5
0
 def setUp(self):
     self.e = Expression()
     self.e.add_data(123, "456")
     other = Expression()
     other.add_data(0, "1")
     other.add_data(2, "3")
     other.decapitate("iuju")
     for _ in xrange(5):
         self.e.decapitate("nouu")
         self.e += other
Example #6
0
    def setUp(self):
        other = Expression()
        other.decapitate("onelevel")
        self.a = Expression()
        for _ in xrange(5):
            self.a.decapitate("step")
            self.a += other

        other = Expression()
        other.decapitate("onelevel", reverse=True)
        self.b = Expression()
        for _ in xrange(5):
            self.b.decapitate("step")
            self.b += other
Example #7
0
 def test_plus_is_conmutative(self):
     other = Expression()
     other.decapitate("blabla")
     a = self.e + other
     b = other + self.e
     self.assertEqual(make_canonical_expression(a),
                      make_canonical_expression(b))
Example #8
0
 def test_merges2(self):
     other = Expression()
     other.decapitate("blabla")
     oldlen = len(other)
     oldhead = other.get_head()
     other.merge(self.e)
     self.assertEqual(other.get_head(), oldhead)
     self.assertEqual(len(other), oldlen + len(self.e) - 1)
Example #9
0
 def test_merges1(self):
     oldlen = len(self.e)
     oldhead = self.e.get_head()
     other = Expression()
     other.decapitate("blabla")
     self.e.merge(other)
     self.assertEqual(self.e.get_head(), oldhead)
     self.assertEqual(len(self.e), oldlen + len(other) - 1)
Example #10
0
 def setUp(self):
     A = Expression()
     A.add_data("bla", "somedata")
     A.decapitate("hier")
     B = Expression()
     B.add_data("ble", "otherdata")
     B.decapitate("hier")
     C = A + B
     C.decapitate("hier")
     C += B
     C.decapitate("hier")
     self.a = C + A
     D = B + A
     D.decapitate("hier")
     D += B
     D.decapitate("hier")
     self.b = D + A
Example #11
0
 def setUp(self):
     self.e = Expression()
     self.e.add_data("key", "1")
     self.e.decapitate(u"µ")
     self.e.add_data("a", "2")
     self.e.add_data("a", "3")
     self.e.add_data(None, None)
     self.e.decapitate(None)
     self.e.add_data(None, None)
Example #12
0
    def test_fixed_relation(self):
        class MyFixedRelation(FixedRelation):
            relation = u"uranium:blowtorch"

        empty = Expression()
        fixedinstance = MyFixedRelation(empty)

        head = fixedinstance.get_head()
        relations = [x[0] for x in fixedinstance.iter_edges(head)]

        self.assertIn(u"uranium:blowtorch", relations)
Example #13
0
 def test_plus_makes_copy(self):
     other = Expression()
     other.decapitate("blabla")
     a = self.e + other
     self.assertFalse(a is other or self.e is other or a is self.e)
Example #14
0
 def setUp(self):
     self.a = Expression()
     self.b = Expression()
     self.b.decapitate("relation")
Example #15
0
 def setUp(self):
     self.e = Expression()
     self.e.add_data("key", "1")
     self.e.add_data("key", "2")
     self.e.add_data(u"~·~··@↓", None)
     self.e.add_data(None, None)
Example #16
0
 def setUp(self):
     self.a = Expression()
     self.b = Expression()
Example #17
0
    def get_queries(self, question):
        """
        Given `question` in natural language, it returns
        :type self: object
        three things:

        - the target of the query in string format
        - the query
        - metadata given by the regex programmer (defaults to None)

        The queries returned corresponds to the regexes that match in
        weight order.
        """
        """
            parse the question so that we split it if it hase "or" or
            "and" in it
            *( maybe on  "," also  at a later date)
        """

        question = encoding_flexible_conversion(question)
        questions = question.split(" and ", (-1))
        expr = Expression()
        first_time = True
        index = 0
        #print questions
        toBeReturned = ReturnModel(None, None)
        for question in questions:
            for expression, userdata in self._iter_compiled_forms(question):
                if first_time:
                    #print expression.rule_used
                    toBeReturned.rule_used = expression.rule_used
                    #print userdata
                    """
                        -- it wont work for all the question or it will but we need more parrsing --
                        base on the type of the expression.rule_used
                        we can take actions to connect the next expressions
                        to the curent one if they are more
                    """
                    if len(questions) > (index + 1):
                        if expression.rule_used == "WhoAreChildrensOfQuestion":
                            print "**************=Next query=**************************"
                            temp_data = question.split(" ", (-1))
                            print temp_data
                            questions[index + 1] = _new_query_string(
                                temp_data, questions[index + 1], userdata.i,
                                userdata.j)
                            print(questions[index + 1])

                    message = u"Interpretation {1}: {0}"
                    print(message.format(str(expression),
                                         expression.rule_used))
                    first_time = False
                    expr = expression
                    expr.rule_used = expression
                else:
                    """
                      will have to see if there is more to parse form question if there are mor conditions
                      in order to make the next question base on the first query !
                    """
                    expr += expression
        index += 1

        target, query = generation.get_code(expr, self.language)
        toBeReturned.query = query
        yield toBeReturned
        print(u"Query generated: {0}".format(query))
Example #18
0
 def setUp(self):
     self.e = Expression()