Skip to content

Commit

Permalink
Add Levenshtein algorithm to guess possible repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Kane committed May 14, 2015
1 parent 0b0dcc9 commit d7c379c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/rb/LevenshteinDistance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package rb;

/**
* Created by IntelliJ IDEA.
* User: gongze
* Date: 5/14/2015
* Time: 1:26 PM
*/
public class LevenshteinDistance {
private static int LevenshteinDistance(String s, String t) {
if (s.equals(t)) {
return 0;
}
if (s.length() == 0) {
return t.length();
}
if (t.length() == 0) {
return s.length();
}

int[] v0 = new int[t.length() + 1];
int[] v1 = new int[t.length() + 1];
for (int i = 0; i < v0.length; i++)
v0[i] = i;
for (int i = 0; i < s.length(); i++) {
v1[0] = i + 1;
for (int j = 0; j < t.length(); j++) {
int cost = s.charAt(i) == t.charAt(j) ? 0 : 1;
int middle = Math.min(v1[j] + 1, v0[j + 1] + 1);
v1[j + 1] = Math.min(middle, v0[j] + cost);
}
for (int j = 0; j < v0.length; j++) {
v0[j] = v1[j];
}
}


return v1[t.length()];
}

public static int getClosest(String target, String[] candidates) {
int minimal = Integer.MAX_VALUE;
int minimalIndex = -1;
for (int i = 0; i < candidates.length; i++) {
int distance = LevenshteinDistance(target, candidates[i]);
if (distance < minimal) {
minimal = distance;
minimalIndex = i;
}

}
return minimalIndex;
}
}
10 changes: 10 additions & 0 deletions src/rb/PostReviewAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;

import java.net.URI;

/**
* Created by IntelliJ IDEA.
* User: Gong Zeng
Expand Down Expand Up @@ -246,6 +248,14 @@ private int getPossibleRepoIndex(@NotNull String repositoryUrl, Repository[] rep
}
}
}
if (possibleRepoIndex == -1) {
String path = URI.create(repositoryUrl).getPath();
String[] repos = new String[repositories.length];
for (int i = 0; i < repos.length; i++) {
repos[i] = repositories[i].name;
}
possibleRepoIndex = LevenshteinDistance.getClosest(path, repos);
}

return possibleRepoIndex;
}
Expand Down

0 comments on commit d7c379c

Please sign in to comment.