Skip to content

Commit

Permalink
Using var-description in lieu of var-printed. (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShikiSuen authored May 21, 2022
1 parent 67699b2 commit 45f34cd
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 45 deletions.
12 changes: 6 additions & 6 deletions Sources/Megrez/3_NodeAnchor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?
/// 節锚所在的位置。
Expand All @@ -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"
}
Expand All @@ -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: "<-")
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/Megrez/4_Node.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
/// 鍵。
Expand All @@ -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))"
}

/// 公開:候選字詞陣列(唯讀),以鍵值陣列的形式存在。
Expand Down
18 changes: 5 additions & 13 deletions Sources/Megrez/6_Bigram.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ 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
/// 前述鍵值。
public var precedingKeyValue: KeyValuePair
/// 權重。
public var score: Double
/// 將當前雙元圖列印成一個字串。
public var printed: String {
"(" + keyValue.printed + "|" + precedingKeyValue.printed + "," + String(score) + ")"
public var description: String {
"(" + keyValue.description + "|" + precedingKeyValue.description + "," + String(score) + ")"
}

/// 初期化一筆「雙元圖」。一筆雙元圖由一組前述鍵值配對、一組當前鍵值配對、與一筆權重數值組成。
Expand Down Expand Up @@ -63,25 +63,17 @@ 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))"
}
}
}

// MARK: - DumpDOT-related functions.

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: ",") + "}"
}
Expand Down
18 changes: 5 additions & 13 deletions Sources/Megrez/6_Unigram.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) + ")"
}

/// 初期化一筆「單元圖」。一筆單元圖由一組鍵值配對與一筆權重數值組成。
Expand Down Expand Up @@ -61,25 +61,17 @@ 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))"
}
}
}

// MARK: - DumpDOT-related functions.

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: ",") + "}"
}
Expand Down
12 changes: 2 additions & 10 deletions Sources/Megrez/7_KeyValuePair.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 + ")"
}

Expand Down Expand Up @@ -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))"
}
}
}

0 comments on commit 45f34cd

Please sign in to comment.