Skip to content

Commit

Permalink
Merge pull request #513 from zonble/master
Browse files Browse the repository at this point in the history
Adds a shortcut to input enclosed numbers like ❶ ② ㈢ and so on
  • Loading branch information
lukhnos authored Jul 31, 2024
2 parents e6277ec + cfe0403 commit e66d6b7
Show file tree
Hide file tree
Showing 12 changed files with 515 additions and 13 deletions.
10 changes: 10 additions & 0 deletions McBopomofoTests/ServiceProviderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,14 @@ final class ServiceProviderTests: XCTestCase {
XCTAssert(result == "⠠⠞⠓⠊⠎ ⠊⠎ ⠁ ⠞⠑⠎⠞ ⠋⠺⠂⠻⠄⠛⠥⠂⠓⠫⠐⠑⠳⠄⠪⠐⠙⠮⠁⠅⠎⠐⠊⠱⠐⠑⠪⠄⠏⠣⠄⠇⠶⠐", result)
}

func testDigit() {
LanguageModelManager.loadDataModels()
let provider = ServiceProvider()
let helper = ServiceProviderInputHelper()
provider.delegate = helper as? any ServiceProviderDelegate
let result = provider.convertToBraille(string: "24")
XCTAssert(result == "⠼⠆⠲", result)
}


}
51 changes: 44 additions & 7 deletions Packages/BopomofoBraille/Sources/BopomofoBraille/Converter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import Foundation

/// Convert Bopomofo to Braille and vice versa.
@objc public class BopomofoBrailleConverter: NSObject {

private enum ConverterState {
case initial
case bpmf
Expand All @@ -33,6 +34,9 @@ import Foundation
}

/// Convert from Bopomofo to Braille.
///
/// - Parameter bopomofo: the input text in Bopomofo.
/// - Returns: Converted Taiwanese Braille.
@objc(convertFromBopomofo:)
public static func convert(bopomofo: String) -> String {
var state = ConverterState.initial
Expand All @@ -41,7 +45,16 @@ import Foundation
let length = bopomofo.count

while readHead < length {
if String(bopomofo[bopomofo.index(bopomofo.startIndex, offsetBy: readHead)]) == " " {
let currentChar = String(
bopomofo[bopomofo.index(bopomofo.startIndex, offsetBy: readHead)])
if currentChar == "\n" || currentChar == "\t" {
output += currentChar
readHead += 1
state = .initial
continue
}

if currentChar == " " {
if output.isEmpty {
output += " "
} else if output[output.index(output.endIndex, offsetBy: -1)] != " " {
Expand Down Expand Up @@ -112,6 +125,9 @@ import Foundation
let substring = bopomofo[start...end]
do {
let syllable = try BopomofoSyllable(rawValue: String(substring))
if state != .bpmf && state != .initial {
output += " "
}
output += syllable.braille
readHead += i + 1
state = .bpmf
Expand All @@ -130,6 +146,9 @@ import Foundation
let substring = String(
bopomofo[bopomofo.index(bopomofo.startIndex, offsetBy: readHead)])
if let punctuation = FullWidthPunctuation(rawValue: substring) {
if state != .bpmf && state != .initial {
output += " "
}
output += punctuation.braille
readHead += 1
state = .bpmf
Expand All @@ -141,7 +160,7 @@ import Foundation
bopomofo[bopomofo.index(bopomofo.startIndex, offsetBy: readHead)])

if ("0"..."9").contains(substring) {
if state != .initial {
if state != .initial && state != .digits {
output += " "
}
output += ""
Expand All @@ -156,7 +175,7 @@ import Foundation
let lowered = substring.lowercased()

if ("a"..."z").contains(lowered) {
if state != .initial {
if state != .initial && state != .letters {
output += " "
}
if ("A"..."Z").contains(substring) {
Expand All @@ -171,7 +190,7 @@ import Foundation
}

if let punctuation = HalfWidthPunctuation(rawValue: substring) {
if state != .initial {
if state != .initial && state != .letters {
output += " "
}
output += punctuation.braille
Expand All @@ -190,6 +209,9 @@ import Foundation
}

/// Convert from Bopomofo to Braille.
///
/// - Parameter braille: The text in Taiwanese Barille.
/// - Returns: The converted text in Bopomofo.
@objc(convertFromBraille:)
public static func convert(braille: String) -> String {
var output = ""
Expand All @@ -205,6 +227,11 @@ import Foundation
return output
}

/// Converts the text in Taiwanese Braille to tokens. The tokens are in two types,
/// one is Bopomofo while another one is for all other kinds of texts.
///
/// - Parameter braille: The text in Taiwanese Braille.
/// - Returns: <#description#>
@objc(convertBrailleToTokens:)
public static func convert(brailleToTokens braille: String) -> [Any] {
var state = ConverterState.initial
Expand All @@ -215,11 +242,21 @@ import Foundation

while readHead < length {

if String(braille[braille.index(braille.startIndex, offsetBy: readHead)]) == " " {
let currentChar = String(braille[braille.index(braille.startIndex, offsetBy: readHead)])
if currentChar == "\n" || currentChar == "\t" {
if nonBpmfText.isEmpty {
nonBpmfText += " "
nonBpmfText += currentChar
}
else if nonBpmfText[nonBpmfText.index(nonBpmfText.endIndex, offsetBy: -1)] != " " {
readHead += 1
state = .initial
continue
}

if currentChar == " " {
if nonBpmfText.isEmpty {
nonBpmfText += " "
} else if nonBpmfText[nonBpmfText.index(nonBpmfText.endIndex, offsetBy: -1)] != " "
{
nonBpmfText += " "
}
readHead += 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@ import XCTest

final class BopomofoBrailleTests: XCTestCase {

func testSpace() {
let input = "ㄎㄜˇ、IBM"
let r1 = BopomofoBrailleConverter.convert(bopomofo: input)
let r2 = BopomofoBrailleConverter.convert(braille: r1)
XCTAssert(r2 == "ㄎㄜˇ、 IBM", r2)
}

func testDigit() {
let input = "24"
let r1 = BopomofoBrailleConverter.convert(bopomofo: input)
XCTAssert(r1 == "⠼⠆⠲", r1)
let r2 = BopomofoBrailleConverter.convert(braille: r1)
XCTAssert(r2 == input, r2)
}

func testConvertDigits1() {
let input = "1"
let r1 = BopomofoBrailleConverter.convert(bopomofo: input)
Expand Down
2 changes: 2 additions & 0 deletions Source/Base.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,5 @@
"Speak" = "Speak";

"Character Information" = "Character Information";

"Enclosed Numbers" = "Enclosed Numbers";
Loading

0 comments on commit e66d6b7

Please sign in to comment.