vChewing-macOS/Packages/vChewing_LangModelAssembly/Sources/LangModelAssembly/LMInstantiator_CassetteExte...

53 lines
2.1 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.

// (c) 2021 and onwards The vChewing Project (MIT-NTL License).
// ====================
// This code is released under the MIT license (SPDX-License-Identifier: MIT)
// ... with NTL restriction stating that:
// No trademark license is granted to use the trade names, trademarks, service
// marks, or product names of Contributor, except as required to fulfill notice
// requirements defined in MIT License.
import Foundation
import Megrez
import Shared
public extension vChewingLM.LMInstantiator {
///
var cassetteWildcardKey: String { Self.lmCassette.wildcardKey }
///
var maxCassetteKeyLength: Int { Self.lmCassette.maxKeyLength }
///
/// - Parameter char:
/// - Returns:
func convertCassetteKeyToDisplay(char: String) -> String {
Self.lmCassette.convertKeyToDisplay(char: char)
}
///
/// - Parameter key:
/// - Returns:
func isThisCassetteKeyAllowed(key: String) -> Bool {
Self.lmCassette.allowedKeys.contains(key)
}
///
/// - Parameter key:
/// - Returns:
func hasCassetteWildcardResultsFor(key: String) -> Bool {
Self.lmCassette.hasUnigramsFor(key: key + Self.lmCassette.wildcard)
}
///
/// - Parameter value:
/// - Returns:
func cassetteReverseLookup(for value: String) -> [String] {
var lookupResult = Self.lmCassette.reverseLookupMap[value] ?? []
guard !lookupResult.isEmpty else { return [] }
lookupResult = lookupResult.map { $0.trimmingCharacters(in: .newlines) }
return lookupResult.stableSort(by: { $0.count < $1.count }).stableSort {
Self.lmCassette.unigramsFor(key: $0).count
< Self.lmCassette.unigramsFor(key: $1).count
}
}
}