org.dmg.pmml.ParameterField Java Examples

The following examples show how to use org.dmg.pmml.ParameterField. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TfidfVectorizer.java    From jpmml-sklearn with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public DefineFunction encodeDefineFunction(){
	TfidfTransformer transformer = getTransformer();

	DefineFunction defineFunction = super.encodeDefineFunction();

	Expression expression = defineFunction.getExpression();

	Boolean sublinearTf = transformer.getSublinearTf();
	if(sublinearTf){
		expression = PMMLUtil.createApply(PMMLFunctions.ADD, PMMLUtil.createApply(PMMLFunctions.LN, expression), PMMLUtil.createConstant(1d));
	} // End if

	Boolean useIdf = transformer.getUseIdf();
	if(useIdf){
		ParameterField weight = new ParameterField(FieldName.create("weight"));

		defineFunction.addParameterFields(weight);

		expression = PMMLUtil.createApply(PMMLFunctions.MULTIPLY, expression, new FieldRef(weight.getName()));
	}

	defineFunction.setExpression(expression);

	return defineFunction;
}
 
Example #2
Source File: ExpressionUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
static
public FieldValue evaluate(DefineFunction defineFunction, List<FieldValue> values, EvaluationContext context){
	List<ParameterField> parameterFields = defineFunction.getParameterFields();

	if(parameterFields.size() != values.size()){
		throw new EvaluationException("Function " + PMMLException.formatKey(defineFunction.getName()) + " expects " + parameterFields.size() + " arguments, got " + values.size() + " arguments");
	}

	DefineFunctionEvaluationContext functionContext = new DefineFunctionEvaluationContext(defineFunction, context);

	for(int i = 0; i < parameterFields.size(); i++){
		ParameterField parameterField = parameterFields.get(i);
		FieldValue value = values.get(i);

		FieldName name = parameterField.getName();
		if(name == null){
			throw new MissingAttributeException(parameterField, PMMLAttributes.PARAMETERFIELD_NAME);
		}

		value = value.cast(parameterField);

		functionContext.declare(name, value);
	}

	return ExpressionUtil.evaluateTypedExpressionContainer(defineFunction, functionContext);
}
 
Example #3
Source File: DefineFunctionEvaluationContext.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public FieldValue prepare(FieldName name, Object value){
	ParameterField parameterField = findParameterField(name);
	if(parameterField == null){
		throw new MissingFieldException(name);
	}

	DataType dataType = parameterField.getDataType();
	if(dataType == null){
		throw new MissingAttributeException(parameterField, PMMLAttributes.PARAMETERFIELD_DATATYPE);
	}

	OpType opType = parameterField.getOpType();
	if(opType == null){
		throw new MissingAttributeException(parameterField, PMMLAttributes.PARAMETERFIELD_OPTYPE);
	}

	return FieldValueUtil.create(dataType, opType, value);
}
 
Example #4
Source File: DefineFunctionEvaluationContext.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
private ParameterField findParameterField(FieldName name){
	DefineFunction defineFunction = getDefineFunction();

	if(defineFunction.hasParameterFields()){
		List<ParameterField> parameterFields = defineFunction.getParameterFields();

		for(ParameterField parameterField : parameterFields){

			if(Objects.equals(parameterField.getName(), name)){
				return parameterField;
			}
		}
	}

	return null;
}
 
Example #5
Source File: CountVectorizerModelConverter.java    From jpmml-sparkml with GNU Affero General Public License v3.0 5 votes vote down vote up
@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(), new FieldRef(termField.getName()))
		.setTokenize(Boolean.TRUE)
		.setWordSeparatorCharacterRE(documentFeature.getWordSeparatorRE())
		.setLocalTermWeights(transformer.getBinary() ? TextIndex.LocalTermWeights.BINARY : null);

	Set<DocumentFeature.StopWordSet> stopWordSets = documentFeature.getStopWordSets();
	for(DocumentFeature.StopWordSet stopWordSet : stopWordSets){

		if(stopWordSet.isEmpty()){
			continue;
		}

		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 + "\"");
		}

		Map<String, List<String>> data = new LinkedHashMap<>();
		data.put("string", Collections.singletonList(tokenRE));
		data.put("stem", Collections.singletonList(" "));
		data.put("regex", Collections.singletonList("true"));

		TextIndexNormalization textIndexNormalization = new TextIndexNormalization(null, PMMLUtil.createInlineTable(data))
			.setCaseSensitive(stopWordSet.isCaseSensitive())
			.setRecursive(Boolean.TRUE); // Handles consecutive matches. See http://stackoverflow.com/a/25085385

		textIndex.addTextIndexNormalizations(textIndexNormalization);
	}

	DefineFunction defineFunction = new DefineFunction("tf" + "@" + String.valueOf(CountVectorizerModelConverter.SEQUENCE.getAndIncrement()), OpType.CONTINUOUS, DataType.INTEGER, null, textIndex)
		.addParameterFields(documentField, termField);

	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("Punctuated vocabulary terms (" + term + ") are not supported");
		}

		result.add(new TermFeature(encoder, defineFunction, documentFeature, term));
	}

	return result;
}
 
Example #6
Source File: BSplineTransformer.java    From jpmml-sklearn with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.BSpline.html
 */
static
private DefineFunction createBSplineFunction(BSpline bspline, SkLearnEncoder encoder){
	int k = bspline.getK();

	List<Number> c = bspline.getC();
	List<Number> t = bspline.getT();

	int n = (t.size() - k - 1);

	ParameterField paramterField = new ParameterField()
		.setName(FieldName.create("x"))
		.setOpType(OpType.CONTINUOUS)
		.setDataType(DataType.DOUBLE);

	Apply sumApply = PMMLUtil.createApply(PMMLFunctions.SUM);

	for(int i = 0; i < n; i++){

		for(int j = k; j >= 0; j--){
			createBFunction(t, i, j, encoder);
		}

		Apply apply = PMMLUtil.createApply(PMMLFunctions.MULTIPLY)
			.addExpressions(PMMLUtil.createConstant(c.get(i)))
			.addExpressions(PMMLUtil.createApply(formatBFunction(i, k), new FieldRef(paramterField.getName())));

		sumApply.addExpressions(apply);
	}

	DefineFunction defineFunction = new DefineFunction(formatBSplineFunction(k), OpType.CONTINUOUS, DataType.DOUBLE, null, sumApply)
		.addParameterFields(paramterField);

	encoder.addDefineFunction(defineFunction);

	return defineFunction;
}
 
Example #7
Source File: CountVectorizer.java    From jpmml-sklearn with GNU Affero General Public License v3.0 4 votes vote down vote up
public DefineFunction encodeDefineFunction(){
	String analyzer = getAnalyzer();
	List<String> stopWords = getStopWords();
	Object[] nGramRange = getNGramRange();
	Boolean binary = getBinary();
	Object preprocessor = getPreprocessor();
	String stripAccents = getStripAccents();
	Splitter tokenizer = getTokenizer();

	switch(analyzer){
		case "word":
			break;
		default:
			throw new IllegalArgumentException(analyzer);
	}

	if(preprocessor != null){
		throw new IllegalArgumentException();
	} // End if

	if(stripAccents != null){
		throw new IllegalArgumentException(stripAccents);
	}

	ParameterField documentField = new ParameterField(FieldName.create("document"));

	ParameterField termField = new ParameterField(FieldName.create("term"));

	TextIndex textIndex = new TextIndex(documentField.getName(), new FieldRef(termField.getName()))
		.setTokenize(Boolean.TRUE)
		.setWordSeparatorCharacterRE(tokenizer.getSeparatorRE())
		.setLocalTermWeights(binary ? TextIndex.LocalTermWeights.BINARY : null);

	if((stopWords != null && stopWords.size() > 0) && !Arrays.equals(nGramRange, new Integer[]{1, 1})){
		Map<String, List<String>> data = new LinkedHashMap<>();
		data.put("string", Collections.singletonList("(^|\\s+)\\p{Punct}*(" + JOINER.join(stopWords) + ")\\p{Punct}*(\\s+|$)"));
		data.put("stem", Collections.singletonList(" "));
		data.put("regex", Collections.singletonList("true"));

		TextIndexNormalization textIndexNormalization = new TextIndexNormalization(null, PMMLUtil.createInlineTable(data))
			.setRecursive(Boolean.TRUE); // Handles consecutive matches. See http://stackoverflow.com/a/25085385

		textIndex.addTextIndexNormalizations(textIndexNormalization);
	}

	String name = functionName() + "@" + String.valueOf(CountVectorizer.SEQUENCE.getAndIncrement());

	DefineFunction defineFunction = new DefineFunction(name, OpType.CONTINUOUS, DataType.DOUBLE, null, textIndex)
		.addParameterFields(documentField, termField);

	return defineFunction;
}
 
Example #8
Source File: VersionInspectorTest.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
@Test
public void inspectFunctions(){
	PMML pmml = createPMML();

	assertVersionRange(pmml, Version.PMML_3_0, Version.PMML_4_4);

	Apply apply = new Apply()
		.setFunction(PMMLFunctions.LOWERCASE);

	DefineFunction defineFunction = new DefineFunction("convert_case", OpType.CATEGORICAL, DataType.STRING, null, apply)
		.addParameterFields(new ParameterField(FieldName.create("string")));

	TransformationDictionary transformationDictionary = new TransformationDictionary()
		.addDefineFunctions(defineFunction);

	pmml.setTransformationDictionary(transformationDictionary);

	assertVersionRange(pmml, Version.PMML_4_1, Version.PMML_4_4);

	apply.setFunction(PMMLFunctions.UPPERCASE);

	assertVersionRange(pmml, Version.PMML_3_0, Version.PMML_4_4);

	apply.setFunction(null);

	assertVersionRange(pmml, Version.PMML_3_0, Version.PMML_3_0);
}