|
| 1 | +import { SpeechOptions } from '../SpeechOptions'; |
| 2 | +import { SsmlFormatterBase } from './SsmlFormatterBase'; |
| 3 | + |
| 4 | +export class SamsungBixbySsmlFormatter extends SsmlFormatterBase { |
| 5 | + |
| 6 | + constructor(public options: SpeechOptions) { |
| 7 | + super(options); |
| 8 | + |
| 9 | + this.modifierKeyToSsmlTagMappings.emphasis = null; |
| 10 | + this.modifierKeyToSsmlTagMappings.address = null; |
| 11 | + this.modifierKeyToSsmlTagMappings.number = null; |
| 12 | + this.modifierKeyToSsmlTagMappings.characters = null; |
| 13 | + this.modifierKeyToSsmlTagMappings.expletive = null; |
| 14 | + this.modifierKeyToSsmlTagMappings.fraction = null; |
| 15 | + this.modifierKeyToSsmlTagMappings.interjection = null; |
| 16 | + this.modifierKeyToSsmlTagMappings.ordinal = null; |
| 17 | + this.modifierKeyToSsmlTagMappings.telephone = null; |
| 18 | + this.modifierKeyToSsmlTagMappings.unit = null; |
| 19 | + this.modifierKeyToSsmlTagMappings.time = null; |
| 20 | + this.modifierKeyToSsmlTagMappings.date = null; |
| 21 | + this.modifierKeyToSsmlTagMappings.sub = null; |
| 22 | + this.modifierKeyToSsmlTagMappings.ipa = null; |
| 23 | + this.modifierKeyToSsmlTagMappings.rate = null; |
| 24 | + this.modifierKeyToSsmlTagMappings.pitch = null; |
| 25 | + this.modifierKeyToSsmlTagMappings.volume = null; |
| 26 | + } |
| 27 | + |
| 28 | + // tslint:disable-next-line: max-func-body-length |
| 29 | + // private getTextModifierObject(ast: any): any { |
| 30 | + // let textModifierObject = { |
| 31 | + // tags: {} |
| 32 | + // }; |
| 33 | + |
| 34 | + // for (let index = 0; index < ast.children.length; index++) { |
| 35 | + // const child = ast.children[index]; |
| 36 | + |
| 37 | + // switch (child.name) { |
| 38 | + // case 'plainText': |
| 39 | + // case 'plainTextSpecialChars': |
| 40 | + // case 'plainTextEmphasis': |
| 41 | + // case 'plainTextPhone': |
| 42 | + // case 'plainTextModifier': { |
| 43 | + // textModifierObject['text'] = child.allText; |
| 44 | + // break; |
| 45 | + // } |
| 46 | + // case 'textModifierKeyOptionalValue': { |
| 47 | + // let key = child.children[0].allText; |
| 48 | + // key = this.modifierKeyMappings[key] || key; |
| 49 | + // const value = child.children.length === 2 ? child.children[1].allText : ''; |
| 50 | + // const ssmlTag = this.modifierKeyToSsmlTagMappings[key]; |
| 51 | + // const sortId = this.ssmlTagSortOrder.indexOf(ssmlTag); |
| 52 | + |
| 53 | + // switch (key) { |
| 54 | + // case 'emphasis': { |
| 55 | + // if (!textModifierObject.tags[ssmlTag]) { |
| 56 | + // textModifierObject.tags[ssmlTag] = { sortId: sortId, attrs: null }; |
| 57 | + // } |
| 58 | + // textModifierObject.tags[ssmlTag].attrs = { level: value || 'moderate' }; |
| 59 | + // break; |
| 60 | + // } |
| 61 | + |
| 62 | + // case 'address': |
| 63 | + // case 'characters': |
| 64 | + // case 'expletive': |
| 65 | + // case 'fraction': |
| 66 | + // case 'number': |
| 67 | + // case 'ordinal': |
| 68 | + // case 'telephone': |
| 69 | + // case 'unit': { |
| 70 | + // if (!textModifierObject.tags[ssmlTag]) { |
| 71 | + // textModifierObject.tags[ssmlTag] = { sortId: sortId, attrs: null }; |
| 72 | + // } |
| 73 | + // textModifierObject.tags[ssmlTag].attrs = { 'interpret-as': key }; |
| 74 | + // break; |
| 75 | + // } |
| 76 | + |
| 77 | + // case 'date': { |
| 78 | + // if (!textModifierObject.tags[ssmlTag]) { |
| 79 | + // textModifierObject.tags[ssmlTag] = { sortId: sortId, attrs: null }; |
| 80 | + // } |
| 81 | + // textModifierObject.tags[ssmlTag].attrs = { 'interpret-as': key, format: value || 'ymd' }; |
| 82 | + // break; |
| 83 | + // } |
| 84 | + |
| 85 | + // case 'time': { |
| 86 | + // if (!textModifierObject.tags[ssmlTag]) { |
| 87 | + // textModifierObject.tags[ssmlTag] = { sortId: sortId, attrs: null }; |
| 88 | + // } |
| 89 | + // textModifierObject.tags[ssmlTag].attrs = { 'interpret-as': key, format: value || 'hms12' }; |
| 90 | + // break; |
| 91 | + // } |
| 92 | + |
| 93 | + // case 'whisper': { |
| 94 | + // if (!textModifierObject.tags[ssmlTag]) { |
| 95 | + // textModifierObject.tags[ssmlTag] = { sortId: sortId, attrs: null }; |
| 96 | + // } |
| 97 | + // textModifierObject.tags[ssmlTag].attrs = { volume: 'x-soft', rate: 'slow' }; |
| 98 | + // break; |
| 99 | + // } |
| 100 | + |
| 101 | + // case 'ipa': { |
| 102 | + // // Google Assistant does not support <phoneme> tag |
| 103 | + // if (!textModifierObject.tags[ssmlTag]) { |
| 104 | + // textModifierObject.tags[ssmlTag] = { sortId: sortId, attrs: null }; |
| 105 | + // } |
| 106 | + // textModifierObject['textOnly'] = true; |
| 107 | + // break; |
| 108 | + // } |
| 109 | + |
| 110 | + // case 'sub': { |
| 111 | + // if (!textModifierObject.tags[ssmlTag]) { |
| 112 | + // textModifierObject.tags[ssmlTag] = { sortId: sortId, attrs: null }; |
| 113 | + // } |
| 114 | + // textModifierObject.tags[ssmlTag].attrs = { alias: value }; |
| 115 | + // break; |
| 116 | + // } |
| 117 | + |
| 118 | + // case 'volume': |
| 119 | + // case 'rate': |
| 120 | + // case 'pitch': { |
| 121 | + |
| 122 | + // if (!textModifierObject.tags[ssmlTag]) { |
| 123 | + // textModifierObject.tags[ssmlTag] = { sortId: sortId, attrs: null }; |
| 124 | + // } |
| 125 | + |
| 126 | + // const attrs = {}; |
| 127 | + // attrs[key] = value || 'medium'; |
| 128 | + // textModifierObject.tags[ssmlTag].attrs = { ...textModifierObject.tags[ssmlTag].attrs, ...attrs }; |
| 129 | + |
| 130 | + // break; |
| 131 | + // } |
| 132 | + |
| 133 | + // default: { |
| 134 | + |
| 135 | + // } |
| 136 | + |
| 137 | + // } |
| 138 | + // break; |
| 139 | + // } |
| 140 | + // } |
| 141 | + |
| 142 | + // } |
| 143 | + |
| 144 | + // return textModifierObject; |
| 145 | + // } |
| 146 | + |
| 147 | + // tslint:disable-next-line: max-func-body-length |
| 148 | + protected formatFromAst(ast: any, lines: string[] = []): string[] { |
| 149 | + |
| 150 | + switch (ast.name) { |
| 151 | + case 'document': { |
| 152 | + if (this.options.includeFormatterComment) { |
| 153 | + this.addComment('Converted from Speech Markdown to SSML for Samsung Bixby', lines); |
| 154 | + } |
| 155 | + |
| 156 | + if (this.options.includeSpeakTag) { |
| 157 | + return this.addSpeakTag(ast.children, true, false, null, lines); |
| 158 | + } else { |
| 159 | + this.processAst(ast.children, lines); |
| 160 | + return lines; |
| 161 | + } |
| 162 | + } |
| 163 | + case 'paragraph': { |
| 164 | + if (this.options.includeParagraphTag) { |
| 165 | + return this.addTag('p', ast.children, true, false, null, lines); |
| 166 | + } else { |
| 167 | + this.processAst(ast.children, lines); |
| 168 | + return lines; |
| 169 | + } |
| 170 | + } |
| 171 | + // case 'shortBreak': { |
| 172 | + // const time = ast.children[0].allText; |
| 173 | + // return this.addTagWithAttrs(lines, null, 'break', { time: time }); |
| 174 | + // } |
| 175 | + // case 'shortEmphasisModerate': { |
| 176 | + // const text = ast.children[0].allText; |
| 177 | + // return this.addTagWithAttrs(lines, text, 'emphasis', { level: 'moderate' }); |
| 178 | + // } |
| 179 | + // case 'shortEmphasisStrong': { |
| 180 | + // const text = ast.children[0].allText; |
| 181 | + // return this.addTagWithAttrs(lines, text, 'emphasis', { level: 'strong' }); |
| 182 | + // } |
| 183 | + // case 'shortEmphasisNone': { |
| 184 | + // const text = ast.children[0].allText; |
| 185 | + // return this.addTagWithAttrs(lines, text, 'emphasis', { level: 'none' }); |
| 186 | + // } |
| 187 | + // case 'shortEmphasisReduced': { |
| 188 | + // const text = ast.children[0].allText; |
| 189 | + // return this.addTagWithAttrs(lines, text, 'emphasis', { level: 'reduced' }); |
| 190 | + // } |
| 191 | + |
| 192 | + // case 'textModifier': { |
| 193 | + // const tmo = this.getTextModifierObject(ast); |
| 194 | + |
| 195 | + // if (tmo.textOnly) { |
| 196 | + // // Quick return if tag is not supported |
| 197 | + // lines.push(tmo.text) |
| 198 | + // return lines |
| 199 | + // } |
| 200 | + |
| 201 | + // const tagsSortedDesc = Object.keys(tmo.tags).sort((a: any, b: any) => { return tmo.tags[b].sortId - tmo.tags[a].sortId }); |
| 202 | + |
| 203 | + // let inner = tmo.text; |
| 204 | + |
| 205 | + // for (let index = 0; index < tagsSortedDesc.length; index++) { |
| 206 | + // const tag = tagsSortedDesc[index]; |
| 207 | + // const attrs = tmo.tags[tag].attrs; |
| 208 | + |
| 209 | + // inner = this.getTagWithAttrs(inner, tag, attrs); |
| 210 | + |
| 211 | + // } |
| 212 | + // lines.push(inner); |
| 213 | + |
| 214 | + // return lines; |
| 215 | + // } |
| 216 | + case 'audio': { |
| 217 | + const url = ast.children[0].allText; |
| 218 | + return this.addTagWithAttrs(lines, null, 'audio', { src: url }); |
| 219 | + } |
| 220 | + case 'simpleLine': { |
| 221 | + this.processAst(ast.children, lines); |
| 222 | + return lines; |
| 223 | + } |
| 224 | + case 'lineEnd': { |
| 225 | + lines.push(ast.allText); |
| 226 | + return lines; |
| 227 | + } |
| 228 | + case 'emptyLine': { |
| 229 | + if (this.options.preserveEmptyLines) { |
| 230 | + lines.push(ast.allText); |
| 231 | + } |
| 232 | + |
| 233 | + return lines; |
| 234 | + } |
| 235 | + case 'plainText': |
| 236 | + case 'plainTextSpecialChars': |
| 237 | + case 'plainTextEmphasis': |
| 238 | + case 'plainTextPhone': |
| 239 | + case 'plainTextModifier': { |
| 240 | + lines.push(ast.allText); |
| 241 | + return lines; |
| 242 | + } |
| 243 | + |
| 244 | + default: { |
| 245 | + this.processAst(ast.children, lines); |
| 246 | + return lines; |
| 247 | + } |
| 248 | + } |
| 249 | + } |
| 250 | +} |
0 commit comments