mirror of
https://github.com/mii443/AzooKeyKanaKanjiConverter.git
synced 2025-08-22 06:55:26 +00:00
feat: introduce package traits of Swift 6.1 (#172)
* feat: introduce package traits of Swift 6.1 * fix: CI * fix: android toolchain install script * fix: ci for windows * chore: add debug dump of sdk list * fix: update devcontainer swift image * fix? * chore: remove LLAMA_MOCK=1 since it is no longer required * chore: add debug print of configuration * fix: typo * chore: use signed xcframework of azooKey/llama.cpp * chore: use updated xcframework * chore: use updated xcframework * chore: use updated xcframework * chore: use updated xcframework * chore: use updated xcframework * docs: add usage of Zenzai trait
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "Swift",
|
||||
"image": "swift:6.0",
|
||||
"image": "swift:6.1",
|
||||
"features": {
|
||||
"ghcr.io/devcontainers/features/common-utils:2": {
|
||||
"installZsh": "false",
|
||||
|
25
.github/workflows/swift.yml
vendored
25
.github/workflows/swift.yml
vendored
@ -13,9 +13,9 @@ jobs:
|
||||
strategy:
|
||||
matrix:
|
||||
os: [macos-latest]
|
||||
swift-version: ["6.0"]
|
||||
swift-version: ["6.1"]
|
||||
steps:
|
||||
- uses: swift-actions/setup-swift@v2
|
||||
- uses: ensan-hcl/setup-swift@swift-6.1.0
|
||||
with:
|
||||
swift-version: ${{ matrix.swift-version }}
|
||||
- uses: actions/checkout@v4
|
||||
@ -31,9 +31,9 @@ jobs:
|
||||
strategy:
|
||||
matrix:
|
||||
os: [ubuntu-latest]
|
||||
swift-version: ["6.0"]
|
||||
swift-version: ["6.1"]
|
||||
steps:
|
||||
- uses: swift-actions/setup-swift@v2
|
||||
- uses: ensan-hcl/setup-swift@swift-6.1.0
|
||||
with:
|
||||
swift-version: ${{ matrix.swift-version }}
|
||||
- uses: actions/checkout@v4
|
||||
@ -59,8 +59,8 @@ jobs:
|
||||
os: [windows-latest]
|
||||
swift-version:
|
||||
[{
|
||||
branch: "swift-6.0.2-release",
|
||||
tag: "6.0.2-RELEASE"
|
||||
branch: "swift-6.1-release",
|
||||
tag: "6.1-RELEASE"
|
||||
}]
|
||||
steps:
|
||||
- uses: compnerd/gha-setup-swift@main
|
||||
@ -97,11 +97,12 @@ jobs:
|
||||
matrix:
|
||||
arch: [x86_64, aarch64, armv7]
|
||||
os: [ubuntu-latest]
|
||||
# You may find the checksum at: https://github.com/finagolfin/swift-android-sdk/blob/main/README.md#swift-cross-compilation-sdk-bundle-for-android
|
||||
swift-version:
|
||||
[{
|
||||
version: "6.0.2",
|
||||
version: "6.1",
|
||||
android: "24",
|
||||
checksum: "d75615eac3e614131133c7cc2076b0b8fb4327d89dce802c25cd53e75e1881f4"
|
||||
checksum: "971f3b1fd03c059803d625f0a412d7e8c4c6f34440f5216ceaf13e886e8e706f"
|
||||
}]
|
||||
|
||||
steps:
|
||||
@ -114,7 +115,10 @@ jobs:
|
||||
${TOOLCHAIN}/bin/swift --version
|
||||
|
||||
- name: Setup Swift release Android SDK
|
||||
run: ${TOOLCHAIN}/bin/swift sdk install https://github.com/finagolfin/swift-android-sdk/releases/download/${{ matrix.swift-version.version }}/swift-${{ matrix.swift-version.version }}-RELEASE-android-${{ matrix.swift-version.android }}-0.1.artifactbundle.tar.gz --checksum ${{ matrix.swift-version.checksum }}
|
||||
run: |
|
||||
${TOOLCHAIN}/bin/swift sdk install https://github.com/finagolfin/swift-android-sdk/releases/download/${{ matrix.swift-version.version }}/swift-${{ matrix.swift-version.version }}-RELEASE-android-${{ matrix.swift-version.android }}-0.1.artifactbundle.tar.gz --checksum ${{ matrix.swift-version.checksum }}
|
||||
${TOOLCHAIN}/bin/swift sdk list
|
||||
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
path: AzooKeyKanaKanjiConverter
|
||||
@ -126,7 +130,8 @@ jobs:
|
||||
if ${{ matrix.arch == 'armv7' }}; then
|
||||
ARCH_TARGET="armv7-unknown-linux-androideabi${{ matrix.swift-version.android }}"
|
||||
fi
|
||||
LLAMA_MOCK=1 ${TOOLCHAIN}/bin/swift build --build-tests --swift-sdk $ARCH_TARGET
|
||||
${TOOLCHAIN}/bin/swift sdk configure --show-configuration swift-${{ matrix.swift-version.version }}-RELEASE-android-${{ matrix.swift-version.android }}-0.1 $ARCH_TARGET
|
||||
${TOOLCHAIN}/bin/swift build --build-tests --swift-sdk $ARCH_TARGET
|
||||
- name: Get cached Termux app
|
||||
if: ${{ matrix.arch == 'x86_64' }}
|
||||
id: cache-termux
|
||||
|
101
Package.swift
101
Package.swift
@ -1,11 +1,12 @@
|
||||
// swift-tools-version: 6.0
|
||||
// swift-tools-version: 6.1
|
||||
// The swift-tools-version declares the minimum version of Swift required to build this package.
|
||||
|
||||
import PackageDescription
|
||||
import Foundation
|
||||
|
||||
var swiftSettings: [SwiftSetting] = [
|
||||
let swiftSettings: [SwiftSetting] = [
|
||||
.enableUpcomingFeature("ExistentialAny"),
|
||||
.interoperabilityMode(.Cxx, .when(traits: ["Zenzai"]))
|
||||
]
|
||||
|
||||
var dependencies: [Package.Dependency] = [
|
||||
@ -17,17 +18,14 @@ var dependencies: [Package.Dependency] = [
|
||||
.package(url: "https://github.com/ensan-hcl/swift-tokenizers", branch: "feat/minimum")
|
||||
]
|
||||
|
||||
var efficientNGramDependencies: [Target.Dependency] = [.product(name: "Transformers", package: "swift-tokenizers")]
|
||||
var efficientNGramDependencies: [Target.Dependency] = [
|
||||
.product(name: "Transformers", package: "swift-tokenizers")
|
||||
]
|
||||
|
||||
#if (!os(Linux) || !canImport(Android)) && !os(Windows)
|
||||
// Android環境・Windows環境ではSwiftyMarisaが利用できないため、除外する。
|
||||
// したがって、EfficientNGramの動作はサポートしない。
|
||||
if let envValue = ProcessInfo.processInfo.environment["LLAMA_MOCK"], envValue == "1" {
|
||||
// LLAMA_MOCK=1の場合もサポートしない
|
||||
} else {
|
||||
dependencies.append(.package(url: "https://github.com/ensan-hcl/SwiftyMarisa", branch: "6e145aef5583aac96dd7ff8f9fbb9944d893128e"))
|
||||
efficientNGramDependencies.append("SwiftyMarisa")
|
||||
swiftSettings.append(.interoperabilityMode(.Cxx))
|
||||
}
|
||||
// Android環境・Windows環境ではSwiftyMarisaが利用できないため、EfficientNGramは除外する。
|
||||
dependencies.append(.package(url: "https://github.com/ensan-hcl/SwiftyMarisa", branch: "6e145aef5583aac96dd7ff8f9fbb9944d893128e"))
|
||||
efficientNGramDependencies.append(.product(name: "SwiftyMarisa", package: "SwiftyMarisa", condition: .when(traits: ["Zenzai"])))
|
||||
#endif
|
||||
|
||||
|
||||
@ -139,58 +137,29 @@ if checkObjcAvailability() {
|
||||
}
|
||||
#endif
|
||||
|
||||
if let envValue = ProcessInfo.processInfo.environment["LLAMA_MOCK"], envValue == "1" {
|
||||
targets.append(contentsOf: [
|
||||
.target(name: "llama-mock"),
|
||||
.target(
|
||||
name: "KanaKanjiConverterModule",
|
||||
dependencies: [
|
||||
"SwiftUtils",
|
||||
"llama-mock",
|
||||
"EfficientNGram",
|
||||
.product(name: "Collections", package: "swift-collections"),
|
||||
],
|
||||
swiftSettings: swiftSettings
|
||||
)
|
||||
])
|
||||
} else {
|
||||
#if os(Windows) || os(Linux)
|
||||
targets.append(contentsOf: [
|
||||
.systemLibrary(
|
||||
name: "llama.cpp"
|
||||
),
|
||||
.target(
|
||||
name: "KanaKanjiConverterModule",
|
||||
dependencies: [
|
||||
"SwiftUtils",
|
||||
"llama.cpp",
|
||||
"EfficientNGram",
|
||||
.product(name: "Collections", package: "swift-collections"),
|
||||
],
|
||||
swiftSettings: swiftSettings
|
||||
)
|
||||
])
|
||||
#else
|
||||
targets.append(contentsOf: [
|
||||
.binaryTarget(
|
||||
name: "llama.cpp",
|
||||
url: "https://github.com/fkunn1326/llama.cpp/releases/download/b4844/llama-b4844-xcframework.zip",
|
||||
// this can be computed `swift package compute-checksum llama-b4844-xcframework.zip`
|
||||
checksum: "40bd1e58e727511649e13a6de9eb577ea8be78fe4183c2e1b382b12054849f05"
|
||||
),
|
||||
.target(
|
||||
name: "KanaKanjiConverterModule",
|
||||
dependencies: [
|
||||
"SwiftUtils",
|
||||
"EfficientNGram",
|
||||
"llama.cpp",
|
||||
.product(name: "Collections", package: "swift-collections"),
|
||||
],
|
||||
swiftSettings: swiftSettings
|
||||
)
|
||||
])
|
||||
#endif
|
||||
}
|
||||
#if os(Windows) || os(Linux)
|
||||
let llamaCppTarget: Target = .systemLibrary(name: "llama.cpp")
|
||||
#else
|
||||
let llamaCppTarget: Target = .binaryTarget(
|
||||
name: "llama.cpp",
|
||||
url: "https://github.com/azooKey/llama.cpp/releases/download/b4846/signed-llama.xcframework.zip",
|
||||
// this can be computed `swift package compute-checksum llama-b4844-xcframework.zip`
|
||||
checksum: "db3b13169df8870375f212e6ac21194225f1c85f7911d595ab64c8c790068e0a"
|
||||
)
|
||||
#endif
|
||||
targets.append(llamaCppTarget)
|
||||
targets.append(
|
||||
.target(
|
||||
name: "KanaKanjiConverterModule",
|
||||
dependencies: [
|
||||
"SwiftUtils",
|
||||
.target(name: "EfficientNGram", condition: .when(traits: ["Zenzai"])),
|
||||
.target(name: "llama.cpp", condition: .when(traits: ["Zenzai"])),
|
||||
.product(name: "Collections", package: "swift-collections"),
|
||||
],
|
||||
swiftSettings: swiftSettings
|
||||
)
|
||||
)
|
||||
|
||||
let package = Package(
|
||||
name: "AzooKeyKanakanjiConverter",
|
||||
@ -212,6 +181,10 @@ let package = Package(
|
||||
targets: ["KanaKanjiConverterModule"]
|
||||
),
|
||||
],
|
||||
traits: [
|
||||
.trait(name: "Zenzai"),
|
||||
.default(enabledTraits: [])
|
||||
],
|
||||
dependencies: dependencies,
|
||||
targets: targets
|
||||
)
|
||||
|
16
README.md
16
README.md
@ -5,7 +5,7 @@ AzooKeyKanaKanjiConverterは[azooKey](https://github.com/ensan-hcl/azooKey)の
|
||||
また、AzooKeyKanaKanjiConverterはニューラルかな漢字変換システム「Zenzai」を利用した高精度な変換もサポートしています。
|
||||
|
||||
## 動作環境
|
||||
iOS 14以降, macOS 11以降, visionOS 1以降, Ubuntu 22.04以降で動作を確認しています。
|
||||
iOS 16以降, macOS 13以降, visionOS 1以降, Ubuntu 22.04以降で動作を確認しています。Swift 6.1以上が必要です。
|
||||
|
||||
AzooKeyKanaKanjiConverterの開発については[開発ガイド](Docs/development_guide.md)をご覧ください。
|
||||
|
||||
@ -18,7 +18,7 @@ AzooKeyKanaKanjiConverterの開発については[開発ガイド](Docs/developm
|
||||
* Swift Packageの場合、Package.swiftの`Package`の引数に`dependencies`以下の記述を追加してください。
|
||||
```swift
|
||||
dependencies: [
|
||||
.package(url: "https://github.com/ensan-hcl/AzooKeyKanaKanjiConverter", .upToNextMinor(from: "0.8.0"))
|
||||
.package(url: "https://github.com/azooKey/AzooKeyKanaKanjiConverter", .upToNextMinor(from: "0.8.0"))
|
||||
],
|
||||
```
|
||||
また、ターゲットの`dependencies`にも同様に追加してください。
|
||||
@ -80,7 +80,16 @@ let options = ConvertRequestOptions.withDefaultDictionary(
|
||||
`ComposingText`は入力管理を行いつつ変換をリクエストするためのAPIです。ローマ字入力などを適切にハンドルするために利用できます。詳しくは[ドキュメント](./Docs/composing_text.md)を参照してください。
|
||||
|
||||
### Zenzaiを使う
|
||||
ニューラルかな漢字変換システム「Zenzai」を利用するには、`ConvertRequestOptions`の`zenzaiMode`を指定します。詳しくは[ドキュメント](./Docs/zenzai.md)を参照してください。
|
||||
ニューラルかな漢字変換システム「Zenzai」を利用するには、追加で[Swift Package Traits](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0450-swiftpm-package-traits.md)の設定を行う必要があります。AzooKeyKanaKanjiConverterは「Zenzai」というTraitをサポートしているので、これを追加してください。
|
||||
|
||||
```swift
|
||||
dependencies: [
|
||||
.package(url: "https://github.com/azooKey/AzooKeyKanaKanjiConverter", .upToNextMinor(from: "0.8.0"), traits: ["Zenzai"])
|
||||
],
|
||||
```
|
||||
|
||||
`ConvertRequestOptions`の`zenzaiMode`を指定します。詳しい引数の情報については[ドキュメント](./Docs/zenzai.md)を参照してください。
|
||||
|
||||
```swift
|
||||
let options = ConvertRequestOptions.withDefaultDictionary(
|
||||
// ...
|
||||
@ -90,6 +99,7 @@ let options = ConvertRequestOptions.withDefaultDictionary(
|
||||
```
|
||||
|
||||
### 辞書データ
|
||||
|
||||
AzooKeyKanaKanjiConverterのデフォルト辞書として[azooKey_dictionary_storage](https://github.com/ensan-hcl/azooKey_dictionary_storage)がサブモジュールとして指定されています。過去のバージョンの辞書データは[Google Drive](https://drive.google.com/drive/folders/1Kh7fgMFIzkpg7YwP3GhWTxFkXI-yzT9E?usp=sharing)からもダウンロードすることができます。
|
||||
|
||||
また、以下のフォーマットであれば自前で用意した辞書データを利用することもできます。カスタム辞書データのサポートは限定的なので、ソースコードを確認の上ご利用ください。
|
||||
|
@ -1,5 +1,5 @@
|
||||
import Foundation
|
||||
#if canImport(SwiftyMarisa)
|
||||
#if canImport(SwiftyMarisa) && Zenzai
|
||||
import SwiftyMarisa
|
||||
|
||||
/// Base64 でエンコードされた Key-Value をデコードする関数
|
||||
|
@ -1,5 +1,5 @@
|
||||
import Foundation
|
||||
#if canImport(SwiftyMarisa)
|
||||
#if canImport(SwiftyMarisa) && Zenzai
|
||||
import SwiftyMarisa
|
||||
|
||||
final class SwiftTrainer {
|
||||
|
@ -1,8 +1,8 @@
|
||||
#if canImport(llama)
|
||||
#if Zenzai
|
||||
// Zenzaiが有効でない場合、llama-mock.swiftの実装が利用可能になる
|
||||
import llama
|
||||
#else
|
||||
import llama_mock
|
||||
#endif
|
||||
|
||||
import SwiftUtils
|
||||
import HeapModule
|
||||
import Algorithms
|
||||
|
@ -1,3 +1,5 @@
|
||||
#if !Zenzai
|
||||
// Zenzaiが有効でない場合、このMock実装を有効化する
|
||||
private func unimplemented<T>() -> T {
|
||||
fatalError("unimplemented")
|
||||
}
|
||||
@ -59,4 +61,5 @@ package func llama_vocab_bos(_ vocab: llama_vocab) -> llama_token { unimplemente
|
||||
package func llama_token_to_piece(_ vocab: llama_vocab, _ token: llama_token, _ buf: UnsafeMutablePointer<Int8>, _ length: Int32, _ lstrip: Int32, _ special: Bool) -> Int32 { unimplemented() }
|
||||
|
||||
package func llama_decode(_ ctx: llama_context, _ batch: llama_batch) -> Int { unimplemented() }
|
||||
package func llama_get_logits(_ ctx: llama_context) -> UnsafeMutablePointer<Float>? { unimplemented() }
|
||||
package func llama_get_logits(_ ctx: llama_context) -> UnsafeMutablePointer<Float>? { unimplemented() }
|
||||
#endif
|
Reference in New Issue
Block a user