Skip to content

Commit b8ea33a

Browse files
committed
Fix voice prefixes
1 parent 93b716c commit b8ea33a

File tree

4 files changed

+56
-14
lines changed

4 files changed

+56
-14
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import simplexity.config.ConfigHandler;
1515
import simplexity.config.LocaleHandler;
1616
import simplexity.config.SpeechEffectRule;
17+
import simplexity.config.TextReplaceRule;
18+
import simplexity.config.VoicePrefixRule;
1719
import simplexity.util.Logging;
1820

1921
import java.io.InputStream;
@@ -57,14 +59,17 @@ public void processSpeech(String text) {
5759
*/
5860

5961
public String replaceText(String text) {
62+
for (VoicePrefixRule prefixRule : ConfigHandler.getInstance().getVoicePrefixRules()) {
63+
if (!prefixRule.matches(text)) continue;
64+
text = prefixRule.applyRule(text);
65+
voiceId = prefixRule.getVoiceId();
66+
break;
67+
}
6068
for (SpeechEffectRule effectRule : ConfigHandler.getInstance().getEffectRules()) {
6169
text = effectRule.applyRule(text);
6270
}
63-
for (String key : ConfigHandler.getInstance().getVoicePrefixes().keySet()) {
64-
if (text.startsWith(key)) {
65-
text = text.replace(key, "");
66-
voiceId = ConfigHandler.getInstance().getVoicePrefixes().get(key);
67-
}
71+
for (TextReplaceRule replaceRule : ConfigHandler.getInstance().getTextReplaceRules()) {
72+
text = replaceRule.applyRule(text);
6873
}
6974
return text;
7075
}

src/main/java/simplexity/commands/ReloadCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ public void execute() {
3636
LocalServer.stop();
3737
Logging.log(logger, "Starting local server", Level.INFO);
3838
LocalServer.run();
39-
Logging.logAndPrint(logger, LocaleHandler.getInstance().getFeedbackReload(), Level.ERROR);
39+
Logging.logAndPrint(logger, LocaleHandler.getInstance().getFeedbackReload(), Level.INFO);
4040
}
4141
}

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@
99
import simplexity.Main;
1010
import simplexity.util.Logging;
1111

12-
import java.util.HashMap;
1312
import java.util.HashSet;
1413
import java.util.Map;
1514

1615
public class ConfigHandler {
1716

1817
private static final Logger logger = LoggerFactory.getLogger(ConfigHandler.class);
19-
private final HashMap<String, VoiceId> voicePrefixes = new HashMap<>();
18+
private final HashSet<VoicePrefixRule> voicePrefixRules = new HashSet<>();
2019
private final HashSet<SpeechEffectRule> effectRules = new HashSet<>();
2120
private final HashSet<TextReplaceRule> textReplaceRules = new HashSet<>();
2221

@@ -52,11 +51,17 @@ public void reloadAwsValues(YmlConfig config) {
5251
}
5352

5453
public void reloadVoicePrefixes(YmlConfig config) {
55-
voicePrefixes.clear();
54+
voicePrefixRules.clear();
5655
for (String key : config.getKeys("aws-api.voice-prefixes")) {
57-
VoiceId voice = config.getOption("aws-api.voice-prefixes." + key, VoiceId.class);
58-
if (voice == null) continue;
59-
voicePrefixes.put(key, voice);
56+
String voiceName = config.getOption("aws-api.voice-prefixes." + key, String.class);
57+
if (voiceName == null) continue;
58+
try {
59+
VoiceId voiceId = VoiceId.valueOf(voiceName);
60+
VoicePrefixRule prefixRule = new VoicePrefixRule(key, voiceId);
61+
voicePrefixRules.add(prefixRule);
62+
} catch (IllegalArgumentException e) {
63+
Logging.logAndPrint(logger, "issue", Level.WARN); //todo actual warning
64+
}
6065
}
6166
}
6267

@@ -102,8 +107,8 @@ public String getAwsSecretKey() {
102107
return awsSecretKey;
103108
}
104109

105-
public HashMap<String, VoiceId> getVoicePrefixes() {
106-
return voicePrefixes;
110+
public HashSet<VoicePrefixRule> getVoicePrefixRules() {
111+
return voicePrefixRules;
107112
}
108113

109114
public Region getAwsRegion() {
@@ -118,6 +123,10 @@ public HashSet<SpeechEffectRule> getEffectRules() {
118123
return effectRules;
119124
}
120125

126+
public HashSet<TextReplaceRule> getTextReplaceRules() {
127+
return textReplaceRules;
128+
}
129+
121130
public Integer getServerPort() {
122131
return serverPort;
123132
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package simplexity.config;
2+
3+
import com.amazonaws.services.polly.model.VoiceId;
4+
5+
import java.util.regex.Pattern;
6+
7+
public class VoicePrefixRule {
8+
private VoiceId voiceId;
9+
private Pattern prefixPattern;
10+
11+
public VoicePrefixRule(String prefix, VoiceId voiceId) {
12+
this.voiceId = voiceId;
13+
this.prefixPattern = Pattern.compile("(?i)^" + Pattern.quote(prefix) + "[:\\s-]?");
14+
}
15+
16+
public VoiceId getVoiceId() {
17+
return voiceId;
18+
}
19+
20+
public boolean matches(String input) {
21+
return prefixPattern.matcher(input).find();
22+
}
23+
24+
public String applyRule(String input) {
25+
return prefixPattern.matcher(input).replaceAll("");
26+
}
27+
28+
}

0 commit comments

Comments
 (0)