Skip to content

Commit

Permalink
Merge pull request #23 from jeanetienne/refactoring-architecture
Browse files Browse the repository at this point in the history
Re-architected spelling alphabets and added more metadata
  • Loading branch information
jeanetienne authored Mar 5, 2023
2 parents c89bd54 + c812132 commit a058449
Show file tree
Hide file tree
Showing 42 changed files with 1,054 additions and 886 deletions.
2 changes: 1 addition & 1 deletion Sources/Speller/Speller.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class Speller {
/// - alphabet: The spelling alphabet to be used to spell out the input phrase
/// - useSpellingAlphabetNumbers: Whether to use the spelling alphabet's numbers, or default to simple number names
/// - Returns: An array of `SpelledCharacter`s describing each character of the input phrase
public static func spell<Alphabet: SpellingAlphabet>(phrase: String, withSpellingAlphabet alphabet: Alphabet.Type, useSpellingAlphabetNumbers: Bool) -> [SpelledCharacter] {
public static func spell(phrase: String, withSpellingAlphabet alphabet: SpellingAlphabet, useSpellingAlphabetNumbers: Bool) -> [SpelledCharacter] {
let spelling = alphabet.spell(phrase, withNumbers: useSpellingAlphabetNumbers)
return describeUnknownCharacters(inSpelling: spelling)
}
Expand Down
35 changes: 25 additions & 10 deletions Sources/Speller/SpellingAlphabet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,37 @@ import Foundation

public typealias SpellingAlphabetContent = [String: CodeWordCollection]

public protocol SpellingAlphabet {
static var mainContent: SpellingAlphabetContent { get }
static var numbersContent: SpellingAlphabetContent { get }
static func spell(_ phrase: String, withNumbers: Bool) -> [SpelledCharacter]
}
public class SpellingAlphabet {

let uniqueIdentifier: String

let associatedLanguageCode: String?

let associatedRegionCode: String?

extension SpellingAlphabet {
let recommendedJoinerWord: String?

let mainContent: SpellingAlphabetContent

let numbersContent: SpellingAlphabetContent

internal init(uniqueIdentifier: String, associatedLanguageCode: String? = nil, associatedRegionCode: String? = nil, recommendedJoinerWord: String? = nil, mainContent: SpellingAlphabetContent, numbersContent: SpellingAlphabetContent) {
self.uniqueIdentifier = uniqueIdentifier
self.associatedLanguageCode = associatedLanguageCode
self.associatedRegionCode = associatedRegionCode
self.recommendedJoinerWord = recommendedJoinerWord
self.mainContent = mainContent
self.numbersContent = numbersContent
}

public static func spell(_ phrase: String, withNumbers: Bool) -> [SpelledCharacter] {
public func spell(_ phrase: String, withNumbers: Bool) -> [SpelledCharacter] {
return enumerate(phrase: phrase)
.enumerated()
.map { spell(character: $1, atIndex: $0, withNumbers: withNumbers) }
}

// MARK: - Private helpers - Spelling
private static func enumerate(phrase: String) -> [String] {
private func enumerate(phrase: String) -> [String] {
var characters: [String] = []
phrase.enumerateSubstrings(in: phrase.startIndex..<phrase.endIndex, options: .byComposedCharacterSequences) { (string, rangeOne, rangeTwo, someBool) in
if let decomposedString = string {
Expand All @@ -33,15 +48,15 @@ extension SpellingAlphabet {
return characters
}

private static func spell(character: String, atIndex index: Int, withNumbers: Bool) -> SpelledCharacter {
private func spell(character: String, atIndex index: Int, withNumbers: Bool) -> SpelledCharacter {
if let codeWordCollection = codeWordCollection(forCharacter: character, withNumbers: withNumbers) {
return SpelledCharacter(character: character, position: index, spellingResult: .match(codeWordCollection))
} else {
return SpelledCharacter(character: character, position: index, spellingResult: .unknown)
}
}

private static func codeWordCollection(forCharacter character: String, withNumbers: Bool) -> CodeWordCollection? {
private func codeWordCollection(forCharacter character: String, withNumbers: Bool) -> CodeWordCollection? {
let spellingContent = withNumbers ? mainContent.merging(numbersContent, uniquingKeysWith: { lhs, _ in lhs }) : mainContent

if let codeWordCollection = spellingContent[character] {
Expand Down
28 changes: 0 additions & 28 deletions Sources/Speller/SpellingAlphabets/Czech.swift

This file was deleted.

30 changes: 0 additions & 30 deletions Sources/Speller/SpellingAlphabets/Danish.swift

This file was deleted.

29 changes: 0 additions & 29 deletions Sources/Speller/SpellingAlphabets/Dutch.swift

This file was deleted.

30 changes: 0 additions & 30 deletions Sources/Speller/SpellingAlphabets/Finnish.swift

This file was deleted.

28 changes: 0 additions & 28 deletions Sources/Speller/SpellingAlphabets/French.swift

This file was deleted.

30 changes: 0 additions & 30 deletions Sources/Speller/SpellingAlphabets/German.swift

This file was deleted.

This file was deleted.

28 changes: 0 additions & 28 deletions Sources/Speller/SpellingAlphabets/Italian.swift

This file was deleted.

28 changes: 0 additions & 28 deletions Sources/Speller/SpellingAlphabets/LAPD.swift

This file was deleted.

30 changes: 0 additions & 30 deletions Sources/Speller/SpellingAlphabets/Norwegian.swift

This file was deleted.

Loading

0 comments on commit a058449

Please sign in to comment.