LMI // Maintenance.

This commit is contained in:
ShikiSuen 2022-06-21 09:42:09 +08:00
parent 413115cbca
commit c387865383
2 changed files with 18 additions and 26 deletions

View File

@ -156,8 +156,8 @@ class KeyHandler {
/// nil
func buildAssociatePhraseArray(withKey key: String) -> [String] {
var arrResult: [String] = []
if currentLM.hasAssociatedPhrasesForKey(key) {
arrResult.append(contentsOf: currentLM.associatedPhrasesForKey(key))
if currentLM.hasAssociatedPhrasesFor(key: key) {
arrResult.append(contentsOf: currentLM.associatedPhrasesFor(key: key))
}
return arrResult
}

View File

@ -96,7 +96,7 @@ extension vChewing {
//
override init() {}
// 調
// MARK: -
public var isLanguageModelLoaded: Bool { lmCore.isLoaded() }
public func loadLanguageModel(path: String) {
@ -185,7 +185,7 @@ extension vChewing {
}
}
// MARK: - Core Functions (Public)
// MARK: -
///
// public func bigramsForKeys(preceedingKey: String, key: String) -> [Megrez.Bigram] { }
@ -238,8 +238,9 @@ extension vChewing {
)
}
/// If the model has unigrams for the given key.
/// @param key The key.
///
/// - Parameter key:
/// - Returns:
override open func hasUnigramsFor(key: String) -> Bool {
if key == " " { return true }
@ -250,46 +251,37 @@ extension vChewing {
return !unigramsFor(key: key).isEmpty
}
public func associatedPhrasesForKey(_ key: String) -> [String] {
public func associatedPhrasesFor(key: String) -> [String] {
lmAssociates.valuesFor(key: key) ?? []
}
public func hasAssociatedPhrasesForKey(_ key: String) -> Bool {
public func hasAssociatedPhrasesFor(key: String) -> Bool {
lmAssociates.hasValuesFor(key: key)
}
// MARK: - Core Functions (Private)
// MARK: -
///
/// - Parameters:
/// - unigrams:
/// - filteredPairs:
/// - Returns:
/// - unigrams:
/// - filteredPairs:
/// - Returns:
func filterAndTransform(
unigrams: [Megrez.Unigram],
filter filteredPairs: Set<Megrez.KeyValuePair>
) -> [Megrez.Unigram] {
var results: [Megrez.Unigram] = []
var insertedPairs: Set<Megrez.KeyValuePair> = []
for unigram in unigrams {
var pair: Megrez.KeyValuePair = unigram.keyValue
if filteredPairs.contains(pair) {
continue
}
if filteredPairs.contains(pair) { continue }
if isPhraseReplacementEnabled {
let replacement = lmReplacements.valuesFor(key: pair.value)
if !replacement.isEmpty {
IME.prtDebugIntel("\(pair.value) -> \(replacement)")
pair.value = replacement
}
}
if !insertedPairs.contains(pair) {
results.append(Megrez.Unigram(keyValue: pair, score: unigram.score))
insertedPairs.insert(pair)
if !replacement.isEmpty { pair.value = replacement }
}
if insertedPairs.contains(pair) { continue }
results.append(Megrez.Unigram(keyValue: pair, score: unigram.score))
insertedPairs.insert(pair)
}
return results
}