2021-11-04 18:10:22 +00:00
|
|
|
//
|
|
|
|
// TokiDictionaryViewModel.swift
|
|
|
|
// Toki Trainer
|
|
|
|
//
|
|
|
|
// Created by Avery Ada Pace on 11/4/21.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
class TokiDictionaryViewModel: ObservableObject
|
|
|
|
{
|
|
|
|
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] = []
|
|
|
|
|
|
|
|
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 = []
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|