001/* 002 * Copyright (c) 2015-2020, Oracle and/or its affiliates. All rights reserved. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.tribuo.data.columnar.processors.response; 018 019import com.oracle.labs.mlrg.olcut.config.Config; 020import com.oracle.labs.mlrg.olcut.provenance.ConfiguredObjectProvenance; 021import com.oracle.labs.mlrg.olcut.provenance.impl.ConfiguredObjectProvenanceImpl; 022import org.tribuo.Output; 023import org.tribuo.OutputFactory; 024import org.tribuo.data.columnar.ResponseProcessor; 025 026import java.util.Optional; 027 028/** 029 * A {@link ResponseProcessor} that takes a single value of the 030 * field as the positive class and all other values as the negative 031 * class. 032 */ 033public class BinaryResponseProcessor<T extends Output<T>> implements ResponseProcessor<T> { 034 035 @Config(mandatory = true,description="The field name to read.") 036 private String fieldName; 037 038 @Config(mandatory = true,description="The string which triggers a positive response.") 039 private String positiveResponse; 040 041 @Config(mandatory = true,description="Output factory to use to create the response.") 042 private OutputFactory<T> outputFactory; 043 044 @Config(description="The positive response to emit.") 045 private String positiveName = "1"; 046 047 @Config(description="The negative response to emit.") 048 private String negativeName = "0"; 049 050 /** 051 * for OLCUT. 052 */ 053 private BinaryResponseProcessor() {} 054 055 /** 056 * Constructs a binary response processor which emits a positive value for a single string 057 * and a negative value for all other field values. 058 * @param fieldName The field name to read. 059 * @param positiveResponse The positive response to look for. 060 * @param outputFactory The output factory to use. 061 */ 062 public BinaryResponseProcessor(String fieldName, String positiveResponse, OutputFactory<T> outputFactory) { 063 this.fieldName = fieldName; 064 this.positiveResponse = positiveResponse; 065 this.outputFactory = outputFactory; 066 } 067 068 @Override 069 public OutputFactory<T> getOutputFactory() { 070 return outputFactory; 071 } 072 073 @Override 074 public String getFieldName() { 075 return fieldName; 076 } 077 078 @Deprecated 079 @Override 080 public void setFieldName(String fieldName) { 081 this.fieldName = fieldName; 082 } 083 084 @Override 085 public Optional<T> process(String value) { 086 return Optional.of(outputFactory.generateOutput(positiveResponse.equals(value) ? positiveName : negativeName)); 087 } 088 089 @Override 090 public String toString() { 091 return "BinaryResponseProcessor(fieldName="+ fieldName +", positiveResponse="+ positiveResponse +", positiveName="+positiveName +", negativeName="+negativeName+")"; 092 } 093 094 @Override 095 public ConfiguredObjectProvenance getProvenance() { 096 return new ConfiguredObjectProvenanceImpl(this,"ResponseProcessor"); 097 } 098}