LMAssoc. // Allow querying with KeyValuePaired.toNGramKey.
This commit is contained in:
parent
6dd8ec6c36
commit
ebde0d088e
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
{
|
||||
|
|
|
@ -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
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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() }
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue