-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implemneted mutliple noun type suggestion
- Loading branch information
1 parent
681ff8b
commit 99a598e
Showing
19 changed files
with
570 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package be.scri.helpers | ||
|
||
import android.content.Context | ||
import android.util.Log | ||
import java.io.FileOutputStream | ||
|
||
class DatabaseFileManager( | ||
private val context: Context, | ||
) { | ||
fun loadDatabaseFile(language: String) { | ||
val databaseName = "${language}LanguageData.sqlite" | ||
val dbFile = context.getDatabasePath(databaseName) | ||
Log.i("ALPHA", "Loaded Database") | ||
|
||
if (!dbFile.exists()) { | ||
context.assets.open("data/$databaseName").use { inputStream -> | ||
FileOutputStream(dbFile).use { outputStream -> | ||
inputStream.copyTo(outputStream) | ||
outputStream.flush() | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package be.scri.helpers | ||
|
||
import ContractDataLoader | ||
import EmojiDataManager | ||
import GenderDataManager | ||
import PluralFormsManager | ||
import android.content.Context | ||
|
||
class DatabaseManagers( | ||
context: Context, | ||
) { | ||
val fileManager = DatabaseFileManager(context) | ||
val contractLoader = ContractDataLoader(context) | ||
val emojiManager = EmojiDataManager(context) | ||
val genderManager = GenderDataManager(context) | ||
val pluralManager = PluralFormsManager(context) | ||
} |
28 changes: 28 additions & 0 deletions
28
app/src/main/java/be/scri/helpers/KeyboardDBHelper/ContractDataLoader.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
import android.content.Context | ||
import android.util.Log | ||
import kotlinx.serialization.json.Json | ||
import java.io.IOException | ||
|
||
class ContractDataLoader( | ||
private val context: Context, | ||
) { | ||
fun loadContract(language: String): DataContract? { | ||
val contractName = "${language.lowercase()}.json" | ||
Log.i("ALPHA", "This is the $language") | ||
|
||
return try { | ||
val json = Json { ignoreUnknownKeys = true } | ||
context.assets.open("data-contracts/$contractName").use { contractFile -> | ||
val content = contractFile.bufferedReader().readText() | ||
Log.i("ALPHA", content) | ||
json.decodeFromString<DataContract>(content).also { | ||
Log.i("MY-TAG", it.toString()) | ||
} | ||
} | ||
} catch (e: IOException) { | ||
Log.e("MY-TAG", "Error loading contract: $contractName", e) | ||
null | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
app/src/main/java/be/scri/helpers/KeyboardDBHelper/EmojiDataManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
|
||
|
||
import android.content.Context | ||
import android.database.Cursor | ||
import android.database.sqlite.SQLiteDatabase | ||
|
||
class EmojiDataManager( | ||
private val context: Context, | ||
) { | ||
fun getEmojiKeywords(language: String): HashMap<String, MutableList<String>> { | ||
val dbFile = context.getDatabasePath("${language}LanguageData.sqlite") | ||
return processEmojiKeywords(dbFile.path) | ||
} | ||
|
||
private fun processEmojiKeywords(dbPath: String): HashMap<String, MutableList<String>> { | ||
val hashMap = HashMap<String, MutableList<String>>() | ||
val db = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READONLY) | ||
|
||
db.use { database -> | ||
database.rawQuery("SELECT * FROM emoji_keywords", null).use { cursor -> | ||
processEmojiCursor(cursor, hashMap) | ||
} | ||
} | ||
return hashMap | ||
} | ||
|
||
private fun processEmojiCursor( | ||
cursor: Cursor, | ||
hashMap: HashMap<String, MutableList<String>>, | ||
) { | ||
if (!cursor.moveToFirst()) return | ||
|
||
do { | ||
val key = cursor.getString(0) | ||
hashMap[key] = getEmojiKeyMaps(cursor) | ||
} while (cursor.moveToNext()) | ||
} | ||
|
||
private fun getEmojiKeyMaps(cursor: Cursor): MutableList<String> = | ||
MutableList(cursor.columnCount - 1) { index -> | ||
cursor.getString(index + 1) | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
app/src/main/java/be/scri/helpers/KeyboardDBHelper/GenderDataManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import android.content.Context | ||
import android.database.sqlite.SQLiteDatabase | ||
import android.util.Log | ||
|
||
class GenderDataManager( | ||
private val context: Context, | ||
) { | ||
fun findGenderOfWord( | ||
language: String, | ||
jsonData: DataContract?, | ||
): HashMap<String, List<String>> = | ||
when { | ||
jsonData == null -> HashMap() | ||
!context.getDatabasePath("${language}LanguageData.sqlite").exists() -> { | ||
Log.e("MY-TAG", "Database file for $language does not exist.") | ||
HashMap() | ||
} | ||
|
||
else -> processGenderData("${language}LanguageData.sqlite", jsonData) | ||
} | ||
|
||
private fun processGenderData( | ||
dbPath: String, | ||
jsonData: DataContract, | ||
): HashMap<String, List<String>> = | ||
HashMap<String, List<String>>().also { genderMap -> | ||
context.getDatabasePath(dbPath).path.let { path -> | ||
SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY).use { db -> | ||
when { | ||
hasCanonicalGender(jsonData) -> | ||
processGenders( | ||
db = db, | ||
nounColumn = jsonData.numbers.keys.firstOrNull(), | ||
genderColumn = jsonData.genders.canonical.firstOrNull(), | ||
genderMap = genderMap, | ||
) | ||
|
||
hasMasculineFeminine(jsonData) -> { | ||
processGenders( | ||
db = db, | ||
nounColumn = jsonData.genders.masculines.firstOrNull(), | ||
genderMap = genderMap, | ||
defaultGender = "masculine", | ||
) | ||
processGenders( | ||
db = db, | ||
nounColumn = jsonData.genders.feminines.firstOrNull(), | ||
genderMap = genderMap, | ||
defaultGender = "feminine", | ||
) | ||
} | ||
|
||
else -> Log.e("MY-TAG", "No valid gender columns found.") | ||
} | ||
} | ||
} | ||
Log.i("MY-TAG", "Found ${genderMap.size} gender entries") | ||
} | ||
|
||
private fun hasCanonicalGender(jsonData: DataContract): Boolean = | ||
jsonData.genders.canonical | ||
.firstOrNull() | ||
?.isNotEmpty() == true | ||
|
||
private fun hasMasculineFeminine(jsonData: DataContract): Boolean = | ||
jsonData.genders.masculines.isNotEmpty() && | ||
jsonData.genders.feminines.isNotEmpty() | ||
|
||
@Suppress("NestedBlockDepth") | ||
private fun processGenders( | ||
db: SQLiteDatabase, | ||
nounColumn: String?, | ||
genderMap: HashMap<String, List<String>>, | ||
genderColumn: String? = null, | ||
defaultGender: String? = null, | ||
) { | ||
db.rawQuery("SELECT * FROM nouns", null)?.use { cursor -> | ||
val nounIndex = cursor.getColumnIndex(nounColumn) | ||
val genderIndex = genderColumn?.let { cursor.getColumnIndex(it) } ?: -1 | ||
|
||
if (nounIndex == -1 || (genderColumn != null && genderIndex == -1)) { | ||
Log.e("MY-TAG", "Required columns not found.") | ||
return | ||
} | ||
|
||
while (cursor.moveToNext()) { | ||
cursor.getString(nounIndex)?.lowercase()?.takeUnless { it.isEmpty() }?.let { noun -> | ||
val gender = | ||
when { | ||
genderColumn != null -> cursor.getString(genderIndex) | ||
else -> defaultGender | ||
} | ||
|
||
if (!gender.isNullOrEmpty()) { | ||
val existingGenders = genderMap[noun]?.toMutableList() ?: mutableListOf() | ||
if (!existingGenders.contains(gender)) { | ||
existingGenders.add(gender) | ||
genderMap[noun] = existingGenders | ||
} | ||
} | ||
} | ||
} | ||
} | ||
Log.i("MY-TAG", genderMap["schild"].toString()) | ||
} | ||
} |
Oops, something went wrong.