Skip to content

Commit 11a8450

Browse files
Femimatandrewtavis
andauthored
Capitalize German nouns in autocompletions even if the keyboard is lower case (#597)
* Capitalize German nouns in autocompletions * Fix variable name --------- Co-authored-by: Andrew Tavis McAllister <[email protected]>
1 parent 6505506 commit 11a8450

1 file changed

Lines changed: 33 additions & 10 deletions

File tree

app/src/main/java/be/scri/helpers/data/AutocompletionDataManager.kt

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ import be.scri.helpers.StringUtils.isWordCapitalized
1313
class AutocompletionDataManager(
1414
private val fileManager: DatabaseFileManager,
1515
) {
16-
private val trie = Trie()
16+
private var trie = Trie()
1717
private var trieLoaded = false
1818
private val nounWords = mutableListOf<String>()
19+
private var isGerman = false
20+
private val germanNouns = mutableSetOf<String>()
1921

2022
/**
2123
* Loads all words from the language-specific database into the trie.
@@ -28,10 +30,19 @@ class AutocompletionDataManager(
2830
language: String,
2931
numbersColumns: List<String> = emptyList(),
3032
) {
33+
trie = Trie()
34+
trieLoaded = false
35+
nounWords.clear()
36+
germanNouns.clear()
37+
isGerman = language.equals("DE", ignoreCase = true)
38+
3139
val db = fileManager.getLanguageDatabase(language) ?: return
3240

3341
db.use { database ->
34-
if (database.tableExists("autocomplete_lexicon")) {
42+
val hasLexicon = database.tableExists("autocomplete_lexicon")
43+
val hasNouns = database.tableExists("nouns")
44+
45+
if (hasLexicon) {
3546
database.rawQuery("SELECT word FROM autocomplete_lexicon", null).use { cursor ->
3647
val wordIndex = cursor.getColumnIndex("word")
3748
while (cursor.moveToNext()) {
@@ -42,7 +53,9 @@ class AutocompletionDataManager(
4253
}
4354
}
4455
trieLoaded = true
45-
} else if (database.tableExists("nouns") && numbersColumns.isNotEmpty()) {
56+
}
57+
58+
if (hasNouns && numbersColumns.isNotEmpty()) {
4659
val unionQuery =
4760
numbersColumns.joinToString(" UNION ") { column ->
4861
"SELECT DISTINCT $column AS word FROM nouns WHERE $column IS NOT NULL AND $column != ''"
@@ -53,7 +66,12 @@ class AutocompletionDataManager(
5366
while (cursor.moveToNext()) {
5467
val word = cursor.getString(wordIndex)?.lowercase()?.trim()
5568
if (!word.isNullOrEmpty()) {
56-
nounWords.add(word)
69+
if (isGerman) {
70+
germanNouns.add(word)
71+
}
72+
if (!hasLexicon) {
73+
nounWords.add(word)
74+
}
5775
}
5876
}
5977
}
@@ -75,17 +93,22 @@ class AutocompletionDataManager(
7593
limit: Int = 3,
7694
): List<String> {
7795
val isCapitalized = isWordCapitalized(prefix)
96+
val normalizedPrefix = prefix.lowercase().trim()
7897

7998
val results =
8099
if (trieLoaded) {
81-
trie.searchPrefix(prefix.lowercase().trim(), limit)
100+
trie.searchPrefix(normalizedPrefix, limit)
101+
} else {
102+
getAutocompletionsFromNouns(normalizedPrefix, limit)
103+
}
104+
105+
return results.map { word ->
106+
val isGermanNoun = isGerman && germanNouns.contains(word)
107+
if (isCapitalized || isGermanNoun) {
108+
word.replaceFirstChar { it.uppercaseChar() }
82109
} else {
83-
getAutocompletionsFromNouns(prefix.lowercase().trim(), limit)
110+
word
84111
}
85-
return if (isCapitalized) {
86-
results.map { it.replaceFirstChar { it.uppercaseChar() } }
87-
} else {
88-
results
89112
}
90113
}
91114

0 commit comments

Comments
 (0)