1
0
Fork 0

Fixed only 8 suggestions appearing, when there are actually more (#257)

* fixed a regession after the optimizations, that caused only 8 exact word matches to appear, when there are more

* code cleanup: replaced Thread classes with lambdas in DictionaryDb
This commit is contained in:
Dimo Karaivanov 2023-05-11 17:00:47 +03:00 committed by GitHub
parent c3787138fa
commit 8949c65f4f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 36 deletions

View file

@ -45,7 +45,7 @@ public class DictionaryDb {
} }
private static void printDebug(String tag, String title, WordList words, long startTime) { private static void printDebug(String tag, String title, String sequence, WordList words, long startTime) {
if (!Logger.isDebugLevel()) { if (!Logger.isDebugLevel()) {
return; return;
} }
@ -54,9 +54,11 @@ public class DictionaryDb {
debugText debugText
.append("\n") .append("\n")
.append("Word Count: ").append(words.size()) .append("Word Count: ").append(words.size())
.append(". Time: ").append(System.currentTimeMillis() - startTime).append(" ms"); .append(". Time: ").append(System.currentTimeMillis() - startTime).append(" ms.");
if (words.size() > 0) { if (words.size() > 0) {
debugText.append("\n").append(words); debugText.append("\n").append(words);
} else {
debugText.append(" Sequence: ").append(sequence);
} }
Logger.d(tag, debugText.toString()); Logger.d(tag, debugText.toString());
@ -92,9 +94,7 @@ public class DictionaryDb {
* This query will finish immediately, if there is nothing to do. It's safe to run it often. * This query will finish immediately, if there is nothing to do. It's safe to run it often.
*/ */
public static void normalizeWordFrequencies(SettingsStore settings) { public static void normalizeWordFrequencies(SettingsStore settings) {
new Thread() { new Thread(() -> {
@Override
public void run() {
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
int affectedRows = getInstance().wordsDao().normalizeFrequencies( int affectedRows = getInstance().wordsDao().normalizeFrequencies(
@ -107,18 +107,16 @@ public class DictionaryDb {
"Normalized " + affectedRows + " words in: " + (System.currentTimeMillis() - time) + " ms" "Normalized " + affectedRows + " words in: " + (System.currentTimeMillis() - time) + " ms"
); );
} }
}.start(); ).start();
} }
public static void areThereWords(ConsumerCompat<Boolean> notification, Language language) { public static void areThereWords(ConsumerCompat<Boolean> notification, Language language) {
new Thread() { new Thread(() -> {
@Override
public void run() {
int langId = language != null ? language.getId() : -1; int langId = language != null ? language.getId() : -1;
notification.accept(getInstance().wordsDao().count(langId) > 0); notification.accept(getInstance().wordsDao().count(langId) > 0);
} }
}.start(); ).start();
} }
@ -137,9 +135,7 @@ public class DictionaryDb {
public static void deleteWords(Runnable notification, ArrayList<Integer> languageIds) { public static void deleteWords(Runnable notification, ArrayList<Integer> languageIds) {
new Thread() { new Thread(() -> {
@Override
public void run() {
if (languageIds == null) { if (languageIds == null) {
getInstance().clearAllTables(); getInstance().clearAllTables();
} else if (languageIds.size() > 0) { } else if (languageIds.size() > 0) {
@ -147,7 +143,7 @@ public class DictionaryDb {
} }
notification.run(); notification.run();
} }
}.start(); ).start();
} }
@ -167,9 +163,7 @@ public class DictionaryDb {
dbWord.length = word.length(); dbWord.length = word.length();
dbWord.frequency = 1; dbWord.frequency = 1;
new Thread() { new Thread(() -> {
@Override
public void run() {
try { try {
getInstance().wordsDao().insert(dbWord); getInstance().wordsDao().insert(dbWord);
getInstance().wordsDao().incrementFrequency(dbWord.langId, dbWord.word, dbWord.sequence); getInstance().wordsDao().incrementFrequency(dbWord.langId, dbWord.word, dbWord.sequence);
@ -185,7 +179,7 @@ public class DictionaryDb {
statusHandler.accept(2); statusHandler.accept(2);
} }
} }
}.start(); ).start();
} }
@ -212,9 +206,7 @@ public class DictionaryDb {
throw new Exception("Cannot increment word frequency. Word: " + word + " | Sequence: " + sequence); throw new Exception("Cannot increment word frequency. Word: " + word + " | Sequence: " + sequence);
} }
new Thread() { new Thread(() -> {
@Override
public void run() {
try { try {
int affectedRows = getInstance().wordsDao().incrementFrequency(language.getId(), word, sequence); int affectedRows = getInstance().wordsDao().incrementFrequency(language.getId(), word, sequence);
@ -246,7 +238,7 @@ public class DictionaryDb {
); );
} }
} }
}.start(); ).start();
} }
@ -264,7 +256,7 @@ public class DictionaryDb {
filter == null || filter.equals("") ? null : filter filter == null || filter.equals("") ? null : filter
)); ));
printDebug("loadWordsExact", "===== Exact Word Matches =====", matches, start); printDebug("loadWordsExact", "===== Exact Word Matches =====", sequence, matches, start);
return matches.toStringList(); return matches.toStringList();
} }
@ -299,7 +291,7 @@ public class DictionaryDb {
matches.addAll(getInstance().wordsDao().getCustom(sql)); matches.addAll(getInstance().wordsDao().getCustom(sql));
} }
printDebug("loadWordsFuzzy", "~=~=~=~ Fuzzy Word Matches ~=~=~=~", matches, start); printDebug("loadWordsFuzzy", "~=~=~=~ Fuzzy Word Matches ~=~=~=~", sequence, matches, start);
return matches.toStringList(); return matches.toStringList();
} }
@ -327,21 +319,15 @@ public class DictionaryDb {
return; return;
} }
new Thread() { new Thread(() -> {
@Override wordList.addAll(loadWordsExact(language, sequence, filter, maxWords));
public void run() {
if (sequence.length() == 1) {
wordList.addAll(loadWordsExact(language, sequence, filter, maxWords));
} else {
wordList.addAll(loadWordsFuzzy(language, sequence, filter, minWords));
}
if (wordList.size() == 0) { if (sequence.length() > 1 && wordList.size() < minWords) {
Logger.i("db.getWords", "No suggestions for sequence: " + sequence); wordList.addAll(loadWordsFuzzy(language, sequence, filter, minWords - wordList.size()));
} }
sendWords(dataHandler, wordList); sendWords(dataHandler, wordList);
} }
}.start(); ).start();
} }
} }

View file

@ -34,7 +34,7 @@ public abstract class TT9Room extends RoomDatabase {
" WHERE 1" + " WHERE 1" +
" AND lang = " + langId + " AND lang = " + langId +
" AND len BETWEEN " + minWordLength + " AND " + maxWordLength + " AND len BETWEEN " + minWordLength + " AND " + maxWordLength +
" AND seq BETWEEN " + sequence + " AND " + sequence + "99 " + " AND seq > " + sequence + " AND seq <= " + sequence + "99 " +
" ORDER BY len ASC, freq DESC " + " ORDER BY len ASC, freq DESC " +
" LIMIT " + limit; " LIMIT " + limit;