Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/me/xdrop/fuzzywuzzy/algorithms/Utils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package me.xdrop.fuzzywuzzy.algorithms;

import me.xdrop.fuzzywuzzy.FuzzySearch;

import java.util.*;

final public class Utils {
Expand All @@ -25,6 +27,32 @@ static String sortAndJoin(List<String> col, String sep){

}

//sort tokens2 with reference to tokens1
static String similaritySortAndJoin(String[] tokens1, String[] tokens2, String sep) {
for (int i = 0; i < tokens1.length; i++) {
final int[] distances = new int[tokens2.length];

for (int j = i; j < tokens2.length; j++) {
distances[j] = FuzzySearch.ratio(tokens1[i], tokens2[j]);
}
//find max match token
int max = 0, maxpos = 0;

for (int a = i; a < distances.length; a++) {
if (distances[a] > max) {
maxpos = a;
max = distances[a];
}
}
//swap
final String tmp = tokens2[maxpos];
tokens2[maxpos] = tokens2[i];
tokens2[i] = tmp;
}
//join
return join(Arrays.asList(tokens2), sep);
}

static String join(List<String> strings, String sep) {
final StringBuilder buf = new StringBuilder(strings.size() * 16);

Expand Down