Skip to content

Commit

Permalink
scribe-org#378 reimplement db manager class across keyboard functiona…
Browse files Browse the repository at this point in the history
…lities
  • Loading branch information
andrewtavis authored and SaurabhJamadagni committed Apr 18, 2024
1 parent 9b047c3 commit 28db383
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 333 deletions.
69 changes: 14 additions & 55 deletions Keyboards/KeyboardsBase/KeyboardViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -367,10 +367,7 @@ class KeyboardViewController: UIInputViewController {
/// - Parameters
/// - word: the word for which corresponding emojis should be shown for.
func getEmojiAutoSuggestions(for word: String) {
let query = "SELECT * FROM emoji_keywords WHERE word = ?"
let args = [word.lowercased()]
let outputCols = ["emoji_0", "emoji_1", "emoji_2"]
let emojisToDisplay = queryDBRow(query: query, outputCols: outputCols, args: args)
let emojisToDisplay = LanguageDBManager.shared.queryEmojis(of: word.lowercased())

if !emojisToDisplay[0].isEmpty {
emojisToDisplayArray = [String]()
Expand Down Expand Up @@ -448,7 +445,7 @@ class KeyboardViewController: UIInputViewController {
}

// Get options for completion that start with the current prefix and are not just one letter.
let completionOptions = queryAutocompletions(word: currentPrefix)
let completionOptions = LanguageDBManager.shared.queryAutocompletions(word: currentPrefix)

if !completionOptions[0].isEmpty {
if completionOptions.count <= 3 {
Expand Down Expand Up @@ -505,20 +502,20 @@ class KeyboardViewController: UIInputViewController {
let prefix = proxy.documentContextBeforeInput?.components(separatedBy: " ").secondToLast() ?? ""

completionWords = [String]()
let query = "SELECT * FROM verbs WHERE verb = ?"
for i in 0 ..< 3 {
// Get conjugations of the preselected verbs.
let args = [verbsAfterPronounsArray[i]]
if let tense = pronounAutosuggestionTenses[prefix.lowercased()] {
let outputCols = [tense]
var suggestion = queryDBRow(query: query, outputCols: outputCols, args: args)[0]
var suggestion = LanguageDBManager.shared.queryVerb(of: verbsAfterPronounsArray[i], with: outputCols)[0]

if suggestion == "" {
suggestion = verbsAfterPronounsArray[i]
}

if suggestion == "REFLEXIVE_PRONOUN" && controllerLanguage == "Spanish" {
suggestion = getESReflexivePronoun(pronoun: prefix.lowercased())
}

if shiftButtonState == .shift {
completionWords.append(suggestion.capitalize())
} else if capsLockButtonState == .locked {
Expand Down Expand Up @@ -579,13 +576,9 @@ class KeyboardViewController: UIInputViewController {
} else {
// We have to consider these different cases as the key always has to match.
// Else, even if the lowercased prefix is present in the dictionary, if the actual prefix isn't present we won't get an output.
let query = "SELECT * FROM autosuggestions WHERE word = ?"
let argsLower = [prefix.lowercased()]
let argsCapitalize = [prefix.capitalized]
let outputCols = ["suggestion_0", "suggestion_1", "suggestion_2"]
let suggestionsLowerCasePrefix = LanguageDBManager.shared.queryAutosuggestions(of: prefix.lowercased())
let suggestionsCapitalizedPrefix = LanguageDBManager.shared.queryAutosuggestions(of: prefix.capitalized)

let suggestionsLowerCasePrefix = queryDBRow(query: query, outputCols: outputCols, args: argsLower)
let suggestionsCapitalizedPrefix = queryDBRow(query: query, outputCols: outputCols, args: argsCapitalize)
if !suggestionsLowerCasePrefix[0].isEmpty {
completionWords = [String]()
for i in 0 ..< 3 {
Expand All @@ -598,12 +591,9 @@ class KeyboardViewController: UIInputViewController {
} else if capsLockButtonState == .locked {
completionWords.append(suggestionsLowerCasePrefix[i].uppercased())
} else {
let nounGenderQuery = "SELECT * FROM nouns WHERE noun = ?"
let nounGenderArgs = [suggestionsLowerCasePrefix[i]]
let outputCols = ["form"]

let nounForm = queryDBRow(query: nounGenderQuery, outputCols: outputCols, args: nounGenderArgs)[0]
let nounForm = LanguageDBManager.shared.queryNounForm(of: suggestionsLowerCasePrefix[i])[0]
hasNounForm = !nounForm.isEmpty

if !hasNounForm {
completionWords.append(suggestionsLowerCasePrefix[i].lowercased())
} else {
Expand All @@ -618,6 +608,7 @@ class KeyboardViewController: UIInputViewController {
completionWords.append(previousWord)
continue
}

if shiftButtonState == .shift {
completionWords.append(suggestionsCapitalizedPrefix[i].capitalize())
} else if capsLockButtonState == .locked {
Expand Down Expand Up @@ -1496,10 +1487,8 @@ class KeyboardViewController: UIInputViewController {
}

// Populate conjugation view buttons.
let query = "SELECT * FROM verbs WHERE verb = ?"
let args = [verbToConjugate]
let outputCols = allConjugations
let conjugationsToDisplay = queryDBRow(query: query, outputCols: outputCols, args: args)
let conjugationsToDisplay = LanguageDBManager.shared.queryVerb(of: verbToConjugate, with: outputCols)
for index in 0 ..< allConjugations.count {
if conjugationsToDisplay[index] == "" {
// Assign the invalid message if the conjugation isn't present in the directory.
Expand Down Expand Up @@ -1879,46 +1868,19 @@ class KeyboardViewController: UIInputViewController {
// Show the name of the keyboard to the user.
showKeyboardLanguage = true

// Initialize the language database and create the autosuggestions lexicon.
languageDB = openDBQueue()

// Add UILexicon words including unpaired first and last names from Contacts to autocompletions.
let addToAutocompleteLexiconQuery = "INSERT OR IGNORE INTO autocomplete_lexicon (word) VALUES (?)"
requestSupplementaryLexicon { (userLexicon: UILexicon?) in
if let lexicon = userLexicon {
for item in lexicon.entries {
if item.documentText.count > 1 {
writeDBRow(query: addToAutocompleteLexiconQuery, args: [item.documentText])
LanguageDBManager.shared.insertAutocompleteLexion(of: item.documentText)
}
}
}
}

// Drop non-unique values in case the lexicon has added words that were already present.
let dropNonUniqueAutosuggestionsQuery = """
DELETE FROM autocomplete_lexicon
WHERE rowid NOT IN (
SELECT
MIN(rowid)
FROM
autocomplete_lexicon
GROUP BY
word
)
"""
do {
try languageDB.write { db in
try db.execute(sql: dropNonUniqueAutosuggestionsQuery)
}
} catch let error as DatabaseError {
let errorMessage = error.message
let errorSQL = error.sql
print(
"An error '\(String(describing: errorMessage))' occurred in the query: \(String(describing: errorSQL))"
)
} catch {}
LanguageDBManager.shared.deleteNonUniqueAutosuggestions()
}

setKeyboard()
Expand Down Expand Up @@ -2437,11 +2399,8 @@ class KeyboardViewController: UIInputViewController {
}
}
}
let prepCaseQuery = "SELECT * FROM prepositions WHERE preposition = ?"
let prepCaseArgs = [wordToCheck.lowercased()]
let outputCols = ["form"]

let prepForm = queryDBRow(query: prepCaseQuery, outputCols: outputCols, args: prepCaseArgs)[0]
let prepForm = LanguageDBManager.shared.queryPrepForm(of: wordToCheck.lowercased())[0]
hasPrepForm = !prepForm.isEmpty
if hasPrepForm {
resetCaseDeclensionState()
Expand Down
Loading

0 comments on commit 28db383

Please sign in to comment.