Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Help in converting Java to PL/SQL

reddy482May 10 2017 — edited Jun 11 2017

Hello Everybody,

I am looking for help in converting below Java Code to PL/SQL. Below code is Lucene Implementation of Jaro-Winkler and is slightly deferred by Oracle UTL_MATCH.Jaro_Winkler.  I don't know much java and tried to convert below with no luck.

Any Help is greatly appreciated.

import java.util.Arrays;

    private static final double DEFAULT_THRESHOLD = 0.7;

    private static final int THREE = 3;

    private static final double JW_COEF = 0.1;

    private double threshold = 0.7;

For mtp[x] it goes:

0=# Matches

1=# Transpositions

2=# prefix match

3=length of the longer of the two compared strings

    public final double similarity(final String s1, final String s2) {

        if (s1 == null) {

            throw new NullPointerException("s1 must not be null");

        }

        if (s2 == null) {

            throw new NullPointerException("s2 must not be null");

        }

        if (s1.equals(s2)) {

            return 1;

        }

        int[] mtp = matches(s1, s2);

        float m = mtp[0];

        if (m == 0) {

            return 0f;

        }

        double j = ((m / s1.length() + m / s2.length() + (m - mtp[1]) / m))

                / THREE;

        double jw = j;

        if (j > threshold) {

            jw = j + Math.min(JW_COEF, 1.0 / mtp[THREE]) * mtp[2] * (1 - j);

        }

        return jw;

    }

    

    public final double distance(final String s1, final String s2) {

        return 1.0 - similarity(s1, s2);

    }

    private int[] matches(final String s1, final String s2) {

        String max, min;

        if (s1.length() > s2.length()) {

            max = s1;

            min = s2;

        } else {

            max = s2;

            min = s1;

        }

        int range = Math.max(max.length() / 2 - 1, 0);

        int[] matchIndexes = new int[min.length()];

        Arrays.fill(matchIndexes, -1);

        boolean[] matchFlags = new boolean[max.length()];

        int matches = 0;

        for (int mi = 0; mi < min.length(); mi++) {

            char c1 = min.charAt(mi);

            for (int xi = Math.max(mi - range, 0),

                    xn = Math.min(mi + range + 1, max.length()); xi < xn; xi++) {

                if (!matchFlags[xi] && c1 == max.charAt(xi)) {

                    matchIndexes[mi] = xi;

                    matchFlags[xi] = true;

                    matches++;

                    break;

                }

            }

        }

        char[] ms1 = new char[matches];

        char[] ms2 = new char[matches];

        for (int i = 0, si = 0; i < min.length(); i++) {

            if (matchIndexes[i] != -1) {

                ms1[si] = min.charAt(i);

                si++;

            }

        }

        for (int i = 0, si = 0; i < max.length(); i++) {

            if (matchFlags[i]) {

                ms2[si] = max.charAt(i);

                si++;

            }

        }

        int transpositions = 0;

        for (int mi = 0; mi < ms1.length; mi++) {

            if (ms1[mi] != ms2[mi]) {

                transpositions++;

            }

        }

        int prefix = 0;

        for (int mi = 0; mi < min.length(); mi++) {

            if (s1.charAt(mi) == s2.charAt(mi)) {

                prefix++;

            } else {

                break;

            }

        }

        return new int[]{matches, transpositions / 2, prefix, max.length()};

    }

Thanks

This post has been answered by Mustafa KALAYCI on Jun 6 2017
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 4 2017
Added on May 10 2017
3 comments
1,127 views