From 45f34cd07c3adc03796ff3ef035d759d49bb2afa Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Sat, 21 May 2022 10:00:46 +0800 Subject: [PATCH] Using var-description in lieu of var-printed. (#27) --- Sources/Megrez/3_NodeAnchor.swift | 12 ++++++------ Sources/Megrez/4_Node.swift | 6 +++--- Sources/Megrez/6_Bigram.swift | 18 +++++------------- Sources/Megrez/6_Unigram.swift | 18 +++++------------- Sources/Megrez/7_KeyValuePair.swift | 12 ++---------- 5 files changed, 21 insertions(+), 45 deletions(-) diff --git a/Sources/Megrez/3_NodeAnchor.swift b/Sources/Megrez/3_NodeAnchor.swift index 0c7ffa9..b450b6a 100644 --- a/Sources/Megrez/3_NodeAnchor.swift +++ b/Sources/Megrez/3_NodeAnchor.swift @@ -25,7 +25,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. extension Megrez { /// 節锚。 - @frozen public struct NodeAnchor { + @frozen public struct NodeAnchor: CustomStringConvertible { /// 節點。一個節锚內不一定有節點。 public var node: Node? /// 節锚所在的位置。 @@ -39,12 +39,12 @@ extension Megrez { node?.key.count ?? 0 } - /// 將當前節點節锚成一個字串。 - public var printed: String { + /// 將當前節锚列印成一個字串。 + public var description: String { var stream = "" stream += "{@(" + String(location) + "," + String(spanningLength) + ")," if let node = node { - stream += node.printed + stream += node.description } else { stream += "null" } @@ -65,10 +65,10 @@ extension Megrez { extension Array where Element == Megrez.NodeAnchor { /// 將節锚陣列列印成一個字串。 - public var printed: String { + public var description: String { var arrOutputContent = [""] for anchor in self { - arrOutputContent.append(anchor.printed) + arrOutputContent.append(anchor.description) } return arrOutputContent.joined(separator: "<-") } diff --git a/Sources/Megrez/4_Node.swift b/Sources/Megrez/4_Node.swift index acb1697..5b658e1 100644 --- a/Sources/Megrez/4_Node.swift +++ b/Sources/Megrez/4_Node.swift @@ -25,7 +25,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. extension Megrez { /// 節點。 - public class Node { + public class Node: CustomStringConvertible { /// 當前節點對應的語言模型。 private let mutLM: LanguageModel = .init() /// 鍵。 @@ -49,8 +49,8 @@ extension Megrez { /// 用來登記要施加給「『被標記為選中狀態』的候選字詞」的複寫權重的數值。 private let kSelectedCandidateScore: Double = 99 /// 將當前節點列印成一個字串。 - public var printed: String { - "(node,key:\(mutKey),fixed:\(mutCandidateFixed ? "true" : "false"),selected:\(mutSelectedUnigramIndex),\(mutUnigrams.printed))" + public var description: String { + "(node,key:\(mutKey),fixed:\(mutCandidateFixed ? "true" : "false"),selected:\(mutSelectedUnigramIndex),\(mutUnigrams))" } /// 公開:候選字詞陣列(唯讀),以鍵值陣列的形式存在。 diff --git a/Sources/Megrez/6_Bigram.swift b/Sources/Megrez/6_Bigram.swift index 7413584..cca1069 100644 --- a/Sources/Megrez/6_Bigram.swift +++ b/Sources/Megrez/6_Bigram.swift @@ -25,7 +25,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. extension Megrez { /// 雙元圖。 - @frozen public struct Bigram: Equatable { + @frozen public struct Bigram: Equatable, CustomStringConvertible { /// 當前鍵值。 public var keyValue: KeyValuePair /// 前述鍵值。 @@ -33,8 +33,8 @@ extension Megrez { /// 權重。 public var score: Double /// 將當前雙元圖列印成一個字串。 - public var printed: String { - "(" + keyValue.printed + "|" + precedingKeyValue.printed + "," + String(score) + ")" + public var description: String { + "(" + keyValue.description + "|" + precedingKeyValue.description + "," + String(score) + ")" } /// 初期化一筆「雙元圖」。一筆雙元圖由一組前述鍵值配對、一組當前鍵值配對、與一筆權重數值組成。 @@ -63,14 +63,6 @@ extension Megrez { lhs.precedingKeyValue < rhs.precedingKeyValue || (lhs.keyValue < rhs.keyValue || (lhs.keyValue == rhs.keyValue && lhs.keyValue < rhs.keyValue)) } - - var description: String { - "\(keyValue):\(score)" - } - - var debugDescription: String { - "Bigram(keyValue: \(keyValue), score: \(score))" - } } } @@ -78,10 +70,10 @@ extension Megrez { extension Array where Element == Megrez.Bigram { /// 將雙元圖陣列列印成一個字串。 - public var printed: String { + public var description: String { var arrOutputContent = [""] for (index, gram) in enumerated() { - arrOutputContent.append(contentsOf: [String(index) + "=>" + gram.printed]) + arrOutputContent.append(contentsOf: [String(index) + "=>" + gram.description]) } return "[" + String(count) + "]=>{" + arrOutputContent.joined(separator: ",") + "}" } diff --git a/Sources/Megrez/6_Unigram.swift b/Sources/Megrez/6_Unigram.swift index 53fe75b..62b0726 100644 --- a/Sources/Megrez/6_Unigram.swift +++ b/Sources/Megrez/6_Unigram.swift @@ -25,14 +25,14 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. extension Megrez { /// 單元圖。 - @frozen public struct Unigram: Equatable { + @frozen public struct Unigram: Equatable, CustomStringConvertible { /// 鍵值。 public var keyValue: KeyValuePair /// 權重。 public var score: Double /// 將當前單元圖列印成一個字串。 - public var printed: String { - "(" + keyValue.printed + "," + String(score) + ")" + public var description: String { + "(" + keyValue.description + "," + String(score) + ")" } /// 初期化一筆「單元圖」。一筆單元圖由一組鍵值配對與一筆權重數值組成。 @@ -61,14 +61,6 @@ extension Megrez { public static func < (lhs: Unigram, rhs: Unigram) -> Bool { lhs.keyValue < rhs.keyValue || (lhs.keyValue == rhs.keyValue && lhs.keyValue < rhs.keyValue) } - - var description: String { - "\(keyValue):\(score)" - } - - var debugDescription: String { - "Unigram(keyValue: \(keyValue), score: \(score))" - } } } @@ -76,10 +68,10 @@ extension Megrez { extension Array where Element == Megrez.Unigram { /// 將單元圖陣列列印成一個字串。 - public var printed: String { + public var description: String { var arrOutputContent = [""] for (index, gram) in enumerated() { - arrOutputContent.append(contentsOf: [String(index) + "=>" + gram.printed]) + arrOutputContent.append(contentsOf: [String(index) + "=>" + gram.description]) } return "[" + String(count) + "]=>{" + arrOutputContent.joined(separator: ",") + "}" } diff --git a/Sources/Megrez/7_KeyValuePair.swift b/Sources/Megrez/7_KeyValuePair.swift index 1756c24..851ab3d 100644 --- a/Sources/Megrez/7_KeyValuePair.swift +++ b/Sources/Megrez/7_KeyValuePair.swift @@ -25,13 +25,13 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. extension Megrez { /// 鍵值配對。 - @frozen public struct KeyValuePair: Equatable, Hashable, Comparable { + @frozen public struct KeyValuePair: Equatable, Hashable, Comparable, CustomStringConvertible { /// 鍵。一般情況下用來放置讀音等可以用來作為索引的內容。 public var key: String /// 資料值。 public var value: String /// 將當前鍵值列印成一個字串。 - public var printed: String { + public var description: String { "(" + key + "," + value + ")" } @@ -68,13 +68,5 @@ extension Megrez { public static func >= (lhs: KeyValuePair, rhs: KeyValuePair) -> Bool { (lhs.key.count >= rhs.key.count) || (lhs.key.count == rhs.key.count && lhs.value >= rhs.value) } - - public var description: String { - "(\(key), \(value))" - } - - public var debugDescription: String { - "KeyValuePair(key: \(key), value: \(value))" - } } }