1
0
Fork 0
mirror of https://github.com/M66B/FairEmail.git synced 2024-12-29 11:15:51 +00:00

Use break iterator for text indexing

This commit is contained in:
M66B 2022-09-29 13:29:38 +02:00
parent f43be071a1
commit b401607315

View file

@ -25,6 +25,7 @@ import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteOpenHelper;
import android.os.Build;
import android.text.TextUtils; import android.text.TextUtils;
import java.io.File; import java.io.File;
@ -117,10 +118,10 @@ public class Fts4DbHelper extends SQLiteOpenHelper {
cv.put("folder", message.folder); cv.put("folder", message.folder);
cv.put("time", message.received); cv.put("time", message.received);
cv.put("address", MessageHelper.formatAddresses(address.toArray(new Address[0]), true, false)); cv.put("address", MessageHelper.formatAddresses(address.toArray(new Address[0]), true, false));
cv.put("subject", message.subject == null ? "" : message.subject); cv.put("subject", breakText(message.subject));
cv.put("keyword", TextUtils.join(", ", message.keywords)); cv.put("keyword", TextUtils.join(" ", message.keywords));
cv.put("text", text); cv.put("text", breakText(text));
cv.put("notes", message.notes); cv.put("notes", breakText(message.notes));
db.insertWithOnConflict("message", null, cv, SQLiteDatabase.CONFLICT_FAIL); db.insertWithOnConflict("message", null, cv, SQLiteDatabase.CONFLICT_FAIL);
} }
@ -132,6 +133,30 @@ public class Fts4DbHelper extends SQLiteOpenHelper {
db.delete("message", "rowid = ?", new String[]{Long.toString(id)}); db.delete("message", "rowid = ?", new String[]{Long.toString(id)});
} }
static String breakText(String text) {
if (TextUtils.isEmpty(text))
return "";
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N)
return text;
StringBuilder sb = new StringBuilder();
android.icu.text.BreakIterator boundary = android.icu.text.BreakIterator.getWordInstance();
boundary.setText(text);
int start = boundary.first();
for (int end = boundary.next(); end != android.icu.text.BreakIterator.DONE; end = boundary.next()) {
String word = text.substring(start, end).trim();
if (!TextUtils.isEmpty(word)) {
if (sb.length() > 0)
sb.append(' ');
sb.append(word);
}
start = end;
}
return sb.toString();
}
static List<String> getSuggestions(SQLiteDatabase db, String query, int max) { static List<String> getSuggestions(SQLiteDatabase db, String query, int max) {
List<String> result = new ArrayList<>(); List<String> result = new ArrayList<>();