|
5 | 5 | // MARK: - Megrez.Unigram |
6 | 6 |
|
7 | 7 | extension Megrez { |
8 | | - /// 語言模型的基礎資料單位結構。 |
9 | | - public struct Unigram: Equatable, CustomStringConvertible, Hashable, Codable { |
| 8 | + /// 語言模型的基礎資料單位類型。 |
| 9 | + public final class Unigram: Codable, CustomStringConvertible, Equatable, Hashable { |
10 | 10 | // MARK: Lifecycle |
11 | 11 |
|
12 | | - /// 建立語言模型基礎資料單位副本。基礎資料單位由詞彙內容與統計權重組成。 |
| 12 | + /// 建立語言模型基礎資料單位副本。基礎資料單位由索引鍵陣列、詞彙內容與統計權重組成。 |
13 | 13 | /// - Parameters: |
| 14 | + /// - keyArray: 對應的索引鍵陣列。 |
14 | 15 | /// - value: 詞彙內容。 |
15 | 16 | /// - score: 統計權重(雙精度浮點數)。 |
16 | | - public init(value: String = "", score: Double = 0) { |
| 17 | + public init(keyArray: [String] = [], value: String = "", score: Double = 0) { |
| 18 | + self.keyArray = keyArray |
17 | 19 | self.value = value |
18 | 20 | self.score = score |
19 | 21 | } |
20 | 22 |
|
| 23 | + public required init(from decoder: any Decoder) throws { |
| 24 | + let container = try decoder.container(keyedBy: CodingKeys.self) |
| 25 | + self.keyArray = try container.decode([String].self, forKey: .keyArray) |
| 26 | + self.value = try container.decode(String.self, forKey: .value) |
| 27 | + self.score = try container.decode(Double.self, forKey: .score) |
| 28 | + } |
| 29 | + |
21 | 30 | // MARK: Public |
22 | 31 |
|
| 32 | + /// 對應的索引鍵陣列。 |
| 33 | + public let keyArray: [String] |
23 | 34 | /// 詞彙內容,可以是單字或詞組。 |
24 | | - public var value: String |
| 35 | + public let value: String |
25 | 36 | /// 統計權重。 |
26 | | - public var score: Double |
| 37 | + public let score: Double |
| 38 | + |
| 39 | + /// 段長(索引鍵陣列的元素數量)。 |
| 40 | + public var segLength: Int { keyArray.count } |
| 41 | + |
| 42 | + /// 檢查是否「讀音字長與候選字字長不一致」。 |
| 43 | + public var isReadingMismatched: Bool { keyArray.count != value.count } |
27 | 44 |
|
28 | 45 | /// 將當前單元圖列印成一個字串。 |
29 | 46 | public var description: String { |
30 | 47 | "(" + value.description + "," + String(score) + ")" |
31 | 48 | } |
32 | 49 |
|
33 | | - public static func == (lhs: Self, rhs: Self) -> Bool { |
34 | | - lhs.hashValue == rhs.hashValue |
| 50 | + /// 單元圖的淺層複製品(保持相同的索引鍵陣列)。 |
| 51 | + public var copy: Unigram { copy(withKeyArray: nil) } |
| 52 | + |
| 53 | + public static func == (lhs: Unigram, rhs: Unigram) -> Bool { |
| 54 | + lhs.keyArray == rhs.keyArray && lhs.value == rhs.value && lhs.score == rhs.score |
35 | 55 | } |
36 | 56 |
|
37 | | - public static func < (lhs: Self, rhs: Self) -> Bool { |
38 | | - lhs.value < rhs.value || (lhs.value == rhs.value && lhs.score < rhs.score) |
| 57 | + /// 建立一個新的單元圖副本。 |
| 58 | + /// - Parameter keyArrayOverride: 若指定,則使用新的索引鍵陣列。 |
| 59 | + /// - Returns: 單元圖副本。 |
| 60 | + public func copy(withKeyArray keyArrayOverride: [String]? = nil) -> Unigram { |
| 61 | + .init(keyArray: keyArrayOverride ?? keyArray, value: value, score: score) |
39 | 62 | } |
40 | 63 |
|
41 | | - /// 做為預設雜湊函式。 |
42 | | - /// - Parameter hasher: 目前物件的雜湊碼。 |
43 | 64 | public func hash(into hasher: inout Hasher) { |
| 65 | + hasher.combine(keyArray) |
44 | 66 | hasher.combine(value) |
45 | 67 | hasher.combine(score) |
46 | 68 | } |
| 69 | + |
| 70 | + public func encode(to encoder: any Encoder) throws { |
| 71 | + var container = encoder.container(keyedBy: CodingKeys.self) |
| 72 | + try container.encode(keyArray, forKey: .keyArray) |
| 73 | + try container.encode(value, forKey: .value) |
| 74 | + try container.encode(score, forKey: .score) |
| 75 | + } |
| 76 | + |
| 77 | + // MARK: Private |
| 78 | + |
| 79 | + private enum CodingKeys: String, CodingKey { |
| 80 | + case keyArray |
| 81 | + case value |
| 82 | + case score |
| 83 | + } |
47 | 84 | } |
48 | 85 | } |
49 | 86 |
|
|
0 commit comments