Last active
April 28, 2023 09:08
-
-
Save glowinthedark/d2153626786827c92d0140a4a75ddc83 to your computer and use it in GitHub Desktop.
MacOS Applescript to detect language of text string and say it using TTS Voice for detected language
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use framework "Foundation" | |
use framework "NaturalLanguage" | |
use framework "AVFoundation" | |
-- https://www.macscripter.net/t/detect-language-from-sentence-for-speech-synthesizer/74560 | |
(** | |
* import Foundation | |
* import NaturalLanguage | |
* import AVFoundation | |
* | |
* var recognizeString = "Bonjour" | |
* let lang = NLLanguageRecognizer.dominantLanguage(for: recognizeString) | |
* print(lang?.rawValue as Any) | |
*) | |
set recognizeString to "Bonjour" | |
set lang to current application's NLLanguageRecognizer's dominantLanguageForString:recognizeString | |
-- return lang as anything | |
(** | |
* let recognizeString = """ | |
* 私はガラスを食べられます。それは私を傷つけません。 | |
* """ | |
* | |
* let recognizer = NLLanguageRecognizer() | |
* recognizer.processString(recognizeString) | |
* print(recognizer.dominantLanguage?.rawValue as Any) | |
*) | |
set recognizeString to "私はガラスを食べられます。それは私を傷つけません." | |
set recognizer to current application's NLLanguageRecognizer's new() | |
recognizer's processString:recognizeString | |
set lang to recognizer's dominantLanguage() | |
-- return lang as anything | |
(** | |
* let recognizeString3 = """ | |
* Je m'baladais sur l'avenue le cœur ouvert à l'inconnu | |
* J'avais envie de dire bonjour à n'importe qui. | |
* N'importe qui et ce fut toi, je t'ai dit n'importe quoi | |
* Il suffisait de te parler, pour t'apprivoiser. | |
* """ | |
* | |
* let languageRecognizer = NLLanguageRecognizer() | |
* languageRecognizer.processString(recognizeString3) | |
* let language = languageRecognizer.dominantLanguage!.rawValue | |
* | |
* let speechSynthesizer = AVSpeechSynthesizer() | |
* let utterance = AVSpeechUtterance(string: recognizeString3) | |
* utterance.voice = AVSpeechSynthesisVoice(language: language) | |
* utterance.rate = 0.2 | |
* speechSynthesizer.speak(utterance) | |
*) | |
set recognizeString to "Je m'baladais sur l'avenue le cœur ouvert à l'inconnu | |
J'avais envie de dire bonjour à n'importe qui. | |
N'importe qui et ce fut toi, je t'ai dit n'importe quoi | |
Il suffisait de te parler, pour t'apprivoiser." | |
set languageRecognizer to current application's NLLanguageRecognizer's new() | |
languageRecognizer's processString:recognizeString | |
set lang to languageRecognizer's dominantLanguage() | |
set speechSynthesizer to current application's AVSpeechSynthesizer's new() | |
set utterance to current application's AVSpeechUtterance's alloc()'s initWithString:recognizeString | |
set speechVoice to current application's AVSpeechSynthesisVoice's voiceWithLanguage:lang | |
utterance's setVoice:speechVoice | |
utterance's setRate:0.2 | |
utterance's setRate:0.5 | |
speechSynthesizer's speakUtterance:utterance |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment