LMAssembly // Let LMInstantiator summarize user data.
This commit is contained in:
parent
c932083c5f
commit
97a9e9aa5c
|
@ -0,0 +1,33 @@
|
|||
// (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
|
||||
|
||||
public extension LMAssembly {
|
||||
struct UserDictionarySummarized: Codable {
|
||||
let isCHS: Bool
|
||||
let userPhrases: [String: [String]]
|
||||
let filter: [String: [String]]
|
||||
let userSymbols: [String: [String]]
|
||||
let replacements: [String: String]
|
||||
let associates: [String: [String]]
|
||||
}
|
||||
}
|
||||
|
||||
public extension LMAssembly.LMInstantiator {
|
||||
func summarize(all: Bool) -> LMAssembly.UserDictionarySummarized {
|
||||
LMAssembly.UserDictionarySummarized(
|
||||
isCHS: isCHS,
|
||||
userPhrases: lmUserPhrases.dictRepresented,
|
||||
filter: lmFiltered.dictRepresented,
|
||||
userSymbols: lmUserSymbols.dictRepresented,
|
||||
replacements: lmReplacements.dictRepresented,
|
||||
associates: all ? lmAssociates.dictRepresented : [:]
|
||||
)
|
||||
}
|
||||
}
|
|
@ -11,7 +11,7 @@ import Megrez
|
|||
extension LMAssembly {
|
||||
struct LMAssociates {
|
||||
public private(set) var filePath: String?
|
||||
var rangeMap: [String: [(Range<String.Index>, Int)]] = [:]
|
||||
var rangeMap: [String: [(Range<String.Index>, Int)]] = [:] // Range 只可能是一整行,所以必須得有 index。
|
||||
var strData: String = ""
|
||||
|
||||
public var count: Int { rangeMap.count }
|
||||
|
@ -97,15 +97,9 @@ extension LMAssembly {
|
|||
|
||||
public func valuesFor(pair: Megrez.KeyValuePaired) -> [String] {
|
||||
var pairs: [String] = []
|
||||
if let arrRangeRecords: [(Range<String.Index>, Int)] = rangeMap[pair.toNGramKey] {
|
||||
for (netaRange, index) in arrRangeRecords {
|
||||
let neta = strData[netaRange].split(separator: " ")
|
||||
let theValue: String = .init(neta[index])
|
||||
pairs.append(theValue)
|
||||
}
|
||||
}
|
||||
if let arrRangeRecords: [(Range<String.Index>, Int)] = rangeMap[pair.value] {
|
||||
for (netaRange, index) in arrRangeRecords {
|
||||
let availableResults = [rangeMap[pair.toNGramKey], rangeMap[pair.value]].compactMap { $0 }
|
||||
availableResults.forEach { arrRangeRecords in
|
||||
arrRangeRecords.forEach { netaRange, index in
|
||||
let neta = strData[netaRange].split(separator: " ")
|
||||
let theValue: String = .init(neta[index])
|
||||
pairs.append(theValue)
|
||||
|
@ -120,3 +114,17 @@ extension LMAssembly {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension LMAssembly.LMAssociates {
|
||||
var dictRepresented: [String: [String]] {
|
||||
var result = [String: [String]]()
|
||||
rangeMap.forEach { key, arrRangeRecords in
|
||||
arrRangeRecords.forEach { netaRange, index in
|
||||
let neta = strData[netaRange].split(separator: " ")
|
||||
let theValue: String = .init(neta[index])
|
||||
result[key, default: []].append(theValue)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
|
|
@ -184,3 +184,15 @@ extension LMAssembly {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension LMAssembly.LMCoreEX {
|
||||
var dictRepresented: [String: [String]] {
|
||||
var result = [String: [String]]()
|
||||
rangeMap.forEach { key, arrValueRanges in
|
||||
result[key, default: []] = arrValueRanges.map { currentRange in
|
||||
strData[currentRange].description
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
|
|
@ -98,3 +98,13 @@ extension LMAssembly {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension LMAssembly.LMReplacements {
|
||||
var dictRepresented: [String: String] {
|
||||
var result = [String: String]()
|
||||
rangeMap.forEach { key, valueRange in
|
||||
result[key] = strData[valueRange].description
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue