def main(): """ Simple demo of the Levenshtein class. """ print("-----------------------------") print("| codedrome.com |") print("| Levenshtein Word Distance |") print("-----------------------------\n") source_words = ["banama", "banama", "levinstein"] target_words = ["banana", "elephant", "levenshtein"] lp = levenshtein.Levenshtein() for i in range(0, len(source_words)): lp.source_word = source_words[i] lp.target_word = target_words[i] lp.calculate() lp.print_grid() lp.print_cost() print("")
def __init__(self, fp): with open(fp) as f: self.codon_data = json.load(f) self.search_space = set() self.inverse_codon_data = {} for amino_acid in self.codon_data: self.search_space.add(amino_acid.lower()) for acid_info in self.codon_data[amino_acid]: data_entry = self.codon_data[amino_acid][acid_info] if type(data_entry) == list: for info in data_entry: self.search_space.add(info.lower()) if info in self.inverse_codon_data: self.inverse_codon_data[info.lower()].append(amino_acid) else: self.inverse_codon_data[info.lower()] = [amino_acid] else: self.search_space.add(data_entry.lower()) key = data_entry.lower() if key in self.inverse_codon_data: self.inverse_codon_data[key].append(amino_acid) else: self.inverse_codon_data[key] = [amino_acid] self.search_space = list(self.search_space) #print(len(self.search_space), str(self.search_space)) self.levenshtein_engine = levenshtein.Levenshtein(u=(7, 1, 4), t=(7, 1, 4))
def test_Levenshtein_class(alphabet, symbol, weight_dict, language, expected_result): levenshtein_class = levenshtein.Levenshtein(alphabet, symbol, weight_dict, language) assert levenshtein_class.weight_dict[ 'a'] == expected_result and levenshtein_class.weight_dict['?'] == (1, 1, 1)
def test_weighted_distance(source_input, target_input, weight_dict, expected_result): metric = levenshtein.Levenshtein('ab', '.', weight_dict) assert metric.distance(source_input, target_input) == expected_result
def test_iterative_matrix(source_input, target_input, expected_result): metric = levenshtein.Levenshtein('a', '.') assert metric.iterative_matrix(source_input, target_input) == expected_result
def test_Levenshtein_class_mixed_params(letter, a_symbol): with pytest.raises(Exception) as e_info: levenshtein_class = levenshtein.Levenshtein(alphabet=a_symbol, symbol=letter)
def test_Levenshtein_class_only_symbol(a_symbol): with pytest.raises(Exception) as e_info: levenshtein_class = levenshtein.Levenshtein(symbol=a_symbol)
def test_Levenshtein_class_only_alphabet(letter): with pytest.raises(Exception) as e_info: levenshtein_class = levenshtein.Levenshtein(alphabet=letter)
def test_Levenshtein_class_no_parameter(): with pytest.raises(Exception) as e_info: levenshtein_class = levenshtein.Levenshtein()
def test_weighted_similarity(source_input, target_input, expected_result): metric = levenshtein.Levenshtein('ab', '.') assert metric.similarity(source_input, target_input) == pytest.approx(expected_result, abs=5e-3)