示例#1
0
    def test_delete_dictionary_entry(self):
        sym_spell = SymSpell()
        sym_spell.create_dictionary_entry("stea", 1)
        sym_spell.create_dictionary_entry("steama", 2)
        sym_spell.create_dictionary_entry("steem", 3)

        result = sym_spell.lookup("steama", Verbosity.TOP, 2)
        self.assertEqual(1, len(result))
        self.assertEqual("steama", result[0].term)
        self.assertEqual(len("steama"), sym_spell.max_length())
        self.assertEqual(3, sym_spell.word_count())

        self.assertTrue(sym_spell.delete_dictionary_entry("steama"))
        self.assertEqual(len("steem"), sym_spell.max_length())
        self.assertEqual(2, sym_spell.word_count())
        result = sym_spell.lookup("steama", Verbosity.TOP, 2)
        self.assertEqual(1, len(result))
        self.assertEqual("steem", result[0].term)

        self.assertTrue(sym_spell.delete_dictionary_entry("stea"))
        self.assertEqual(len("steem"), sym_spell.max_length())
        self.assertEqual(1, sym_spell.word_count())
        result = sym_spell.lookup("steama", Verbosity.TOP, 2)
        self.assertEqual(1, len(result))
        self.assertEqual("steem", result[0].term)
示例#2
0
    def test_pickle_compressed(self):
        pickle_path = os.path.join(self.fortests_path, "dictionary.pickle")
        edit_distance_max = 2
        prefix_length = 7
        sym_spell = SymSpell(edit_distance_max, prefix_length)
        sym_spell.load_dictionary(self.dictionary_path, 0, 1)
        sym_spell.save_pickle(pickle_path)

        sym_spell_2 = SymSpell(edit_distance_max, prefix_length)
        sym_spell_2.load_pickle(pickle_path)
        self.assertEqual(sym_spell.max_length(), sym_spell_2.max_length())
        self.assertEqual(
            sym_spell.lookup("flam", Verbosity.TOP, 0, True)[0].term,
            sym_spell_2.lookup("flam", Verbosity.TOP, 0, True)[0].term)
        os.remove(pickle_path)
示例#3
0
    def test_create_dictionary(self):
        corpus_path = os.path.join(self.fortests_path, "big_modified.txt")
        big_words_path = os.path.join(self.fortests_path, "big_words.txt")

        edit_distance_max = 2
        prefix_length = 7
        sym_spell = SymSpell(edit_distance_max, prefix_length)
        sym_spell.create_dictionary(corpus_path)
        self.assertEqual(68, sym_spell.max_length())
示例#4
0
def test_save_pickle_symspellcpppy(benchmark):
    sym_spell = SymSpellCpp(max_dictionary_edit_distance=2, prefix_length=7)
    sym_spell.load_dictionary(dict_path,
                              term_index=0,
                              count_index=1,
                              separator=" ")
    os.makedirs("temp_cpppy", exist_ok=True)
    result = benchmark(sym_spell.save_pickle, "temp_cpppy/temp.bin")
    assert (sym_spell.max_length() == 28)
示例#5
0
 def test_save_load(self):
     before_save = self.symSpell.lookup("tke", Verbosity.CLOSEST)[0].term
     before_max_length = self.symSpell.max_length()
     os.makedirs("temp", exist_ok=True)
     self.symSpell.save_pickle("temp/temp.bin")
     load_sym_spell = SymSpell()
     load_sym_spell.load_pickle("temp/temp.bin")
     after_load = load_sym_spell.lookup("tke", Verbosity.CLOSEST)[0].term
     after_max_length = load_sym_spell.max_length()
     os.remove("temp/temp.bin")
     os.rmdir("temp")
     assert (before_save == after_load)
     assert (before_max_length == after_max_length)