From 4f881e44eabf899328dba8c5145714f4d5f18def Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Tue, 10 May 2022 18:10:35 +0800 Subject: [PATCH] LMs // Use split() to boost loading speed. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 非常感謝李昇輯老師的提議。換掉 components() 之後真的變得超快。 --- .../Modules/LangModelRelated/SubLMs/lmAssociates.swift | 8 ++++---- Source/Modules/LangModelRelated/SubLMs/lmCoreEX.swift | 10 +++++----- .../LangModelRelated/SubLMs/lmReplacements.swift | 6 +++--- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Source/Modules/LangModelRelated/SubLMs/lmAssociates.swift b/Source/Modules/LangModelRelated/SubLMs/lmAssociates.swift index 495ca22d..e45554c6 100644 --- a/Source/Modules/LangModelRelated/SubLMs/lmAssociates.swift +++ b/Source/Modules/LangModelRelated/SubLMs/lmAssociates.swift @@ -53,9 +53,9 @@ extension vChewing { do { strData = try String(contentsOfFile: path, encoding: .utf8).replacingOccurrences(of: "\t", with: " ") strData.ranges(splitBy: "\n").forEach { - let neta = strData[$0].components(separatedBy: " ") + let neta = strData[$0].split(separator: " ") if neta.count >= 2 { - let theKey = neta[0] + let theKey = String(neta[0]) if !neta[0].isEmpty, !neta[1].isEmpty, theKey.first != "#" { let theValue = $0 rangeMap[theKey, default: []].append(theValue) @@ -94,8 +94,8 @@ extension vChewing { var pairs: [String] = [] if let arrRangeRecords: [Range] = rangeMap[key] { for netaRange in arrRangeRecords { - let neta = strData[netaRange].components(separatedBy: " ") - let theValue: String = neta[1] + let neta = strData[netaRange].split(separator: " ") + let theValue: String = String(neta[1]) pairs.append(theValue) } } diff --git a/Source/Modules/LangModelRelated/SubLMs/lmCoreEX.swift b/Source/Modules/LangModelRelated/SubLMs/lmCoreEX.swift index 0f07eaaf..f9fb705e 100644 --- a/Source/Modules/LangModelRelated/SubLMs/lmCoreEX.swift +++ b/Source/Modules/LangModelRelated/SubLMs/lmCoreEX.swift @@ -71,9 +71,9 @@ extension vChewing { do { strData = try String(contentsOfFile: path, encoding: .utf8).replacingOccurrences(of: "\t", with: " ") strData.ranges(splitBy: "\n").forEach { - let neta = strData[$0].components(separatedBy: " ") + let neta = strData[$0].split(separator: " ") if neta.count >= 2 { - let theKey = shouldReverse ? neta[1] : neta[0] + let theKey = shouldReverse ? String(neta[1]) : String(neta[0]) if !neta[0].isEmpty, !neta[1].isEmpty, theKey.first != "#" { let theValue = $0 rangeMap[theKey, default: []].append(theValue) @@ -120,12 +120,12 @@ extension vChewing { var grams: [Megrez.Unigram] = [] if let arrRangeRecords: [Range] = rangeMap[key] { for netaRange in arrRangeRecords { - let neta = strData[netaRange].components(separatedBy: " ") - let theValue: String = shouldReverse ? neta[0] : neta[1] + let neta = strData[netaRange].split(separator: " ") + let theValue: String = shouldReverse ? String(neta[0]) : String(neta[1]) let kvPair = Megrez.KeyValuePair(key: key, value: theValue) var theScore = defaultScore if neta.count >= 3, !shouldForceDefaultScore { - theScore = .init(neta[2]) ?? defaultScore + theScore = .init(String(neta[2])) ?? defaultScore } if theScore > 0 { theScore *= -1 // 應對可能忘記寫負號的情形 diff --git a/Source/Modules/LangModelRelated/SubLMs/lmReplacements.swift b/Source/Modules/LangModelRelated/SubLMs/lmReplacements.swift index 989a7625..f70cb1d7 100644 --- a/Source/Modules/LangModelRelated/SubLMs/lmReplacements.swift +++ b/Source/Modules/LangModelRelated/SubLMs/lmReplacements.swift @@ -53,9 +53,9 @@ extension vChewing { do { strData = try String(contentsOfFile: path, encoding: .utf8).replacingOccurrences(of: "\t", with: " ") strData.ranges(splitBy: "\n").forEach { - let neta = strData[$0].components(separatedBy: " ") + let neta = strData[$0].split(separator: " ") if neta.count >= 2 { - let theKey = neta[0] + let theKey = String(neta[0]) if !neta[0].isEmpty, !neta[1].isEmpty, theKey.first != "#" { let theValue = $0 rangeMap[theKey] = theValue @@ -89,7 +89,7 @@ extension vChewing { guard let range = rangeMap[key] else { return "" } - let arrNeta = strData[range].components(separatedBy: " ") + let arrNeta = strData[range].split(separator: " ") guard arrNeta.count >= 2 else { return "" }