Refactoring

pull/190/head
M66B 5 years ago
parent 1940e45799
commit f0317197cb

@ -63,51 +63,53 @@ public class WorkerFts extends Worker {
try { try {
Log.i("FTS index"); Log.i("FTS index");
Context context = getApplicationContext();
int indexed = 0; int indexed = 0;
List<Long> ids = new ArrayList<>(INDEX_BATCH_SIZE); List<Long> ids = new ArrayList<>(INDEX_BATCH_SIZE);
DB db = DB.getInstance(getApplicationContext()); DB db = DB.getInstance(context);
SQLiteDatabase sdb = FtsDbHelper.getInstance(getApplicationContext()); SQLiteDatabase sdb = FtsDbHelper.getInstance(context);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
try (Cursor cursor = db.message().getMessageFts()) { try (Cursor cursor = db.message().getMessageFts()) {
while (cursor.moveToNext()) { while (cursor.moveToNext()) {
boolean fts = prefs.getBoolean("fts", false);
if (!fts)
break;
long id = cursor.getLong(0); long id = cursor.getLong(0);
EntityMessage message = db.message().getMessage(id); try {
if (message != null) Log.i("FTS index=" + id);
try {
Log.i("FTS index=" + message.id); EntityMessage message = db.message().getMessage(id);
if (message == null)
File file = message.getFile(getApplicationContext()); throw new FileNotFoundException("Message gone");
String text = HtmlHelper.getFullText(file);
File file = message.getFile(context);
if (BuildConfig.DEBUG) { String text = HtmlHelper.getFullText(file);
EntityFolder folder = db.folder().getFolder(message.folder); if (TextUtils.isEmpty(text))
if (folder != null) { throw new FileNotFoundException("Message empty");
// \\P{L}+
List<String> features = new ArrayList<>(); if (BuildConfig.DEBUG) {
for (String word : text.trim().toLowerCase().split("\\W+")) { EntityFolder folder = db.folder().getFolder(message.folder);
if (word.matches(".*\\d.*")) if (folder != null) {
continue; List<String> features = new ArrayList<>();
if (word.endsWith(".")) for (String word : text.trim().toLowerCase().split("\\W+")) {
word = word.substring(0, word.length() - 1); if (word.matches(".*\\d.*"))
features.add(word); continue;
} if (word.endsWith("."))
word = word.substring(0, word.length() - 1);
Collection<Classification<String, String>> classifications = classifier.classifyDetailed(features); features.add(word);
for (Classification<String, String> classification : classifications)
Log.i("MMM folder=" + folder.name +
" classified=" + classification.getCategory() +
" probability=" + classification.getProbability() +
" features=" + TextUtils.join(", ", features.subList(0, Math.min(features.size(), 20))));
classifier.learn(EntityFolder.JUNK.equals(folder.type) ? "spam" : "ham", features);
} }
Collection<Classification<String, String>> classifications = classifier.classifyDetailed(features);
for (Classification<String, String> classification : classifications)
Log.i("MMM folder=" + folder.name +
" classified=" + classification.getCategory() +
" probability=" + classification.getProbability() +
" features=" + TextUtils.join(", ", features.subList(0, Math.min(features.size(), 20))));
classifier.learn(EntityFolder.JUNK.equals(folder.type) ? "spam" : "ham", features);
} }
}
boolean fts = prefs.getBoolean("fts", false);
if (fts)
try { try {
sdb.beginTransaction(); sdb.beginTransaction();
FtsDbHelper.insert(sdb, message, text); FtsDbHelper.insert(sdb, message, text);
@ -116,18 +118,19 @@ public class WorkerFts extends Worker {
sdb.endTransaction(); sdb.endTransaction();
} }
indexed++; indexed++;
ids.add(id);
if (ids.size() > INDEX_BATCH_SIZE)
markIndexed(db, ids);
} catch (Throwable ex) {
if (ex instanceof FileNotFoundException ||
ex instanceof OutOfMemoryError)
ids.add(id); ids.add(id);
if (ids.size() > INDEX_BATCH_SIZE) Log.e(ex);
markIndexed(db, ids); }
} catch (Throwable ex) {
if (ex instanceof FileNotFoundException ||
ex instanceof OutOfMemoryError)
ids.add(id);
Log.e(ex);
}
} }
markIndexed(db, ids); markIndexed(db, ids);
} }

Loading…
Cancel
Save