BudouX Swift implementation.
BudouX is the machine learning powered line break organizer tool.
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.
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
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)
}
}
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.10.0")
)
package.targets.append(
.target(name: "Foo", dependencies: [
.productItem(name: "BudouX", package: "BudouX.swift")
])
)
For this package maintainer, run following command to update built-in models from original BudouX.
make generate_data