You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Loop through each character in the source string
63
74
foriin1...source.count {
75
+
64
76
current[0]= i
77
+
78
+
// Calculate cost: 0 if characters match, 1 otherwise
65
79
forjin1...target.count {
80
+
81
+
// Compute the minimum edit distance considering insertion, deletion, and substitution
66
82
letcost=source[i -1]==target[j -1]?0:1
83
+
67
84
current[j]=Swift.min(
68
85
current[j -1]+1, // insertion
69
86
previous[j]+1, // deletion
70
87
previous[j -1]+ cost // substitution
71
88
)
72
89
}
90
+
91
+
// Move current row to previous for next iteration
73
92
swap(&previous,¤t)
74
93
}
75
94
95
+
// Final distance is the last value in the previous row
76
96
returnprevious[target.count]
77
97
}
78
98
}
79
99
80
100
extensionDictionarywhere Key ==String, Value ==DynamicJSON{
101
+
102
+
/// Note: The Levenshtein distance is a metric for measuring the difference between two strings. Specifically, it calculates the minimum number of single-character edits required to transform one string into the other
81
103
/// Attempts to find the best-matching key in the dictionary for a given lookup key.
82
104
///
83
105
/// Matching is performed in the following order:
@@ -90,34 +112,43 @@ extension Dictionary where Key == String, Value == DynamicJSON {
90
112
/// - logMatch: A closure used to report fallback key matches for debugging.
91
113
/// - Returns: The best matching `DynamicJSON` value, or `nil` if no reasonable match found.
0 commit comments