in tools/template_based_query_generation/src/main/java/token/QueryRegex.java [20:109]
public QueryRegex(List<String> featureRegexList, int maxOptionalExpressions) throws IOException {
if (maxOptionalExpressions < 1) {
// throw error
}
this.maxOptionalExpressions = maxOptionalExpressions - 1;
for (String featureRegex : featureRegexList) {
String currentString = featureRegex;
StringBuilder sb = new StringBuilder();
while (currentString.contains("[") || currentString.contains("{") || currentString.contains("|")) {
int i = 0;
while (i < currentString.length()) {
if (currentString.charAt(i) == '[') {
int start = i;
int matchingBracketCount = 1;
// find the index of the matching closing bracket
while (matchingBracketCount > 0) {
i++;
if (currentString.charAt(i) == '[') {
matchingBracketCount++;
} else if (currentString.charAt(i) == ']') {
matchingBracketCount--;
}
}
String optionalExpression = currentString.substring(start + 1, i);
int randomExpressionCount = Utils.getRandomInteger(this.maxOptionalExpressions);
for (int j = 0; j < randomExpressionCount; j++) {
sb.append(optionalExpression);
}
} else if (currentString.charAt(i) == '{') {
int start = i;
int matchingBraceCount = 1;
// find the index of the matching closing brace
while (matchingBraceCount > 0) {
i++;
if (currentString.charAt(i) == '{') {
matchingBraceCount++;
} else if (currentString.charAt(i) == '}') {
matchingBraceCount--;
}
}
String choiceExpression = currentString.substring(start + 1, i);
List<String> choices;
if (choiceExpression.contains("|")) {
choices = new ArrayList<>(Arrays.asList(choiceExpression.split("\\|", -1)));
} else {
choices = new ArrayList<>();
choices.add(choiceExpression);
}
int choiceIndex = Utils.getRandomInteger(choices.size() - 1);
String choice = choices.get(choiceIndex);
sb.append(choice);
} else {
sb.append(currentString.charAt(i));
}
i++;
}
currentString = sb.toString();
sb = new StringBuilder();
}
List<String> rawQuery = new ArrayList<>(Arrays.asList(currentString.split(" ")));
List<SkeletonPiece> querySkeleton = new ArrayList<>();
for (String querySlice : rawQuery) {
if (querySlice.charAt(0) == '<') {
// slice is an expression
if (querySlice.contains(",")) {
List<String> optionals = new ArrayList<>(Arrays.asList(querySlice.split(",")));
for (String optional : optionals) {
String tokenExpression = optional.substring(1, optional.length() - 1);
querySkeleton.add(tokenProvider.tokenize(tokenExpression));
}
} else {
String tokenExpression = querySlice.substring(1, querySlice.length() - 1);
querySkeleton.add(tokenProvider.tokenize(tokenExpression));
}
} else {
// otherwise the slice is a feature
SkeletonPiece sp = new SkeletonPiece();
sp.setKeyword(querySlice);
querySkeleton.add(sp);
}
}
querySkeletons.add(querySkeleton);
}
}