<commit_before>//
//  Speller.swift
//  Speller
//
//  Created by Jean-Étienne on 7/11/16.
//  Copyright © 2016 Jean-Étienne. All rights reserved.
//

import Foundation

/// A `Speller` provides an interface to spell out strings using `SpellingAlphabet`s
public class Speller {

    public init() {
        
    }

    /// Spells the given string using a spelling alphabet
    ///
    /// - Parameters:
    ///   - phrase: The phrase to be spelled out
    ///   - alphabet: The spelling alphabet to be used to spell out the input phrase
    /// - Returns: An array of `SpelledCharacter`s describing each character of the input phrase
    public func spell(phrase: String, withSpellingAlphabet alphabet: SpellingAlphabet) -> [SpelledCharacter] {
        return phrase.characters.map { character -> SpelledCharacter in
            return spell(character: character,
                         withSpellingAlphabets: fallbackAlphabets(forAlphabet: alphabet))
        }
    }

    // MARK: - Private methods
    private func spell(character: Character, withSpellingAlphabets alphabets: [SpellingAlphabet]) -> SpelledCharacter {
        if let codeWord = self.codeWord(forCharacter: character, withSpellingAlphabets: alphabets) {
            return SpelledCharacter.Match(character, codeWord)
        } else {
            return SpelledCharacter.Unknown(character)
        }
    }

    private func codeWord(forCharacter character: Character, withSpellingAlphabets alphabets: [SpellingAlphabet]) -> CodeWordCollection? {
        guard let alphabetContent = alphabets.first?.load() else {
            return nil
        }

        var codeWord = alphabetContent[character]<commit_msg>More concise init()<commit_after>//
//  Speller.swift
//  Speller
//
//  Created by Jean-Étienne on 7/11/16.
//  Copyright © 2016 Jean-Étienne. All rights reserved.
//

import Foundation

/// A `Speller` provides an interface to spell out strings using `SpellingAlphabet`s
public class Speller {

    public init() {}

    /// Spells the given string using a spelling alphabet
    ///
    /// - Parameters:
    ///   - phrase: The phrase to be spelled out
    ///   - alphabet: The spelling alphabet to be used to spell out the input phrase
    /// - Returns: An array of `SpelledCharacter`s describing each character of the input phrase
    public func spell(phrase: String, withSpellingAlphabet alphabet: SpellingAlphabet) -> [SpelledCharacter] {
        return phrase.characters.map { character -> SpelledCharacter in
            return spell(character: character,
                         withSpellingAlphabets: fallbackAlphabets(forAlphabet: alphabet))
        }
    }

    // MARK: - Private methods
    private func spell(character: Character, withSpellingAlphabets alphabets: [SpellingAlphabet]) -> SpelledCharacter {
        if let codeWord = self.codeWord(forCharacter: character, withSpellingAlphabets: alphabets) {
            return SpelledCharacter.Match(character, codeWord)
        } else {
            return SpelledCharacter.Unknown(character)
        }
    }

    private func codeWord(forCharacter character: Character, withSpellingAlphabets alphabets: [SpellingAlphabet]) -> CodeWordCollection? {
        guard let alphabetContent = alphabets.first?.load() else {
            return nil
        }

        var codeWord = alphabetContent[character]
{'fraction_non_alphanumeric': 0.08791866028708134, 'fraction_numerical': 0.005382775119617225, 'mean_word_length': 2.720800889877642, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '195623', 'n_tokens_mistral': 931, 'n_tokens_neox': 861, 'n_words': 273}