vChewing-macOS/Packages/vChewing_Megrez/Sources/Megrez/7_LangModel.swift

62 lines
2.3 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 protocol LangModelProtocol {
///
func unigramsFor(keyArray: [String]) -> [Megrez.Unigram]
///
func hasUnigramsFor(keyArray: [String]) -> Bool
}
public extension Megrez.Compositor {
///
class LangModelRanked: LangModelProtocol {
private let langModel: LangModelProtocol
///
/// - Parameter withLM:
public init(withLM: LangModelProtocol) {
langModel = withLM
}
///
/// - Parameter key:
/// - Returns:
public func unigramsFor(keyArray: [String]) -> [Megrez.Unigram] {
langModel.unigramsFor(keyArray: keyArray).stableSorted { $0.score > $1.score }
}
///
/// - Parameter key:
/// - Returns:
public func hasUnigramsFor(keyArray: [String]) -> Bool {
langModel.hasUnigramsFor(keyArray: keyArray)
}
}
}
// MARK: - Stable Sort Extension
// Reference: https://stackoverflow.com/a/50545761/4162914
private extension Sequence {
/// Return a stable-sorted collection.
///
/// - Parameter areInIncreasingOrder: Return nil when two element are equal.
/// - Returns: The sorted collection.
func stableSorted(
by areInIncreasingOrder: (Element, Element) throws -> Bool
)
rethrows -> [Element]
{
try enumerated()
.sorted { a, b -> Bool in
try areInIncreasingOrder(a.element, b.element)
|| (a.offset < b.offset && !areInIncreasingOrder(b.element, a.element))
}
.map(\.element)
}
}