Using a speech engine to highlight words: Difference between revisions

Added Go
No edit summary
(Added Go)
Line 23:
}
Else word .= lf, i++
}</lang>
 
=={{header|Go}}==
{{works with|Ubuntu 16.04}}
<br>
This uses the eSpeak speech synthesizer which is invoked for each word in the text. As the word is spoken it is printed to the terminal in capitalized form (and the previous word is uncapitalized). After a second's delay the final word is uncapitalized.
 
Very robotic but it works.
<lang go>package main
 
import (
"fmt"
"log"
"os/exec"
"strings"
"time"
)
 
func main() {
s := "Actions speak louder than words."
prev := ""
prevLen := 0
bs := ""
for _, word := range strings.Fields(s) {
cmd := exec.Command("espeak", word)
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
if prevLen > 0 {
bs = strings.Repeat("\b", prevLen)
}
fmt.Printf("%s%s%s ", bs, prev, strings.ToUpper(word))
prev = word + " "
prevLen = len(word) + 1
}
bs = strings.Repeat("\b", prevLen)
time.Sleep(time.Second)
fmt.Printf("%s%s\n", bs, prev)
}</lang>
 
9,490

edits