Skip to content

Commit

Permalink
refactor: re build searching algo
Browse files Browse the repository at this point in the history
andskur committed Jan 21, 2024
1 parent 16d90f2 commit 8ad424d
Showing 1 changed file with 89 additions and 133 deletions.
222 changes: 89 additions & 133 deletions Source/Services/WordService.swift
Original file line number Diff line number Diff line change
@@ -105,143 +105,99 @@ class WordService {
})
}

// This function checks if a word matches a query
func wordMatchesQuery(_ word: Word, query: String) -> Bool {
// nouns
let wordMatchesQuery = word.oldNorseWord.lowercased().contains(query)
let nominativeSingularMatchesQuery = word.generateNounNominative(number: Number.singular, article: false)?.lowercased().contains(query) == true
let nominativeDualMatchesQuery = word.generateNounNominative(number: Number.dual, article: false)?.lowercased().contains(query) == true
let nominativePluralMatchesQuery = word.generateNounNominative(number: Number.plural, article: false)?.lowercased().contains(query) == true
let accusativeSingularMatchesQuery = word.generateNounAccusative(number: Number.singular, article: false)?.lowercased().contains(query) == true
let accusativeDualMatchesQuery = word.generateNounAccusative(number: Number.dual, article: false)?.lowercased().contains(query) == true
let accusativePluralMatchesQuery = word.generateNounAccusative(number: Number.plural, article: false)?.lowercased().contains(query) == true
let dativeSingularMatchesQuery = word.generateNounDative(number: Number.singular, article: false)?.lowercased().contains(query) == true
let dativeDualMatchesQuery = word.generateNounDative(number: Number.dual, article: false)?.lowercased().contains(query) == true
let dativePluralMatchesQuery = word.generateNounDative(number: Number.plural, article: false)?.lowercased().contains(query) == true
let genitiveSingularMatchesQuery = word.generateNounGenitive(number: Number.singular, article: false)?.lowercased().contains(query) == true
let genitiveDualMatchesQuery = word.generateNounGenitive(number: Number.dual, article: false)?.lowercased().contains(query) == true
let genitivePluralMatchesQuery = word.generateNounGenitive(number: Number.plural, article: false)?.lowercased().contains(query) == true


// verbs
let firstSingularMatchesQuery = word.generateConjugationNonReflexive(person: .first, number: .singular, tense: .present, mood: .indicative)?.lowercased().contains(query) == true
let secondSingularMatchesQuery = word.generateConjugationNonReflexive(person: .second, number: .singular, tense: .present, mood: .indicative)?.lowercased().contains(query) == true
let thirdSingularMatchesQuery = word.generateConjugationNonReflexive(person: .third, number: .singular, tense: .present, mood: .indicative)?.lowercased().contains(query) == true
let firstPluralMatchesQuery = word.generateConjugationNonReflexive(person: .first, number: .plural, tense: .present, mood: .indicative)?.lowercased().contains(query) == true
let secondPluralMatchesQuery = word.generateConjugationNonReflexive(person: .second, number: .plural, tense: .present, mood: .indicative)?.lowercased().contains(query) == true
let thirdPluralMatchesQuery = word.generateConjugationNonReflexive(person: .third, number: .plural, tense: .present, mood: .indicative)?.lowercased().contains(query) == true

let firstSingularMatchesQueryPast = word.generateConjugationNonReflexive(person: .first, number: .singular, tense: .past, mood: .indicative)?.lowercased().contains(query) == true
let secondSingularMatchesQueryPast = word.generateConjugationNonReflexive(person: .second, number: .singular, tense: .past, mood: .indicative)?.lowercased().contains(query) == true
let thirdSingularMatchesQueryPast = word.generateConjugationNonReflexive(person: .third, number: .singular, tense: .past, mood: .indicative)?.lowercased().contains(query) == true
let firstPluralMatchesQueryPast = word.generateConjugationNonReflexive(person: .first, number: .plural, tense: .past, mood: .indicative)?.lowercased().contains(query) == true
let secondPluralMatchesQueryPast = word.generateConjugationNonReflexive(person: .second, number: .plural, tense: .past, mood: .indicative)?.lowercased().contains(query) == true
let thirdPluralMatchesQueryPast = word.generateConjugationNonReflexive(person: .third, number: .plural, tense: .past, mood: .indicative)?.lowercased().contains(query) == true

// reflexive verbs
let firstSingularMatchesQueryR = word.generateConjugationReflexive(person: .first, number: .singular, tense: .present, mood: .indicative)?.lowercased().contains(query) == true
let secondSingularMatchesQueryR = word.generateConjugationReflexive(person: .second, number: .singular, tense: .present, mood: .indicative)?.lowercased().contains(query) == true
let thirdSingularMatchesQueryR = word.generateConjugationReflexive(person: .third, number: .singular, tense: .present, mood: .indicative)?.lowercased().contains(query) == true
let firstPluralMatchesQueryR = word.generateConjugationReflexive(person: .first, number: .plural, tense: .present, mood: .indicative)?.lowercased().contains(query) == true
let secondPluralMatchesQueryR = word.generateConjugationReflexive(person: .second, number: .plural, tense: .present, mood: .indicative)?.lowercased().contains(query) == true
let thirdPluralMatchesQueryR = word.generateConjugationReflexive(person: .third, number: .plural, tense: .present, mood: .indicative)?.lowercased().contains(query) == true

let firstSingularMatchesQueryPastR = word.generateConjugationReflexive(person: .first, number: .singular, tense: .past, mood: .indicative)?.lowercased().contains(query) == true
let secondSingularMatchesQueryPastR = word.generateConjugationReflexive(person: .second, number: .singular, tense: .past, mood: .indicative)?.lowercased().contains(query) == true
let thirdSingularMatchesQueryPastR = word.generateConjugationReflexive(person: .third, number: .singular, tense: .past, mood: .indicative)?.lowercased().contains(query) == true
let firstPluralMatchesQueryPastR = word.generateConjugationReflexive(person: .first, number: .plural, tense: .past, mood: .indicative)?.lowercased().contains(query) == true
let secondPluralMatchesQueryPastR = word.generateConjugationReflexive(person: .second, number: .plural, tense: .past, mood: .indicative)?.lowercased().contains(query) == true
let thirdPluralMatchesQueryPastR = word.generateConjugationReflexive(person: .third, number: .plural, tense: .past, mood: .indicative)?.lowercased().contains(query) == true

// nominative
let accNominativeSingularMasculine = word.generateCase(wordCase: .nominative, number: .singular, gender: .masculine)?.lowercased().contains(query) == true
let accNominativeSingularFemenine = word.generateCase(wordCase: .nominative, number: .singular, gender: .feminine)?.lowercased().contains(query) == true
let accNominativeSingularNeuter = word.generateCase(wordCase: .nominative, number: .singular, gender: .neuter)?.lowercased().contains(query) == true

let accNominativeDualMasculine = word.generateCase(wordCase: .nominative, number: .dual, gender: .masculine)?.lowercased().contains(query) == true
let accNominativeDualFemenine = word.generateCase(wordCase: .nominative, number: .dual, gender: .feminine)?.lowercased().contains(query) == true
let accNominativeDualNeuter = word.generateCase(wordCase: .nominative, number: .dual, gender: .neuter)?.lowercased().contains(query) == true

let accNominativePluralMasculine = word.generateCase(wordCase: .nominative, number: .plural, gender: .masculine)?.lowercased().contains(query) == true
let accNominativePluralFemenine = word.generateCase(wordCase: .nominative, number: .plural, gender: .feminine)?.lowercased().contains(query) == true
let accNominativePluralNeuter = word.generateCase(wordCase: .nominative, number: .plural, gender: .neuter)?.lowercased().contains(query) == true


// accusative
let accAccusativeSingularMasculine = word.generateCase(wordCase: .accusative, number: .singular, gender: .masculine)?.lowercased().contains(query) == true
let accAccusativeSingularFemenine = word.generateCase(wordCase: .accusative, number: .singular, gender: .feminine)?.lowercased().contains(query) == true
let accAccusativeSingularNeuter = word.generateCase(wordCase: .accusative, number: .singular, gender: .neuter)?.lowercased().contains(query) == true

let accAccusativeDualMasculine = word.generateCase(wordCase: .accusative, number: .dual, gender: .masculine)?.lowercased().contains(query) == true
let accAccusativeDualFemenine = word.generateCase(wordCase: .accusative, number: .dual, gender: .feminine)?.lowercased().contains(query) == true
let accAccusativeDualNeuter = word.generateCase(wordCase: .accusative, number: .dual, gender: .neuter)?.lowercased().contains(query) == true

let accAccusativePluralMasculine = word.generateCase(wordCase: .accusative, number: .plural, gender: .masculine)?.lowercased().contains(query) == true
let accAccusativePluralFemenine = word.generateCase(wordCase: .accusative, number: .plural, gender: .feminine)?.lowercased().contains(query) == true
let accAccusativePluralNeuter = word.generateCase(wordCase: .accusative, number: .plural, gender: .neuter)?.lowercased().contains(query) == true


// dative
let accDativeSingularMasculine = word.generateCase(wordCase: .dative, number: .singular, gender: .masculine)?.lowercased().contains(query) == true
let accDativeSingularFemenine = word.generateCase(wordCase: .dative, number: .singular, gender: .feminine)?.lowercased().contains(query) == true
let accDativeSingularNeuter = word.generateCase(wordCase: .dative, number: .singular, gender: .neuter)?.lowercased().contains(query) == true

let accDativeDualMasculine = word.generateCase(wordCase: .dative, number: .dual, gender: .masculine)?.lowercased().contains(query) == true
let accDativeDualFemenine = word.generateCase(wordCase: .dative, number: .dual, gender: .feminine)?.lowercased().contains(query) == true
let accDativeDualNeuter = word.generateCase(wordCase: .dative, number: .dual, gender: .neuter)?.lowercased().contains(query) == true

let accDativePluralMasculine = word.generateCase(wordCase: .dative, number: .plural, gender: .masculine)?.lowercased().contains(query) == true
let accDativePluralFemenine = word.generateCase(wordCase: .dative, number: .plural, gender: .feminine)?.lowercased().contains(query) == true
let accDativePluralNeuter = word.generateCase(wordCase: .dative, number: .plural, gender: .neuter)?.lowercased().contains(query) == true

// genitive
let accGenitiveSingularMasculine = word.generateCase(wordCase: .genitive, number: .singular, gender: .masculine)?.lowercased().contains(query) == true
let accGenitiveSingularFemenine = word.generateCase(wordCase: .genitive, number: .singular, gender: .feminine)?.lowercased().contains(query) == true
let accGenitiveSingularNeuter = word.generateCase(wordCase: .genitive, number: .singular, gender: .neuter)?.lowercased().contains(query) == true

let accGenitiveDualMasculine = word.generateCase(wordCase: .genitive, number: .dual, gender: .masculine)?.lowercased().contains(query) == true
let accGenitiveDualFemenine = word.generateCase(wordCase: .genitive, number: .dual, gender: .feminine)?.lowercased().contains(query) == true
let accGenitiveDualNeuter = word.generateCase(wordCase: .genitive, number: .dual, gender: .neuter)?.lowercased().contains(query) == true

let accGenitivePluralMasculine = word.generateCase(wordCase: .genitive, number: .plural, gender: .masculine)?.lowercased().contains(query) == true
let accGenitivePluralFemenine = word.generateCase(wordCase: .genitive, number: .plural, gender: .feminine)?.lowercased().contains(query) == true
let accGenitivePluralNeuter = word.generateCase(wordCase: .genitive, number: .plural, gender: .neuter)?.lowercased().contains(query) == true

let accComparative = word.generateComparative()?.lowercased().contains(query) == true
let accComparison = word.generateComparison()?.lowercased().contains(query) == true

func verbsMatchesQuery(_ word: Word, query: String) -> Bool {
let accInfinitive = word.generateInfinitive()?.lowercased().contains(query) == true
let accImerativeSingular = word.generateImperative(number: .singular)?.lowercased().contains(query) == true
let accImerativePlural = word.generateImperative(number: .plural)?.lowercased().contains(query) == true

let accComparativeAdjective1 = word.generateComparativeAdjective(number: .singular, gender: .masculine, caseWeak: .nominative)?.lowercased().contains(query) == true
let accComparativeAdjective2 = word.generateComparativeAdjective(number: .singular, gender: .neuter, caseWeak: .nominative)?.lowercased().contains(query) == true
let accComparativeAdjective3 = word.generateComparativeAdjective(number: .plural, gender: .any, caseWeak: .dative)?.lowercased().contains(query) == true
let accImerativeSingularR = word.generateImperativeReflexive(number: .singular)?.lowercased().contains(query) == true
let accImerativePluralR = word.generateImperativeReflexive(number: .plural)?.lowercased().contains(query) == true

for tense in Tense.allCases {
for person in Person.allCases {
for mood in Mood.allCases {
for num in Number.allCases {
if let w = word.generateConjugation(person: person, number: num, tense: tense, reflexive: true, mood: mood) {
if w.lowercased().contains(query) {
return true
}
}
}
}
}
}

let accComparisonWeakAdjective1 = word.generateComparisonWeakAdjective(number: .singular, gender: .masculine, caseWeak: .nominative)?.lowercased().contains(query) == true
let accComparisonWeakAdjective2 = word.generateComparisonWeakAdjective(number: .singular, gender: .neuter, caseWeak: .nominative)?.lowercased().contains(query) == true
let accComparisonWeakAdjective3 = word.generateComparisonWeakAdjective(number: .plural, gender: .any, caseWeak: .dative)?.lowercased().contains(query) == true
// reflexive
for tense in Tense.allCases {
for person in Person.allCases {
for mood in Mood.allCases {
for num in Number.allCases {
if let w = word.generateConjugation(person: person, number: num, tense: tense, reflexive: false, mood: mood) {
if w.lowercased().contains(query) {
return true
}
}
}
}
}
}

let accComparisonStrongAdjective1 = word.generateComparisonStrongAdjective(number: .singular, gender: .masculine, caseStrong: .nominative)?.lowercased().contains(query) == true
let accComparisonStrongAdjective2 = word.generateComparisonStrongAdjective(number: .singular, gender: .neuter, caseStrong: .nominative)?.lowercased().contains(query) == true
let accComparisonStrongAdjective3 = word.generateComparisonStrongAdjective(number: .singular, gender: .feminine, caseStrong: .nominative)?.lowercased().contains(query) == true
let accComparisonStrongAdjective4 = word.generateComparisonStrongAdjective(number: .singular, gender: .masculine, caseStrong: .accusative)?.lowercased().contains(query) == true
let accComparisonStrongAdjective5 = word.generateComparisonStrongAdjective(number: .singular, gender: .feminine, caseStrong: .accusative)?.lowercased().contains(query) == true
let accComparisonStrongAdjective6 = word.generateComparisonStrongAdjective(number: .singular, gender: .masculine, caseStrong: .dative)?.lowercased().contains(query) == true
let accComparisonStrongAdjective7 = word.generateComparisonStrongAdjective(number: .singular, gender: .neuter, caseStrong: .dative)?.lowercased().contains(query) == true
let accComparisonStrongAdjective8 = word.generateComparisonStrongAdjective(number: .singular, gender: .feminine, caseStrong: .dative)?.lowercased().contains(query) == true
let accComparisonStrongAdjective9 = word.generateComparisonStrongAdjective(number: .singular, gender: .masculine, caseStrong: .genitive)?.lowercased().contains(query) == true
let accComparisonStrongAdjective10 = word.generateComparisonStrongAdjective(number: .singular, gender: .neuter, caseStrong: .genitive)?.lowercased().contains(query) == true
let accComparisonStrongAdjective11 = word.generateComparisonStrongAdjective(number: .singular, gender: .feminine, caseStrong: .genitive)?.lowercased().contains(query) == true
return accInfinitive || accImerativeSingular || accImerativePlural || accImerativeSingularR || accImerativePluralR
}

func commonMatchesQuery(_ word: Word, query: String) -> Bool {
for c in Case.allCases {
for num in Number.allCases {
for gen in Gender.allCases {
if let w = word.generateNounCase(nounCase: c, number: num, article: false) {
if w.lowercased().contains(query) {
return true
}
}

if let w = word.generateNounCase(nounCase: c, number: num, article: true) {
if w.lowercased().contains(query) {
return true
}
}

if let w = word.generateCase(wordCase: c, number: num, gender: gen) {
if w.lowercased().contains(query) {
return true
}
}
}
}
}

return false
}

func adjectivesMatchesQuery(_ word: Word, query: String) -> Bool {
for f in Formation.allCases {
for c in Case.allCases {
for num in Number.allCases {
for gen in Gender.allCases {
if let w = word.generateAdjective(number: num, gender: gen, caseAdjective: c, formation: f) {
if w.lowercased().contains(query) {
return true
}
}
}
}
}
}

let accComparisonStrongAdjective12 = word.generateComparisonStrongAdjective(number: .plural, gender: .masculine, caseStrong: .nominative)?.lowercased().contains(query) == true
let accComparisonStrongAdjective13 = word.generateComparisonStrongAdjective(number: .plural, gender: .neuter, caseStrong: .nominative)?.lowercased().contains(query) == true
let accComparisonStrongAdjective14 = word.generateComparisonStrongAdjective(number: .plural, gender: .feminine, caseStrong: .nominative)?.lowercased().contains(query) == true
let accComparisonStrongAdjective15 = word.generateComparisonStrongAdjective(number: .plural, gender: .masculine, caseStrong: .accusative)?.lowercased().contains(query) == true
let accComparisonStrongAdjective16 = word.generateComparisonStrongAdjective(number: .plural, gender: .masculine, caseStrong: .dative)?.lowercased().contains(query) == true
let accComparisonStrongAdjective17 = word.generateComparisonStrongAdjective(number: .plural, gender: .masculine, caseStrong: .genitive)?.lowercased().contains(query) == true
return false
}

func wordMatchesQuery(_ word: Word, query: String) -> Bool {
let wordMatchesQuery = word.oldNorseWord.lowercased().contains(query)
let commonMatchesQuery = commonMatchesQuery(word, query: query)
let verbMatchesQuery = verbsMatchesQuery(word, query: query)
let adjectivesMatchesQuery = adjectivesMatchesQuery(word, query: query)

let accInfinitive = word.generateInfinitive()?.lowercased().contains(query) == true
let accImerativeSingular = word.generateImperative(number: .singular)?.lowercased().contains(query) == true
let accImerativePlural = word.generateImperative(number: .plural)?.lowercased().contains(query) == true


return wordMatchesQuery || nominativeSingularMatchesQuery || nominativeDualMatchesQuery || nominativePluralMatchesQuery || accusativeSingularMatchesQuery || accusativeDualMatchesQuery || accusativePluralMatchesQuery || dativeSingularMatchesQuery || dativeDualMatchesQuery || dativePluralMatchesQuery || firstSingularMatchesQuery || secondSingularMatchesQuery || thirdSingularMatchesQuery || firstPluralMatchesQuery || secondPluralMatchesQuery || thirdPluralMatchesQuery || firstSingularMatchesQueryPast || secondSingularMatchesQueryPast || thirdSingularMatchesQueryPast || firstPluralMatchesQueryPast || secondPluralMatchesQueryPast || thirdPluralMatchesQueryPast || genitiveSingularMatchesQuery || genitiveDualMatchesQuery || genitivePluralMatchesQuery || accNominativeSingularMasculine || accNominativeSingularFemenine || accNominativeSingularNeuter || accNominativeDualMasculine || accNominativeDualFemenine || accNominativeDualNeuter || accNominativePluralMasculine || accNominativePluralFemenine || accAccusativePluralNeuter || accAccusativeSingularMasculine || accAccusativeSingularFemenine || accAccusativeSingularNeuter || accAccusativeDualMasculine || accAccusativeDualFemenine || accAccusativeDualNeuter || accAccusativePluralMasculine || accDativePluralFemenine || accDativePluralNeuter || accGenitiveSingularMasculine || accGenitiveSingularFemenine || accGenitiveSingularNeuter || accGenitiveDualMasculine || accGenitiveDualFemenine || accGenitiveDualNeuter || accGenitivePluralMasculine || accGenitivePluralFemenine || accGenitivePluralNeuter || accNominativePluralNeuter || accDativeSingularMasculine || accDativePluralMasculine || accDativeDualNeuter || accDativeDualFemenine || accDativeDualMasculine || accDativeSingularFemenine || accDativeSingularNeuter || accAccusativePluralFemenine || accComparative || accInfinitive || accImerativeSingular || accImerativePlural || accComparativeAdjective1 || accComparativeAdjective2 || accComparativeAdjective3 || accComparisonWeakAdjective1 || accComparisonWeakAdjective2 || accComparisonWeakAdjective3 || accComparisonStrongAdjective1 || accComparisonStrongAdjective2 || accComparisonStrongAdjective3 || accComparisonStrongAdjective4 || accComparisonStrongAdjective5 || accComparisonStrongAdjective6 || accComparisonStrongAdjective7 || accComparisonStrongAdjective8 || accComparisonStrongAdjective9 || accComparisonStrongAdjective10 || accComparisonStrongAdjective11 || accComparisonStrongAdjective12 || accComparisonStrongAdjective13 || accComparisonStrongAdjective14 || accComparisonStrongAdjective15 || accComparisonStrongAdjective16 || accComparisonStrongAdjective17 || accComparison || firstSingularMatchesQueryR || secondSingularMatchesQueryR || thirdSingularMatchesQueryR || firstPluralMatchesQueryR || secondPluralMatchesQueryR || thirdPluralMatchesQueryR || firstSingularMatchesQueryPastR || secondSingularMatchesQueryPastR || thirdSingularMatchesQueryPastR || firstPluralMatchesQueryPastR || secondPluralMatchesQueryPastR || thirdPluralMatchesQueryPastR
return wordMatchesQuery || verbMatchesQuery || commonMatchesQuery || adjectivesMatchesQuery
}
}

0 comments on commit 8ad424d

Please sign in to comment.