Search in sources :

Example 6 with DefineFunction

use of org.dmg.pmml.DefineFunction in project drools by kiegroup.

the class KiePMMLTransformationDictionaryFactoryTest method getDefineFunction.

private DefineFunction getDefineFunction(int counter) {
    ParameterField parameterField1 = new ParameterField(FieldName.create(PARAM_1 + counter));
    parameterField1.setDataType(DataType.DOUBLE);
    parameterField1.setOpType(OpType.CONTINUOUS);
    parameterField1.setDisplayName("displayName1" + counter);
    ParameterField parameterField2 = new ParameterField(FieldName.create(PARAM_2 + counter));
    parameterField2.setDataType(DataType.DOUBLE);
    parameterField2.setOpType(OpType.CONTINUOUS);
    parameterField2.setDisplayName("displayName2" + counter);
    Constant constant = new Constant();
    constant.setValue(value1);
    FieldRef fieldRef = new FieldRef();
    fieldRef.setField(FieldName.create("FIELD_REF" + counter));
    Apply apply = new Apply();
    apply.setFunction("/");
    apply.addExpressions(constant, fieldRef);
    DefineFunction toReturn = new DefineFunction();
    toReturn.setName(CUSTOM_FUNCTION + counter);
    toReturn.addParameterFields(parameterField1, parameterField2);
    toReturn.setDataType(DataType.DOUBLE);
    toReturn.setOpType(OpType.CONTINUOUS);
    toReturn.setExpression(apply);
    return toReturn;
}
Also used : KiePMMLFieldRef(org.kie.pmml.commons.model.expressions.KiePMMLFieldRef) FieldRef(org.dmg.pmml.FieldRef) KiePMMLConstant(org.kie.pmml.commons.model.expressions.KiePMMLConstant) Constant(org.dmg.pmml.Constant) KiePMMLApply(org.kie.pmml.commons.model.expressions.KiePMMLApply) Apply(org.dmg.pmml.Apply) DefineFunction(org.dmg.pmml.DefineFunction) KiePMMLDefineFunction(org.kie.pmml.commons.transformations.KiePMMLDefineFunction) KiePMMLParameterField(org.kie.pmml.commons.transformations.KiePMMLParameterField) ParameterField(org.dmg.pmml.ParameterField)

Example 7 with DefineFunction

use of org.dmg.pmml.DefineFunction in project drools by kiegroup.

the class KiePMMLDefineFunctionInstanceFactoryTest method getKiePMMLDefineFunction.

@Test
public void getKiePMMLDefineFunction() {
    final String functionName = "functionName";
    final DefineFunction toConvert = getDefineFunction(functionName);
    KiePMMLDefineFunction retrieved = KiePMMLDefineFunctionInstanceFactory.getKiePMMLDefineFunction(toConvert);
    commonVerifyKiePMMLDefineFunction(retrieved, toConvert);
}
Also used : KiePMMLDefineFunction(org.kie.pmml.commons.transformations.KiePMMLDefineFunction) InstanceFactoriesTestCommon.commonVerifyKiePMMLDefineFunction(org.kie.pmml.compiler.commons.factories.InstanceFactoriesTestCommon.commonVerifyKiePMMLDefineFunction) KiePMMLDefineFunction(org.kie.pmml.commons.transformations.KiePMMLDefineFunction) DefineFunction(org.dmg.pmml.DefineFunction) PMMLModelTestUtils.getDefineFunction(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getDefineFunction) InstanceFactoriesTestCommon.commonVerifyKiePMMLDefineFunction(org.kie.pmml.compiler.commons.factories.InstanceFactoriesTestCommon.commonVerifyKiePMMLDefineFunction) Test(org.junit.Test)

Example 8 with DefineFunction

use of org.dmg.pmml.DefineFunction in project drools by kiegroup.

the class KiePMMLTransformationDictionaryInstanceFactoryTest method getKiePMMLTransformationDictionary.

@Test
public void getKiePMMLTransformationDictionary() {
    final TransformationDictionary toConvert = getRandomTransformationDictionary();
    KiePMMLTransformationDictionary retrieved = KiePMMLTransformationDictionaryInstanceFactory.getKiePMMLTransformationDictionary(toConvert, Collections.emptyList());
    assertNotNull(retrieved);
    List<DerivedField> derivedFields = toConvert.getDerivedFields();
    List<KiePMMLDerivedField> derivedFieldsToVerify = retrieved.getDerivedFields();
    assertEquals(derivedFields.size(), derivedFieldsToVerify.size());
    derivedFields.forEach(derivedFieldSource -> {
        Optional<KiePMMLDerivedField> derivedFieldToVerify = derivedFieldsToVerify.stream().filter(param -> param.getName().equals(derivedFieldSource.getName().getValue())).findFirst();
        assertTrue(derivedFieldToVerify.isPresent());
        commonVerifyKiePMMLDerivedField(derivedFieldToVerify.get(), derivedFieldSource);
    });
    List<DefineFunction> defineFunctions = toConvert.getDefineFunctions();
    List<KiePMMLDefineFunction> defineFunctionsToVerify = retrieved.getDefineFunctions();
    assertEquals(defineFunctions.size(), defineFunctionsToVerify.size());
    defineFunctions.forEach(defineFunctionSource -> {
        Optional<KiePMMLDefineFunction> defineFunctionToVerify = defineFunctionsToVerify.stream().filter(param -> param.getName().equals(defineFunctionSource.getName())).findFirst();
        assertTrue(defineFunctionToVerify.isPresent());
        commonVerifyKiePMMLDefineFunction(defineFunctionToVerify.get(), defineFunctionSource);
    });
}
Also used : InstanceFactoriesTestCommon.commonVerifyKiePMMLDerivedField(org.kie.pmml.compiler.commons.factories.InstanceFactoriesTestCommon.commonVerifyKiePMMLDerivedField) KiePMMLDefineFunction(org.kie.pmml.commons.transformations.KiePMMLDefineFunction) DefineFunction(org.dmg.pmml.DefineFunction) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) List(java.util.List) TransformationDictionary(org.dmg.pmml.TransformationDictionary) DerivedField(org.dmg.pmml.DerivedField) KiePMMLTransformationDictionary(org.kie.pmml.commons.transformations.KiePMMLTransformationDictionary) PMMLModelTestUtils.getRandomTransformationDictionary(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomTransformationDictionary) Optional(java.util.Optional) Collections(java.util.Collections) InstanceFactoriesTestCommon.commonVerifyKiePMMLDefineFunction(org.kie.pmml.compiler.commons.factories.InstanceFactoriesTestCommon.commonVerifyKiePMMLDefineFunction) Assert.assertEquals(org.junit.Assert.assertEquals) KiePMMLDerivedField(org.kie.pmml.commons.transformations.KiePMMLDerivedField) TransformationDictionary(org.dmg.pmml.TransformationDictionary) KiePMMLTransformationDictionary(org.kie.pmml.commons.transformations.KiePMMLTransformationDictionary) PMMLModelTestUtils.getRandomTransformationDictionary(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomTransformationDictionary) KiePMMLTransformationDictionary(org.kie.pmml.commons.transformations.KiePMMLTransformationDictionary) InstanceFactoriesTestCommon.commonVerifyKiePMMLDerivedField(org.kie.pmml.compiler.commons.factories.InstanceFactoriesTestCommon.commonVerifyKiePMMLDerivedField) KiePMMLDerivedField(org.kie.pmml.commons.transformations.KiePMMLDerivedField) KiePMMLDefineFunction(org.kie.pmml.commons.transformations.KiePMMLDefineFunction) InstanceFactoriesTestCommon.commonVerifyKiePMMLDefineFunction(org.kie.pmml.compiler.commons.factories.InstanceFactoriesTestCommon.commonVerifyKiePMMLDefineFunction) KiePMMLDefineFunction(org.kie.pmml.commons.transformations.KiePMMLDefineFunction) DefineFunction(org.dmg.pmml.DefineFunction) InstanceFactoriesTestCommon.commonVerifyKiePMMLDefineFunction(org.kie.pmml.compiler.commons.factories.InstanceFactoriesTestCommon.commonVerifyKiePMMLDefineFunction) InstanceFactoriesTestCommon.commonVerifyKiePMMLDerivedField(org.kie.pmml.compiler.commons.factories.InstanceFactoriesTestCommon.commonVerifyKiePMMLDerivedField) DerivedField(org.dmg.pmml.DerivedField) KiePMMLDerivedField(org.kie.pmml.commons.transformations.KiePMMLDerivedField) Test(org.junit.Test)

Example 9 with DefineFunction

use of org.dmg.pmml.DefineFunction in project jpmml-sparkml by jpmml.

the class CountVectorizerModelConverter method encodeFeatures.

@Override
public List<Feature> encodeFeatures(SparkMLEncoder encoder) {
    CountVectorizerModel transformer = getTransformer();
    DocumentFeature documentFeature = (DocumentFeature) encoder.getOnlyFeature(transformer.getInputCol());
    ParameterField documentField = new ParameterField(FieldName.create("document"));
    ParameterField termField = new ParameterField(FieldName.create("term"));
    TextIndex textIndex = new TextIndex(documentField.getName()).setTokenize(Boolean.TRUE).setWordSeparatorCharacterRE(documentFeature.getWordSeparatorRE()).setLocalTermWeights(transformer.getBinary() ? TextIndex.LocalTermWeights.BINARY : null).setExpression(new FieldRef(termField.getName()));
    Set<DocumentFeature.StopWordSet> stopWordSets = documentFeature.getStopWordSets();
    for (DocumentFeature.StopWordSet stopWordSet : stopWordSets) {
        if (stopWordSet.isEmpty()) {
            continue;
        }
        DocumentBuilder documentBuilder = DOMUtil.createDocumentBuilder();
        String tokenRE;
        String wordSeparatorRE = documentFeature.getWordSeparatorRE();
        switch(wordSeparatorRE) {
            case "\\s+":
                tokenRE = "(^|\\s+)\\p{Punct}*(" + JOINER.join(stopWordSet) + ")\\p{Punct}*(\\s+|$)";
                break;
            case "\\W+":
                tokenRE = "(\\W+)(" + JOINER.join(stopWordSet) + ")(\\W+)";
                break;
            default:
                throw new IllegalArgumentException("Expected \"\\s+\" or \"\\W+\" as splitter regex pattern, got \"" + wordSeparatorRE + "\"");
        }
        InlineTable inlineTable = new InlineTable().addRows(DOMUtil.createRow(documentBuilder, Arrays.asList("string", "stem", "regex"), Arrays.asList(tokenRE, " ", "true")));
        TextIndexNormalization textIndexNormalization = new TextIndexNormalization().setCaseSensitive(stopWordSet.isCaseSensitive()).setRecursive(// Handles consecutive matches. See http://stackoverflow.com/a/25085385
        Boolean.TRUE).setInlineTable(inlineTable);
        textIndex.addTextIndexNormalizations(textIndexNormalization);
    }
    DefineFunction defineFunction = new DefineFunction("tf" + "@" + String.valueOf(CountVectorizerModelConverter.SEQUENCE.getAndIncrement()), OpType.CONTINUOUS, null).setDataType(DataType.INTEGER).addParameterFields(documentField, termField).setExpression(textIndex);
    encoder.addDefineFunction(defineFunction);
    List<Feature> result = new ArrayList<>();
    String[] vocabulary = transformer.vocabulary();
    for (int i = 0; i < vocabulary.length; i++) {
        String term = vocabulary[i];
        if (TermUtil.hasPunctuation(term)) {
            throw new IllegalArgumentException(term);
        }
        result.add(new TermFeature(encoder, defineFunction, documentFeature, term));
    }
    return result;
}
Also used : InlineTable(org.dmg.pmml.InlineTable) FieldRef(org.dmg.pmml.FieldRef) TextIndex(org.dmg.pmml.TextIndex) DocumentFeature(org.jpmml.sparkml.DocumentFeature) ArrayList(java.util.ArrayList) Feature(org.jpmml.converter.Feature) DocumentFeature(org.jpmml.sparkml.DocumentFeature) TermFeature(org.jpmml.sparkml.TermFeature) TermFeature(org.jpmml.sparkml.TermFeature) TextIndexNormalization(org.dmg.pmml.TextIndexNormalization) CountVectorizerModel(org.apache.spark.ml.feature.CountVectorizerModel) DocumentBuilder(javax.xml.parsers.DocumentBuilder) DefineFunction(org.dmg.pmml.DefineFunction) ParameterField(org.dmg.pmml.ParameterField)

Example 10 with DefineFunction

use of org.dmg.pmml.DefineFunction in project drools by kiegroup.

the class KiePMMLTextIndexFactoryTest method setup.

@BeforeClass
public static void setup() throws Exception {
    PMML pmmlModel = KiePMMLUtil.load(getFileInputStream(TRANSFORMATIONS_SAMPLE), TRANSFORMATIONS_SAMPLE);
    DefineFunction definedFunction = pmmlModel.getTransformationDictionary().getDefineFunctions().stream().filter(defineFunction -> TEXT_INDEX_NORMALIZATION_FUNCTION.equals(defineFunction.getName())).findFirst().orElseThrow(() -> new RuntimeException("Missing derived field " + TEXT_INDEX_NORMALIZATION_FUNCTION));
    TEXTINDEX = ((TextIndex) definedFunction.getExpression());
}
Also used : TextIndex(org.dmg.pmml.TextIndex) KiePMMLTextIndex(org.kie.pmml.commons.model.expressions.KiePMMLTextIndex) PMML(org.dmg.pmml.PMML) DefineFunction(org.dmg.pmml.DefineFunction) BeforeClass(org.junit.BeforeClass)

Aggregations

DefineFunction (org.dmg.pmml.DefineFunction)12 Constant (org.dmg.pmml.Constant)5 FieldRef (org.dmg.pmml.FieldRef)5 ParameterField (org.dmg.pmml.ParameterField)5 Apply (org.dmg.pmml.Apply)4 TextIndex (org.dmg.pmml.TextIndex)4 KiePMMLDefineFunction (org.kie.pmml.commons.transformations.KiePMMLDefineFunction)4 Test (org.junit.Test)3 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Optional (java.util.Optional)2 DerivedField (org.dmg.pmml.DerivedField)2 InlineTable (org.dmg.pmml.InlineTable)2 PMML (org.dmg.pmml.PMML)2 TextIndexNormalization (org.dmg.pmml.TextIndexNormalization)2 KiePMMLApply (org.kie.pmml.commons.model.expressions.KiePMMLApply)2 KiePMMLConstant (org.kie.pmml.commons.model.expressions.KiePMMLConstant)2 KiePMMLFieldRef (org.kie.pmml.commons.model.expressions.KiePMMLFieldRef)2 KiePMMLTextIndex (org.kie.pmml.commons.model.expressions.KiePMMLTextIndex)2