Remove diacrits from index/query

pull/209/head
M66B 2 years ago
parent 1ddff2764f
commit df3c2ea147

@ -345,23 +345,21 @@ public class BoundaryCallbackMessages extends PagedList.BoundaryCallback<TupleMe
} }
if (!matched && criteria.in_subject) { if (!matched && criteria.in_subject) {
if (message.subject != null && if (contains(message.subject, query))
message.subject.toLowerCase().contains(query))
matched = true; matched = true;
} }
if (!matched && criteria.in_keywords) { if (!matched && criteria.in_keywords) {
if (message.keywords != null) if (message.keywords != null)
for (String keyword : message.keywords) for (String keyword : message.keywords)
if (keyword.toLowerCase().contains(query)) { if (contains(keyword, query)) {
matched = true; matched = true;
break; break;
} }
} }
if (!matched && criteria.in_notes) { if (!matched && criteria.in_notes) {
if (message.notes != null && if (contains(message.notes, query))
message.notes.toLowerCase().contains(query))
matched = true; matched = true;
} }
@ -370,10 +368,9 @@ public class BoundaryCallbackMessages extends PagedList.BoundaryCallback<TupleMe
File file = EntityMessage.getFile(context, id); File file = EntityMessage.getFile(context, id);
if (file.exists()) { if (file.exists()) {
String html = Helper.readText(file); String html = Helper.readText(file);
if (html.toLowerCase().contains(query)) { if (contains(html, query)) {
String text = HtmlHelper.getFullText(html); String text = HtmlHelper.getFullText(html);
if (text != null && if (contains(text, query))
text.toLowerCase().contains(query))
matched = true; matched = true;
} }
} }
@ -782,15 +779,26 @@ public class BoundaryCallbackMessages extends PagedList.BoundaryCallback<TupleMe
return imessages; return imessages;
} }
private static boolean contains(Address[] addresses, String text) { private static boolean contains(Address[] addresses, String query) {
if (addresses == null) if (addresses == null)
return false; return false;
for (Address address : addresses) for (Address address : addresses)
if (address.toString().toLowerCase().contains(text)) if (contains(address.toString(), query))
return true; return true;
return false; return false;
} }
private static boolean contains(String text, String query) {
if (TextUtils.isEmpty(text))
return false;
text = text.toLowerCase();
if (text.contains(query))
return true;
String normalized = Normalizer.normalize(text, Normalizer.Form.NFKD)
.replaceAll("[\\p{InCombiningDiacriticalMarks}]", "");
return normalized.contains(query);
}
State getState() { State getState() {
return this.state; return this.state;
} }

@ -29,6 +29,7 @@ import android.os.Build;
import android.text.TextUtils; import android.text.TextUtils;
import java.io.File; import java.io.File;
import java.text.Normalizer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -145,8 +146,10 @@ public class Fts4DbHelper extends SQLiteOpenHelper {
boundary.setText(text); boundary.setText(text);
int start = boundary.first(); int start = boundary.first();
for (int end = boundary.next(); end != android.icu.text.BreakIterator.DONE; end = boundary.next()) { for (int end = boundary.next(); end != android.icu.text.BreakIterator.DONE; end = boundary.next()) {
String word = text.substring(start, end).trim(); String word = text.substring(start, end).trim().toLowerCase();
if (!TextUtils.isEmpty(word)) { if (!TextUtils.isEmpty(word)) {
word = Normalizer.normalize(word, Normalizer.Form.NFKD)
.replaceAll("[\\p{InCombiningDiacriticalMarks}]", "");
if (sb.length() > 0) if (sb.length() > 0)
sb.append(' '); sb.append(' ');
sb.append(word); sb.append(word);
@ -179,12 +182,16 @@ public class Fts4DbHelper extends SQLiteOpenHelper {
Long account, Long folder, long[] exclude, Long account, Long folder, long[] exclude,
BoundaryCallbackMessages.SearchCriteria criteria) { BoundaryCallbackMessages.SearchCriteria criteria) {
String query = criteria.query.trim();
query = Normalizer.normalize(query, Normalizer.Form.NFKD)
.replaceAll("[\\p{InCombiningDiacriticalMarks}]", "");
List<String> word = new ArrayList<>(); List<String> word = new ArrayList<>();
List<String> plus = new ArrayList<>(); List<String> plus = new ArrayList<>();
List<String> minus = new ArrayList<>(); List<String> minus = new ArrayList<>();
List<String> opt = new ArrayList<>(); List<String> opt = new ArrayList<>();
StringBuilder all = new StringBuilder(); StringBuilder all = new StringBuilder();
for (String w : criteria.query.trim().split("\\s+")) { for (String w : query.split("\\s+")) {
if (all.length() > 0) if (all.length() > 0)
all.append(' '); all.append(' ');

Loading…
Cancel
Save