aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorScott Jackson <daneren2005@gmail.com>2014-07-25 23:03:56 -0700
committerScott Jackson <daneren2005@gmail.com>2014-07-25 23:03:56 -0700
commit8356e838140c6bb271b138f656a08e2185ec2de5 (patch)
treea85017b6d15086654017cba5139ea58c8501d39c /src
parent7bffdf4bb6242ff744b621d202aab5d6c0f6fdfe (diff)
downloaddsub-8356e838140c6bb271b138f656a08e2185ec2de5.tar.gz
dsub-8356e838140c6bb271b138f656a08e2185ec2de5.tar.bz2
dsub-8356e838140c6bb271b138f656a08e2185ec2de5.zip
#380 Sort search results by how close they are to query
Diffstat (limited to 'src')
-rw-r--r--src/github/daneren2005/dsub/provider/DSubSearchProvider.java28
-rw-r--r--src/github/daneren2005/dsub/util/Util.java52
2 files changed, 69 insertions, 11 deletions
diff --git a/src/github/daneren2005/dsub/provider/DSubSearchProvider.java b/src/github/daneren2005/dsub/provider/DSubSearchProvider.java
index bac05a51..1da614af 100644
--- a/src/github/daneren2005/dsub/provider/DSubSearchProvider.java
+++ b/src/github/daneren2005/dsub/provider/DSubSearchProvider.java
@@ -25,6 +25,11 @@ import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+
import github.daneren2005.dsub.R;
import github.daneren2005.dsub.domain.Artist;
import github.daneren2005.dsub.domain.MusicDirectory;
@@ -32,6 +37,7 @@ import github.daneren2005.dsub.domain.SearchCritera;
import github.daneren2005.dsub.domain.SearchResult;
import github.daneren2005.dsub.service.MusicService;
import github.daneren2005.dsub.service.MusicServiceFactory;
+import github.daneren2005.dsub.util.Util;
/**
* Provides search suggestions based on recent searches.
@@ -76,17 +82,17 @@ public class DSubSearchProvider extends ContentProvider {
// Add all results into one pot
List<Object> results = new ArrayList<Object>();
results.addAll(searchResult.getArtists());
- results.addAll(searchResults.getAlbums());
- results.addAll(searchResults.getSongs());
+ results.addAll(searchResult.getAlbums());
+ results.addAll(searchResult.getSongs());
// For each, calculate its string distance to the query
for(Object obj: results) {
if(obj instanceof Artist) {
Artist artist = (Artist) obj;
- artist.setCloseness(Util.getStringDistance(query, artist.getName());
+ artist.setCloseness(Util.getStringDistance(query, artist.getName()));
} else {
MusicDirectory.Entry entry = (MusicDirectory.Entry) obj;
- entry.setCloseness(Util.getStringDistance(query, entry.getTitle());
+ entry.setCloseness(Util.getStringDistance(query, entry.getTitle()));
}
}
@@ -96,23 +102,23 @@ public class DSubSearchProvider extends ContentProvider {
public int compare(Object lhs, Object rhs) {
// Get the closeness of the two objects
int left, right;
- if(lhs instanceof Artist) {
+ if (lhs instanceof Artist) {
left = ((Artist) lhs).getCloseness();
} else {
left = ((MusicDirectory.Entry) lhs).getCloseness();
}
- if(rhs instanceof Artist) {
+ if (rhs instanceof Artist) {
right = ((Artist) rhs).getCloseness();
} else {
right = ((MusicDirectory.Entry) rhs).getCloseness();
}
-
- if(left == right) {
+
+ if (left == right) {
return 0;
- } else if(left > right) {
- return -1
- } else {
+ } else if (left > right) {
return 1;
+ } else {
+ return -1;
}
}
});
diff --git a/src/github/daneren2005/dsub/util/Util.java b/src/github/daneren2005/dsub/util/Util.java
index a4ed527e..10218d65 100644
--- a/src/github/daneren2005/dsub/util/Util.java
+++ b/src/github/daneren2005/dsub/util/Util.java
@@ -839,6 +839,58 @@ public final class Util {
return Util.getScaledHeight((double) bitmap.getHeight(), (double) bitmap.getWidth(), width);
}
+ public static int getStringDistance(CharSequence s, CharSequence t) {
+ if (s == null || t == null) {
+ throw new IllegalArgumentException("Strings must not be null");
+ }
+
+ int n = s.length();
+ int m = t.length();
+
+ if (n == 0) {
+ return m;
+ } else if (m == 0) {
+ return n;
+ }
+
+ if (n > m) {
+ final CharSequence tmp = s;
+ s = t;
+ t = tmp;
+ n = m;
+ m = t.length();
+ }
+
+ int p[] = new int[n + 1];
+ int d[] = new int[n + 1];
+ int _d[];
+
+ int i;
+ int j;
+ char t_j;
+ int cost;
+
+ for (i = 0; i <= n; i++) {
+ p[i] = i;
+ }
+
+ for (j = 1; j <= m; j++) {
+ t_j = t.charAt(j - 1);
+ d[0] = j;
+
+ for (i = 1; i <= n; i++) {
+ cost = s.charAt(i - 1) == t_j ? 0 : 1;
+ d[i] = Math.min(Math.min(d[i - 1] + 1, p[i] + 1), p[i - 1] + cost);
+ }
+
+ _d = p;
+ p = d;
+ d = _d;
+ }
+
+ return p[n];
+ }
+
public static boolean isNetworkConnected(Context context) {
return isNetworkConnected(context, false);
}