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

62 lines
2.2 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 by (c) 2022 and onwards The vChewing Project (MIT License).
// 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(key: String) -> [Megrez.Unigram]
///
func hasUnigramsFor(key: String) -> Bool
}
extension Megrez.Compositor {
///
public class LangModelRanked: LangModelProtocol {
private let langModel: LangModelProtocol
///
/// - Parameter withLM:
public init(withLM: LangModelProtocol) {
langModel = withLM
}
///
/// - Parameter key:
/// - Returns:
public func unigramsFor(key: String) -> [Megrez.Unigram] {
langModel.unigramsFor(key: key).stableSorted { $0.score > $1.score }
}
///
/// - Parameter key:
/// - Returns:
public func hasUnigramsFor(key: String) -> Bool {
langModel.hasUnigramsFor(key: key)
}
}
}
// MARK: - Stable Sort Extension
// Reference: https://stackoverflow.com/a/50545761/4162914
extension Sequence {
/// Return a stable-sorted collection.
///
/// - Parameter areInIncreasingOrder: Return nil when two element are equal.
/// - Returns: The sorted collection.
fileprivate 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)
}
}