LMs // Use split() to boost loading speed.

- 非常感謝李昇輯老師的提議。換掉 components() 之後真的變得超快。
This commit is contained in:
ShikiSuen 2022-05-10 18:10:35 +08:00
parent 788b9a60ee
commit 4f881e44ea
3 changed files with 12 additions and 12 deletions

View File

@ -53,9 +53,9 @@ extension vChewing {
do { do {
strData = try String(contentsOfFile: path, encoding: .utf8).replacingOccurrences(of: "\t", with: " ") strData = try String(contentsOfFile: path, encoding: .utf8).replacingOccurrences(of: "\t", with: " ")
strData.ranges(splitBy: "\n").forEach { strData.ranges(splitBy: "\n").forEach {
let neta = strData[$0].components(separatedBy: " ") let neta = strData[$0].split(separator: " ")
if neta.count >= 2 { if neta.count >= 2 {
let theKey = neta[0] let theKey = String(neta[0])
if !neta[0].isEmpty, !neta[1].isEmpty, theKey.first != "#" { if !neta[0].isEmpty, !neta[1].isEmpty, theKey.first != "#" {
let theValue = $0 let theValue = $0
rangeMap[theKey, default: []].append(theValue) rangeMap[theKey, default: []].append(theValue)
@ -94,8 +94,8 @@ extension vChewing {
var pairs: [String] = [] var pairs: [String] = []
if let arrRangeRecords: [Range<String.Index>] = rangeMap[key] { if let arrRangeRecords: [Range<String.Index>] = rangeMap[key] {
for netaRange in arrRangeRecords { for netaRange in arrRangeRecords {
let neta = strData[netaRange].components(separatedBy: " ") let neta = strData[netaRange].split(separator: " ")
let theValue: String = neta[1] let theValue: String = String(neta[1])
pairs.append(theValue) pairs.append(theValue)
} }
} }

View File

@ -71,9 +71,9 @@ extension vChewing {
do { do {
strData = try String(contentsOfFile: path, encoding: .utf8).replacingOccurrences(of: "\t", with: " ") strData = try String(contentsOfFile: path, encoding: .utf8).replacingOccurrences(of: "\t", with: " ")
strData.ranges(splitBy: "\n").forEach { strData.ranges(splitBy: "\n").forEach {
let neta = strData[$0].components(separatedBy: " ") let neta = strData[$0].split(separator: " ")
if neta.count >= 2 { 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 != "#" { if !neta[0].isEmpty, !neta[1].isEmpty, theKey.first != "#" {
let theValue = $0 let theValue = $0
rangeMap[theKey, default: []].append(theValue) rangeMap[theKey, default: []].append(theValue)
@ -120,12 +120,12 @@ extension vChewing {
var grams: [Megrez.Unigram] = [] var grams: [Megrez.Unigram] = []
if let arrRangeRecords: [Range<String.Index>] = rangeMap[key] { if let arrRangeRecords: [Range<String.Index>] = rangeMap[key] {
for netaRange in arrRangeRecords { for netaRange in arrRangeRecords {
let neta = strData[netaRange].components(separatedBy: " ") let neta = strData[netaRange].split(separator: " ")
let theValue: String = shouldReverse ? neta[0] : neta[1] let theValue: String = shouldReverse ? String(neta[0]) : String(neta[1])
let kvPair = Megrez.KeyValuePair(key: key, value: theValue) let kvPair = Megrez.KeyValuePair(key: key, value: theValue)
var theScore = defaultScore var theScore = defaultScore
if neta.count >= 3, !shouldForceDefaultScore { if neta.count >= 3, !shouldForceDefaultScore {
theScore = .init(neta[2]) ?? defaultScore theScore = .init(String(neta[2])) ?? defaultScore
} }
if theScore > 0 { if theScore > 0 {
theScore *= -1 // theScore *= -1 //

View File

@ -53,9 +53,9 @@ extension vChewing {
do { do {
strData = try String(contentsOfFile: path, encoding: .utf8).replacingOccurrences(of: "\t", with: " ") strData = try String(contentsOfFile: path, encoding: .utf8).replacingOccurrences(of: "\t", with: " ")
strData.ranges(splitBy: "\n").forEach { strData.ranges(splitBy: "\n").forEach {
let neta = strData[$0].components(separatedBy: " ") let neta = strData[$0].split(separator: " ")
if neta.count >= 2 { if neta.count >= 2 {
let theKey = neta[0] let theKey = String(neta[0])
if !neta[0].isEmpty, !neta[1].isEmpty, theKey.first != "#" { if !neta[0].isEmpty, !neta[1].isEmpty, theKey.first != "#" {
let theValue = $0 let theValue = $0
rangeMap[theKey] = theValue rangeMap[theKey] = theValue
@ -89,7 +89,7 @@ extension vChewing {
guard let range = rangeMap[key] else { guard let range = rangeMap[key] else {
return "" return ""
} }
let arrNeta = strData[range].components(separatedBy: " ") let arrNeta = strData[range].split(separator: " ")
guard arrNeta.count >= 2 else { guard arrNeta.count >= 2 else {
return "" return ""
} }