// // DemoData.swift // AIGrammar // // Created by oscar on 2024/4/2. // import Foundation // 确保GrammarRes遵循Codable协议,以支持JSON解析 struct GrammarRes: Codable { var plain: String var type: String var reason: String var correction: [String] } enum GrammarResType : String { case ok = "ok" case grammar = "grammar" case spell = "spell" } // 因为tResult是动态的,所以我们定义GrammarData为类 class GrammarData { var inputText: String var correctText: String var results: [GrammarRes] init(inputText: String, correctText: String, tResult: [GrammarRes] = []) { self.inputText = inputText self.correctText = correctText self.results = tResult } // 方法:解析长字符串为tResult func parseResult(from jsonString: String) -> Bool { // 移除字符串中可能出现的多余逗号和空格 let cleanedString = jsonString.trimmingCharacters(in: .whitespacesAndNewlines).replacingOccurrences(of: ", ]", with: "]") guard let jsonData = cleanedString.data(using: .utf8) else { print("Error: Cannot create jsonData") return false } do { // 解析JSON数据 self.results = try JSONDecoder().decode([GrammarRes].self, from: jsonData) return true } catch { print("Error: \(error)") return false } } } extension GrammarData { // 静态方法返回包含Demo数据的GrammarData实例 static func demoInstance() -> GrammarData { //let demoInputText = "This is a demo text with more complex grammar checking algorithm of the pro version. This app have been designed to help you to write correctly and appear more professional. I work really hard to make this app as god as possible. If you are happy with the results, please consider supporting the app by subscribing to the PRO plan. The text writen here is to show you how much the app is capable of with the pro version. is this something you would like to use? " let demoInputText = "Paris are hosting the Olypmic Games in 2024. Athletes from arround the world comes to compet in many sports, wich makes it an excting event to watch." let demoCorrectText = demoInputText // 假设的Demo错误数据 let demoErrors = [ GrammarRes(plain: "This is a demo text with more complex grammar checking algorithm of the pro version.", type: "ok", reason: "", correction: []), GrammarRes( plain: "This app have", type: "grammar", reason: "subject-verb agreement", correction: ["This app has"] ), GrammarRes( plain: "been designed to help you to write correctly and appear more professional.", type: "ok", reason: "", correction: [] ), GrammarRes( plain: "I work really hard", type: "ok", reason: "", correction: [] ), GrammarRes( plain: "to make this app as god", type: "spell", reason: "typo", correction: ["as good"] ), GrammarRes( plain: "as possible.", type: "ok", reason: "", correction: [] ), GrammarRes( plain: "If you are happy with the results, please consider supporting the app by subscribing to the PRO plan.", type: "ok", reason: "", correction: [] ), GrammarRes( plain: "The text writen", type: "spell", reason: "misspelling", correction: ["written"] ), GrammarRes( plain: "here is to show you how much the app is capable of with the pro version.", type: "ok", reason: "", correction: [] ), GrammarRes( plain: "is this something you would like to use?", type: "grammar", reason: "capitalization", correction: ["Is this something you would like to use?"] ) ] return GrammarData(inputText: demoInputText, correctText: demoCorrectText, tResult: demoErrors) } }