Accéder au contenu principal Passer au contenu complémentaire

Créer un algorithme de correspondance personnalisé

Le composant tRecordMatching vous permet d'utiliser un algorithme de rapprochement personnalisé afin d'obtenir les résultats dont vous avez besoin.

Un algorithme de mise en correspondance personnalisé est écrit manuellement et stocké dans un fichier .jar (archive Java). Talend fournit un exemple de fichier .jar sur la base duquel vous pouvez créer votre propre fichier.

Procédure

  1. Dans Eclipse, effectuez un check out sur le projet SVN test.mydistance :
  2. Dans ce projet, accédez à la classe Java nommée MyDistance.Java : https://github.com/Talend/tdq-studio-se/tree/master/sample/test.mydistance/src/main/java/org/talend/mydistance.
  3. Ouvrez ce fichier contenant le code suivant :
    package org.talend.mydistance;
    
    import org.talend.dataquality.record.linkage.attribute.AbstractAttributeMatcher;
    import org.talend.dataquality.record.linkage.constant.AttributeMatcherType;
    
    /**
     * @author scorreia
     * 
     * Example of Matching distance.
     */
    public class MyDistance extends AbstractAttributeMatcher {
    
        /*
         * (non-Javadoc)
         * 
         * @see org.talend.dataquality.record.linkage.attribute.IAttributeMatcher#getMatchType()
         */
        @Override
        public AttributeMatcherType getMatchType() {
            // a custom implementation should return this type AttributeMatcherType.custom
            return AttributeMatcherType.CUSTOM;
        }
    
        /*
         * (non-Javadoc)
         * 
         * @see org.talend.dataquality.record.linkage.attribute.IAttributeMatcher#getMatchingWeight(java.lang.String,
         * java.lang.String)
         */
        @Override
        public double getWeight(String arg0, String arg1) {
            // Here goes the custom implementation of the matching distance between the two given strings.
            // the algorithm should return a value between 0 and 1.
    
            // in this example, we consider that 2 strings match if their first 4 characters are identical
            // the arguments are not null (the check for nullity is done by the caller)
            int MAX_CHAR = 4;
            final int max = Math.min(MAX_CHAR, Math.min(arg0.length(), arg1.length()));
            int nbIdenticalChar = 0;
            for (; nbIdenticalChar < max; nbIdenticalChar++) {
                if (arg0.charAt(nbIdenticalChar) != arg1.charAt(nbIdenticalChar)) {
                    break;
                }
            }
            if (arg0.length() < MAX_CHAR && arg1.length() < MAX_CHAR) {
                MAX_CHAR = Math.max(arg0.length(), arg1.length());
            }
            return (nbIdenticalChar) / ((double) MAX_CHAR);
    }
  4. Dans ce fichier, saisissez le nom de la classe de l'algorithme personnalisé que vous créez afin de remplacer le nom par défaut.
    Le nom par défaut est MyDistance et vous pouvez le trouver dans la ligne : public class MyDistance implements IAttributeMatcher.
  5. A la place de l'algorithme par défaut dans le fichier, saisissez l'algorithme que vous souhaitez créer.
    L'algorithme par défaut se présente comme suit :
            int MAX_CHAR = 4;
            final int max = Math.min(MAX_CHAR, Math.min(arg0.length(), arg1.length()));
            int nbIdenticalChar = 0;
            for (; nbIdenticalChar < max; nbIdenticalChar++) {
                if (arg0.charAt(nbIdenticalChar) != arg1.charAt(nbIdenticalChar)) {
                    break;
                }
            }
            if (arg0.length() < MAX_CHAR && arg1.length() < MAX_CHAR) {
                MAX_CHAR = Math.max(arg0.length(), arg1.length());
            }
            return (nbIdenticalChar) / ((double) MAX_CHAR);
    
  6. Sauvegardez vos modifications.
  7. Via Eclipse, exportez ce nouveau fichier .jar.

Résultats

Cet algorithme personnalisé est prêt à être utilisé par le composant tRecordMatching.

Cette page vous a-t-elle aidé ?

Si vous rencontrez des problèmes sur cette page ou dans son contenu – une faute de frappe, une étape manquante ou une erreur technique – dites-nous comment nous améliorer !