Translation works
This commit is contained in:
parent
d5e3d29225
commit
21408fb957
@ -11,15 +11,50 @@ class TokiDictionaryViewModel: ObservableObject
|
|||||||
{
|
{
|
||||||
let jsonLoader: TokiJSONLoader = TokiJSONLoader()
|
let jsonLoader: TokiJSONLoader = TokiJSONLoader()
|
||||||
|
|
||||||
|
private var fullDictionary: [TokiDictEntry] = []
|
||||||
|
private var fullPartsOfSpeech: [TokiPartOfSpeech] = []
|
||||||
|
|
||||||
@Published var dictionary: [TokiDictEntry] = []
|
@Published var dictionary: [TokiDictEntry] = []
|
||||||
@Published var partsOfSpeech: [TokiPartOfSpeech] = []
|
@Published var partsOfSpeech: [TokiPartOfSpeech] = []
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
if let safeDictionary = jsonLoader.loadDictionary() {
|
if let safeDictionary = jsonLoader.loadDictionary() {
|
||||||
dictionary = safeDictionary
|
dictionary = safeDictionary
|
||||||
|
fullDictionary = safeDictionary
|
||||||
}
|
}
|
||||||
if let safePartsOfSpeech = jsonLoader.loadPOS() {
|
if let safePartsOfSpeech = jsonLoader.loadPOS() {
|
||||||
partsOfSpeech = safePartsOfSpeech
|
partsOfSpeech = safePartsOfSpeech
|
||||||
|
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)!])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,12 +17,18 @@ struct ContentView: View {
|
|||||||
|
|
||||||
@ObservedObject var tokiDictViewModel = TokiDictionaryViewModel()
|
@ObservedObject var tokiDictViewModel = TokiDictionaryViewModel()
|
||||||
@State private var selectedPartOfSpeech: String? = nil
|
@State private var selectedPartOfSpeech: String? = nil
|
||||||
|
@State private var tokiInput: String = ""
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
VStack {
|
VStack {
|
||||||
TextField("Enter Toki Pona Word or Phrase", text: /*@START_MENU_TOKEN@*//*@PLACEHOLDER=Value@*/.constant("")/*@END_MENU_TOKEN@*/)
|
TextField("Enter Toki Pona Word or Phrase", text: $tokiInput)
|
||||||
.multilineTextAlignment(.center)
|
.multilineTextAlignment(.center)
|
||||||
|
.textInputAutocapitalization(.never)
|
||||||
|
.disableAutocorrection(true)
|
||||||
.padding(8)
|
.padding(8)
|
||||||
|
.onSubmit {
|
||||||
|
tokiDictViewModel.filterDictionary(tokiInput)
|
||||||
|
}
|
||||||
List(tokiDictViewModel.dictionary, id: \.word) { entry in
|
List(tokiDictViewModel.dictionary, id: \.word) { entry in
|
||||||
VStack(alignment: .leading) {
|
VStack(alignment: .leading) {
|
||||||
Text(entry.word)
|
Text(entry.word)
|
||||||
|
Loading…
Reference in New Issue
Block a user