LMAssembly // Add replaceData() to certain submodels.
- LMCoreEX // Add replaceData(), etc. - LMReplacements // Add replaceData(), etc. - LMAssociates // Add replaceData(), etc.
This commit is contained in:
parent
2913c22d40
commit
cd85f821d2
|
@ -42,20 +42,8 @@ extension vChewingLM {
|
||||||
LMConsolidator.consolidate(path: path, pragma: true)
|
LMConsolidator.consolidate(path: path, pragma: true)
|
||||||
|
|
||||||
do {
|
do {
|
||||||
strData = try String(contentsOfFile: path, encoding: .utf8).replacingOccurrences(of: "\t", with: " ")
|
let rawStrData = try String(contentsOfFile: path, encoding: .utf8)
|
||||||
strData = strData.replacingOccurrences(of: "\r", with: "\n")
|
replaceData(textData: rawStrData)
|
||||||
strData.ranges(splitBy: "\n").filter { !$0.isEmpty }.forEach {
|
|
||||||
let neta = strData[$0].split(separator: " ")
|
|
||||||
if neta.count >= 2 {
|
|
||||||
let theKey = String(neta[0])
|
|
||||||
if !theKey.isEmpty, theKey.first != "#" {
|
|
||||||
for (i, _) in neta.filter({ $0.first != "#" && !$0.isEmpty }).enumerated() {
|
|
||||||
if i == 0 { continue }
|
|
||||||
rangeMap[cnvNGramKeyFromPinyinToPhona(target: theKey), default: []].append(($0, i))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch {
|
} catch {
|
||||||
vCLog("\(error)")
|
vCLog("\(error)")
|
||||||
vCLog("↑ Exception happened when reading data at: \(path).")
|
vCLog("↑ Exception happened when reading data at: \(path).")
|
||||||
|
@ -65,6 +53,26 @@ extension vChewingLM {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 將資料從檔案讀入至資料庫辭典內。
|
||||||
|
/// - parameters:
|
||||||
|
/// - path: 給定路徑。
|
||||||
|
public mutating func replaceData(textData rawStrData: String) {
|
||||||
|
if strData == rawStrData { return }
|
||||||
|
strData = rawStrData
|
||||||
|
strData.ranges(splitBy: "\n").filter { !$0.isEmpty }.forEach {
|
||||||
|
let neta = strData[$0].split(separator: " ")
|
||||||
|
if neta.count >= 2 {
|
||||||
|
let theKey = String(neta[0])
|
||||||
|
if !theKey.isEmpty, theKey.first != "#" {
|
||||||
|
for (i, _) in neta.filter({ $0.first != "#" && !$0.isEmpty }).enumerated() {
|
||||||
|
if i == 0 { continue }
|
||||||
|
rangeMap[cnvNGramKeyFromPinyinToPhona(target: theKey), default: []].append(($0, i))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public mutating func clear() {
|
public mutating func clear() {
|
||||||
rangeMap.removeAll()
|
rangeMap.removeAll()
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,27 +60,21 @@ extension vChewingLM {
|
||||||
/// - path: 給定路徑。
|
/// - path: 給定路徑。
|
||||||
@discardableResult public mutating func open(_ path: String) -> Bool {
|
@discardableResult public mutating func open(_ path: String) -> Bool {
|
||||||
if isLoaded { return false }
|
if isLoaded { return false }
|
||||||
|
var consolidated = false
|
||||||
|
|
||||||
if allowConsolidation {
|
if allowConsolidation {
|
||||||
LMConsolidator.fixEOF(path: path)
|
LMConsolidator.fixEOF(path: path)
|
||||||
LMConsolidator.consolidate(path: path, pragma: true)
|
LMConsolidator.consolidate(path: path, pragma: true)
|
||||||
|
consolidated = true
|
||||||
}
|
}
|
||||||
|
|
||||||
do {
|
do {
|
||||||
strData = try String(contentsOfFile: path, encoding: .utf8).replacingOccurrences(of: "\t", with: " ")
|
var rawStrData = try String(contentsOfFile: path, encoding: .utf8)
|
||||||
strData = strData.replacingOccurrences(of: "\r", with: "\n")
|
if !consolidated {
|
||||||
strData.ranges(splitBy: "\n").filter { !$0.isEmpty }.forEach {
|
rawStrData = rawStrData.replacingOccurrences(of: "\t", with: " ")
|
||||||
let neta = strData[$0].split(separator: " ")
|
rawStrData = rawStrData.replacingOccurrences(of: "\r", with: "\n")
|
||||||
if neta.count >= 2, String(neta[0]).first != "#" {
|
|
||||||
if !neta[0].isEmpty, !neta[1].isEmpty {
|
|
||||||
var theKey = shouldReverse ? String(neta[1]) : String(neta[0])
|
|
||||||
let theValue = $0
|
|
||||||
theKey.converToPhonabets()
|
|
||||||
rangeMap[theKey, default: []].append(theValue)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
temporaryMap.removeAll()
|
replaceData(textData: rawStrData)
|
||||||
} catch {
|
} catch {
|
||||||
vCLog("\(error)")
|
vCLog("\(error)")
|
||||||
vCLog("↑ Exception happened when reading data at: \(path).")
|
vCLog("↑ Exception happened when reading data at: \(path).")
|
||||||
|
@ -90,6 +84,26 @@ extension vChewingLM {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 將資料從檔案讀入至資料庫辭典內。
|
||||||
|
/// - parameters:
|
||||||
|
/// - path: 給定路徑。
|
||||||
|
public mutating func replaceData(textData rawStrData: String) {
|
||||||
|
if strData == rawStrData { return }
|
||||||
|
strData = rawStrData
|
||||||
|
strData.ranges(splitBy: "\n").filter { !$0.isEmpty }.forEach {
|
||||||
|
let neta = strData[$0].split(separator: " ")
|
||||||
|
if neta.count >= 2, String(neta[0]).first != "#" {
|
||||||
|
if !neta[0].isEmpty, !neta[1].isEmpty {
|
||||||
|
var theKey = shouldReverse ? String(neta[1]) : String(neta[0])
|
||||||
|
let theValue = $0
|
||||||
|
theKey.converToPhonabets()
|
||||||
|
rangeMap[theKey, default: []].append(theValue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
temporaryMap.removeAll()
|
||||||
|
}
|
||||||
|
|
||||||
/// 將當前語言模組的資料庫辭典自記憶體內卸除。
|
/// 將當前語言模組的資料庫辭典自記憶體內卸除。
|
||||||
public mutating func clear() {
|
public mutating func clear() {
|
||||||
rangeMap.removeAll()
|
rangeMap.removeAll()
|
||||||
|
|
|
@ -29,18 +29,8 @@ extension vChewingLM {
|
||||||
LMConsolidator.consolidate(path: path, pragma: true)
|
LMConsolidator.consolidate(path: path, pragma: true)
|
||||||
|
|
||||||
do {
|
do {
|
||||||
strData = try String(contentsOfFile: path, encoding: .utf8).replacingOccurrences(of: "\t", with: " ")
|
let rawStrData = try String(contentsOfFile: path, encoding: .utf8)
|
||||||
strData = strData.replacingOccurrences(of: "\r", with: "\n")
|
replaceData(textData: rawStrData)
|
||||||
strData.ranges(splitBy: "\n").filter { !$0.isEmpty }.forEach {
|
|
||||||
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] = theValue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch {
|
} catch {
|
||||||
vCLog("\(error)")
|
vCLog("\(error)")
|
||||||
vCLog("↑ Exception happened when reading data at: \(path).")
|
vCLog("↑ Exception happened when reading data at: \(path).")
|
||||||
|
@ -50,6 +40,24 @@ extension vChewingLM {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 將資料從檔案讀入至資料庫辭典內。
|
||||||
|
/// - parameters:
|
||||||
|
/// - path: 給定路徑。
|
||||||
|
public mutating func replaceData(textData rawStrData: String) {
|
||||||
|
if strData == rawStrData { return }
|
||||||
|
strData = rawStrData
|
||||||
|
strData.ranges(splitBy: "\n").filter { !$0.isEmpty }.forEach {
|
||||||
|
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] = theValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public mutating func clear() {
|
public mutating func clear() {
|
||||||
rangeMap.removeAll()
|
rangeMap.removeAll()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue