-
-
Notifications
You must be signed in to change notification settings - Fork 347
Expand file tree
/
Copy pathlevenshtein.r
More file actions
38 lines (31 loc) · 1005 Bytes
/
levenshtein.r
File metadata and controls
38 lines (31 loc) · 1005 Bytes
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
# Levenshtein Distance in R
# Calculates minimum number of edits (insert, delete, replace)
# to convert one string into another and prints the distance.
levenshtein_distance <- function(s1, s2) {
n <- nchar(s1)
m <- nchar(s2)
# Initialize DP matrix
dp <- matrix(0, nrow = n + 1, ncol = m + 1)
for (i in 0:n) dp[i + 1, 1] <- i
for (j in 0:m) dp[1, j + 1] <- j
# Fill DP table
for (i in 1:n) {
for (j in 1:m) {
if (substr(s1, i, i) == substr(s2, j, j)) {
dp[i + 1, j + 1] <- dp[i, j]
} else {
dp[i + 1, j + 1] <- min(
dp[i, j + 1] + 1, # Deletion
dp[i + 1, j] + 1, # Insertion
dp[i, j] + 1 # Substitution
)
}
}
}
return(dp[n + 1, m + 1])
}
# Interactive input
s1 <- tolower(readline("Enter first string: "))
s2 <- tolower(readline("Enter second string: "))
distance <- levenshtein_distance(s1, s2)
cat("Levenshtein distance between '", s1, "' and '", s2, "' is:", distance, "\n")