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 /// nil
func buildAssociatePhraseArray(withKey key: String) -> [String] { func buildAssociatePhraseArray(withKey key: String) -> [String] {
var arrResult: [String] = [] var arrResult: [String] = []
if currentLM.hasAssociatedPhrasesForKey(key) { if currentLM.hasAssociatedPhrasesFor(key: key) {
arrResult.append(contentsOf: currentLM.associatedPhrasesForKey(key)) arrResult.append(contentsOf: currentLM.associatedPhrasesFor(key: key))
} }
return arrResult return arrResult
} }

View File

@ -96,7 +96,7 @@ extension vChewing {
// //
override init() {} override init() {}
// 調 // MARK: -
public var isLanguageModelLoaded: Bool { lmCore.isLoaded() } public var isLanguageModelLoaded: Bool { lmCore.isLoaded() }
public func loadLanguageModel(path: String) { 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] { } // 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 { override open func hasUnigramsFor(key: String) -> Bool {
if key == " " { return true } if key == " " { return true }
@ -250,46 +251,37 @@ extension vChewing {
return !unigramsFor(key: key).isEmpty return !unigramsFor(key: key).isEmpty
} }
public func associatedPhrasesForKey(_ key: String) -> [String] { public func associatedPhrasesFor(key: String) -> [String] {
lmAssociates.valuesFor(key: key) ?? [] lmAssociates.valuesFor(key: key) ?? []
} }
public func hasAssociatedPhrasesForKey(_ key: String) -> Bool { public func hasAssociatedPhrasesFor(key: String) -> Bool {
lmAssociates.hasValuesFor(key: key) lmAssociates.hasValuesFor(key: key)
} }
// MARK: - Core Functions (Private) // MARK: -
/// ///
/// - Parameters: /// - Parameters:
/// - unigrams: /// - unigrams:
/// - filteredPairs: /// - filteredPairs:
/// - Returns: /// - Returns:
func filterAndTransform( func filterAndTransform(
unigrams: [Megrez.Unigram], unigrams: [Megrez.Unigram],
filter filteredPairs: Set<Megrez.KeyValuePair> filter filteredPairs: Set<Megrez.KeyValuePair>
) -> [Megrez.Unigram] { ) -> [Megrez.Unigram] {
var results: [Megrez.Unigram] = [] var results: [Megrez.Unigram] = []
var insertedPairs: Set<Megrez.KeyValuePair> = [] var insertedPairs: Set<Megrez.KeyValuePair> = []
for unigram in unigrams { for unigram in unigrams {
var pair: Megrez.KeyValuePair = unigram.keyValue var pair: Megrez.KeyValuePair = unigram.keyValue
if filteredPairs.contains(pair) { if filteredPairs.contains(pair) { continue }
continue
}
if isPhraseReplacementEnabled { if isPhraseReplacementEnabled {
let replacement = lmReplacements.valuesFor(key: pair.value) let replacement = lmReplacements.valuesFor(key: pair.value)
if !replacement.isEmpty { if !replacement.isEmpty { pair.value = replacement }
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 insertedPairs.contains(pair) { continue }
results.append(Megrez.Unigram(keyValue: pair, score: unigram.score))
insertedPairs.insert(pair)
} }
return results return results
} }