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.field;
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.data.columnar.ColumnarFeature;
023import org.tribuo.data.columnar.FieldProcessor;
024
025import java.util.Collections;
026import java.util.List;
027import java.util.logging.Logger;
028
029/**
030 * Processes a column that contains a real value. The name of the feature
031 * will be the name given for the column and the value will be the value in the 
032 * column.
033 * <p>
034 * Returns an empty list if the value failed to parse or was empty.
035 */
036public class DoubleFieldProcessor implements FieldProcessor {
037    
038    private static final Logger logger = Logger.getLogger(DoubleFieldProcessor.class.getName());
039
040    @Config(mandatory = true,description="The field name to read.")
041    private String fieldName;
042
043    /**
044     * For olcut.
045     */
046    private DoubleFieldProcessor() {}
047
048    /**
049     * Constructs a field processor which extracts a single double valued feature from the specified field name.
050     * @param fieldName The field name to read.
051     */
052    public DoubleFieldProcessor(String fieldName) {
053        this.fieldName = fieldName;
054    }
055
056    @Override
057    public String getFieldName() {
058        return fieldName;
059    }
060
061    @Override
062    public List<ColumnarFeature> process(String value) {
063        try {
064            double parsedValue = Double.parseDouble(value);
065            if (parsedValue == 0.0) {
066                return Collections.emptyList();
067            } else {
068                return Collections.singletonList(new ColumnarFeature(fieldName, "value", Double.parseDouble(value)));
069            }
070        } catch (NumberFormatException ex) {
071            if(!value.trim().isEmpty()){
072                logger.warning(String.format("Non-double value %s in %s", value, fieldName));
073            }
074            return Collections.emptyList();
075        }
076
077    }
078
079    @Override
080    public GeneratedFeatureType getFeatureType() {
081        return GeneratedFeatureType.REAL;
082    }
083
084    @Override
085    public DoubleFieldProcessor copy(String newFieldName) {
086        return new DoubleFieldProcessor(newFieldName);
087    }
088
089    @Override
090    public String toString() {
091        return "DoubleFieldProcessor(fieldName=" + getFieldName() + ")";
092    }
093
094    @Override
095    public ConfiguredObjectProvenance getProvenance() {
096        return new ConfiguredObjectProvenanceImpl(this,"FieldProcessor");
097    }
098}