LMAssembly & LMMgr // Add countPhrasePairs(), etc.

This commit is contained in:
ShikiSuen 2023-03-23 12:07:27 +08:00
parent 6a2a2de195
commit 78a950e355
8 changed files with 38 additions and 9 deletions

View File

@ -305,6 +305,17 @@ public extension vChewingLM {
: unigramsFor(keyArray: keyArray).map(\.value).contains(value)
}
///
/// - Parameters:
/// - keyArray:
/// - factoryDictionaryOnly:
/// - Returns:
public func countKeyValuePairs(keyArray: [String], factoryDictionaryOnly: Bool = false) -> Int {
factoryDictionaryOnly
? lmCore.unigramsFor(key: keyArray.joined(separator: "-")).count
: unigramsFor(keyArray: keyArray).count
}
/// LMI
/// - Parameter key:
/// - Returns:

View File

@ -15,7 +15,7 @@ public protocol PhraseEditorDelegate {
func retrieveData(mode: Shared.InputMode, type: vChewingLM.ReplacableUserDataType) -> String
@discardableResult func saveData(mode: Shared.InputMode, type: vChewingLM.ReplacableUserDataType, data: String)
-> String
func checkIfUserPhraseExist(userPhrase: String, mode: Shared.InputMode, key unigramKey: String) -> Bool
func checkIfPhrasePairExists(userPhrase: String, mode: Shared.InputMode, key unigramKey: String) -> Bool
func consolidate(text strProcessed: inout String, pragma shouldCheckPragma: Bool)
func openPhraseFile(mode: Shared.InputMode, type: vChewingLM.ReplacableUserDataType, app: String)
func tagOverrides(in strProcessed: inout String, mode: Shared.InputMode)

View File

@ -140,7 +140,7 @@ public struct VwrPhraseEditorUI: View {
}
if !txtAddPhraseField4.isEmpty { arrResult.append("#" + txtAddPhraseField4) }
if let delegate = delegate,
delegate.checkIfUserPhraseExist(
delegate.checkIfPhrasePairExists(
userPhrase: txtAddPhraseField1, mode: selInputMode, key: txtAddPhraseField2
)
{

View File

@ -77,7 +77,7 @@ public struct IMEStateData: IMEStateDataProtocol {
public var markedTargetExists: Bool {
let pair = userPhraseKVPair
return LMMgr.checkIfUserPhraseExist(
return LMMgr.checkIfPhrasePairExists(
userPhrase: pair.value, mode: IMEApp.currentInputMode, keyArray: pair.keyArray
)
}

View File

@ -274,7 +274,7 @@ public class LMMgr {
)
}
public static func checkIfUserPhraseExist(
public static func checkIfPhrasePairExists(
userPhrase: String,
mode: Shared.InputMode,
keyArray: [String],
@ -293,6 +293,24 @@ public class LMMgr {
}
}
public static func countPhrasePairs(
keyArray: [String],
mode: Shared.InputMode,
factoryDictionaryOnly: Bool = false
) -> Int {
switch mode {
case .imeModeCHS:
return lmCHS.countKeyValuePairs(
keyArray: keyArray, factoryDictionaryOnly: factoryDictionaryOnly
)
case .imeModeCHT:
return lmCHT.countKeyValuePairs(
keyArray: keyArray, factoryDictionaryOnly: factoryDictionaryOnly
)
case .imeModeNULL: return 0
}
}
public static func setPhraseReplacementEnabled(_ state: Bool) {
Self.lmCHT.isPhraseReplacementEnabled = state
Self.lmCHS.isPhraseReplacementEnabled = state

View File

@ -25,8 +25,8 @@ extension LMMgr: PhraseEditorDelegate {
vChewingLM.LMConsolidator.consolidate(text: &strProcessed, pragma: shouldCheckPragma)
}
public func checkIfUserPhraseExist(userPhrase: String, mode: Shared.InputMode, key unigramKey: String) -> Bool {
Self.checkIfUserPhraseExist(userPhrase: userPhrase, mode: mode, keyArray: [unigramKey])
public func checkIfPhrasePairExists(userPhrase: String, mode: Shared.InputMode, key unigramKey: String) -> Bool {
Self.checkIfPhrasePairExists(userPhrase: userPhrase, mode: mode, keyArray: [unigramKey])
}
public func retrieveData(mode: Shared.InputMode, type: vChewingLM.ReplacableUserDataType) -> String {
@ -95,7 +95,7 @@ extension LMMgr: PhraseEditorDelegate {
for currentLine in strProcessed.split(separator: "\n") {
let arr = currentLine.split(separator: " ")
guard arr.count >= 2 else { continue }
let exists = Self.checkIfUserPhraseExist(
let exists = Self.checkIfPhrasePairExists(
userPhrase: arr[0].description, mode: mode,
keyArray: arr[1].split(separator: "-").map(\.description),
factoryDictionaryOnly: true

View File

@ -21,7 +21,7 @@ public extension LMMgr {
public var weight: Double?
private var isDuplicated: Bool {
LMMgr.checkIfUserPhraseExist(userPhrase: value, mode: inputMode, keyArray: keyArray)
LMMgr.checkIfPhrasePairExists(userPhrase: value, mode: inputMode, keyArray: keyArray)
}
public var description: String {

View File

@ -214,7 +214,7 @@ extension CtlPrefWindow: NSTextViewDelegate, NSTextFieldDelegate {
arrResult.append(weightVal.description)
}
if !txtPECommentField.stringValue.isEmpty { arrResult.append("#" + txtPECommentField.stringValue) }
if LMMgr.shared.checkIfUserPhraseExist(
if LMMgr.shared.checkIfPhrasePairExists(
userPhrase: txtPEField1.stringValue, mode: selInputMode, key: txtPEField2.stringValue
) {
arrResult.append(" #𝙾𝚟𝚎𝚛𝚛𝚒𝚍𝚎")