Megrez // Turn KeyValuePaired and Unigram into Structs again.

- This can solve the crash issues while being deduplicated in Swift-only method.
This commit is contained in:
ShikiSuen 2023-12-18 12:40:14 +08:00
parent 1dcdc21411
commit 87f7328636
3 changed files with 25 additions and 14 deletions

View File

@ -5,11 +5,15 @@
public extension Megrez { public extension Megrez {
/// ///
class KeyValuePaired: Unigram, Comparable { struct KeyValuePaired: Equatable, CustomStringConvertible, Hashable, Comparable {
/// ///
public var keyArray: [String] = [] public let keyArray: [String]
///
public let value: String
///
public let score: Double
/// ///
override public var description: String { "(\(keyArray.description),\(value),\(score))" } public var description: String { "(\(keyArray.description),\(value),\(score))" }
/// false /// false
public var isValid: Bool { !keyArray.joined().isEmpty && !value.isEmpty } public var isValid: Bool { !keyArray.joined().isEmpty && !value.isEmpty }
/// () /// ()
@ -24,25 +28,28 @@ public extension Megrez {
/// - keyArray: /// - keyArray:
/// - value: /// - value:
/// - score: /// - score:
public init(keyArray: [String], value: String = "N/A", score: Double = 0) { public init(keyArray: [String] = [], value: String = "N/A", score: Double = 0) {
super.init(value: value.isEmpty ? "N/A" : value, score: score)
self.keyArray = keyArray.isEmpty ? ["N/A"] : keyArray self.keyArray = keyArray.isEmpty ? ["N/A"] : keyArray
self.value = value.isEmpty ? "N/A" : value
self.score = score
} }
/// ///
/// - Parameter tripletExpression: /// - Parameter tripletExpression:
public init(_ tripletExpression: (keyArray: [String], value: String, score: Double)) { public init(_ tripletExpression: (keyArray: [String], value: String, score: Double)) {
let theValue = tripletExpression.value.isEmpty ? "N/A" : tripletExpression.value
super.init(value: theValue, score: tripletExpression.score)
keyArray = tripletExpression.keyArray.isEmpty ? ["N/A"] : tripletExpression.keyArray keyArray = tripletExpression.keyArray.isEmpty ? ["N/A"] : tripletExpression.keyArray
let theValue = tripletExpression.value.isEmpty ? "N/A" : tripletExpression.value
value = theValue
score = tripletExpression.score
} }
/// ///
/// - Parameter tuplet: /// - Parameter tuplet:
public init(_ tupletExpression: (keyArray: [String], value: String)) { public init(_ tupletExpression: (keyArray: [String], value: String)) {
let theValue = tupletExpression.value.isEmpty ? "N/A" : tupletExpression.value
super.init(value: theValue, score: 0)
keyArray = tupletExpression.keyArray.isEmpty ? ["N/A"] : tupletExpression.keyArray keyArray = tupletExpression.keyArray.isEmpty ? ["N/A"] : tupletExpression.keyArray
let theValue = tupletExpression.value.isEmpty ? "N/A" : tupletExpression.value
value = theValue
score = 0
} }
/// ///
@ -51,18 +58,23 @@ public extension Megrez {
/// - value: /// - value:
/// - score: /// - score:
public init(key: String = "N/A", value: String = "N/A", score: Double = 0) { public init(key: String = "N/A", value: String = "N/A", score: Double = 0) {
super.init(value: value.isEmpty ? "N/A" : value, score: score)
keyArray = key.isEmpty ? ["N/A"] : key.sliced(by: Megrez.Compositor.theSeparator) keyArray = key.isEmpty ? ["N/A"] : key.sliced(by: Megrez.Compositor.theSeparator)
self.value = value.isEmpty ? "N/A" : value
self.score = score
} }
/// ///
/// - Parameter hasher: /// - Parameter hasher:
override public func hash(into hasher: inout Hasher) { public func hash(into hasher: inout Hasher) {
hasher.combine(keyArray) hasher.combine(keyArray)
hasher.combine(value) hasher.combine(value)
hasher.combine(score) hasher.combine(score)
} }
public var hardCopy: KeyValuePaired {
.init(keyArray: keyArray, value: value, score: score)
}
public func joinedKey(by separator: String = Megrez.Compositor.theSeparator) -> String { public func joinedKey(by separator: String = Megrez.Compositor.theSeparator) -> String {
keyArray.joined(separator: separator) keyArray.joined(separator: separator)
} }

View File

@ -5,7 +5,7 @@
public extension Megrez { public extension Megrez {
/// ///
class Unigram: Equatable, CustomStringConvertible, Hashable { struct Unigram: Equatable, CustomStringConvertible, Hashable {
/// ///
public var value: String public var value: String
/// ///

View File

@ -19,8 +19,7 @@ class SimpleLM: LangModelProtocol {
let col0 = String(linestream[0]) let col0 = String(linestream[0])
let col1 = String(linestream[1]) let col1 = String(linestream[1])
let col2 = Double(linestream[2]) ?? 0.0 let col2 = Double(linestream[2]) ?? 0.0
let u = Megrez.Unigram(value: swapKeyValue ? col0 : col1, score: 0) let u = Megrez.Unigram(value: swapKeyValue ? col0 : col1, score: col2)
u.score = col2
mutDatabase[swapKeyValue ? col1 : col0, default: []].append(u) mutDatabase[swapKeyValue ? col1 : col0, default: []].append(u)
} }
} }