vChewing-macOS/Packages/vChewing_Megrez/Sources/Megrez/8_Unigram.swift

59 lines
2.0 KiB
Swift
Raw 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.

// Swiftified and further development by (c) 2022 and onwards The vChewing Project (MIT License).
// Was initially rebranded from (c) Lukhnos Liu's C++ library "Gramambular 2" (MIT License).
// ====================
// This code is released under the MIT license (SPDX-License-Identifier: MIT)
public extension Megrez {
///
@frozen struct Unigram: Equatable, CustomStringConvertible, Hashable {
///
public var value: String
///
public var score: Double
///
public var description: String {
"(" + value.description + "," + String(score) + ")"
}
///
/// - Parameters:
/// - value:
/// - score:
public init(value: String = "", score: Double = 0) {
self.value = value
self.score = score
}
///
/// - Parameter hasher:
public func hash(into hasher: inout Hasher) {
hasher.combine(value)
hasher.combine(score)
}
public static func == (lhs: Unigram, rhs: Unigram) -> Bool {
lhs.value == rhs.value && lhs.score == rhs.score
}
public static func < (lhs: Unigram, rhs: Unigram) -> Bool {
lhs.value < rhs.value || (lhs.value == rhs.value && lhs.score < rhs.score)
}
}
}
// MARK: - Array Extensions.
public extension Array where Element == Megrez.Unigram {
///
mutating func consolidate(filter theFilter: Set<String> = .init()) {
var inserted: [String: Double] = [:]
var insertedArray: [Megrez.Unigram] = []
for neta in filter({ !theFilter.contains($0.value) }) {
if inserted.keys.contains(neta.value) { continue }
inserted[neta.value] = neta.score
insertedArray.append(neta)
}
self = insertedArray
}
}