From 1fa09fe1ebc564056ba310c97772804ebff41074 Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Sun, 8 May 2022 22:37:37 +0800 Subject: [PATCH] LMs // Deprecating LMCore and LMLite. --- .../LangModelRelated/SubLMs/lmCore.swift | 155 ------------------ .../LangModelRelated/SubLMs/lmLite.swift | 124 -------------- vChewing.xcodeproj/project.pbxproj | 8 - 3 files changed, 287 deletions(-) delete mode 100644 Source/Modules/LangModelRelated/SubLMs/lmCore.swift delete mode 100644 Source/Modules/LangModelRelated/SubLMs/lmLite.swift diff --git a/Source/Modules/LangModelRelated/SubLMs/lmCore.swift b/Source/Modules/LangModelRelated/SubLMs/lmCore.swift deleted file mode 100644 index 607c14f1..00000000 --- a/Source/Modules/LangModelRelated/SubLMs/lmCore.swift +++ /dev/null @@ -1,155 +0,0 @@ -// Copyright (c) 2021 and onwards The vChewing Project (MIT-NTL 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. -*/ - -// 威注音重新設計原廠詞庫語言模組。不排序,但使用 Swift 內建的 String 處理。 - -import Foundation - -extension vChewing { - @frozen public struct LMCore { - var keyValueScoreMap: [String: [Megrez.Unigram]] = [:] - var shouldReverse: Bool = false - var allowConsolidation: Bool = false - var defaultScore: Double = 0 - var shouldForceDefaultScore: Bool = false - - public var count: Int { - keyValueScoreMap.count - } - - public init( - reverse: Bool = false, consolidate: Bool = false, defaultScore scoreDefault: Double = 0, - forceDefaultScore: Bool = false - ) { - keyValueScoreMap = [:] - allowConsolidation = consolidate - shouldReverse = reverse - defaultScore = scoreDefault - shouldForceDefaultScore = forceDefaultScore - } - - public func isLoaded() -> Bool { - !keyValueScoreMap.isEmpty - } - - @discardableResult public mutating func open(_ path: String) -> Bool { - if isLoaded() { - return false - } - - if allowConsolidation { - LMConsolidator.fixEOF(path: path) - LMConsolidator.consolidate(path: path, pragma: true) - } - - var arrData: [String] = [] - - do { - arrData = try String(contentsOfFile: path, encoding: .utf8).components(separatedBy: "\n") - } catch { - IME.prtDebugIntel("\(error)") - IME.prtDebugIntel("↑ Exception happened when reading data at: \(path).") - return false - } - - for (lineID, lineContent) in arrData.enumerated() { - if !lineContent.hasPrefix("#") { - let lineContent = lineContent.replacingOccurrences(of: "\t", with: " ") - if lineContent.components(separatedBy: " ").count < 2 { - if lineContent != "", lineContent != " " { - IME.prtDebugIntel("Line #\(lineID + 1) Wrecked: \(lineContent)") - } - continue - } - var currentUnigram = Megrez.Unigram(keyValue: Megrez.KeyValuePair(), score: defaultScore) - var columnOne = "" - var columnTwo = "" - for (unitID, unitContent) in lineContent.components(separatedBy: " ").enumerated() { - switch unitID { - case 0: - columnOne = unitContent - case 1: - columnTwo = unitContent - case 2: - if !shouldForceDefaultScore { - if let unitContentConverted = Double(unitContent) { - currentUnigram.score = unitContentConverted - } else { - IME.prtDebugIntel("Line #\(lineID) Score Data Wrecked: \(lineContent)") - } - } - default: break - } - } - // 標點符號的頻率最好鎖定一下。 - if columnOne.contains("_punctuation_") { - currentUnigram.score -= (Double(lineID) * 0.000001) - } - let kvPair = - shouldReverse - ? Megrez.KeyValuePair(key: columnTwo, value: columnOne) - : Megrez.KeyValuePair(key: columnOne, value: columnTwo) - currentUnigram.keyValue = kvPair - let key = shouldReverse ? columnTwo : columnOne - keyValueScoreMap[key, default: []].append(currentUnigram) - } - } - return true - } - - public mutating func close() { - if isLoaded() { - keyValueScoreMap.removeAll() - } - } - - // MARK: - Advanced features - - public func dump() { - var strDump = "" - for entry in keyValueScoreMap { - let rows: [Megrez.Unigram] = entry.value - for row in rows { - let addline = row.keyValue.key + " " + row.keyValue.value + " " + String(row.score) + "\n" - strDump += addline - } - } - IME.prtDebugIntel(strDump) - } - - public func bigramsForKeys(precedingKey: String, key: String) -> [Megrez.Bigram] { - // 這裡用了點廢話處理,不然函數構建體會被 Swift 格式整理工具給毀掉。 - // 其實只要一句「[Megrez.Bigram]()」就夠了。 - precedingKey == key ? [Megrez.Bigram]() : [Megrez.Bigram]() - } - - public func unigramsFor(key: String) -> [Megrez.Unigram] { - keyValueScoreMap[key] ?? [Megrez.Unigram]() - } - - public func hasUnigramsFor(key: String) -> Bool { - keyValueScoreMap[key] != nil - } - } -} diff --git a/Source/Modules/LangModelRelated/SubLMs/lmLite.swift b/Source/Modules/LangModelRelated/SubLMs/lmLite.swift deleted file mode 100644 index e381bb1b..00000000 --- a/Source/Modules/LangModelRelated/SubLMs/lmLite.swift +++ /dev/null @@ -1,124 +0,0 @@ -// 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 { - @frozen public struct LMLite { - var keyValueMap: [String: [Megrez.KeyValuePair]] = [:] - var allowConsolidation = false - - public var count: Int { - keyValueMap.count - } - - public init(consolidate: Bool = false) { - keyValueMap = [:] - allowConsolidation = consolidate - } - - public func isLoaded() -> Bool { - !keyValueMap.isEmpty - } - - @discardableResult public mutating func open(_ path: String) -> Bool { - if isLoaded() { - return false - } - - if allowConsolidation { - LMConsolidator.fixEOF(path: path) - LMConsolidator.consolidate(path: path, pragma: true) - } - - var arrData: [String] = [] - - do { - arrData = try String(contentsOfFile: path, encoding: .utf8).components(separatedBy: "\n") - } catch { - IME.prtDebugIntel("\(error)") - IME.prtDebugIntel("↑ Exception happened when reading data at: \(path).") - return false - } - - for (lineID, lineContent) in arrData.enumerated() { - if !lineContent.hasPrefix("#") { - let lineContent = lineContent.replacingOccurrences(of: "\t", with: " ") - if lineContent.components(separatedBy: " ").count < 2 { - if lineContent != "", lineContent != " " { - 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) - } - } - return true - } - - public mutating func close() { - if isLoaded() { - keyValueMap.removeAll() - } - } - - public func dump() { - var strDump = "" - for entry in keyValueMap { - let rows: [Megrez.KeyValuePair] = entry.value - for row in rows { - let addline = row.key + " " + row.value + "\n" - strDump += addline - } - } - IME.prtDebugIntel(strDump) - } - - public func unigramsFor(key: String, score givenScore: Double = 0.0) -> [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: givenScore)) - } - } - return v - } - - public func hasUnigramsFor(key: String) -> Bool { - keyValueMap[key] != nil - } - } -} diff --git a/vChewing.xcodeproj/project.pbxproj b/vChewing.xcodeproj/project.pbxproj index 5685aff1..58647c9c 100644 --- a/vChewing.xcodeproj/project.pbxproj +++ b/vChewing.xcodeproj/project.pbxproj @@ -7,7 +7,6 @@ objects = { /* Begin PBXBuildFile section */ - 5B00A230282011980058E5DB /* lmLite.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B00A22F282011980058E5DB /* lmLite.swift */; }; 5B0AF8B527B2C8290096FE54 /* StringExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B0AF8B427B2C8290096FE54 /* StringExtension.swift */; }; 5B11328927B94CFB00E58451 /* AppleKeyboardConverter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B11328827B94CFB00E58451 /* AppleKeyboardConverter.swift */; }; 5B27AD6A27CB1F9B000ED75B /* data-symbols.txt in Resources */ = {isa = PBXBuildFile; fileRef = 5B27AD6827CB1F9B000ED75B /* data-symbols.txt */; }; @@ -52,7 +51,6 @@ 5B949BD92816DC5400D87B5D /* LineReader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B949BD82816DC5400D87B5D /* LineReader.swift */; }; 5B949BDB2816DDBC00D87B5D /* LMConsolidator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B949BDA2816DDBC00D87B5D /* LMConsolidator.swift */; }; 5BA0DF312817857D009E73BB /* lmUserOverride.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BA0DF2E2817857D009E73BB /* lmUserOverride.swift */; }; - 5BA0DF322817857D009E73BB /* lmCore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BA0DF2F2817857D009E73BB /* lmCore.swift */; }; 5BA9FD0F27FEDB6B002DE248 /* suiPrefPaneGeneral.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BA9FD0A27FEDB6B002DE248 /* suiPrefPaneGeneral.swift */; }; 5BA9FD1027FEDB6B002DE248 /* suiPrefPaneKeyboard.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BA9FD0B27FEDB6B002DE248 /* suiPrefPaneKeyboard.swift */; }; 5BA9FD1127FEDB6B002DE248 /* ctlPrefUI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BA9FD0C27FEDB6B002DE248 /* ctlPrefUI.swift */; }; @@ -162,7 +160,6 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ - 5B00A22F282011980058E5DB /* lmLite.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = lmLite.swift; sourceTree = ""; usesTabs = 0; }; 5B04305327B529D800CB65BC /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/InfoPlist.strings"; sourceTree = ""; }; 5B04305427B529D800CB65BC /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/Localizable.strings"; sourceTree = ""; }; 5B04305527B529D800CB65BC /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/MainMenu.strings"; sourceTree = ""; }; @@ -230,7 +227,6 @@ 5B949BD82816DC5400D87B5D /* LineReader.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; lineEnding = 0; path = LineReader.swift; sourceTree = ""; usesTabs = 0; }; 5B949BDA2816DDBC00D87B5D /* LMConsolidator.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; lineEnding = 0; path = LMConsolidator.swift; sourceTree = ""; usesTabs = 0; }; 5BA0DF2E2817857D009E73BB /* lmUserOverride.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; lineEnding = 0; path = lmUserOverride.swift; sourceTree = ""; usesTabs = 0; }; - 5BA0DF2F2817857D009E73BB /* lmCore.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; lineEnding = 0; path = lmCore.swift; sourceTree = ""; usesTabs = 0; }; 5BA9FD0A27FEDB6B002DE248 /* suiPrefPaneGeneral.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; indentWidth = 2; lineEnding = 0; path = suiPrefPaneGeneral.swift; sourceTree = ""; tabWidth = 2; usesTabs = 0; }; 5BA9FD0B27FEDB6B002DE248 /* suiPrefPaneKeyboard.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; indentWidth = 2; lineEnding = 0; path = suiPrefPaneKeyboard.swift; sourceTree = ""; tabWidth = 2; usesTabs = 0; }; 5BA9FD0C27FEDB6B002DE248 /* ctlPrefUI.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; indentWidth = 2; lineEnding = 0; path = ctlPrefUI.swift; sourceTree = ""; tabWidth = 2; usesTabs = 0; }; @@ -395,9 +391,7 @@ isa = PBXGroup; children = ( 5B407309281672610023DFFF /* lmAssociates.swift */, - 5BA0DF2F2817857D009E73BB /* lmCore.swift */, 5B887F2F2826AEA400B6651E /* lmCoreEX.swift */, - 5B00A22F282011980058E5DB /* lmLite.swift */, 5B40730A281672610023DFFF /* lmReplacements.swift */, 5BA0DF2E2817857D009E73BB /* lmUserOverride.swift */, ); @@ -1080,7 +1074,6 @@ 5B707CE827D9F4590099EF99 /* OpenCCBridge.swift in Sources */, D427F76C278CA2B0004A2160 /* AppDelegate.swift in Sources */, 5BA9FD4527FEF3C9002DE248 /* ToolbarItemStyleViewController.swift in Sources */, - 5BA0DF322817857D009E73BB /* lmCore.swift in Sources */, 5BA9FD4127FEF3C8002DE248 /* PreferencesStyle.swift in Sources */, 5B7F225D2808501000DDD3CB /* KeyHandler_HandleInput.swift in Sources */, 5BA9FD1227FEDB6B002DE248 /* suiPrefPaneExperience.swift in Sources */, @@ -1134,7 +1127,6 @@ 5B62A34827AE7CD900A19448 /* ctlCandidateVertical.swift in Sources */, 5BA9FD4027FEF3C8002DE248 /* Localization.swift in Sources */, 5BA9FD1327FEDB6B002DE248 /* suiPrefPaneDictionary.swift in Sources */, - 5B00A230282011980058E5DB /* lmLite.swift in Sources */, 5BBBB77A27AEDC690023B93A /* clsSFX.swift in Sources */, 5BA9FD4727FEF3C9002DE248 /* PreferencesStyleController.swift in Sources */, 5BF8423127BAA942008E7E4C /* vChewingKanjiConverter.swift in Sources */,