Skip to content

Commit 148942c

Browse files
committed
Noun annotation: fix multiple annotation case
1 parent 503c90b commit 148942c

2 files changed

Lines changed: 60 additions & 26 deletions

File tree

Keyboards/DataManager/LanguageDBManager.swift

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -279,30 +279,36 @@ extension LanguageDBManager {
279279
let language = getControllerLanguageAbbr()
280280
let contract = ContractManager.shared.loadContract(language: language)
281281

282+
var allGenders: [String] = []
283+
282284
let queries = GenderManager.shared.buildGenderQueries(word: word, contract: contract)
283285

284286
for queryInfo in queries {
285-
let result = queryDBRow(
287+
let results = queryDBRows(
286288
query: queryInfo.query,
287289
outputCols: queryInfo.outputCols,
288290
args: StatementArguments(queryInfo.args)
289291
)
290292

291-
// For canonical gender: return the actual gender value from DB
293+
// For canonical gender: results are the actual genders
292294
if queryInfo.fallbackGender == nil {
293-
if !result.isEmpty && !result[0].isEmpty {
294-
return result
295+
for gender in results where !gender.isEmpty {
296+
if !allGenders.contains(gender) {
297+
allGenders.append(gender)
298+
}
295299
}
296300
}
297-
// For masculine/feminine: if word found, return the fallback gender
301+
// For masculine/feminine: if any result, use fallback
298302
else {
299-
if !result.isEmpty && !result[0].isEmpty {
300-
return [queryInfo.fallbackGender!]
303+
if !results.isEmpty && !results[0].isEmpty,
304+
let fallback = queryInfo.fallbackGender,
305+
!allGenders.contains(fallback) {
306+
allGenders.append(fallback)
301307
}
302308
}
303309
}
304310

305-
return [""]
311+
return allGenders.isEmpty ? [""] : [allGenders.joined(separator: "/")]
306312
}
307313

308314
/// Query the plural form of word in `nouns`.

Keyboards/KeyboardsBase/ScribeFunctionality/Annotate.swift

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,33 @@ func convertFullGenderToAbbr(_ genderFull: String) -> String {
5252
/// - wordToAnnotate: the word that an annotation should be created for.
5353
/// - KVC: the keyboard view controller.
5454
func wordAnnotation(wordToAnnotate: String, KVC: KeyboardViewController) {
55-
var nounForm: String
55+
var genderAnnotations: [String] = []
56+
var pluralAnnotation: String?
57+
58+
// Get gender(s)
59+
let nounForm = LanguageDBManager.shared.queryNounForm(of: wordToAnnotate)[0]
60+
if !nounForm.isEmpty {
61+
// nounForm might already be "M/F" if multiple genders found
62+
genderAnnotations = nounForm.components(separatedBy: "/")
63+
}
5664

57-
// Check if word is plural first
65+
// Check if plural
5866
if let pluralWords = pluralWords, pluralWords.contains(wordToAnnotate.lowercased()) {
59-
nounForm = "PL"
60-
} else {
61-
nounForm = LanguageDBManager.shared.queryNounForm(of: wordToAnnotate)[0]
67+
pluralAnnotation = "PL"
68+
}
69+
70+
// Combine: gender(s) first, then PL if applicable
71+
var allAnnotations = genderAnnotations
72+
if let pl = pluralAnnotation, !allAnnotations.contains("PL") {
73+
allAnnotations.append(pl)
6274
}
6375

76+
// Join back with "/"
77+
let combinedNounForm = allAnnotations.joined(separator: "/")
78+
6479
prepAnnotationForm = LanguageDBManager.shared.queryPrepForm(of: wordToAnnotate.lowercased())[0]
6580

66-
hasNounForm = !nounForm.isEmpty
81+
hasNounForm = !combinedNounForm.isEmpty
6782
hasPrepForm = !prepAnnotationForm.isEmpty
6883

6984
annotationsToAssign = [String]()
@@ -105,10 +120,10 @@ func wordAnnotation(wordToAnnotate: String, KVC: KeyboardViewController) {
105120
)
106121
} else {
107122
if hasNounForm {
108-
if !nounForm.contains("/") {
109-
annotationsToAssign.append(nounForm)
123+
if !combinedNounForm.contains("/") {
124+
annotationsToAssign.append(combinedNounForm)
110125
} else {
111-
for a in nounForm.components(separatedBy: "/") {
126+
for a in combinedNounForm.components(separatedBy: "/") {
112127
annotationsToAssign.append(a)
113128
}
114129
}
@@ -262,16 +277,29 @@ func typedWordAnnotation(KVC: KeyboardViewController) {
262277
/// - index: the auto action key index that the annotation should be set for.
263278
/// - KVC: the keyboard view controller.
264279
func autoActionAnnotation(autoActionWord: String, index: Int, KVC: KeyboardViewController) {
265-
var nounForm: String
280+
var genderAnnotations: [String] = []
281+
var pluralAnnotation: String?
266282

267-
// Check if word is plural first
283+
// Get gender(s)
284+
let nounForm = LanguageDBManager.shared.queryNounForm(of: autoActionWord)[0]
285+
if !nounForm.isEmpty {
286+
genderAnnotations = nounForm.components(separatedBy: "/")
287+
}
288+
289+
// Check if plural
268290
if let pluralWords = pluralWords, pluralWords.contains(autoActionWord.lowercased()) {
269-
nounForm = "PL"
270-
} else {
271-
nounForm = LanguageDBManager.shared.queryNounForm(of: autoActionWord)[0]
291+
pluralAnnotation = "PL"
272292
}
273293

274-
hasNounForm = !nounForm.isEmpty
294+
// Combine
295+
var allAnnotations = genderAnnotations
296+
if let pl = pluralAnnotation, !allAnnotations.contains("PL") {
297+
allAnnotations.append(pl)
298+
}
299+
300+
let combinedNounForm = allAnnotations.joined(separator: "/")
301+
302+
hasNounForm = !combinedNounForm.isEmpty
275303

276304
newAutoActionAnnotationsToAssign = [String]()
277305
newAutoActionAnnotationBtns = [UIButton]()
@@ -282,10 +310,10 @@ func autoActionAnnotation(autoActionWord: String, index: Int, KVC: KeyboardViewC
282310
let annotationHeight = KVC.scribeKey.frame.height * 0.1
283311

284312
if hasNounForm {
285-
if !nounForm.contains("/") {
286-
newAutoActionAnnotationsToAssign.append(nounForm)
313+
if !combinedNounForm.contains("/") {
314+
newAutoActionAnnotationsToAssign.append(combinedNounForm)
287315
} else {
288-
for a in nounForm.components(separatedBy: "/") {
316+
for a in combinedNounForm.components(separatedBy: "/") {
289317
newAutoActionAnnotationsToAssign.append(a)
290318
}
291319
}

0 commit comments

Comments
 (0)