<commit_before>var speech = angular.module('speech', []);

speech.factory('Speech', function ($log) {

    var is_available = ('speechSynthesis' in window);

    var get_voice_for_language = function (language) {
        var voices = window.speechSynthesis.getVoices();
        var preferred_voice = null;
        angular.forEach(voices, function (voice) {
            if (language === voice.lang) {
                preferred_voice = voice;
            }
        });

        if (!preferred_voice) {
            $log.warn('Could not select voice for "%s"', language);
        }
        return preferred_voice;
    };

    var service = {
        say: function (text, language) {
            var utterance = new SpeechSynthesisUtterance(text);
            var voice = get_voice_for_language(language);
            if (voice) {
                utterance.voice = voice;
            }
            window.speechSynthesis.speak(utterance);
        }

    };
    return service;
});<commit_msg>Added safe fallback for when voices are initialised<commit_after>var speech = angular.module('speech', []);

speech.factory('Speech', function ($log) {

    var is_available = ('speechSynthesis' in window);

    var voices = [];
    speechSynthesis.onvoiceschanged = function (event) {
        voices = speechSynthesis.getVoices();
    };

    var get_voice_for_language = function (language) {
        var preferred_voice = null;
        angular.forEach(voices, function (voice) {
            if (language === voice.lang) {
                preferred_voice = voice;
            }
        });

        if (!preferred_voice) {
            $log.warn('Could not select voice for "%s"', language);
        }
        return preferred_voice;
    };

    var service = {
        say: function (text, language) {
            var utterance = new SpeechSynthesisUtterance(text);
            var voice = get_voice_for_language(language);
            if (voice) {
                utterance.voice = voice;
            } else {
                utterance.lang = language;
            }
            window.speechSynthesis.speak(utterance);
        }

    };
    return service;
});
{'fraction_non_alphanumeric': 0.1111111111111111, 'fraction_numerical': 0.0, 'mean_word_length': 1.9930458970792768, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5956897', 'n_tokens_mistral': 606, 'n_tokens_neox': 572, 'n_words': 134}