Пример #1
0
 def test_auto_name_simple_op(self):
     for OpCls in AndOperation, OrOperation, UnknownOperation:
         with self.subTest("operation %r" % OpCls):
             tree = OpCls(
                 Word("test"),
                 Phrase('"test"'),
             )
             auto_name(tree)
             self.assertEqual(get_name(tree), "0")
             self.assertEqual(get_name(tree.children[0]), "0_0")
             self.assertEqual(get_name(tree.children[1]), "0_1")
Пример #2
0
    def test_auto_name_one_term(self):
        tree = Word("test")
        auto_name(tree)
        self.assertEqual(get_name(tree), "0")

        tree = Phrase('"test"')
        auto_name(tree)
        self.assertEqual(get_name(tree), "0")

        tree = Range("test", "*")
        auto_name(tree)
        self.assertEqual(get_name(tree), "0")
Пример #3
0
 def test_auto_name_simple_op(self):
     for OpCls in AndOperation, OrOperation, UnknownOperation:
         with self.subTest("operation %r" % OpCls):
             tree = OpCls(
                 Word("test"),
                 Phrase('"test"'),
             )
             names = auto_name(tree)
             self.assertEqual(get_name(tree), None)
             self.assertEqual(get_name(tree.children[0]), "a")
             self.assertEqual(get_name(tree.children[1]), "b")
             self.assertEqual(names, {"a": (0, ), "b": (1, )})
Пример #4
0
def names_to_path(node, path=()):
    names = {}
    node_name = get_name(node)
    if node_name:
        names[node_name] = path
    for i, child in enumerate(node.children):
        names.update(names_to_path(child, path=path + (i, )))
    return names
Пример #5
0
    def test_auto_name_one_term(self):
        tree = Word("test")
        names = auto_name(tree)
        self.assertEqual(get_name(tree), "a")
        self.assertEqual(names, {"a": ()})

        tree = Phrase('"test"')
        names = auto_name(tree)
        self.assertEqual(get_name(tree), "a")
        self.assertEqual(names, {"a": ()})

        tree = Range(Word("test"), Word("*"))
        names = auto_name(tree)
        self.assertEqual(get_name(tree), "a")
        self.assertEqual(names, {"a": ()})

        tree = Regex("/test/")
        names = auto_name(tree)
        self.assertEqual(get_name(tree), "a")
        self.assertEqual(names, {"a": ()})
Пример #6
0
    def test_auto_name_nested(self):
        tree = AndOperation(
            OrOperation(
                SearchField("bar", Word("test")),
                AndOperation(
                    Proximity(Phrase('"test"'), 2),
                    SearchField("baz", Word("test")),
                ),
            ),
            Group(
                UnknownOperation(
                    Fuzzy(Word("test")),
                    Phrase('"test"'),
                ),
            ),
        )

        auto_name(tree)
        # and
        and1 = tree
        self.assertEqual(get_name(and1), "0")
        # - or
        or1 = and1.children[0]
        self.assertEqual(get_name(or1), "0_0")
        # --- search field word
        sfield1 = or1.children[0]
        self.assertFalse(get_name(sfield1))
        self.assertEqual(get_name(sfield1.expr), "0_0_0")
        # --- and
        and2 = or1.children[1]
        self.assertEqual(get_name(and2), "0_0_1")
        # ----- proximity phrase
        self.assertEqual(get_name(and2.children[0].term), "0_0_1_0")
        # ----- search field word
        sfield2 = and2.children[1]
        self.assertFalse(get_name(sfield2))
        self.assertEqual(get_name(sfield2.expr), "0_0_1_1")
        # - group
        group1 = and1.children[1]
        self.assertEqual(get_name(group1), None)
        # --- unknown op
        unknownop1 = group1.children[0]
        self.assertEqual(get_name(unknownop1), "0_1")
        # ----- fuzzy word
        self.assertEqual(get_name(unknownop1.children[0].term), "0_1_0")
        # ----- phrase
        self.assertEqual(get_name(unknownop1.children[1]), "0_1_1")
Пример #7
0
def paths_to_names(tree, paths):
    return {get_name(element_from_path(tree, path)) for path in paths}
Пример #8
0
 def test_auto_name_nested(self):
     tree = AndOperation(
         OrOperation(
             SearchField("bar", Word("test")),
             AndOperation(
                 Proximity(Phrase('"test"'), 2),
                 SearchField("baz", Word("test")),
             ),
         ),
         Group(UnknownOperation(
             Fuzzy(Word("test")),
             Phrase('"test"'),
         ), ),
     )
     names = auto_name(tree)
     self.assertEqual(sorted(names.keys()), list("abcdefgh"))
     # and
     and1 = tree
     self.assertEqual(get_name(and1), None)
     # - or
     or1 = and1.children[0]
     self.assertEqual(get_name(or1), "a")
     self.assertEqual(names["a"], (0, ))
     # -- search field
     sfield1 = or1.children[0]
     self.assertEqual(get_name(sfield1), "c")
     self.assertEqual(names["c"], (0, 0))
     self.assertEqual(get_name(sfield1.expr), None)
     # -- and
     and2 = or1.children[1]
     self.assertEqual(get_name(and2), "d")
     self.assertEqual(names["d"], (0, 1))
     # --- proximity phrase
     self.assertEqual(get_name(and2.children[0]), "e")
     self.assertEqual(names["e"], (0, 1, 0))
     self.assertEqual(get_name(and2.children[0].term), None)
     # --- search field
     sfield2 = and2.children[1]
     self.assertEqual(get_name(sfield2), "f")
     self.assertEqual(names["f"], (0, 1, 1))
     self.assertEqual(get_name(sfield2.expr), None)
     # - group
     group1 = and1.children[1]
     self.assertEqual(get_name(group1), "b")
     self.assertEqual(names["b"], (1, ))
     # -- unknown op
     unknownop1 = group1.children[0]
     self.assertEqual(get_name(unknownop1), None)
     # --- fuzzy word
     self.assertEqual(get_name(unknownop1.children[0]), "g")
     self.assertEqual(names["g"], (1, 0, 0))
     self.assertEqual(get_name(unknownop1.children[0].term), None)
     # --- phrase
     self.assertEqual(get_name(unknownop1.children[1]), "h")
     self.assertEqual(names["h"], (1, 0, 1))