Skip to content

Commit 5bfb1a7

Browse files
committed
Fix TTS reading invisible characters
1 parent b8ea33a commit 5bfb1a7

File tree

9 files changed

+30
-32
lines changed

9 files changed

+30
-32
lines changed

src/main/java/simplexity/Main.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class Main {
3030
public static Scanner scanner;
3131
public static boolean runApp = true;
3232

33+
@SuppressWarnings("CallToPrintStackTrace")
3334
public static void main(String[] args) {
3435
Logging.log(logger, "Starting application", Level.INFO);
3536
scanner = new Scanner(System.in);
@@ -52,6 +53,7 @@ public static void main(String[] args) {
5253
while (runApp) {
5354
String input = scanner.nextLine();
5455
if (!commandManager.runCommand(input)) {
56+
input = stripInvalidCharacters(input);
5557
speechHandler.processSpeech(input);
5658
}
5759
}
@@ -63,6 +65,12 @@ private static void registerCommands(CommandManager commandManager) {
6365
commandManager.registerCommand(new ReloadCommand("--reload", "Reloads the configuration"));
6466
}
6567

68+
private static String stripInvalidCharacters(String input) {
69+
if (input == null) return null;
70+
return input.replaceAll("\u001B\\[[;\\d]*[ -/]*[@-~]", "")
71+
.replaceAll("\\p{C}", "");
72+
}
73+
6674
public static CommandManager getCommandManager() {
6775
return commandManager;
6876
}

src/main/java/simplexity/amazon/SpeechHandler.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
import simplexity.Main;
1414
import simplexity.config.ConfigHandler;
1515
import simplexity.config.LocaleHandler;
16-
import simplexity.config.SpeechEffectRule;
17-
import simplexity.config.TextReplaceRule;
18-
import simplexity.config.VoicePrefixRule;
16+
import simplexity.config.rules.SpeechEffectRule;
17+
import simplexity.config.rules.TextReplaceRule;
18+
import simplexity.config.rules.VoicePrefixRule;
1919
import simplexity.util.Logging;
2020

2121
import java.io.InputStream;
@@ -71,6 +71,7 @@ public String replaceText(String text) {
7171
for (TextReplaceRule replaceRule : ConfigHandler.getInstance().getTextReplaceRules()) {
7272
text = replaceRule.applyRule(text);
7373
}
74+
text = Logging.stripAnsiCodes(text);
7475
return text;
7576
}
7677

src/main/java/simplexity/config/ConfigHandler.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
import org.slf4j.LoggerFactory;
88
import org.slf4j.event.Level;
99
import simplexity.Main;
10+
import simplexity.config.rules.SpeechEffectRule;
11+
import simplexity.config.rules.TextReplaceRule;
12+
import simplexity.config.rules.VoicePrefixRule;
1013
import simplexity.util.Logging;
1114

1215
import java.util.HashSet;
@@ -28,7 +31,6 @@ public class ConfigHandler {
2831
public static ConfigHandler getInstance() {
2932
if (instance == null) {
3033
instance = new ConfigHandler();
31-
Logging.log(logger, "Generating new instance of AwsConfig", Level.INFO);
3234
}
3335
return instance;
3436
}
@@ -42,6 +44,7 @@ public void reloadValues() {
4244
}
4345

4446
public void reloadAwsValues(YmlConfig config) {
47+
Logging.log(logger, "Reloading AWS Values", Level.INFO);
4548
awsAccessID = config.getOption("aws-api.access-key", String.class, "");
4649
awsSecretKey = config.getOption("aws-api.secret-key", String.class, "");
4750
Regions region = config.getOption("aws-api.region", Regions.class, Regions.US_EAST_1);
@@ -51,6 +54,7 @@ public void reloadAwsValues(YmlConfig config) {
5154
}
5255

5356
public void reloadVoicePrefixes(YmlConfig config) {
57+
Logging.log(logger, "Reloading Voice Prefixes", Level.INFO);
5458
voicePrefixRules.clear();
5559
for (String key : config.getKeys("aws-api.voice-prefixes")) {
5660
String voiceName = config.getOption("aws-api.voice-prefixes." + key, String.class);
@@ -60,12 +64,14 @@ public void reloadVoicePrefixes(YmlConfig config) {
6064
VoicePrefixRule prefixRule = new VoicePrefixRule(key, voiceId);
6165
voicePrefixRules.add(prefixRule);
6266
} catch (IllegalArgumentException e) {
63-
Logging.logAndPrint(logger, "issue", Level.WARN); //todo actual warning
67+
String message = "[Config] Config value at 'aws-api.voice-prefixes." + key + "' is invalid";
68+
Logging.log(logger, message, Level.WARN);
6469
}
6570
}
6671
}
6772

6873
public void reloadSpeechEffects(YmlConfig config) {
74+
Logging.log(logger, "Reloading Speech Effects", Level.INFO);
6975
effectRules.clear();
7076
for (String key : config.getKeys("speech-effect-markdown")) {
7177
StringBuilder openingBuilder = new StringBuilder();
@@ -89,6 +95,7 @@ public void reloadSpeechEffects(YmlConfig config) {
8995
}
9096

9197
public void reloadTextReplace(YmlConfig config) {
98+
Logging.log(logger, "Reloading Text Replacements", Level.INFO);
9299
textReplaceRules.clear();
93100
for (String key : config.getKeys("text-replacements")) {
94101
String replacementText = config.getOption("text-replacements." + key, String.class);

src/main/java/simplexity/config/LocaleHandler.java

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,12 @@ public static LocaleHandler getInstance() {
1212
return instance;
1313
}
1414

15-
private String errorGeneral, invalidInput, invalidVoice, invalidRegion, messageNotParsable, unknownCommand,
15+
private String errorGeneral, messageNotParsable, unknownCommand,
1616
nullAwsCreds, saveAwsInfo, helpHeader, helpCommands, feedbackReload, feedbackShuttingDown;
1717

1818
public void reloadMessages() {
1919
YmlConfig config = Main.getLocaleConfig();
2020
errorGeneral = config.getOption("error.general", String.class, "<br-red>ERROR: %error%</color>");
21-
invalidInput = config.getOption("error.invalid-input", String.class, "<br-red>ERROR: Invalid input</color>");
22-
invalidVoice = config.getOption("error.invalid-voice", String.class, "<br-red>Error: '%voice%' is not a valid voice. \\nPlease make sure you are only choosing from standard voices.</color> \\nStandard voices can be found here: \\n<yellow>https://docs.aws.amazon.com/polly/latest/dg/voicelist.html</color>");
23-
invalidRegion = config.getOption("error.invalid-region", String.class, "<br-red>Error: '%region%' is not a valid region.</color> \\n<yellow>Regions can be found here: \\nhttps://aws.amazon.com/about-aws/global-infrastructure/regions_az/ \\nUsing default region of 'US_EAST_1'");
2421
messageNotParsable = config.getOption("error.message-not-parsable", String.class, "<br-red>ERROR: Message was not parsable, attempted to send: %message%</color>");
2522
unknownCommand = config.getOption("error.unknown-command", String.class, "<br-red>ERROR: '%command%' is not a recognized command</color>");
2623
nullAwsCreds = config.getOption("error.null-aws-credentials", String.class, "<br-red>ERROR: AWS credentials are null, please fill them out</color>");
@@ -36,18 +33,6 @@ public String getErrorGeneral() {
3633
return errorGeneral;
3734
}
3835

39-
public String getInvalidInput() {
40-
return invalidInput;
41-
}
42-
43-
public String getInvalidVoice() {
44-
return invalidVoice;
45-
}
46-
47-
public String getInvalidRegion() {
48-
return invalidRegion;
49-
}
50-
5136
public String getMessageNotParsable() {
5237
return messageNotParsable;
5338
}

src/main/java/simplexity/config/SpeechEffectRule.java renamed to src/main/java/simplexity/config/rules/SpeechEffectRule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package simplexity.config;
1+
package simplexity.config.rules;
22

33
import java.util.regex.Pattern;
44

src/main/java/simplexity/config/TextReplaceRule.java renamed to src/main/java/simplexity/config/rules/TextReplaceRule.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
package simplexity.config;
1+
package simplexity.config.rules;
22

33
import java.util.regex.Pattern;
44

55
public class TextReplaceRule {
66

7-
private String replacementText;
8-
private Pattern textPattern;
7+
private final String replacementText;
8+
private final Pattern textPattern;
99

1010
public TextReplaceRule(String textToFind, String replacementText) {
1111
this.replacementText = replacementText;

src/main/java/simplexity/config/VoicePrefixRule.java renamed to src/main/java/simplexity/config/rules/VoicePrefixRule.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
package simplexity.config;
1+
package simplexity.config.rules;
22

33
import com.amazonaws.services.polly.model.VoiceId;
44

55
import java.util.regex.Pattern;
66

77
public class VoicePrefixRule {
8-
private VoiceId voiceId;
9-
private Pattern prefixPattern;
8+
private final VoiceId voiceId;
9+
private final Pattern prefixPattern;
1010

1111
public VoicePrefixRule(String prefix, VoiceId voiceId) {
1212
this.voiceId = voiceId;

src/main/java/simplexity/util/Logging.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
public class Logging {
99

10-
private static final Pattern ANSI_PATTERN = Pattern.compile("\u001B\\[[;\\d]*m");
10+
public static final Pattern ANSI_PATTERN = Pattern.compile("\u001B\\[[;\\d]*m");
1111

1212
public static void logAndPrint(Logger logger, String message, Level level) {
1313
String logMessage = stripAnsiCodes(message);

src/main/resources/locale.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
error:
22
general: "<br-red>ERROR: %error%</color>"
3-
invalid-input: "<br-red>ERROR: Invalid input</color>"
4-
invalid-voice: "<br-red>Error: '%voice%' is not a valid voice. \\nPlease make sure you are only choosing from standard voices.</color> \\nStandard voices can be found here: \\n<yellow>https://docs.aws.amazon.com/polly/latest/dg/voicelist.html</color>"
5-
invalid-region: "<br-red>Error: '%region%' is not a valid region.</color> \\n<yellow>Regions can be found here: \\nhttps://aws.amazon.com/about-aws/global-infrastructure/regions_az/ \\nUsing default region of 'US_EAST_1'"
63
message-not-parsable: "<br-red>ERROR: Message was not parsable, attempted to send: %message%</color>"
74
unknown-command: "<br-red>ERROR: '%command%' is not a recognized command</color>"
85
null-aws-credentials: "<br-red>ERROR: AWS credentials are null, please fill them out</color>"

0 commit comments

Comments
 (0)