Files
swiftGrammar/AIGrammar/ViewModel/GrammarData.swift
2024-08-12 10:49:20 +08:00

134 lines
4.5 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// DemoData.swift
// AIGrammar
//
// Created by oscar on 2024/4/2.
//
import Foundation
// GrammarResCodableJSON
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"
}
// tResultGrammarData
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 {
// DemoGrammarData
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)
}
}