Skip to content

Commit

Permalink
WIP: Escaping characters sent to TTS
Browse files Browse the repository at this point in the history
- Fixes #90
- Fixes #93
- SEMVER: This is a MAJOR change, but since pre 1.0, can be a MINOR
  • Loading branch information
tlhunter committed Sep 29, 2019
1 parent 444a49c commit 803530d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
14 changes: 12 additions & 2 deletions platform/base.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const childProcess = require('child_process')
const once = require('one-time')
const symbolTTS = require('../symbol-tts.js')

class SayPlatformBase {
constructor () {
Expand Down Expand Up @@ -28,7 +29,11 @@ class SayPlatformBase {
})
}

let { command, args, pipedData, options } = this.buildSpeakCommand({ text, voice, speed })
let { command, args, pipedData, options } = this.buildSpeakCommand({
text: symbolTTS(text),
voice,
speed
})

this.child = childProcess.spawn(command, args, options)

Expand Down Expand Up @@ -84,7 +89,12 @@ class SayPlatformBase {
}

try {
var { command, args, pipedData, options } = this.buildExportCommand({ text, voice, speed, filename })
var { command, args, pipedData, options } = this.buildExportCommand({
text: symbolTTS(text),
voice,
speed,
filename
})
} catch (error) {
return setImmediate(() => {
callback(error)
Expand Down
46 changes: 46 additions & 0 deletions symbol-tts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// This file converts symbols into their audible form
// It's essentially a tool for 'escaping' characters for TTS purposes
const lookup = new Map()

// lookup.set(/!/g, ' exclamation mark ')
lookup.set(/@/g, ' at ')
lookup.set(/#/g, ' octothorpe ')
lookup.set(/\$/g, ' dollar sign ')
lookup.set(/%/g, ' percent ')
lookup.set(/\^/g, ' caret ')
lookup.set(/&/g, ' and ')
lookup.set(/\*/g, ' asterisk ')
lookup.set(/\(/g, ' left paren ')
lookup.set(/\)/g, ' right paren ')
lookup.set(/-/g, ' minus ')
lookup.set(/\+/g, ' plus ')
lookup.set(/_/g, ' underscore ')
lookup.set(/=/g, ' equals ')
lookup.set(/"/g, ' quote ')
// lookup.set(/'/g, ' single quote ') // would break words like "don't"
lookup.set(/\]/g, ' right bracket ')
lookup.set(/\[/g, ' left bracket ')
lookup.set(/\}/g, ' right curly ')
lookup.set(/\{/g, ' left curly ')
lookup.set(/\|/g, ' pipe ')
lookup.set(/\//g, ' slash ')
lookup.set(/\\/g, ' backslash ')
// lookup.set(/:/g, ' colon ')
// lookup.set(/;/g, ' semicolon ')
// lookup.set(/./g, ' period ')
// lookup.set(/,/g, ' comma ')
// lookup.set(/?/g, ' question mark ')
lookup.set(/`/g, ' backtick ')
lookup.set(/~/g, ' tilde ')
lookup.set(/>/g, ' greater than ')
lookup.set(/</g, ' less than ')
lookup.set(/\r/g, ' ')
lookup.set(/\n/g, ' ')

module.exports = (text) => {
lookup.forEach((after, before) => {
text = text.replace(before, after)
})

return text
}

0 comments on commit 803530d

Please sign in to comment.