generated from eigerco/beerus
-
Notifications
You must be signed in to change notification settings - Fork 103
/
levenshtein_distance.cairo
67 lines (58 loc) · 2.54 KB
/
levenshtein_distance.cairo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// The Levenshtein Distance
use core::dict::Felt252DictTrait;
/// Compute the edit distance between two byte arrays
/// * `arr1` - The first byte array.
/// * `arr2` - The second byte array.
/// # Returns
/// * `usize` - The edit distance between the two byte arrays.
pub fn levenshtein_distance(arr1: @ByteArray, arr2: @ByteArray) -> usize {
// Get the lengths of both arrays
let arr1_len = arr1.len();
let arr2_len = arr2.len();
// If the first array is empty, the distance is the length of the second array
if arr1_len == 0 {
return arr2_len;
}
// Initialize a dictionary to store previous distances, with keys and values as indices
let mut prev_distances = Default::default();
let mut index: usize = 0;
// Break the loop when index equals the length of the first array plus 1
while index != arr1_len + 1 {
prev_distances.insert(index.into(), index);
index += 1;
};
// Initialize a variable to keep track of the current row
let mut current_row: usize = 0;
// Break the loop when current row equals the length of the second array
while current_row != arr2_len {
let second_array_element = arr2.at(current_row).unwrap();
let mut previous_substitution_cost = prev_distances.get(0);
prev_distances.insert(0, current_row + 1);
// Initialize a variable to keep track of the current column
let mut current_column: usize = 0;
// Break the loop when current column equals the length of the first array
while current_column != arr1_len {
let first_array_element = arr1.at(current_column).unwrap();
let deletion_cost = prev_distances.get(current_column.into()) + 1;
let insertion_cost = prev_distances.get((current_column + 1).into()) + 1;
let substitution_cost = if first_array_element == second_array_element {
previous_substitution_cost
} else {
previous_substitution_cost + 1
};
previous_substitution_cost = prev_distances.get((current_column + 1).into());
let min_cost = if insertion_cost < deletion_cost {
insertion_cost
} else if substitution_cost < deletion_cost {
substitution_cost
} else {
deletion_cost
};
prev_distances.insert((current_column + 1).into(), min_cost);
current_column += 1
};
current_row += 1;
};
// Return the Levenshtein distance
prev_distances.get(arr1_len.into())
}