Created
December 19, 2016 02:29
-
-
Save mfurtak/55be73fba303f2b61f6a59b7de9a7a64 to your computer and use it in GitHub Desktop.
SSML building
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
package io.fabric.alexaskills.util; | |
import java.util.function.Consumer; | |
public class SsmlBuilder implements SsmlPhrase { | |
private final StringBuilder stringBuilder; | |
private boolean built = false; | |
public SsmlBuilder() { | |
stringBuilder = new StringBuilder(); | |
} | |
public SsmlBuilder text(String format, Object... params) { | |
return text(String.format(format, params)); | |
} | |
public SsmlBuilder text(String text) { | |
stringBuilder.append(" "); | |
stringBuilder.append(text); | |
return this; | |
} | |
public SsmlBuilder sentence(String text) { | |
return sentence(builder -> builder.text(text)); | |
} | |
public SsmlBuilder sentence(String format, Object... params) { | |
return sentence(builder -> builder.text(String.format(format, params))); | |
} | |
public SsmlBuilder sentence(Consumer<SsmlPhrase> consumer) { | |
stringBuilder.append("<s>"); | |
consumer.accept(this); | |
stringBuilder.append("</s>"); | |
return this; | |
} | |
public SsmlBuilder paragraph(String text) { | |
return paragraph(builder -> builder.text(text)); | |
} | |
public SsmlBuilder paragraph(String format, Object... params) { | |
return paragraph(builder -> builder.text(String.format(format, params))); | |
} | |
public SsmlBuilder paragraph(Consumer<SsmlPhrase> consumer) { | |
stringBuilder.append("<p>"); | |
consumer.accept(this); | |
stringBuilder.append("</p>"); | |
return this; | |
} | |
public SsmlBuilder comma() { | |
stringBuilder.append(" <break strength=\"medium\"/> "); | |
return this; | |
} | |
public SsmlBuilder strongBreak() { | |
stringBuilder.append(" <break strength=\"strong\"/> "); | |
return this; | |
} | |
public SsmlBuilder newParagraph() { | |
stringBuilder.append(" <break strength=\"x-strong\"/> "); | |
return this; | |
} | |
public SsmlBuilder cardinalNumber(long number) { | |
return cardinalNumber(Long.toString(number)); | |
} | |
public SsmlBuilder cardinalNumber(String number) { | |
return sayAs("cardinal", number); | |
} | |
public SsmlBuilder ordinalNumber(long number) { | |
return ordinalNumber(Long.toString(number)); | |
} | |
public SsmlBuilder ordinalNumber(String number) { | |
return sayAs("ordinal", number); | |
} | |
public SsmlBuilder spellOut(String text) { | |
return sayAs("spell-out", text); | |
} | |
private SsmlBuilder sayAs(String kind, String text) { | |
stringBuilder.append(" <say-as interpret-as=\"") | |
.append(kind) | |
.append("\">") | |
.append(text) | |
.append("</say-as> "); | |
return this; | |
} | |
public String build() { | |
if (!built) { | |
built = true; | |
stringBuilder.insert(0, "<speak>").append("</speak>"); | |
} | |
return stringBuilder.toString(); | |
} | |
} |
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
package io.fabric.alexaskills.util; | |
import java.util.function.Consumer; | |
/** | |
* Defines the methods used to build a SSML speech phrase | |
*/ | |
public interface SsmlPhrase { | |
SsmlPhrase text(String text); | |
SsmlPhrase text(String format, Object... params); | |
SsmlPhrase sentence(String text); | |
SsmlPhrase sentence(Consumer<SsmlPhrase> consumer); | |
SsmlPhrase sentence(String format, Object... params); | |
SsmlPhrase paragraph(String text); | |
SsmlPhrase paragraph(Consumer<SsmlPhrase> consumer); | |
SsmlPhrase paragraph(String format, Object... params); | |
SsmlPhrase comma(); | |
SsmlPhrase strongBreak(); | |
SsmlPhrase newParagraph(); | |
/** | |
* Requests that the number be spoken like a quantity, e.g.: | |
* <ul> | |
* <li>1 is spoken as "one"</li> | |
* <li>100 is spoken as "one-hundred"</li> | |
* </ul> | |
* | |
* @param number to be spoken | |
* @return the builder object for further method chaining | |
*/ | |
SsmlPhrase cardinalNumber(long number); | |
/** | |
* @see #cardinalNumber(long) | |
*/ | |
SsmlPhrase cardinalNumber(String number); | |
/** | |
* Requests that the number be spoken like position in a series, e.g.: | |
* <ul> | |
* <li>1 is spoken as "first"</li> | |
* <li>100 is spoken as "one-hundredth"</li> | |
* </ul> | |
* | |
* @param number to be spoken | |
* @return the builder object for further method chaining | |
*/ | |
SsmlPhrase ordinalNumber(long number); | |
/** | |
* @see #ordinalNumber(long) | |
*/ | |
SsmlPhrase ordinalNumber(String number); | |
SsmlPhrase spellOut(String text); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment