Skip to content

Commit 0ca13d6

Browse files
committed
Preposition annotation: update queryPrepForm and add conversion preposition to abbreviation
1 parent 148942c commit 0ca13d6

2 files changed

Lines changed: 62 additions & 20 deletions

File tree

Keyboards/DataManager/LanguageDBManager.swift

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class LanguageDBManager {
1212
static let shared = LanguageDBManager(translate: false)
1313
static let translations = LanguageDBManager(translate: true)
1414
private var database: DatabaseQueue?
15+
private var cachedHasGrammaticalCase: Bool?
1516

1617
private init(translate: Bool) {
1718
if translate {
@@ -358,22 +359,46 @@ extension LanguageDBManager {
358359

359360
/// Query preposition form of word in `prepositions`.
360361
func queryPrepForm(of word: String) -> [String] {
361-
let query = """
362-
SELECT
363-
*
364-
365-
FROM
366-
prepositions
362+
// Check if this language's database has grammaticalCase column
363+
guard hasGrammaticalCaseColumn() else {
364+
return [""]
365+
}
367366

368-
WHERE
369-
preposition = ?
370-
"""
371-
let outputCols = ["form"]
367+
let query = """
368+
SELECT grammaticalCase
369+
FROM prepositions
370+
WHERE preposition = ?
371+
"""
372+
let outputCols = ["grammaticalCase"]
372373
let args = [word]
373374

374375
return queryDBRow(query: query, outputCols: outputCols, args: StatementArguments(args))
375376
}
376377

378+
/// Check if the prepositions table has a grammaticalCase column.
379+
/// Result is cached to avoid repeated PRAGMA queries.
380+
private func hasGrammaticalCaseColumn() -> Bool {
381+
if let cached = cachedHasGrammaticalCase {
382+
return cached
383+
}
384+
385+
var hasColumn = false
386+
387+
do {
388+
try database?.read { db in
389+
let columns = try Row.fetchAll(db, sql: "PRAGMA table_info(prepositions)")
390+
hasColumn = columns.contains { row in
391+
(row["name"] as? String) == "grammaticalCase"
392+
}
393+
}
394+
} catch {
395+
hasColumn = false
396+
}
397+
398+
cachedHasGrammaticalCase = hasColumn
399+
return hasColumn
400+
}
401+
377402
/// Query the translation of word in the current language. Only works with the `translations` manager.
378403
func queryTranslation(of word: String) -> [String] {
379404
let translateLanguage = getKeyInDict(givenValue: getControllerTranslateLangCode(), dict: languagesAbbrDict)

Keyboards/KeyboardsBase/ScribeFunctionality/Annotate.swift

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,30 @@ let prepAnnotationConversionDict = [
3434

3535
// Converts full gender names to abbreviations (e.g., "feminine" → "F")
3636
func convertFullGenderToAbbr(_ genderFull: String) -> String {
37-
let genderMap: [String: String] = [
38-
"feminine": "F",
39-
"masculine": "M",
40-
"neuter": "N",
41-
"common": "C",
42-
"common of two genders": "C",
43-
"PL": "PL"
44-
]
45-
46-
return genderMap[genderFull.lowercased()] ?? genderFull
37+
let genderMap: [String: String] = [
38+
"feminine": "F",
39+
"masculine": "M",
40+
"neuter": "N",
41+
"common": "C",
42+
"common of two genders": "C",
43+
"PL": "PL"
44+
]
45+
46+
return genderMap[genderFull.lowercased()] ?? genderFull
47+
}
48+
49+
// Converts full preposition to abbreviations
50+
func convertFullPrepositionToAbbr(_ prepositionFull: String) -> String {
51+
let prepositionMap: [String: String] = [
52+
"genitive case": "Gen",
53+
"accusative case": "Acc",
54+
"dative case": "Dat",
55+
"locative case": "Loc",
56+
"prepositional case": "Pre",
57+
"instrumental case": "Ins"
58+
]
59+
60+
return prepositionMap[prepositionFull.lowercased()] ?? prepositionFull
4761
}
4862

4963
/// The base function for annotation that's accessed by `typedWordAnnotation`.
@@ -77,6 +91,9 @@ func wordAnnotation(wordToAnnotate: String, KVC: KeyboardViewController) {
7791
let combinedNounForm = allAnnotations.joined(separator: "/")
7892

7993
prepAnnotationForm = LanguageDBManager.shared.queryPrepForm(of: wordToAnnotate.lowercased())[0]
94+
if !prepAnnotationForm.isEmpty {
95+
prepAnnotationForm = convertFullPrepositionToAbbr(prepAnnotationForm)
96+
}
8097

8198
hasNounForm = !combinedNounForm.isEmpty
8299
hasPrepForm = !prepAnnotationForm.isEmpty

0 commit comments

Comments
 (0)