Improve tag search

This commit is contained in:
NekoInverter committed 2020-10-30 19:47:07 +08:00
1 parent 508a5964be
commit 06d31b81c9
2 files changed
+30 -17

No files matched your search

@@ -358,12 +358,12 @@ public class EhTagDatabase {
return null;
}
public ArrayList<Pair<String, String>> getTagFromTranslation(String translation) {
return searchTag(tags, translation);
public ArrayList<Pair<String, String>> suggest(String keyword) {
return searchTag(tags, keyword);
}
@Nullable
private ArrayList<Pair<String, String>> searchTag(byte[] tags, String translation) {
private ArrayList<Pair<String, String>> searchTag(byte[] tags, String keyword) {
ArrayList<Pair<String, String>> searchHints = new ArrayList<>();
int begin = 0;
while (begin < tags.length - 1) {
@@ -390,19 +390,19 @@ public class EhTagDatabase {
byte[] hintBytes = new byte[end - middle - 1];
System.arraycopy(tags, start + middle + 1, hintBytes, 0, end - middle - 1);
String hint = new String(hintBytes, TextUrl.UTF_8);
byte[] keywordBytes = new byte[middle];
System.arraycopy(tags, start + 1, keywordBytes, 0, middle);
String keyword = new String(keywordBytes, TextUrl.UTF_8);
int index = keyword.indexOf(':');
byte[] tagBytes = new byte[middle];
System.arraycopy(tags, start + 1, tagBytes, 0, middle);
String tag = new String(tagBytes, TextUrl.UTF_8);
int index = tag.indexOf(':');
boolean keywordMatches;
if (index == -1 || index >= keyword.length() - 1) {
keywordMatches = keyword.contains(translation);
if (index == -1 || index >= tag.length() - 1 || keyword.length() > 2) {
keywordMatches = containsIgnoreSpace(tag, keyword);
} else {
keywordMatches = keyword.substring(index + 1).contains(translation);
keywordMatches = containsIgnoreSpace(tag.substring(index + 1), keyword);
}
if (hint.contains(translation) || keywordMatches) {
Pair<String, String> pair = new Pair<>(hint, keyword);
if (keywordMatches || containsIgnoreSpace(hint, keyword)) {
Pair<String, String> pair = new Pair<>(hint, tag);
if (!searchHints.contains(pair)) {
searchHints.add(pair);
}
@@ -414,4 +414,8 @@ public class EhTagDatabase {
}
return searchHints;
}
private boolean containsIgnoreSpace(String text, String key){
return text.replace(" ","").contains(key.replace(" ",""));
}
}
@@ -171,12 +171,16 @@ public class SearchBar extends MaterialCardView implements View.OnClickListener,
}
EhTagDatabase ehTagDatabase = EhTagDatabase.getInstance(getContext());
if (!TextUtils.isEmpty(text) && ehTagDatabase != null && !text.endsWith(" ")) {
String[] s = text.split(" ");
if (s != null && s.length > 0) {
String keyword = s[s.length - 1];
ArrayList<Pair<String, String>> searchHints = ehTagDatabase.suggest(keyword);
if (!TextUtils.isEmpty(text) && ehTagDatabase != null) {
ArrayList<Pair<String, String>> searchHints = ehTagDatabase.getTagFromTranslation(text);
for (Pair<String, String> searchHint : searchHints) {
mSuggestionList.add(new TagSuggestion(searchHint.first, searchHint.second));
}
for (Pair<String, String> searchHint : searchHints) {
mSuggestionList.add(new TagSuggestion(searchHint.first, searchHint.second));
}
}
@@ -573,7 +577,12 @@ public class SearchBar extends MaterialCardView implements View.OnClickListener,
@Override
public void onClick() {
mEditText.setText(wrapTagKeyword(mKeyword));
String text = mEditText.getText().toString();
String temp = wrapTagKeyword(mKeyword) + " ";
if (text.contains(" ")) {
temp = text.substring(0, text.lastIndexOf(" ")) + " " + temp;
}
mEditText.setText(temp);
mEditText.setSelection(mEditText.getText().length());
}