-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLevenshtein.java
More file actions
109 lines (101 loc) · 3.17 KB
/
Copy pathLevenshtein.java
File metadata and controls
109 lines (101 loc) · 3.17 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* Copyright (c) 2025 - 2026 PCazzaniga (github.com)
*
* Levenshtein.java is part of SIMPLE.
*
* SIMPLE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SIMPLE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with SIMPLE. If not, see <http://www.gnu.org/licenses/>.
*/
import static java.lang.Math.abs;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
class Levenshtein {
public static boolean distanceLoE(String first, String second, int max){
if (max < 0) return false;
if (first.equals(second)) return true;
int lenF = first.length();
int lenS = second.length();
if(abs(lenF - lenS) > max) return false;
if (lenF == 0) return lenS <= max;
if (lenS == 0) return lenF <= max;
if (lenF < lenS) {
int tempLen = lenF;
lenF = lenS;
lenS = tempLen;
String tempStr = first;
first = second;
second = tempStr;
}
int[] cost = new int[lenS + 1];
for (int i = 0; i <= lenS; i++) cost[i] = i;
for (int i = 1; i <= lenF; i++) {
cost[0] = i;
int prev = i-1;
int min = prev;
for (int j = 1; j <= lenS; j++) {
int act = prev;
if (first.charAt(i-1) != second.charAt(j-1)) act++;
prev = cost[j];
cost[j] = min(prev + 1, cost[j-1] + 1, act);
if (prev < min) min = prev;
}
if (min > max) return false;
}
return cost[lenS] <= max;
}
public static int distance(String first, String second) {
if (first.equals(second)) return 0;
int lenF = first.length();
int lenS = second.length();
if (lenF == 0) return lenS;
if (lenS == 0) return lenF;
if (lenF < lenS) {
int tempL = lenF;
lenF = lenS;
lenS = tempL;
String tempS = first;
first = second;
second = tempS;
}
int[] cost = new int[lenS+1];
for (int i=0; i<=lenS; i+=1) cost[i] = i;
for (int i=1; i<=lenF; i+=1) {
cost[0] = i;
int prev = i-1;
int min = prev;
for (int j=1; j<=lenS; j+=1) {
int act = prev;
if (first.charAt(i-1) != second.charAt(j-1)) act++;
prev = cost[j];
cost[j] = min(cost[j] + 1, cost[j-1] + 1, act);
if (prev < min) min = prev;
}
}
return cost[lenS];
}
private static int min(int a, int b, int c) {
int min = a;
if (b < min) min = b;
if (c < min) min = c;
return min;
}
public static List<String> filterByMaxDistance(String matcher, List<String> targets, int max){
return targets.stream().filter(str -> distanceLoE(matcher, str, max)).toList();
}
public static List<String> sortByDistance(String matcher, List<String> targets){
List<String> copy = new ArrayList<>(targets);
copy.sort(Comparator.comparingInt(str -> distance(matcher, str)));
return copy;
}
}