MacOS – Correcting Pronunciation Errors in ‘Say’ Command in Terminal

macosterminaltext to speechvoiceover

I'm having difficulty correcting the pronunciation in the 'say' command, using 10.9.

I tried to add new pronunciations through:

System Preferences > Dictation and Speech > Open Accessibility Preferences > Open VoiceOver Utility > Speech > Pronunciation > +

After I added a new entry I tried it in both Terminal and TextEdit and neither pronounced it correctly:

$ say -v "Samantha" -f filename.txt -o audiofile.aiff

Although it did not work in TextEdit (Edit > Speech > Start Speaking), I want to be able to accomplish this in Terminal.

Does anyone have any idea of how I can go about doing this?

Best Answer

Here you go:

# read.sh <file-to-read> [name-of-voice]
#!/bin/bash

textToRead=$(cat $1)

IFS=$'\n'
while read rep; do
        IFS=" "
        repArray=( $rep )
        textToRead=${textToRead//${repArray[0]}//${repArray[1]}}
done < replacements.txt

if [ -z $2 ]; then
        echo "$textToRead" | say
else
        echo "$textToRead" | say -v $2
fi

This shell script read replacements from replacements.txt and uses the say command to read the files content after replacing what's defined in replacements.txt.

replacements.txt: One line per replacement, <search> <replace>.

Sorry for the ugly code... I hate bash scripting.