org.pentaho.di.trans.steps.fuzzymatch.FuzzyMatch Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kettle-engine Show documentation
Show all versions of kettle-engine Show documentation
Container pom for Pentaho Data Integration modules
The newest version!
/*! ******************************************************************************
*
* Pentaho Data Integration
*
* Copyright (C) 2002-2017 by Hitachi Vantara : http://www.pentaho.com
*
*******************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
package org.pentaho.di.trans.steps.fuzzymatch;
import java.util.Iterator;
import org.apache.commons.codec.language.DoubleMetaphone;
import org.apache.commons.codec.language.Metaphone;
import org.apache.commons.codec.language.RefinedSoundex;
import org.apache.commons.codec.language.Soundex;
import org.apache.commons.lang.StringUtils;
import org.pentaho.di.core.Const;
import org.pentaho.di.core.util.Utils;
import org.pentaho.di.core.RowSet;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.exception.KettleStepException;
import org.pentaho.di.core.exception.KettleValueException;
import org.pentaho.di.core.row.RowDataUtil;
import org.pentaho.di.core.row.RowMeta;
import org.pentaho.di.core.row.RowMetaInterface;
import org.pentaho.di.core.row.ValueMetaInterface;
import org.pentaho.di.i18n.BaseMessages;
import org.pentaho.di.trans.Trans;
import org.pentaho.di.trans.TransMeta;
import org.pentaho.di.trans.step.BaseStep;
import org.pentaho.di.trans.step.StepDataInterface;
import org.pentaho.di.trans.step.StepInterface;
import org.pentaho.di.trans.step.StepMeta;
import org.pentaho.di.trans.step.StepMetaInterface;
import com.wcohen.ss.Jaro;
import com.wcohen.ss.JaroWinkler;
import com.wcohen.ss.NeedlemanWunsch;
/**
* Performs a fuzzy match for each main stream field row An approximative match is done in a lookup stream
*
* @author Samatar
* @since 03-mars-2008
*/
public class FuzzyMatch extends BaseStep implements StepInterface {
private static Class> PKG = FuzzyMatchMeta.class; // for i18n purposes, needed by Translator2!!
private FuzzyMatchMeta meta;
private FuzzyMatchData data;
public FuzzyMatch( StepMeta stepMeta, StepDataInterface stepDataInterface, int copyNr, TransMeta transMeta,
Trans trans ) {
super( stepMeta, stepDataInterface, copyNr, transMeta, trans );
}
private boolean readLookupValues() throws KettleException {
data.infoStream = meta.getStepIOMeta().getInfoStreams().get( 0 );
if ( data.infoStream.getStepMeta() == null ) {
logError( BaseMessages.getString( PKG, "FuzzyMatch.Log.NoLookupStepSpecified" ) );
return false;
}
if ( isDetailed() ) {
logDetailed( BaseMessages.getString( PKG, "FuzzyMatch.Log.ReadingFromStream" )
+ data.infoStream.getStepname() + "]" );
}
boolean firstRun = true;
// Which row set do we read from?
//
RowSet rowSet = findInputRowSet( data.infoStream.getStepname() );
Object[] rowData = getRowFrom( rowSet ); // rows are originating from "lookup_from"
while ( rowData != null ) {
if ( firstRun ) {
data.infoMeta = rowSet.getRowMeta().clone();
// Check lookup field
int indexOfLookupField = data.infoMeta.indexOfValue( environmentSubstitute( meta.getLookupField() ) );
if ( indexOfLookupField < 0 ) {
// The field is unreachable !
throw new KettleException( BaseMessages.getString(
PKG, "FuzzyMatch.Exception.CouldnotFindLookField", meta.getLookupField() ) );
}
data.infoCache = new RowMeta();
ValueMetaInterface keyValueMeta = data.infoMeta.getValueMeta( indexOfLookupField );
keyValueMeta.setStorageType( ValueMetaInterface.STORAGE_TYPE_NORMAL );
data.infoCache.addValueMeta( keyValueMeta );
// Add key
data.indexOfCachedFields[0] = indexOfLookupField;
// Check additional fields
if ( data.addAdditionalFields ) {
ValueMetaInterface additionalFieldValueMeta;
for ( int i = 0; i < meta.getValue().length; i++ ) {
int fi = i + 1;
data.indexOfCachedFields[fi] = data.infoMeta.indexOfValue( meta.getValue()[i] );
if ( data.indexOfCachedFields[fi] < 0 ) {
// The field is unreachable !
throw new KettleException( BaseMessages.getString(
PKG, "FuzzyMatch.Exception.CouldnotFindLookField", meta.getValue()[i] ) );
}
additionalFieldValueMeta = data.infoMeta.getValueMeta( data.indexOfCachedFields[fi] );
additionalFieldValueMeta.setStorageType( ValueMetaInterface.STORAGE_TYPE_NORMAL );
data.infoCache.addValueMeta( additionalFieldValueMeta );
}
data.nrCachedFields += meta.getValue().length;
}
}
if ( log.isRowLevel() ) {
logRowlevel( BaseMessages.getString( PKG, "FuzzyMatch.Log.ReadLookupRow" )
+ rowSet.getRowMeta().getString( rowData ) );
}
// Look up the keys in the source rows
// and store values in cache
Object[] storeData = new Object[data.nrCachedFields];
// Add key field
if ( rowData[data.indexOfCachedFields[0]] == null ) {
storeData[0] = "";
} else {
ValueMetaInterface fromStreamRowMeta = rowSet.getRowMeta().getValueMeta( data.indexOfCachedFields[0] );
if ( fromStreamRowMeta.isStorageBinaryString() ) {
storeData[0] = fromStreamRowMeta.convertToNormalStorageType( rowData[data.indexOfCachedFields[0]] );
} else {
storeData[0] = rowData[data.indexOfCachedFields[0]];
}
}
// Add additional fields?
for ( int i = 1; i < data.nrCachedFields; i++ ) {
ValueMetaInterface fromStreamRowMeta = rowSet.getRowMeta().getValueMeta( data.indexOfCachedFields[i] );
if ( fromStreamRowMeta.isStorageBinaryString() ) {
storeData[i] = fromStreamRowMeta.convertToNormalStorageType( rowData[data.indexOfCachedFields[i]] );
} else {
storeData[i] = rowData[data.indexOfCachedFields[i]];
}
}
if ( isDebug() ) {
logDebug( BaseMessages.getString( PKG, "FuzzyMatch.Log.AddingValueToCache", data.infoCache
.getString( storeData ) ) );
}
addToCache( storeData );
rowData = getRowFrom( rowSet );
if ( firstRun ) {
firstRun = false;
}
}
return true;
}
private Object[] lookupValues( RowMetaInterface rowMeta, Object[] row ) throws KettleException {
if ( first ) {
first = false;
data.outputRowMeta = getInputRowMeta().clone();
meta.getFields(
data.outputRowMeta, getStepname(), new RowMetaInterface[] { data.infoMeta }, null, this, repository,
metaStore );
// Check lookup field
data.indexOfMainField = getInputRowMeta().indexOfValue( environmentSubstitute( meta.getMainStreamField() ) );
if ( data.indexOfMainField < 0 ) {
// The field is unreachable !
throw new KettleException( BaseMessages.getString( PKG, "FuzzyMatch.Exception.CouldnotFindMainField", meta
.getMainStreamField() ) );
}
}
Object[] add = null;
if ( row[ data.indexOfMainField ] == null ) {
add = buildEmptyRow();
} else {
try {
add = getFromCache( row );
} catch ( Exception e ) {
throw new KettleStepException( e );
}
}
return RowDataUtil.addRowData( row, rowMeta.size(), add );
}
private void addToCache( Object[] value ) throws KettleException {
try {
data.look.add( value );
} catch ( java.lang.OutOfMemoryError o ) {
// exception out of memory
throw new KettleException( BaseMessages.getString( PKG, "FuzzyMatch.Error.JavaHeap", o.toString() ) );
}
}
private Object[] getFromCache( Object[] keyRow ) throws KettleValueException {
if ( isDebug() ) {
logDebug( BaseMessages.getString( PKG, "FuzzyMatch.Log.ReadingMainStreamRow", getInputRowMeta().getString(
keyRow ) ) );
}
Object[] retval = null;
switch ( meta.getAlgorithmType() ) {
case FuzzyMatchMeta.OPERATION_TYPE_LEVENSHTEIN:
case FuzzyMatchMeta.OPERATION_TYPE_DAMERAU_LEVENSHTEIN:
case FuzzyMatchMeta.OPERATION_TYPE_NEEDLEMAN_WUNSH:
retval = doDistance( keyRow );
break;
case FuzzyMatchMeta.OPERATION_TYPE_DOUBLE_METAPHONE:
case FuzzyMatchMeta.OPERATION_TYPE_METAPHONE:
case FuzzyMatchMeta.OPERATION_TYPE_SOUNDEX:
case FuzzyMatchMeta.OPERATION_TYPE_REFINED_SOUNDEX:
retval = doPhonetic( keyRow );
break;
case FuzzyMatchMeta.OPERATION_TYPE_JARO:
case FuzzyMatchMeta.OPERATION_TYPE_JARO_WINKLER:
case FuzzyMatchMeta.OPERATION_TYPE_PAIR_SIMILARITY:
retval = doSimilarity( keyRow );
break;
default:
break;
}
return retval;
}
private Object[] doDistance( Object[] row ) throws KettleValueException {
// Reserve room
Object[] rowData = buildEmptyRow();
Iterator