Skip to main content
added 854 characters in body
Source Link
Temps
  • 49
  • 6

Edit:

I managed to take a different approach which seems to work in all cases:

public static int temprovichDistance(String referenceString, String comparisonString) {
        if (referenceString == null || comparisonString == null) return -1;
        if (referenceString.equals(comparisonString)) return 0;
        if (referenceString.length() == 0 || comparisonString.length() == 0) return -1;
            
        int dist = 0;
            
        for (int i = 0; i < referenceString.length(); i++) {
            int tmp = 0;
            for (int j = 0; j < comparisonString.length(); j++) {
                if (referenceString.charAt(i) != comparisonString.charAt(j)) tmp++;
                else continue;
            }
            if (tmp == comparisonString.length()) dist++;
        }

        return dist == 0 ? dist + Mathf.abs(referenceString.length() - comparisonString.length()) : dist;
    }

Thanks again for any comments <3

Edit:

I managed to take a different approach which seems to work in all cases:

public static int temprovichDistance(String referenceString, String comparisonString) {
        if (referenceString == null || comparisonString == null) return -1;
        if (referenceString.equals(comparisonString)) return 0;
        if (referenceString.length() == 0 || comparisonString.length() == 0) return -1;
            
        int dist = 0;
            
        for (int i = 0; i < referenceString.length(); i++) {
            int tmp = 0;
            for (int j = 0; j < comparisonString.length(); j++) {
                if (referenceString.charAt(i) != comparisonString.charAt(j)) tmp++;
                else continue;
            }
            if (tmp == comparisonString.length()) dist++;
        }

        return dist == 0 ? dist + Mathf.abs(referenceString.length() - comparisonString.length()) : dist;
    }

Thanks again for any comments <3

Source Link
Temps
  • 49
  • 6

New String similarity algorithm (java)

Recently I have been developing a library for Java which provides utility functions for arrays, strings, etc.. While researching string similarity algorithms, I managed to write one of my own. I am curious as to if anyone has come across a similar approach or if it is a commonly known one. The code is sampled below.

public static int temprovichDistance(String ref, String check) {
    if (ref == null || check == null) return -1;
    if (ref.equals(check)) return 0;
    if (ref.length() == 0 || check.length() == 0) return -1;
            
    // if negative, then ref is shorter than check
    int lendiff = ref.length() - check.length();
    int dist = 0;

    for (int i = 0; i < ref.length(); i++) {
        char r = ref.charAt(i);
        char x = 0;

        if (lendiff < 0) x = check.charAt(i);
        else if (i < check.length()) x = check.charAt(i);
        else break;

        if (r == x) continue;
        else {
            if (lendiff > 0) ref = remove(ref, r);
            else if (lendiff < 0) check = remove(check, x);
                    
            if (r != x) dist++;
        }
    }

    if (dist == 0 && lendiff != 0) dist += Math.abs(lendiff);

    return dist;
}

Thanks again to anyone that proves an answer!