LMAssoc. // Allow querying with KeyValuePaired.toNGramKey.

This commit is contained in:
ShikiSuen 2022-07-16 17:58:23 +08:00
parent 6dd8ec6c36
commit ebde0d088e
6 changed files with 44 additions and 10 deletions

View File

@ -149,10 +149,10 @@ class KeyHandler {
/// - Parameter key:
/// - Returns:
/// nil
func buildAssociatePhraseArray(withKey key: String) -> [(String, String)] {
func buildAssociatePhraseArray(withPair pair: Megrez.KeyValuePaired) -> [(String, String)] {
var arrResult: [(String, String)] = []
if currentLM.hasAssociatedPhrasesFor(key: key) {
arrResult = currentLM.associatedPhrasesFor(key: key).map { ("", $0) }
if currentLM.hasAssociatedPhrasesFor(pair: pair) {
arrResult = currentLM.associatedPhrasesFor(pair: pair).map { ("", $0) }
}
return arrResult
}

View File

@ -225,6 +225,7 @@ extension KeyHandler {
)
if choosingCandidates.candidates.count == 1 {
clear()
let reading: String = choosingCandidates.candidates.first?.0 ?? ""
let text: String = choosingCandidates.candidates.first?.1 ?? ""
stateCallback(InputState.Committing(textToCommit: text))
@ -233,7 +234,7 @@ extension KeyHandler {
} else {
if let associatedPhrases =
buildAssociatePhraseState(
withKey: text,
withPair: .init(key: reading, value: text),
isTypingVertical: input.isTypingVertical
), !associatedPhrases.candidates.isEmpty
{

View File

@ -198,12 +198,12 @@ extension KeyHandler {
/// - isTypingVertical:
/// - Returns:
func buildAssociatePhraseState(
withKey key: String!,
withPair pair: Megrez.KeyValuePaired,
isTypingVertical: Bool
) -> InputState.AssociatedPhrases! {
//  Xcode
InputState.AssociatedPhrases(
candidates: buildAssociatePhraseArray(withKey: key), isTypingVertical: isTypingVertical
candidates: buildAssociatePhraseArray(withPair: pair), isTypingVertical: isTypingVertical
)
}

View File

@ -122,7 +122,8 @@ extension ctlInputMethod: ctlCandidateDelegate {
handle(state: InputState.Committing(textToCommit: composingBuffer))
if mgrPrefs.associatedPhrasesEnabled,
let associatePhrases = keyHandler.buildAssociatePhraseState(
withKey: composingBuffer, isTypingVertical: state.isTypingVertical
withPair: .init(key: selectedValue.0, value: selectedValue.1),
isTypingVertical: state.isTypingVertical
), !associatePhrases.candidates.isEmpty
{
handle(state: associatePhrases)
@ -140,7 +141,8 @@ extension ctlInputMethod: ctlCandidateDelegate {
handle(state: InputState.Committing(textToCommit: selectedValue.1))
if mgrPrefs.associatedPhrasesEnabled,
let associatePhrases = keyHandler.buildAssociatePhraseState(
withKey: selectedValue.1, isTypingVertical: state.isTypingVertical
withPair: .init(key: selectedValue.0, value: selectedValue.1),
isTypingVertical: state.isTypingVertical
), !associatePhrases.candidates.isEmpty
{
handle(state: associatePhrases)

View File

@ -249,13 +249,21 @@ extension vChewing {
}
public func associatedPhrasesFor(key: String) -> [String] {
lmAssociates.valuesFor(key: key) ?? []
lmAssociates.valuesFor(key: key)
}
public func hasAssociatedPhrasesFor(key: String) -> Bool {
lmAssociates.hasValuesFor(key: key)
}
public func associatedPhrasesFor(pair: Megrez.KeyValuePaired) -> [String] {
lmAssociates.valuesFor(pair: pair)
}
public func hasAssociatedPhrasesFor(pair: Megrez.KeyValuePaired) -> Bool {
lmAssociates.hasValuesFor(pair: pair)
}
/// 滿 LangModelProtocol
public func bigramsFor(precedingKey _: String, key _: String) -> [Megrez.Bigram] { .init() }

View File

@ -90,7 +90,7 @@ extension vChewing {
IME.prtDebugIntel(strDump)
}
public func valuesFor(key: String) -> [String]? {
public func valuesFor(key: String) -> [String] {
var pairs: [String] = []
if let arrRangeRecords: [Range<String.Index>] = rangeMap[key] {
for netaRange in arrRangeRecords {
@ -105,6 +105,29 @@ extension vChewing {
public func hasValuesFor(key: String) -> Bool {
rangeMap[key] != nil
}
public func valuesFor(pair: Megrez.KeyValuePaired) -> [String] {
var pairs: [String] = []
if let arrRangeRecords: [Range<String.Index>] = rangeMap[pair.toNGramKey] {
for netaRange in arrRangeRecords {
let neta = strData[netaRange].split(separator: " ")
let theValue: String = .init(neta[1])
pairs.append(theValue)
}
} else if let arrRangeRecords: [Range<String.Index>] = rangeMap[pair.value] {
for netaRange in arrRangeRecords {
let neta = strData[netaRange].split(separator: " ")
let theValue: String = .init(neta[1])
pairs.append(theValue)
}
}
return pairs
}
public func hasValuesFor(pair: Megrez.KeyValuePaired) -> Bool {
if rangeMap[pair.toNGramKey] != nil { return true }
return rangeMap[pair.value] != nil
}
}
}