示例#1
0
    def test_get_tree_without_filename(self, output):
        configurator = Configurator(self.args)
        tree = configurator.get_tree()

        output_msg = output.getvalue()
        self.assertEqual(
            output_msg, "The bk-tree is building...\n"
            "The bk-tree built successfully\n")
        self.assertEqual(
            tree.tree,
            BKTree(distance, configurator.get_frequency_words()).tree)
示例#2
0
    def test_get_tree_correct_load(self, output):
        configurator = Configurator(self.args_tree)
        tree1 = bk.BuildBKTree.build_tree(["teststring"])
        with open(self.tree_file, 'wb') as file:
            pickle.dump(tree1, file)

        tree2 = configurator.get_tree()

        output = output.getvalue()
        self.assertEqual(
            output, f"The bk-tree is loading from {self.tree_file}...\n"
            "The bk-tree loaded successfully\n")

        self.assertEqual(tree1.tree, tree2.tree)
        self.assertNotEqual(BKTree(distance, ['abcdrasdsf']).tree, tree1.tree)
示例#3
0
    def test_get_tree_without_existing_file_correct_tree(self, output):
        configurator = Configurator(self.args_tree)
        tree = configurator.get_tree()

        output_msg = output.getvalue()
        self.assertEqual(
            output_msg, "The bk-tree is building...\n"
            "The bk-tree built successfully\n"
            f"The bk-tree is saving to {self.tree_file}...\n"
            "The bk-tree saved successfully\n")

        self.assertTrue(os.path.isfile(self.tree_file))
        self.assertEqual(
            BKTree(distance, configurator.get_frequency_words()).tree,
            tree.tree)
        self.assertNotEqual(BKTree(distance, ['abcdrasdsf']).tree, tree.tree)
示例#4
0
    def test_get_tree_with_existing_empty_file(self, output, err):
        configurator = Configurator(self.args_tree)
        file = open(self.tree_file, 'w').close()
        tree = configurator.get_tree()

        output = output.getvalue()
        err = err.getvalue()
        self.assertEqual(
            output, f"The bk-tree is loading from {self.tree_file}...\n"
            "The bk-tree is building...\n"
            "The bk-tree built successfully\n")
        self.assertEqual(
            err,
            "Error: The bk-tree file you specified is empty and can't be loaded\n"
        )
        self.assertEqual(
            BKTree(distance, configurator.get_frequency_words()).tree,
            tree.tree)
        self.assertNotEqual(BKTree(distance, ['abcdrasdsf']).tree, tree.tree)
示例#5
0
    def test_get_tree_with_existing_wrong_pickling_file(self, output, err):
        configurator = Configurator(self.args_tree)
        with open(self.tree_file, 'wb') as file:
            pickle.dump("teststring", file)
        tree = configurator.get_tree()

        output = output.getvalue()
        err = err.getvalue()
        self.assertEqual(
            output, f"The bk-tree is loading from {self.tree_file}...\n"
            "The bk-tree is building...\n"
            "The bk-tree built successfully\n")
        self.assertEqual(
            err,
            "Error: You're trying to load from the file not a BK-tree object\n"
        )
        self.assertEqual(
            BKTree(distance, configurator.get_frequency_words()).tree,
            tree.tree)
        self.assertNotEqual(BKTree(distance, ['abcdrasdsf']).tree, tree.tree)
示例#6
0
    def test_get_tree_with_existing_not_unpickling_file(self, output, err):
        configurator = Configurator(self.args_tree)
        with open(self.tree_file, 'w') as file:
            file.write("teststring")
        tree = configurator.get_tree()

        output = output.getvalue()
        err = err.getvalue()
        self.assertEqual(
            output, f"The bk-tree is loading from {self.tree_file}...\n"
            "The bk-tree is building...\n"
            "The bk-tree built successfully\n")
        self.assertEqual(
            err,
            "Error: This file of the bk-tree structure doesn't contain any bk-tree structure actually\n"
        )
        self.assertEqual(
            BKTree(distance, configurator.get_frequency_words()).tree,
            tree.tree)
        self.assertNotEqual(BKTree(distance, ['abcdrasdsf']).tree, tree.tree)
示例#7
0
    def build(configurator: cfg.Configurator):
        analyzer = WordAnalyzer()
        analyzer.words = configurator.get_words(verbose=True)
        analyzer.frequency_words = configurator.get_frequency_words(
            verbose=True)
        analyzer.splitter = TextSplitter(analyzer.frequency_words)
        analyzer.tree = configurator.get_tree()
        analyzer.mode = configurator.get_mode()
        analyzer.verbose = configurator.get_verbose()

        # This flag specifies that's need add extra information with a word when analyzing words
        analyzer.verbose_file = False

        # If the word isn't in the dictionary, then its cost is default_cost
        analyzer.default_cost = 1000

        # How many similar words will be returned by get_similar_words method
        analyzer.number_similar_words = configurator.get_configuration_values(
        )['similar_words']
        # What distance will be used to search for similar words
        analyzer.distance = configurator.get_configuration_values()['distance']
        # How many parts will be spliced in the get_correct_words method
        analyzer.threshold = configurator.get_configuration_values(
        )['threshold']
        # How many words will be returned by get_correct_words method
        analyzer.number_of_corrected_words = configurator.get_configuration_values(
        )['number_corrected']

        analyzer.max_len = max(len(x) for x in analyzer.words)
        # Define dictionary with values like (number: list_of_divisors).
        # There are defined all numbers from 1 to max length of all words.
        # It's necessary to improve efficiency, because divisors won't calculated again for same number
        analyzer.divisors = dict((number, factorize(number))
                                 for number in range(1, analyzer.max_len + 1))

        return analyzer