From d7550f465b97160630dcc85d83b00dfff08d4cfb Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Sun, 17 Jul 2022 13:41:14 +0800 Subject: [PATCH] LMAssoc. // Also parse data exported from Windows IME. --- .../SubLMs/lmAssociates.swift | 50 +++++++++---------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/Source/Modules/LangModelRelated/SubLMs/lmAssociates.swift b/Source/Modules/LangModelRelated/SubLMs/lmAssociates.swift index 958da8bd..67c2bef9 100644 --- a/Source/Modules/LangModelRelated/SubLMs/lmAssociates.swift +++ b/Source/Modules/LangModelRelated/SubLMs/lmAssociates.swift @@ -27,7 +27,7 @@ import Foundation extension vChewing { @frozen public struct LMAssociates { - var rangeMap: [String: [Range]] = [:] + var rangeMap: [String: [(Range, Int)]] = [:] var strData: String = "" public var count: Int { @@ -56,9 +56,11 @@ extension vChewing { let neta = strData[$0].split(separator: " ") if neta.count >= 2 { let theKey = String(neta[0]) - if !neta[0].isEmpty, !neta[1].isEmpty, theKey.first != "#" { - let theValue = $0 - rangeMap[theKey, default: []].append(theValue) + if !theKey.isEmpty, theKey.first != "#" { + for (i, _) in neta.filter({ $0.first != "#" && !$0.isEmpty }).enumerated() { + if i == 0 { continue } + rangeMap[theKey, default: []].append(($0, i)) + } } } } @@ -78,24 +80,16 @@ extension vChewing { } public func dump() { - var strDump = "" - for entry in rangeMap { - let netaRanges: [Range] = entry.value - for netaRange in netaRanges { - let neta = strData[netaRange] - let addline = neta + "\n" - strDump += addline - } - } - IME.prtDebugIntel(strDump) + // We remove this function in order to reduce out maintenance workload. + // This function will be implemented only if further hard-necessity comes. } public func valuesFor(key: String) -> [String] { var pairs: [String] = [] - if let arrRangeRecords: [Range] = rangeMap[key] { - for netaRange in arrRangeRecords { + if let arrRangeRecords: [(Range, Int)] = rangeMap[key] { + for (netaRange, index) in arrRangeRecords { let neta = strData[netaRange].split(separator: " ") - let theValue: String = .init(neta[1]) + let theValue: String = .init(neta[index]) pairs.append(theValue) } } @@ -108,20 +102,22 @@ extension vChewing { public func valuesFor(pair: Megrez.KeyValuePaired) -> [String] { var pairs: [String] = [] - if let arrRangeRecords: [Range] = rangeMap[pair.toNGramKey] { - for netaRange in arrRangeRecords { + if let arrRangeRecords: [(Range, Int)] = rangeMap[pair.toNGramKey] { + for (netaRange, index) in arrRangeRecords { let neta = strData[netaRange].split(separator: " ") - let theValue: String = .init(neta[1]) - pairs.append(theValue) - } - } else if let arrRangeRecords: [Range] = rangeMap[pair.value] { - for netaRange in arrRangeRecords { - let neta = strData[netaRange].split(separator: " ") - let theValue: String = .init(neta[1]) + let theValue: String = .init(neta[index]) pairs.append(theValue) } } - return pairs + if let arrRangeRecords: [(Range, Int)] = rangeMap[pair.value] { + for (netaRange, index) in arrRangeRecords { + let neta = strData[netaRange].split(separator: " ") + let theValue: String = .init(neta[index]) + pairs.append(theValue) + } + } + var set = Set() + return pairs.filter { set.insert($0).inserted } } public func hasValuesFor(pair: Megrez.KeyValuePaired) -> Bool {