Swiftpack.co - griffin-stewie/BudouX.swift as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by griffin-stewie.
griffin-stewie/BudouX.swift v0.4.0
BudouX for Swift
⭐ 26
🕓 5 days ago
.package(url: "https://github.com/griffin-stewie/BudouX.swift.git", from: "v0.4.0")

BudouX.swift

BudouX Swift implementation.

BudouX is the machine learning powered line break organizer tool.

How it works

The original BudouX uses HTML markup to ensure that clauses are broken properly. BudouX.swift inserts a U+2060(word joiner) and a U+200B(zero width space) between each character and clause to ensure that Cocoa's UI component to do the line breaking properly.

Here is a sample project in this repository "Example.swiftpm".

CLI tool budoux-swift contains in this repository as well.

Usage

You can get a list of phrases by feeding a sentence to the parser.

import BudouX
// Load Default Japanese Parser
let parser = Parser()
// Parse
print(parser.parse("あăȘăŸă«ćŻ„ă‚Šæ·»ă†æœ€ć…ˆç«Żăźăƒ†ă‚ŻăƒŽăƒ­ă‚žăƒŒă€‚"))
// ["あăȘăŸă«", "ćŻ„ă‚Šæ·»ă†", "æœ€ć…ˆç«Żăź", "ăƒ†ă‚ŻăƒŽăƒ­ă‚žăƒŒă€‚"]

You can also translate an Swift's String with word joiners and zero width spaces for semantic line breaks.

import BudouX
// Load Default Japanese Parser
let parser = Parser()
let sample = "あăȘăŸă«ćŻ„ă‚Šæ·»ă†æœ€ć…ˆç«Żăźăƒ†ă‚ŻăƒŽăƒ­ă‚žăƒŒă€‚"
print(parser.translate(sentence: sample))
// あ⁠ăȘâ ăŸâ ă«â€‹ćŻ„â ă‚Šâ æ·»â ă†â€‹æœ€â ć…ˆâ ç«Żâ ăźâ€‹ăƒ†â ă‚Żâ ăƒŽâ ăƒ­â ă‚žâ ăƒŒâ ă€‚

Here's a convinience String extension method as well.

import BudouX

let sample = "あăȘăŸă«ćŻ„ă‚Šæ·»ă†æœ€ć…ˆç«Żăźăƒ†ă‚ŻăƒŽăƒ­ă‚žăƒŒă€‚"
print(sample.budouxed())
// あ⁠ăȘâ ăŸâ ă«â€‹ćŻ„â ă‚Šâ æ·»â ă†â€‹æœ€â ć…ˆâ ç«Żâ ăźâ€‹ăƒ†â ă‚Żâ ăƒŽâ ăƒ­â ă‚žâ ăƒŒâ ă€‚

If you need a function which translate an HTML string by wrapping phrases with non-breaking markup. Here's a support package for it.

griffin-stewie/HTMLBudouX.swift

For SwiftUI.Text

You can also use methods for SwiftUI's Text in iOS 13.0+, macOS 10.15+, Mac Catalyst 13.0+, tvOS 13.0+, and watchOS 6.0+.

import SwiftUI
import BudouX

struct ContentView: View {
    static let content = "ă‚ăźă‚€ăƒŒăƒăƒˆăƒŒăƒŽă‚©ăźă™ăăšăŠăŁăŸéąšă€ć€ă§ă‚‚ćș•ă«ć†·ăŸă•ă‚’ă‚‚ă€é’ă„ăă‚‰ă€ă†ă€ăă—ă„æŁźă§éŁŸă‚‰ă‚ŒăŸăƒąăƒȘăƒŒă‚Șćž‚ă€éƒŠć€–ăźăŽă‚‰ăŽă‚‰ăČă‹ă‚‹è‰ăźæłąă€‚"

    let swiftuiText: SwiftUI.Text = Text(content)
    let budouxText: SwiftUI.Text = BudouXText(content)
    
    var body: some View {
        VStack {
            swiftuiText
            budouxText
        }
        .multilineTextAlignment(.center)
    }
}

Install

Support Swift Package Manager only. There are no plans to support other package management tools at this time.

package.append(
    .package(url: "https://github.com/griffin-stewie/BudouX.swift", from: "0.4.0")
)

package.targets.append(
    .target(name: "Foo", dependencies: [
        .productItem(name: "BudouX", package: "BudouX.swift")
    ])
)

Update Data from BudouX

For this package maintainer, run following command to update Sources/BudouX/Data/JaKNBCModel.swift and Sources/BudouX/Data/UnicodeBlocks.swift from original BudouX.

make update_data 

GitHub

link
Stars: 26
Last commit: 5 days ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

v0.4.0
5 days ago

Now BudouX.swift supports SwiftUI's Text. (Thanks treastrain!). More details.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics