LMs // + LMLite
This commit is contained in:
parent
9c3d697076
commit
466bc6fc23
|
@ -0,0 +1,145 @@
|
||||||
|
// Copyright (c) 2021 and onwards The vChewing Project (MIT-NTL License).
|
||||||
|
// Refactored from the ObjCpp-version of this class by:
|
||||||
|
// (c) 2011 and onwards The OpenVanilla Project (MIT License).
|
||||||
|
/*
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||||
|
this software and associated documentation files (the "Software"), to deal in
|
||||||
|
the Software without restriction, including without limitation the rights to
|
||||||
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||||
|
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||||
|
subject to the following conditions:
|
||||||
|
|
||||||
|
1. The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
2. 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 above.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||||
|
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||||
|
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||||
|
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||||
|
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
extension vChewing {
|
||||||
|
public class LMLite {
|
||||||
|
var keyValueMap: [String: [Megrez.KeyValuePair]] = [:]
|
||||||
|
var defaultScore: Double = 0
|
||||||
|
var theData: String = ""
|
||||||
|
var allowConsolidation = false
|
||||||
|
|
||||||
|
public init(defaultScore scoreDefault: Double = 0, consolidate: Bool = false) {
|
||||||
|
keyValueMap = [:]
|
||||||
|
theData = ""
|
||||||
|
defaultScore = scoreDefault
|
||||||
|
allowConsolidation = consolidate
|
||||||
|
}
|
||||||
|
|
||||||
|
deinit {
|
||||||
|
if isLoaded() {
|
||||||
|
close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public func isLoaded() -> Bool {
|
||||||
|
!keyValueMap.isEmpty
|
||||||
|
}
|
||||||
|
|
||||||
|
@discardableResult public func open(_ path: String) -> Bool {
|
||||||
|
if isLoaded() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if allowConsolidation {
|
||||||
|
if !LMConsolidator.fixEOF(path: path) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if !LMConsolidator.consolidate(path: path, pragma: true) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
do {
|
||||||
|
theData = try String(contentsOfFile: path, encoding: .utf8)
|
||||||
|
} catch {
|
||||||
|
IME.prtDebugIntel("\(error)")
|
||||||
|
IME.prtDebugIntel("↑ Exception happened when reading Associated Phrases data.")
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
let length = theData.count
|
||||||
|
guard length > 0 else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
let arrData = theData.components(separatedBy: "\n")
|
||||||
|
for (lineID, lineContent) in arrData.enumerated() {
|
||||||
|
if !lineContent.hasPrefix("#") {
|
||||||
|
if lineContent.components(separatedBy: " ").count < 2 {
|
||||||
|
if arrData.last != "" {
|
||||||
|
IME.prtDebugIntel("Line #\(lineID + 1) Wrecked: \(lineContent)")
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
var currentKV = Megrez.KeyValuePair()
|
||||||
|
for (unitID, unitContent) in lineContent.components(separatedBy: " ").enumerated() {
|
||||||
|
switch unitID {
|
||||||
|
case 0:
|
||||||
|
currentKV.value = unitContent
|
||||||
|
case 1:
|
||||||
|
currentKV.key = unitContent
|
||||||
|
default: break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
keyValueMap[currentKV.key, default: []].append(currentKV)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
IME.prtDebugIntel("\(keyValueMap.count) entries of data loaded from: \(path)")
|
||||||
|
theData = ""
|
||||||
|
if path.contains("vChewing/") {
|
||||||
|
dump()
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
public func close() {
|
||||||
|
if isLoaded() {
|
||||||
|
keyValueMap.removeAll()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public func dump() {
|
||||||
|
var strDump = ""
|
||||||
|
for entry in keyValueMap {
|
||||||
|
let rows: [Megrez.KeyValuePair] = entry.1
|
||||||
|
for row in rows {
|
||||||
|
let addline = row.key + " " + row.value + "\n"
|
||||||
|
strDump += addline
|
||||||
|
}
|
||||||
|
}
|
||||||
|
IME.prtDebugIntel(strDump)
|
||||||
|
}
|
||||||
|
|
||||||
|
public func unigramsFor(key: String) -> [Megrez.Unigram] {
|
||||||
|
var v: [Megrez.Unigram] = []
|
||||||
|
if let matched = keyValueMap[key] {
|
||||||
|
for entry in matched as [Megrez.KeyValuePair] {
|
||||||
|
v.append(Megrez.Unigram(keyValue: entry, score: defaultScore))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
public func hasUnigramsFor(key: String) -> Bool {
|
||||||
|
if let arrEntry = keyValueMap[key] {
|
||||||
|
return !arrEntry.isEmpty
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -27,6 +27,7 @@
|
||||||
5B38F5A4281E2E49007D5F5D /* 5_LanguageModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A0D4F1915FC0EB100ABF4B3 /* 5_LanguageModel.swift */; };
|
5B38F5A4281E2E49007D5F5D /* 5_LanguageModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A0D4F1915FC0EB100ABF4B3 /* 5_LanguageModel.swift */; };
|
||||||
5B40730C281672610023DFFF /* lmAssociates.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B407309281672610023DFFF /* lmAssociates.swift */; };
|
5B40730C281672610023DFFF /* lmAssociates.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B407309281672610023DFFF /* lmAssociates.swift */; };
|
||||||
5B40730D281672610023DFFF /* lmReplacements.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B40730A281672610023DFFF /* lmReplacements.swift */; };
|
5B40730D281672610023DFFF /* lmReplacements.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B40730A281672610023DFFF /* lmReplacements.swift */; };
|
||||||
|
5B5D28AC281EA1E900523D4D /* lmLite.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B5D28AB281EA1E800523D4D /* lmLite.swift */; };
|
||||||
5B5E535227EF261400C6AA1E /* IME.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B5E535127EF261400C6AA1E /* IME.swift */; };
|
5B5E535227EF261400C6AA1E /* IME.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B5E535127EF261400C6AA1E /* IME.swift */; };
|
||||||
5B61B0CA280BEFD4002E3CFA /* KeyHandler_Misc.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B61B0C9280BEFD4002E3CFA /* KeyHandler_Misc.swift */; };
|
5B61B0CA280BEFD4002E3CFA /* KeyHandler_Misc.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B61B0C9280BEFD4002E3CFA /* KeyHandler_Misc.swift */; };
|
||||||
5B62A32927AE77D100A19448 /* FSEventStreamHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B62A32827AE77D100A19448 /* FSEventStreamHelper.swift */; };
|
5B62A32927AE77D100A19448 /* FSEventStreamHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B62A32827AE77D100A19448 /* FSEventStreamHelper.swift */; };
|
||||||
|
@ -202,6 +203,7 @@
|
||||||
5B3133BE280B229700A4A505 /* KeyHandler_States.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; indentWidth = 2; lineEnding = 0; path = KeyHandler_States.swift; sourceTree = "<group>"; tabWidth = 2; usesTabs = 1; };
|
5B3133BE280B229700A4A505 /* KeyHandler_States.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; indentWidth = 2; lineEnding = 0; path = KeyHandler_States.swift; sourceTree = "<group>"; tabWidth = 2; usesTabs = 1; };
|
||||||
5B407309281672610023DFFF /* lmAssociates.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; lineEnding = 0; path = lmAssociates.swift; sourceTree = "<group>"; usesTabs = 1; };
|
5B407309281672610023DFFF /* lmAssociates.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; lineEnding = 0; path = lmAssociates.swift; sourceTree = "<group>"; usesTabs = 1; };
|
||||||
5B40730A281672610023DFFF /* lmReplacements.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; lineEnding = 0; path = lmReplacements.swift; sourceTree = "<group>"; usesTabs = 1; };
|
5B40730A281672610023DFFF /* lmReplacements.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; lineEnding = 0; path = lmReplacements.swift; sourceTree = "<group>"; usesTabs = 1; };
|
||||||
|
5B5D28AB281EA1E800523D4D /* lmLite.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = lmLite.swift; sourceTree = "<group>"; };
|
||||||
5B5E535127EF261400C6AA1E /* IME.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; indentWidth = 2; lineEnding = 0; path = IME.swift; sourceTree = "<group>"; tabWidth = 2; usesTabs = 1; };
|
5B5E535127EF261400C6AA1E /* IME.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; indentWidth = 2; lineEnding = 0; path = IME.swift; sourceTree = "<group>"; tabWidth = 2; usesTabs = 1; };
|
||||||
5B61B0C9280BEFD4002E3CFA /* KeyHandler_Misc.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; indentWidth = 2; lineEnding = 0; path = KeyHandler_Misc.swift; sourceTree = "<group>"; tabWidth = 2; usesTabs = 1; };
|
5B61B0C9280BEFD4002E3CFA /* KeyHandler_Misc.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; indentWidth = 2; lineEnding = 0; path = KeyHandler_Misc.swift; sourceTree = "<group>"; tabWidth = 2; usesTabs = 1; };
|
||||||
5B62A32627AE77BB00A19448 /* LMConsolidator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = LMConsolidator.h; sourceTree = "<group>"; tabWidth = 4; usesTabs = 0; };
|
5B62A32627AE77BB00A19448 /* LMConsolidator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = LMConsolidator.h; sourceTree = "<group>"; tabWidth = 4; usesTabs = 0; };
|
||||||
|
@ -410,6 +412,7 @@
|
||||||
5BA0DF2F2817857D009E73BB /* lmCore.swift */,
|
5BA0DF2F2817857D009E73BB /* lmCore.swift */,
|
||||||
5B40730A281672610023DFFF /* lmReplacements.swift */,
|
5B40730A281672610023DFFF /* lmReplacements.swift */,
|
||||||
5BA0DF2E2817857D009E73BB /* lmUserOverride.swift */,
|
5BA0DF2E2817857D009E73BB /* lmUserOverride.swift */,
|
||||||
|
5B5D28AB281EA1E800523D4D /* lmLite.swift */,
|
||||||
);
|
);
|
||||||
path = SubLMs;
|
path = SubLMs;
|
||||||
sourceTree = "<group>";
|
sourceTree = "<group>";
|
||||||
|
@ -1114,6 +1117,7 @@
|
||||||
5B40730C281672610023DFFF /* lmAssociates.swift in Sources */,
|
5B40730C281672610023DFFF /* lmAssociates.swift in Sources */,
|
||||||
5B707CE827D9F4590099EF99 /* OpenCCBridge.swift in Sources */,
|
5B707CE827D9F4590099EF99 /* OpenCCBridge.swift in Sources */,
|
||||||
D427F76C278CA2B0004A2160 /* AppDelegate.swift in Sources */,
|
D427F76C278CA2B0004A2160 /* AppDelegate.swift in Sources */,
|
||||||
|
5B5D28AC281EA1E900523D4D /* lmLite.swift in Sources */,
|
||||||
5BA9FD4527FEF3C9002DE248 /* ToolbarItemStyleViewController.swift in Sources */,
|
5BA9FD4527FEF3C9002DE248 /* ToolbarItemStyleViewController.swift in Sources */,
|
||||||
5BA0DF322817857D009E73BB /* lmCore.swift in Sources */,
|
5BA0DF322817857D009E73BB /* lmCore.swift in Sources */,
|
||||||
5BA9FD4127FEF3C8002DE248 /* PreferencesStyle.swift in Sources */,
|
5BA9FD4127FEF3C8002DE248 /* PreferencesStyle.swift in Sources */,
|
||||||
|
|
Loading…
Reference in New Issue