Skip to content

Commit 6df1b89

Browse files
committed
Renovation & Change license to LGPLv3.
Remove dedicated StableSort API. - Swift 5 and 6 always stable-sort things by default. According to Xcode documentation: "The sorting algorithm is guaranteed to be stable. A stable sort preserves the relative order of elements for which areInIncreasingOrder does not establish an order." P.S.: The C# version (TekkonNT) still keeps the dedicated stable-sort API since its default sorting API only stable-sorts for dotNET 5 and later. Node // Optimize .contextRange(). SpanUnit // Drop unused APIs. Compositor // Use a final class instead. Compositor // Simplify .clear(). Update .gitignore Compositor // Trim useless APIs. Compositor // Fix an issue with copy. Compositor // Fixing unit tests. Make maxSpanLength thread-safe. Compositor // Mark theSeparator as `nonisolated`. CompositorConfig // Add .clear(). Compositor // Content consolidation. CompositorConfig // Drop invalid notes after changing maxSpanLength. Optimize the use of ranges. CompositorConfig // Update documentation. Compositor // Fix update(updateExisting: true). Compositor // Deprecating LangModelRanked. CompositorConfig // Conforming to Equatable. Repo // Update Hashable conformation. Unit Tests // Renovation. Fix LICENSE format.
1 parent 29f91ad commit 6df1b89

18 files changed

+1481
-1259
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,4 @@ _TeamCity*
227227
_UpgradeReport_Files/
228228
__pycache__/
229229
~$*
230+
.index-build/

CUSTOM_LGPLv3_EXCEPTION.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Custom Exception to LGPLv3 for Swift Package
2+
3+
This Swift package is licensed under the GNU Lesser General Public License version 3 (LGPLv3) with the following additional permission:
4+
5+
### Additional Permission under LGPLv3 Section 7
6+
7+
Notwithstanding any other provision of the LGPLv3, you have permission to link or combine this Swift package ("Package") with other modules to produce an executable, regardless of the code signing requirements imposed by Apple Inc. Specifically, if you use this Package in an Xcode project, you are not required to designate a paid Apple Developer ID to sign the resulting dynamic library when building and distributing the executable.
8+
9+
You are still required to comply with all other terms of the LGPLv3 in all respects for all of the code used other than the Package. If you modify this Package, you may extend this exception to your version of the Package, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.
10+
11+
This Additional Permission applies only to the code released with this Package under LGPLv3 and does not extend to any other code or libraries it interacts with.
12+
13+
### How to Apply This Exception
14+
15+
To apply this exception, you must include a copy of this document and ensure that it is referenced in your project's licensing documentation. This ensures that users of your Swift package and any derivative works are aware of the additional permission granted by this exception.
16+
17+
### Example Usage
18+
19+
Include the following notice in your project’s documentation:
20+
21+
```
22+
This project includes a Swift package licensed under LGPLv3 with an additional permission granted to allow linking and combining with other modules without requiring a paid Apple Developer ID for code signing. For details, see the CUSTOM_LGPLv3_EXCEPTION.md file included with this project.
23+
```

LICENSE

Lines changed: 165 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,165 @@
1-
MIT License
2-
3-
Copyright (c) 2022 and onwards Lukhnos Liu for upstream contents.
4-
Copyright (c) 2022 and onwards The vChewing Project for Megrez-specific changes.
5-
6-
Permission is hereby granted, free of charge, to any person obtaining a copy
7-
of this software and associated documentation files (the "Software"), to deal
8-
in the Software without restriction, including without limitation the rights
9-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10-
copies of the Software, and to permit persons to whom the Software is
11-
furnished to do so, subject to the following conditions:
12-
13-
The above copyright notice and this permission notice shall be included in all
14-
copies or substantial portions of the Software.
15-
16-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22-
SOFTWARE.
1+
GNU LESSER GENERAL PUBLIC LICENSE
2+
Version 3, 29 June 2007
3+
4+
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
5+
Everyone is permitted to copy and distribute verbatim copies
6+
of this license document, but changing it is not allowed.
7+
8+
9+
This version of the GNU Lesser General Public License incorporates
10+
the terms and conditions of version 3 of the GNU General Public
11+
License, supplemented by the additional permissions listed below.
12+
13+
0. Additional Definitions.
14+
15+
As used herein, "this License" refers to version 3 of the GNU Lesser
16+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
17+
General Public License.
18+
19+
"The Library" refers to a covered work governed by this License,
20+
other than an Application or a Combined Work as defined below.
21+
22+
An "Application" is any work that makes use of an interface provided
23+
by the Library, but which is not otherwise based on the Library.
24+
Defining a subclass of a class defined by the Library is deemed a mode
25+
of using an interface provided by the Library.
26+
27+
A "Combined Work" is a work produced by combining or linking an
28+
Application with the Library. The particular version of the Library
29+
with which the Combined Work was made is also called the "Linked
30+
Version".
31+
32+
The "Minimal Corresponding Source" for a Combined Work means the
33+
Corresponding Source for the Combined Work, excluding any source code
34+
for portions of the Combined Work that, considered in isolation, are
35+
based on the Application, and not on the Linked Version.
36+
37+
The "Corresponding Application Code" for a Combined Work means the
38+
object code and/or source code for the Application, including any data
39+
and utility programs needed for reproducing the Combined Work from the
40+
Application, but excluding the System Libraries of the Combined Work.
41+
42+
1. Exception to Section 3 of the GNU GPL.
43+
44+
You may convey a covered work under sections 3 and 4 of this License
45+
without being bound by section 3 of the GNU GPL.
46+
47+
2. Conveying Modified Versions.
48+
49+
If you modify a copy of the Library, and, in your modifications, a
50+
facility refers to a function or data to be supplied by an Application
51+
that uses the facility (other than as an argument passed when the
52+
facility is invoked), then you may convey a copy of the modified
53+
version:
54+
55+
a) under this License, provided that you make a good faith effort to
56+
ensure that, in the event an Application does not supply the
57+
function or data, the facility still operates, and performs
58+
whatever part of its purpose remains meaningful, or
59+
60+
b) under the GNU GPL, with none of the additional permissions of
61+
this License applicable to that copy.
62+
63+
3. Object Code Incorporating Material from Library Header Files.
64+
65+
The object code form of an Application may incorporate material from
66+
a header file that is part of the Library. You may convey such object
67+
code under terms of your choice, provided that, if the incorporated
68+
material is not limited to numerical parameters, data structure
69+
layouts and accessors, or small macros, inline functions and templates
70+
(ten or fewer lines in length), you do both of the following:
71+
72+
a) Give prominent notice with each copy of the object code that the
73+
Library is used in it and that the Library and its use are
74+
covered by this License.
75+
76+
b) Accompany the object code with a copy of the GNU GPL and this license
77+
document.
78+
79+
4. Combined Works.
80+
81+
You may convey a Combined Work under terms of your choice that,
82+
taken together, effectively do not restrict modification of the
83+
portions of the Library contained in the Combined Work and reverse
84+
engineering for debugging such modifications, if you also do each of
85+
the following:
86+
87+
a) Give prominent notice with each copy of the Combined Work that
88+
the Library is used in it and that the Library and its use are
89+
covered by this License.
90+
91+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
92+
document.
93+
94+
c) For a Combined Work that displays copyright notices during
95+
execution, include the copyright notice for the Library among
96+
these notices, as well as a reference directing the user to the
97+
copies of the GNU GPL and this license document.
98+
99+
d) Do one of the following:
100+
101+
0) Convey the Minimal Corresponding Source under the terms of this
102+
License, and the Corresponding Application Code in a form
103+
suitable for, and under terms that permit, the user to
104+
recombine or relink the Application with a modified version of
105+
the Linked Version to produce a modified Combined Work, in the
106+
manner specified by section 6 of the GNU GPL for conveying
107+
Corresponding Source.
108+
109+
1) Use a suitable shared library mechanism for linking with the
110+
Library. A suitable mechanism is one that (a) uses at run time
111+
a copy of the Library already present on the user's computer
112+
system, and (b) will operate properly with a modified version
113+
of the Library that is interface-compatible with the Linked
114+
Version.
115+
116+
e) Provide Installation Information, but only if you would otherwise
117+
be required to provide such information under section 6 of the
118+
GNU GPL, and only to the extent that such information is
119+
necessary to install and execute a modified version of the
120+
Combined Work produced by recombining or relinking the
121+
Application with a modified version of the Linked Version. (If
122+
you use option 4d0, the Installation Information must accompany
123+
the Minimal Corresponding Source and Corresponding Application
124+
Code. If you use option 4d1, you must provide the Installation
125+
Information in the manner specified by section 6 of the GNU GPL
126+
for conveying Corresponding Source.)
127+
128+
5. Combined Libraries.
129+
130+
You may place library facilities that are a work based on the
131+
Library side by side in a single library together with other library
132+
facilities that are not Applications and are not covered by this
133+
License, and convey such a combined library under terms of your
134+
choice, if you do both of the following:
135+
136+
a) Accompany the combined library with a copy of the same work based
137+
on the Library, uncombined with any other library facilities,
138+
conveyed under the terms of this License.
139+
140+
b) Give prominent notice with the combined library that part of it
141+
is a work based on the Library, and explaining where to find the
142+
accompanying uncombined form of the same work.
143+
144+
6. Revised Versions of the GNU Lesser General Public License.
145+
146+
The Free Software Foundation may publish revised and/or new versions
147+
of the GNU Lesser General Public License from time to time. Such new
148+
versions will be similar in spirit to the present version, but may
149+
differ in detail to address new problems or concerns.
150+
151+
Each version is given a distinguishing version number. If the
152+
Library as you received it specifies that a certain numbered version
153+
of the GNU Lesser General Public License "or any later version"
154+
applies to it, you have the option of following the terms and
155+
conditions either of that published version or of any later version
156+
published by the Free Software Foundation. If the Library as you
157+
received it does not specify a version number of the GNU Lesser
158+
General Public License, you may choose any version of the GNU Lesser
159+
General Public License ever published by the Free Software Foundation.
160+
161+
If the Library as you received it specifies that a proxy can decide
162+
whether future versions of the GNU Lesser General Public License shall
163+
apply, that proxy's public statement of acceptance of any version is
164+
permanent authorization for you to choose that version for the
165+
Library.

README.md

Lines changed: 13 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -5,129 +5,25 @@
55

66
> 該引擎已經實裝於基於純 Swift 語言完成的 **威注音輸入法** 內,歡迎好奇者嘗試:[GitHub](https://github.com/vChewing/vChewing-macOS ) | [Gitee](https://gitee.com/vchewing/vChewing-macOS )
77
8-
天權星引擎是用來處理輸入法語彙庫的一個模組。該倉庫乃威注音專案的弒神行動(Operation Longinus)的一部分。
8+
天權星引擎是用來處理輸入法語彙庫的一個模組。
99

10-
Megrez Engine is a module made for processing lingual data of an input method. This repository is part of Operation Longinus of The vChewing Project.
10+
Megrez Engine is a module made for processing lingual data of an input method.
1111

12-
## 與 Gramambular 2 的區別
12+
## 專案特色
1313

14-
敝專案一開始是 Gramambular 2 (Lukhnos Liu 著,MIT License) 的 Swift 實作,但經歷了大量修改。主要區別如下:
15-
16-
- 原生 Swift 實作,擁有完備的 Swift 5.3 ~ 5.9 支援、也可以用作任何 Swift 6 專案的相依套件(需使用者自行處理對跨執行緒安全性的需求)
17-
- API 經過重新設計,以陣列的形式處理輸入的 key。而且,在獲取候選字詞內容的時候,也可以徹底篩除橫跨游標的詞
18-
- 爬軌算法(Walking Algorithm)改為 Dijkstra 的算法,且經過效能最佳化處理、擁有比 DAG-Relax 算法更優的效能。
14+
- 原生 Swift 實作,擁有完備的 Swift 5.3 ~ 5.9 支援、也可以用作任何 Swift 6 專案的相依套件。
15+
- 注意:`Megrez.Compositor.theSeparator` **沒有多執行緒安全性**。對該 API 的保留乃是出於相容性之考量。新專案請避免使用該 static API。
16+
- 以陣列的形式處理輸入的 key
17+
- 在獲取候選字詞內容的時候,不會出現橫跨游標的詞
18+
- 使用經過效能最佳化處理的 Dijkstra 的算法,擁有比 DAG-Relax 算法更優的效能。
1919

2020
## 使用說明
2121

22-
### §1. 初期化
23-
24-
在你的 IMKInputController 或者 InputHandler 內初期化一份 Megrez.Compositor 組字器副本(這裡將該副本命名為「`compositor`」)。由於 Megrez.Compositor 的型別是 Struct 型別(為了讓 Compositor 可以 deep copy),所以其副本可以用 var 來宣告。
25-
26-
以 InputHandler 為例:
27-
```swift
28-
class InputHandler {
29-
// 先設定好變數
30-
var compositor: Megrez.Compositor = .init()
31-
...
32-
}
33-
```
34-
35-
以 IMKInputController 為例:
36-
```swift
37-
@objc(IMKMyInputController) // 根據 info.plist 內的情況來確定型別的命名
38-
class IMKMyInputController: IMKInputController {
39-
// 先設定好變數
40-
var compositor: Megrez.Compositor = .init()
41-
...
42-
}
43-
```
44-
45-
由於 Swift 會在某個大副本(InputHandler 或者 IMKInputController 副本)被銷毀的時候自動銷毀其中的全部副本,所以 Megrez.Compositor 的副本初期化沒必要寫在 init() 當中。但你很可能會想在 init() 時指定 Tekkon.Composer 所對接的語言模組型別、以及其可以允許的最大詞長。
46-
47-
這裡就需要在 init() 時使用參數:
48-
```swift
49-
/// 組字器。
50-
/// - Parameters:
51-
/// - lm: 語言模型。可以是任何基於 Megrez.LangModel 的衍生型別。
52-
/// - length: 指定該組字器內可以允許的最大詞長,預設為 10 字。
53-
/// - separator: 多字讀音鍵當中用以分割漢字讀音的記號,預設為空。
54-
var compositor: Megrez.Compositor = .init(lm: lmTest, length: 13, separator: "-")
55-
```
56-
57-
### §2. 使用範例
58-
59-
請結合 MegrezTests.swift 檔案來學習。這裡只是給個概述。
60-
61-
#### // 1. 準備用作語言模型的專用型別
62-
63-
首先,Megrez 內建的 LangModel 型別是遠遠不夠用的,只能說是個類似於 protocol 一樣的存在。你需要自己單獨寫一個新的衍生型別:
64-
65-
```swift
66-
class ExampleLM: Megrez.LangModel {
67-
...
68-
override func unigramsFor(keyArray: [String]) -> [Megrez.Unigram] {
69-
...
70-
}
71-
...
72-
}
73-
```
74-
75-
這個型別需要下述兩個函式能夠針對給定的鍵回饋對應的資料值、或其存無狀態:
76-
- unigramsFor(keyArray: [String]) -> [Megrez.Unigram]
77-
- hasUnigramsFor(keyArray: [String]) -> Bool
78-
79-
MegrezTests.swift 檔案內的 SimpleLM 可以作為範例。
80-
81-
至於該檔案內的 FiniteStateMachine 則是一個「一次性讀取檔案/大字串且分析資料」的範例。
82-
83-
如果需要更實戰的範例的話,可以洽威注音專案的倉庫內的 LMInstantiator.swift 及其關聯檔案。
84-
85-
#### // 2. 怎樣與 compositor 互動:
86-
87-
這裡只講幾個常用函式:
88-
89-
- 游標位置 `compositor.cursorIndex` 是可以賦值與取值的動態變數,且會在賦值內容為超出位置範圍的數值時自動修正。初期值為 0。
90-
- `compositor.insertKey("gao1")` 可以在當前的游標位置插入讀音「gao1」。
91-
- `compositor.dropKey(direction: .front)` 的作用是:朝著往文字輸入方向、砍掉一個與游標相鄰的讀音。反之,`dropKey(direction: .rear)` 則朝著與文字輸入方向相反的方向、砍掉一個與游標相鄰的讀音。
92-
- 在威注音的術語體系當中,「文字輸入方向」為向前(Front)、與此相反的方向為向後(Rear)。
93-
- `compositor.overrideCandidate(.init(keyArray: ["讀音"], value: "候選字"), at: 游標位置, overrideType: 覆寫模式)` 用來根據輸入法選中的候選字詞、據此更新當前游標位置選中的候選字詞節點當中的候選字詞。
94-
95-
輸入完內容之後,可以聲明一個用來接收結果的變數:
96-
97-
```swift
98-
/// 對已給定的軌格按照給定的位置與條件進行正向爬軌。
99-
var walked = compositor.walk()
100-
```
101-
102-
MegrezTests.swift 是輸入了很多內容之後再 walk 的。實際上一款輸入法會在你每次插入讀音或刪除讀音的時候都重新 walk。那些處於候選字詞鎖定狀態的節點不會再受到之後的 walk 的行為的影響,但除此之外的節點會因為每次 walk 而可能各自的候選字詞會出現自動變化。如果給了 nodesLimit 一個非零的數值的話,則 walk 的範圍外的節點不會受到影響。
103-
104-
walk 之後的取值的方法及利用方法可以有很多種。這裡有其中的一個:
105-
106-
```swift
107-
var composed: [String] = walked.map(\.value)
108-
print(composed)
109-
```
110-
111-
類似於:
112-
113-
```swift
114-
for phrase in walked {
115-
composed.append(phrase.value)
116-
}
117-
print(composed)
118-
```
119-
120-
上述 print 結果就是 compositor 目前的組句,是這種陣列格式(以吳宗憲的詩句為例):
121-
```swift
122-
["八月", "中秋", "山林", "", "風吹", "大地", "草枝", ""]
123-
```
124-
125-
自己看 MegrezTests.swift 慢慢研究吧。
22+
`MegrezTests.swift` 展示了詳細的使用方法。
12623

12724
## 著作權 (Credits)
12825

129-
- Swiftified and further development by (c) 2022 and onwards The vChewing Project (MIT License).
130-
- Swift programmer: Shiki Suen
131-
- Was initially rebranded from (c) Lukhnos Liu's C++ library "Gramambular 2" (MIT License).
132-
- Walking algorithm (Dijkstra) implemented by (c) 2025 and onwards The vChewing Project (MIT License).
133-
- Swift programmer: Shiki Suen
26+
- (c) 2022 and onwards The vChewing Project (LGPL v3.0 License or later).
27+
- The unit tests utilizes certain contents extracted from libvchewing-data by (c) 2022 and onwards The vChewing Project (BSD-3-Clause).
28+
29+
敝專案採雙授權發佈措施。除了 LGPLv3 以外,對商業使用者也提供不同的授權條款(比如允許閉源使用等)。詳情請[電郵聯絡作者](shikisuen@yeah.net)

Sources/Megrez/0_Megrez.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
// Swiftified and further development by (c) 2022 and onwards The vChewing Project (MIT License).
2-
// Was initially rebranded from (c) Lukhnos Liu's C++ library "Gramambular 2" (MIT License).
3-
// Walking algorithm (Dijkstra) implemented by (c) 2025 and onwards The vChewing Project (MIT License).
1+
// (c) 2022 and onwards The vChewing Project (LGPL v3.0 License or later).
42
// ====================
5-
// This code is released under the MIT license (SPDX-License-Identifier: MIT)
3+
// This code is released under the SPDX-License-Identifier: `LGPL-3.0-or-later`.
64

75
/// The namespace for this package.
86
public enum Megrez {}

0 commit comments

Comments
 (0)