Add dictionary view and change how phrase lookup works
This commit is contained in:
@@ -17,6 +17,8 @@ class TokiDictionaryViewModel: ObservableObject {
|
||||
@Published var dictionary: [TokiDictEntry] = []
|
||||
@Published var partsOfSpeech: [TokiPartOfSpeech] = []
|
||||
|
||||
@Published var translatedDictionary: [TokiSubWordListEntry] = []
|
||||
|
||||
init() {
|
||||
if let safeDictionary = jsonLoader.loadDictionary() {
|
||||
dictionary = safeDictionary
|
||||
@@ -30,17 +32,32 @@ class TokiDictionaryViewModel: ObservableObject {
|
||||
|
||||
func filterDictionary(_ input: String) {
|
||||
dictionary = []
|
||||
if(input.isEmpty) {
|
||||
dictionary = fullDictionary
|
||||
} else {
|
||||
let capturePattern = #"(\w+)"#
|
||||
let captures = self.searchStringForRegex(string: input, regex: capturePattern)
|
||||
for capture in captures {
|
||||
print(capture)
|
||||
for value in fullDictionary {
|
||||
if value.word == capture {
|
||||
dictionary.append(value)
|
||||
}
|
||||
|
||||
for value in fullDictionary {
|
||||
if value.word.hasPrefix(input) {
|
||||
dictionary.append(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func translatePhrase(_ input: String) {
|
||||
dictionary = []
|
||||
translatedDictionary = []
|
||||
|
||||
let capturePattern = #"(\w+)"#
|
||||
let captures = self.searchStringForRegex(string: input, regex: capturePattern)
|
||||
for capture in captures {
|
||||
let translatedWord = TokiSubWordListEntry(capture)
|
||||
translatedDictionary.append(translatedWord)
|
||||
for value in fullDictionary {
|
||||
if value.word == capture {
|
||||
dictionary.append(value)
|
||||
translatedWord.subDictionary.removeAll()
|
||||
translatedWord.subDictionary.append(value)
|
||||
break
|
||||
} else if value.word.hasPrefix(capture) {
|
||||
dictionary.append(value)
|
||||
translatedWord.subDictionary.append(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -58,3 +75,12 @@ class TokiDictionaryViewModel: ObservableObject {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class TokiSubWordListEntry: Identifiable {
|
||||
let header: String
|
||||
var subDictionary: [TokiDictEntry] = []
|
||||
|
||||
init(_ header: String) {
|
||||
self.header = header
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user