LMInstantiator // Add ability for supplying NumPad results.

This commit is contained in:
ShikiSuen 2024-01-28 00:32:23 +08:00
parent 586822c981
commit cfe9a1ce5d
3 changed files with 58 additions and 0 deletions

View File

@ -31,6 +31,9 @@ public extension vChewingLM {
/// ///
class LMInstantiator: LangModelProtocol { class LMInstantiator: LangModelProtocol {
public struct Config { public struct Config {
/// nil
/// true = false =
public var numPadFWHWStatus: Bool?
public var isCassetteEnabled = false public var isCassetteEnabled = false
public var isPhraseReplacementEnabled = false public var isPhraseReplacementEnabled = false
public var isCNSEnabled = false public var isCNSEnabled = false
@ -338,6 +341,8 @@ public extension vChewingLM {
rawAllUnigrams += lmUserPhrases.unigramsFor(key: keyChain).reversed() rawAllUnigrams += lmUserPhrases.unigramsFor(key: keyChain).reversed()
if !config.isCassetteEnabled || config.isCassetteEnabled && keyChain.map(\.description)[0] == "_" { if !config.isCassetteEnabled || config.isCassetteEnabled && keyChain.map(\.description)[0] == "_" {
// NumPad
rawAllUnigrams += supplyNumPadUnigrams(key: keyChain)
// LMMisc LMCore score (-10.0, 0.0) // LMMisc LMCore score (-10.0, 0.0)
rawAllUnigrams += factoryUnigramsFor(key: keyChain, column: .theDataCHEW) rawAllUnigrams += factoryUnigramsFor(key: keyChain, column: .theDataCHEW)
rawAllUnigrams += factoryCoreUnigramsFor(key: keyChain) rawAllUnigrams += factoryCoreUnigramsFor(key: keyChain)

View File

@ -0,0 +1,23 @@
// (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
public extension vChewingLM.LMInstantiator {
func supplyNumPadUnigrams(key: String) -> [Megrez.Unigram] {
guard let status = config.numPadFWHWStatus else { return [] }
let initials = "_NumPad_"
guard key.hasPrefix(initials) else { return [] }
let char = key.replacingOccurrences(of: initials, with: "")
guard char.count == 1 else { return [] }
let gram1 = Megrez.Unigram(value: char.applyingTransformFW2HW(reverse: status), score: 0)
let gram2 = Megrez.Unigram(value: char.applyingTransformFW2HW(reverse: !status), score: -0.1)
return [gram1, gram2]
}
}

View File

@ -0,0 +1,30 @@
//// (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 XCTest
@testable import LangModelAssembly
final class LMInstantiatorNumericPadTests: XCTestCase {
func testSQL() throws {
let instance = vChewingLM.LMInstantiator(isCHS: true)
instance.setOptions { config in
config.numPadFWHWStatus = nil
}
XCTAssertEqual(instance.unigramsFor(keyArray: ["_NumPad_0"]).description, "[]")
instance.setOptions { config in
config.numPadFWHWStatus = true
}
XCTAssertEqual(instance.unigramsFor(keyArray: ["_NumPad_0"]).description, "[(,0.0), (0,-0.1)]")
instance.setOptions { config in
config.numPadFWHWStatus = false
}
XCTAssertEqual(instance.unigramsFor(keyArray: ["_NumPad_0"]).description, "[(0,0.0), (,-0.1)]")
}
}