use of org.kie.pmml.api.enums.COUNT_HITS in project drools by kiegroup.
the class KiePMMLTextIndexFactory method getTextIndexVariableDeclaration.
static BlockStmt getTextIndexVariableDeclaration(final String variableName, final TextIndex textIndex) {
final MethodDeclaration methodDeclaration = TEXTINDEX_TEMPLATE.getMethodsByName(GETKIEPMMLTEXTINDEX).get(0).clone();
final BlockStmt textIndexBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(textIndexBody, TEXTINDEX).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, TEXTINDEX, textIndexBody)));
variableDeclarator.setName(variableName);
final BlockStmt toReturn = new BlockStmt();
String expressionVariableName = String.format("%s_Expression", variableName);
final BlockStmt expressionBlockStatement = getKiePMMLExpressionBlockStmt(expressionVariableName, textIndex.getExpression());
expressionBlockStatement.getStatements().forEach(toReturn::addStatement);
int counter = 0;
final NodeList<Expression> arguments = new NodeList<>();
if (textIndex.hasTextIndexNormalizations()) {
for (TextIndexNormalization textIndexNormalization : textIndex.getTextIndexNormalizations()) {
String nestedVariableName = String.format(VARIABLE_NAME_TEMPLATE, variableName, counter);
arguments.add(new NameExpr(nestedVariableName));
BlockStmt toAdd = getTextIndexNormalizationVariableDeclaration(nestedVariableName, textIndexNormalization);
toAdd.getStatements().forEach(toReturn::addStatement);
counter++;
}
}
final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, TEXTINDEX, toReturn))).asMethodCallExpr();
final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
final StringLiteralExpr nameExpr = new StringLiteralExpr(textIndex.getTextField().getValue());
final NameExpr expressionExpr = new NameExpr(expressionVariableName);
builder.setArgument(0, nameExpr);
builder.setArgument(2, expressionExpr);
Expression localTermWeightsExpression;
if (textIndex.getLocalTermWeights() != null) {
final LOCAL_TERM_WEIGHTS localTermWeights = LOCAL_TERM_WEIGHTS.byName(textIndex.getLocalTermWeights().value());
localTermWeightsExpression = new NameExpr(LOCAL_TERM_WEIGHTS.class.getName() + "." + localTermWeights.name());
} else {
localTermWeightsExpression = new NullLiteralExpr();
}
getChainedMethodCallExprFrom("withLocalTermWeights", initializer).setArgument(0, localTermWeightsExpression);
getChainedMethodCallExprFrom("withIsCaseSensitive", initializer).setArgument(0, getExpressionForObject(textIndex.isCaseSensitive()));
getChainedMethodCallExprFrom("withMaxLevenshteinDistance", initializer).setArgument(0, getExpressionForObject(textIndex.getMaxLevenshteinDistance()));
Expression countHitsExpression;
if (textIndex.getCountHits() != null) {
final COUNT_HITS countHits = COUNT_HITS.byName(textIndex.getCountHits().value());
countHitsExpression = new NameExpr(COUNT_HITS.class.getName() + "." + countHits.name());
} else {
countHitsExpression = new NullLiteralExpr();
}
getChainedMethodCallExprFrom("withCountHits", initializer).setArgument(0, countHitsExpression);
Expression wordSeparatorCharacterREExpression;
if (textIndex.getWordSeparatorCharacterRE() != null) {
String wordSeparatorCharacterRE = StringEscapeUtils.escapeJava(textIndex.getWordSeparatorCharacterRE());
wordSeparatorCharacterREExpression = new StringLiteralExpr(wordSeparatorCharacterRE);
} else {
wordSeparatorCharacterREExpression = new NullLiteralExpr();
}
getChainedMethodCallExprFrom("withWordSeparatorCharacterRE", initializer).setArgument(0, wordSeparatorCharacterREExpression);
getChainedMethodCallExprFrom("withTokenize", initializer).setArgument(0, getExpressionForObject(textIndex.isTokenize()));
getChainedMethodCallExprFrom("asList", initializer).setArguments(arguments);
textIndexBody.getStatements().forEach(toReturn::addStatement);
return toReturn;
}
use of org.kie.pmml.api.enums.COUNT_HITS in project drools by kiegroup.
the class KiePMMLTextIndexInstanceFactory method getKiePMMLTextIndex.
static KiePMMLTextIndex getKiePMMLTextIndex(final TextIndex textIndex) {
final LOCAL_TERM_WEIGHTS localTermWeights = textIndex.getLocalTermWeights() != null ? LOCAL_TERM_WEIGHTS.byName(textIndex.getLocalTermWeights().value()) : null;
final COUNT_HITS countHits = textIndex.getCountHits() != null ? COUNT_HITS.byName(textIndex.getCountHits().value()) : null;
final String wordSeparatorCharacterRE = textIndex.getWordSeparatorCharacterRE() != null ? StringEscapeUtils.escapeJava(textIndex.getWordSeparatorCharacterRE()) : null;
return KiePMMLTextIndex.builder(textIndex.getTextField().getValue(), getKiePMMLExtensions(textIndex.getExtensions()), getKiePMMLExpression(textIndex.getExpression())).withTextIndexNormalizations(getKiePMMLTextIndexNormalizations(textIndex.getTextIndexNormalizations())).withLocalTermWeights(localTermWeights).withIsCaseSensitive(textIndex.isCaseSensitive()).withMaxLevenshteinDistance(textIndex.getMaxLevenshteinDistance()).withCountHits(countHits).withWordSeparatorCharacterRE(wordSeparatorCharacterRE).withTokenize(textIndex.isTokenize()).build();
}
Aggregations