LMAssociates // Upgrade parsing methods.

This commit is contained in:
ShikiSuen 2022-12-28 14:49:51 +08:00
parent 6d874e78d9
commit 32de1534f5
1 changed files with 13 additions and 23 deletions

View File

@ -1,5 +1,4 @@
// (c) 2021 and onwards The vChewing Project (MIT-NTL License). // (c) 2021 and onwards The vChewing Project (MIT-NTL License).
// StringView Ranges extension by (c) 2022 and onwards Isaac Xen (MIT License).
// ==================== // ====================
// This code is released under the MIT license (SPDX-License-Identifier: MIT) // This code is released under the MIT license (SPDX-License-Identifier: MIT)
// ... with NTL restriction stating that: // ... with NTL restriction stating that:
@ -25,7 +24,7 @@ extension vChewingLM {
public var isLoaded: Bool { !rangeMap.isEmpty } public var isLoaded: Bool { !rangeMap.isEmpty }
internal func cnvNGramKeyFromPinyinToPhona(target: String) -> String { internal static func cnvNGramKeyFromPinyinToPhona(target: String) -> String {
guard target.contains("("), target.contains(","), target.contains(")") else { guard target.contains("("), target.contains(","), target.contains(")") else {
return target return target
} }
@ -64,18 +63,23 @@ extension vChewingLM {
public mutating func replaceData(textData rawStrData: String) { public mutating func replaceData(textData rawStrData: String) {
if strData == rawStrData { return } if strData == rawStrData { return }
strData = rawStrData strData = rawStrData
strData.ranges(splitBy: "\n").filter { !$0.isEmpty }.forEach { var newMap: [String: [(Range<String.Index>, Int)]] = [:]
let neta = strData[$0].split(separator: " ") strData.parse(splitee: "\n") { theRange in
if neta.count >= 2 { let theCells = rawStrData[theRange].split(separator: " ")
let theKey = String(neta[0]) if theCells.count >= 2 {
if !theKey.isEmpty, theKey.first != "#" { let theKey = theCells[0].description
for (i, _) in neta.filter({ $0.first != "#" && !$0.isEmpty }).enumerated() { if theKey.first != "#" {
for (i, _) in theCells.enumerated() {
if i == 0 { continue } if i == 0 { continue }
rangeMap[cnvNGramKeyFromPinyinToPhona(target: theKey), default: []].append(($0, i)) if theCells[i].first == "#" { continue }
let newKey = Self.cnvNGramKeyFromPinyinToPhona(target: theKey)
newMap[newKey, default: []].append((theRange, i))
} }
} }
} }
} }
rangeMap = newMap
newMap.removeAll()
} }
public mutating func clear() { public mutating func clear() {
@ -119,17 +123,3 @@ extension vChewingLM {
} }
} }
} }
// MARK: - StringView Ranges Extension (by Isaac Xen)
extension String {
fileprivate func ranges(splitBy separator: Element) -> [Range<String.Index>] {
var startIndex = startIndex
return split(separator: separator).reduce(into: []) { ranges, substring in
_ = range(of: substring, range: startIndex..<endIndex).map { range in
ranges.append(range)
startIndex = range.upperBound
}
}
}
}