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.9.0
BudouX for Swift
⭐ 49
🕓 22 weeks ago
.package(url: "https://github.com/griffin-stewie/BudouX.swift.git", from: "v0.9.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 convenience String extension method as well.

import BudouX

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

You can use an external model as follows.

import BudouX

// This case, directory download latest Japanese model from BudouX.
let url = URL(string: "https://raw.githubusercontent.com/google/budoux/main/budoux/models/ja.json")!
let (data, _) = try await URLSession.shared.data(from: url)

// Initialize `CustomModel` class from data you download.
let model = try CustomModel(modelJSON: data, supportedNaturalLanguages: ["ja"])

// Use the `CustomModel`
let parser = BudouX.Parser(model: model)
print(parser.parse(sentence: "あăȘăŸă«ćŻ„ă‚Šæ·»ă†æœ€ć…ˆç«Żăźăƒ†ă‚ŻăƒŽăƒ­ă‚žăƒŒă€‚"))

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

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.9.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 built-in models from original BudouX.

make generate_data 

GitHub

link
Stars: 49
Last commit: 1 week ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

v0.9.0
22 weeks ago

What's Changed

Full Changelog: https://github.com/griffin-stewie/BudouX.swift/compare/v0.8.0...v0.9.0

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