2021-11-04 18:10:22 +00:00
|
|
|
//
|
|
|
|
// TokiDictionaryViewModel.swift
|
|
|
|
// Toki Trainer
|
|
|
|
//
|
|
|
|
// Created by Avery Ada Pace on 11/4/21.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
2021-11-06 17:35:56 +00:00
|
|
|
class TokiDictionaryViewModel: ObservableObject {
|
|
|
|
|
2021-11-04 18:10:22 +00:00
|
|
|
let jsonLoader: TokiJSONLoader = TokiJSONLoader()
|
|
|
|
|
2021-11-05 20:21:46 +00:00
|
|
|
private var fullDictionary: [TokiDictEntry] = []
|
|
|
|
private var fullPartsOfSpeech: [TokiPartOfSpeech] = []
|
|
|
|
|
2021-11-04 18:10:22 +00:00
|
|
|
@Published var dictionary: [TokiDictEntry] = []
|
|
|
|
@Published var partsOfSpeech: [TokiPartOfSpeech] = []
|
|
|
|
|
2022-10-08 10:36:19 +00:00
|
|
|
@Published var translatedDictionary: [TokiSubWordListEntry] = []
|
|
|
|
|
2021-11-04 18:10:22 +00:00
|
|
|
init() {
|
|
|
|
if let safeDictionary = jsonLoader.loadDictionary() {
|
|
|
|
dictionary = safeDictionary
|
2021-11-05 20:21:46 +00:00
|
|
|
fullDictionary = safeDictionary
|
2021-11-04 18:10:22 +00:00
|
|
|
}
|
|
|
|
if let safePartsOfSpeech = jsonLoader.loadPOS() {
|
|
|
|
partsOfSpeech = safePartsOfSpeech
|
2021-11-05 20:21:46 +00:00
|
|
|
fullPartsOfSpeech = safePartsOfSpeech
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func filterDictionary(_ input: String) {
|
|
|
|
dictionary = []
|
2022-10-08 10:36:19 +00:00
|
|
|
|
|
|
|
for value in fullDictionary {
|
|
|
|
if value.word.hasPrefix(input) {
|
|
|
|
dictionary.append(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-26 19:46:50 +00:00
|
|
|
func filterDictionaryEnglishMode(_ input: String) {
|
|
|
|
dictionary = []
|
|
|
|
|
|
|
|
for value in fullDictionary {
|
|
|
|
|
|
|
|
var entryMatch = false
|
|
|
|
|
|
|
|
// Check if word matches toki pona form, even partially
|
|
|
|
if value.word.hasPrefix(input) {
|
|
|
|
entryMatch = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if any part of the word definitions match in English, even partially
|
|
|
|
for definition in value.definitions {
|
|
|
|
if definition.definition.contains(input) {
|
|
|
|
entryMatch = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add to dictionary
|
|
|
|
if entryMatch == true {
|
|
|
|
dictionary.append(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-08 10:36:19 +00:00
|
|
|
func translatePhrase(_ input: String) {
|
|
|
|
dictionary = []
|
|
|
|
translatedDictionary = []
|
|
|
|
|
2022-10-08 12:35:14 +00:00
|
|
|
if input.isEmpty {
|
|
|
|
dictionary = fullDictionary
|
|
|
|
translatedDictionary.append(TokiSubWordListEntry("Dictionary"))
|
|
|
|
}
|
|
|
|
|
2022-10-08 10:36:19 +00:00
|
|
|
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)
|
2021-11-05 20:21:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func searchStringForRegex(string: String, regex: String) -> [String] {
|
|
|
|
let lowerCaseString = string.lowercased()
|
|
|
|
let strRange = NSRange(lowerCaseString.startIndex..<lowerCaseString.endIndex, in: lowerCaseString)
|
|
|
|
let nameRegex = try! NSRegularExpression(pattern: regex, options: [])
|
|
|
|
|
|
|
|
let results = nameRegex.matches(in: lowerCaseString, options: [], range: strRange)
|
|
|
|
|
|
|
|
return results.map {
|
|
|
|
String(lowerCaseString[Range($0.range, in: lowerCaseString)!])
|
2021-11-04 18:10:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-08 10:36:19 +00:00
|
|
|
|
|
|
|
class TokiSubWordListEntry: Identifiable {
|
|
|
|
let header: String
|
|
|
|
var subDictionary: [TokiDictEntry] = []
|
|
|
|
|
|
|
|
init(_ header: String) {
|
|
|
|
self.header = header
|
|
|
|
}
|
|
|
|
}
|