Anyone know a AHK Native Equivalent or Know how to rewrite this Fuzzy Search?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
jsong55
Posts: 263
Joined: 30 Mar 2021, 22:02

Anyone know a AHK Native Equivalent or Know how to rewrite this Fuzzy Search?

09 Apr 2024, 10:55

Code: Select all

csharp_code(){
    return c := "
(
using System;

class FuzzyMatcher
{
    public int LevenshteinDistance(string string1, string string2)
    {
        if (string.IsNullOrEmpty(string1))
        {
            if (string.IsNullOrEmpty(string2))
                return 0;
            return string2.Length;
        }

        if (string.IsNullOrEmpty(string2))
        {
            if (string.IsNullOrEmpty(string1))
                return 0;
            return string1.Length;
        }

        var distance = new int[string1.Length + 1, string2.Length + 1];

        for (var i = 0; i <= string1.Length; i++)
            distance[i, 0] = i;

        for (var j = 0; j <= string2.Length; j++)
            distance[0, j] = j;

        for (var i = 1; i <= string1.Length; i++)
        {
            for (var j = 1; j <= string2.Length; j++)
            {
                var cost = (string1[i - 1] == string2[j - 1]) ? 0 : 1;

                distance[i, j] = Math.Min(
                    Math.Min(distance[i - 1, j] + 1, distance[i, j - 1] + 1),
                    distance[i - 1, j - 1] + cost);
            }
        }

        return distance[string1.Length, string2.Length];
    }
    public int JaroWinklerDistance(string str1, string str2)
    {
        int prefixLength = 0;
        int maxPrefixLength = Math.Min(4, Math.Min(str1.Length, str2.Length));

        for (int i = 0; i < maxPrefixLength; i++)
        {
            if (str1[i] == str2[i])
                prefixLength++;
            else
                break;
        }

        int jaroDistance = JaroDistance(str1, str2);
        int winklerDistance = jaroDistance + (int)Math.Round(prefixLength * 0.1 * (1 - jaroDistance));

        return 1 - winklerDistance;
    }

    public int JaroDistance(string str1, string str2)
    {
        int matchingCharacters = 0;
        int transpositions = 0;
        int str1Length = str1.Length;
        int str2Length = str2.Length;
        int searchRange = Math.Max(0, Math.Max(str1Length, str2Length) / 2 - 1);

        bool[] str1Matched = new bool[str1Length];
        bool[] str2Matched = new bool[str2Length];

        for (int i = 0; i < str1Length; i++)
        {
            int start = Math.Max(0, i - searchRange);
            int end = Math.Min(i + searchRange + 1, str2Length);

            for (int j = start; j < end; j++)
            {
                if (!str2Matched[j] && str1[i] == str2[j])
                {
                    str1Matched[i] = true;
                    str2Matched[j] = true;
                    matchingCharacters++;
                    break;
                }
            }
        }

        if (matchingCharacters == 0)
            return 0;

        int k = 0;
        for (int i = 0; i < str1Length; i++)
        {
            if (str1Matched[i])
            {
                while (!str2Matched[k]) k++;
                if (str1[i] != str2[k])
                    transpositions++;
                k++;
            }
        }

        return (matchingCharacters / str1Length + matchingCharacters / str2Length + (matchingCharacters - transpositions / 2) / matchingCharacters) / 3;
    }

    public double Match(string string1, string string2)
    {
        var maxLen = Math.Max(string1.Length, string2.Length);
        if (maxLen == 0)
            return 1;

        var dist = LevenshteinDistance(string1, string2);
        return (1 - (double)dist / maxLen);
    }
}
)"
}

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: emp00, mikeyww and 58 guests