Skip to content

Commit

Permalink
Prepare for cmd-line interface
Browse files Browse the repository at this point in the history
  • Loading branch information
KayLerch committed Oct 26, 2017
1 parent ad4bce5 commit ac89c65
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 19 deletions.
28 changes: 23 additions & 5 deletions src/main/java/io/klerch/alexa/utterances/UtteranceGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.klerch.alexa.utterances.output.OutputWriter;
import io.klerch.alexa.utterances.util.Resolver;
import io.klerch.alexa.utterances.util.ResourceReader;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.apache.commons.lang3.tuple.ImmutablePair;
Expand All @@ -21,22 +22,22 @@

public class UtteranceGenerator {
// 1) Set the key of a file with utterances you created in the utterances-folder
private final static String utteranceFileKey = "harmony"; // e.g. "booking" for using "/resources/output/utterances/booking.grammar"
private final static String utteranceFileKey = "booking"; // e.g. "booking" for using "/resources/output/utterances/booking.grammar"

// 2) choose one of the output writers
private static final OutputWriter OUTPUT_WRITER = new FileOutputWriter(utteranceFileKey);
//private static final OutputWriter OUTPUT_WRITER = new ConsoleOutputWriter();

// 3) choose formatter
//private static final Formatter FORMATTER = new SMAPIFormatter("my invocation name");
//private static final Formatter FORMATTER = new SkillBuilderFormatter();
private static final Formatter FORMATTER = new UtteranceListFormatter();
private static final Formatter FORMATTER = new SkillBuilderFormatter();
//private static final Formatter FORMATTER = new UtteranceListFormatter();
//private static final Formatter FORMATTER = new WeightedSegmentsFormatter(1); // use booking2 as utteranceFileKey for an example

// 4) run and done
public static void main(final String [] args) {
generateUtterances(Arrays.stream(args).findFirst().orElse(utteranceFileKey));
OUTPUT_WRITER.beforeWrite(FORMATTER);
generateUtterances(getUtteranceFileKey(args).orElse(utteranceFileKey));
OUTPUT_WRITER.beforeWrite(getFormatter(args).orElse(FORMATTER));
try {
final List<String> utterances = new ArrayList<>();
intentsAndUtterances.forEach((intent, utterancesOfIntent) -> {
Expand All @@ -48,6 +49,23 @@ public static void main(final String [] args) {
}
}

private static Optional<Formatter> getFormatter(final String[] args) {
return ArrayUtils.contains(args, "-smapi") ? Optional.of(new SMAPIFormatter(args)) :
ArrayUtils.contains(args, "-sb") || ArrayUtils.contains(args, "-skillBuilder") ? Optional.of(new SkillBuilderFormatter()) :
ArrayUtils.contains(args, "-ul") || ArrayUtils.contains(args, "-utteranceList") ? Optional.of(new UtteranceListFormatter()) :
ArrayUtils.contains(args, "-ws") || ArrayUtils.contains(args, "-weightedSegments") ? Optional.of(new WeightedSegmentsFormatter(args)) : Optional.empty();
}

private static Optional<String> getUtteranceFileKey(final String[] args) {
if (args != null) {
int index = ArrayUtils.indexOf(args, "-f");
if (index < args.length - 1) {
return Optional.of(args[index + 1]);
}
}
return Optional.empty();
}

// stores the list of values contained in slots of utterances
private static final Map<String, List<String>> placeholderValues = new HashMap<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,28 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import io.klerch.alexa.utterances.model.LanguageModel;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.Validate;

import java.util.Arrays;

public class SMAPIFormatter implements Formatter {
private final LanguageModel model;
private LanguageModel model;

public SMAPIFormatter(final String[] args) {
if (args != null) {
int index = ArrayUtils.indexOf(args, "-in");
if (index < args.length - 1) {
final String invocationName = args[index + 1];
Validate.notBlank(invocationName, "Please provide an invocation-name.");
Validate.isTrue(!invocationName.startsWith("-"), "Please provide a valid invocation-name.");
this.model = new LanguageModel(invocationName);
}
}
}

public SMAPIFormatter(final String invocatioName) {
this.model = new LanguageModel(invocatioName);
this(new String[] { "-in", invocatioName});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
public class UtteranceListFormatter implements Formatter {
private StringBuilder sb;
private int numOfSamples = 0;
private final boolean firstWordIsIntentName;

public UtteranceListFormatter() {
this(true);
}

public UtteranceListFormatter(final boolean firstWordIsIntentName) {
this.firstWordIsIntentName = firstWordIsIntentName;
}

@Override
public void before() {
Expand All @@ -13,8 +22,8 @@ public void before() {

@Override
public boolean write(final String sample) {
if (!StringUtils.startsWithIgnoreCase(sample, "AMAZON.") || sample.split(" ").length > 1) {
sb.append(numOfSamples++ > 0 ? "\n" : "").append(sample);
if (!firstWordIsIntentName || sample.split(" ").length > 1) {
sb.append(numOfSamples++ > 0 ? "\n" : "").append(sample.trim());
return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,49 @@
package io.klerch.alexa.utterances.format;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.Validate;
import org.apache.commons.lang3.math.NumberUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WeightedSegmentsFormatter implements Formatter {
private final static String DEFAULT_SEPARATOR = ";";
private final int weight;
private final String valueSeparator;
private final boolean prependPriority;
private int weight;
private String valueSeparator = ";";
private boolean prependPriority = false;
private final List<String> samples = new ArrayList<>();

public WeightedSegmentsFormatter(final String[] args) {
if (args != null) {
prependPriority = ArrayUtils.contains(args, "-pp") || ArrayUtils.contains(args, "-prependPriority");

int index1 = ArrayUtils.indexOf(args, "-weight");
if (index1 < args.length - 1) {
final String weightString = args[index1 + 1];
Validate.isTrue(NumberUtils.isParsable(weightString), "Please provide a numeric value for weight.");
this.weight = Integer.parseInt(weightString);
}

int index2 = ArrayUtils.indexOf(args, "-separator");
if (index2 < args.length - 1) {
valueSeparator = args[index2 + 1];
}
}
}

public WeightedSegmentsFormatter(final int weight) {
this(weight, false);
this(new String[] { "weight", String.valueOf(weight)});
}

public WeightedSegmentsFormatter(final int weight, final boolean prependPriority) {
this(weight, prependPriority, DEFAULT_SEPARATOR);
this(new String[] { "weight", String.valueOf(weight), prependPriority ? "-pp" : ""});
}

public WeightedSegmentsFormatter(final int weight, final boolean prependPriority, final String valueSeparator) {
this.weight = weight;
this.valueSeparator = valueSeparator;
this.prependPriority = prependPriority;
this(new String[] { "weight", String.valueOf(weight), prependPriority ? "-pp" : "", "-separator", valueSeparator});
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/klerch/alexa/utterances/model/Intent.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

public class Intent {
@JsonIgnore
private static List<String> slotSuffix = Arrays.asList("A","B","C","D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y");
private static List<String> slotSuffix = Arrays.asList("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");

@JsonProperty
private final String name;
Expand Down

0 comments on commit ac89c65

Please sign in to comment.