Ejemplo n.º 1
0
    def runTest(self):

        root = ['abcd']  # needs to be mutable for nested access, stupid python
        tree = BKTree.create_tree(root[0])

        def _check_insert(node, tree_depth):
            BKTree.insert(tree, root[0], node, get_distance)
            self.assertEqual(BKTree.get_depth(tree, root[0]), tree_depth)

        def _check_query(query, max_d, num_hit, true_result):
            result, perc_hit = BKTree.find(tree,
                                           root[0],
                                           query,
                                           max_d,
                                           get_distance,
                                           profile=True)
            self.assertEqual(set(result), set(true_result))
            self.assertEqual(perc_hit, num_hit / 7.0)

        # 0 indexed depth
        self.assertEqual(BKTree.get_depth(tree, root[0]), 0)

        #inserts
        nodes = ['abcde', 'bcde', 'abc', 'bc', 'bbcde', 'abcz']
        tree_depths = [1, 1, 2, 2, 2, 3]
        map(_check_insert, nodes, tree_depths)

        #queries
        _check_query('abc', 0, 3, ['abc'])
        _check_query('abc', 1, 6, ['abcd', 'abc', 'abcz', 'bc'])
        _check_query('abcd', 2, 7,
                     ['abcd', 'abcde', 'abc', 'abcz', 'bcde', 'bc', 'bbcde'])
        _check_query('bc', 1, 7, ['bc', 'abc'])

        self.assertEqual(BKTree.get_average_distance(tree), 1.5)
Ejemplo n.º 2
0
    def runTest(self):

        root = ['abcd']  # needs to be mutable for nested access, stupid python
        tree = BKTree.create_tree(root[0])

        def _check_insert(node, tree_depth):
            BKTree.insert(tree, root[0], node, get_distance)
            self.assertEqual(BKTree.get_depth(tree, root[0]), tree_depth)          

        def _check_query(query, max_d, num_hit, true_result):
            result, perc_hit = BKTree.find(tree, root[0], query, max_d, get_distance, profile = True) 
            self.assertEqual(set(result), set(true_result))
            self.assertEqual(perc_hit, num_hit / 7.0)        

        # 0 indexed depth
        self.assertEqual(BKTree.get_depth(tree, root[0]), 0)

        #inserts
        nodes = ['abcde', 'bcde', 'abc', 'bc', 'bbcde', 'abcz']
        tree_depths = [1, 1, 2, 2, 2, 3]
        map(_check_insert, nodes, tree_depths)

        #queries
        _check_query('abc', 0, 3, ['abc'])
        _check_query('abc', 1, 6, ['abcd', 'abc', 'abcz', 'bc'])
        _check_query('abcd', 2, 7, ['abcd', 'abcde', 'abc', 'abcz', 'bcde', 'bc', 'bbcde'])
        _check_query('bc', 1, 7, ['bc', 'abc'])

        self.assertEqual(BKTree.get_average_distance(tree), 1.5)
Ejemplo n.º 3
0
class BrokenPhone:
    def __init__(self):
        self._tree = BKTree()

    def load(self, path):
        file = open(path, 'r')
        for word in file:
            self._tree.insert(word.rstrip())
Ejemplo n.º 4
0
 def _check_query(query, max_d, num_hit, true_result):
     result, perc_hit = BKTree.find(tree,
                                    root[0],
                                    query,
                                    max_d,
                                    get_distance,
                                    profile=True)
     self.assertEqual(set(result), set(true_result))
     self.assertEqual(perc_hit, num_hit / 7.0)
Ejemplo n.º 5
0
import sys

from Astar import Astar
from BKTree import BKTree

from BrokenPhone import BrokenPhone

bk_tree = BKTree()
file = open(sys.argv[1], 'r')
print("Start loading file!")
for word in file:
    bk_tree.insert(word.rstrip())
print("End loading file!")

astar = Astar()
astar.search(bk_tree, "life", "death")

for item in astar.getChain():
    print(item)
Ejemplo n.º 6
0
 def _check_insert(node, tree_depth):
     BKTree.insert(tree, root[0], node, get_distance)
     self.assertEqual(BKTree.get_depth(tree, root[0]), tree_depth)
Ejemplo n.º 7
0
def gen_bktree(words):
    """Generate BKTree from words."""
    return BKTree(words)
Ejemplo n.º 8
0
 def _check_query(query, max_d, num_hit, true_result):
     result, perc_hit = BKTree.find(tree, root[0], query, max_d, get_distance, profile = True) 
     self.assertEqual(set(result), set(true_result))
     self.assertEqual(perc_hit, num_hit / 7.0)        
Ejemplo n.º 9
0
 def _check_insert(node, tree_depth):
     BKTree.insert(tree, root[0], node, get_distance)
     self.assertEqual(BKTree.get_depth(tree, root[0]), tree_depth)          
Ejemplo n.º 10
0
 def __init__(self):
     self._tree = BKTree()
Ejemplo n.º 11
0
from BKTree import BKTree

tree = BKTree()

tree.insert("food")
tree.insert("good")
tree.insert("cook")
tree.insert("fowl")
tree.insert("spoon")
tree.insert("fork")

print(tree._tree)
print(tree.search("aork", 1))