SA-MP Forums Archive
[Include] similar_text.inc - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Filterscripts (https://sampforum.blast.hk/forumdisplay.php?fid=17)
+---- Forum: Includes (https://sampforum.blast.hk/forumdisplay.php?fid=83)
+---- Thread: [Include] similar_text.inc (/showthread.php?tid=468216)



similar_text.inc - ipsBruno - 07.10.2013

Code

This calculate similarity of words. And returns percentage

pawn Код:
/*
 * similar_text
 *
 * © Copyright 2010-2013, Bruno da Silva
 * Funзгo para checar proximidade de textos
 */

 
#if defined _similart_included
  #endinput
#endif
#define _similart_included

#if !defined MAX_STRING
    #define MAX_STRING 0xff
#endif

stock function_r[MAX_STRING][MAX_STRING];
stock ff_calcule[MAX_STRING][MAX_STRING];
   
   
stock Float:similar_text(texto1[], texto2[]) {

    static len1, len2, x, y ;

    len1 = strlen(texto1);
    len2 = strlen(texto2);

    ff_calcule = function_r ;


    for (x = 1; x <= len2; x++) {
        ff_calcule[x][0] = ff_calcule[x-1][0] + 1;
    }

    for(y = 1; y <= len1; y++) {
        ff_calcule[0][y] = ff_calcule[0][y-1] + 1;
    }

    for (x = 1; x <= len2; x++) {
        for (y = 1; y <= len1; y++) {
            ff_calcule[x][y] = MIN3D(ff_calcule[x-1][y] + 1,ff_calcule[x][y-1] + 1,ff_calcule[x-1][y-1] + _: !(texto1[y-1] == texto2[x-1]));
        }
    }

    return (1.0 - (float(ff_calcule[len2][len1]) / float(max(len1,len2)))) * 100.0;
}

stock MIN3D(v0,v1,v2) {
    return v0 < v1 ? v0 < v2 ? v0 : v2 : v1 < v2 ? v1 : v2;
}
similar_text.inc




Exemples

PHP код:
    printf("Bruno e Breno  %f%% similar"similar_text("Bruno""Breno")); 
PHP код:
    printf("Carlos e Joгo %f%% similar"similar_text("Carlos""Joгo")); 
Why?
This can be used for a range of things. Like: anti palaver, players with a similar name, search's etc

Thanks.

Sorry my english.


AW: similar_text.inc - BigETI - 07.10.2013

Quote:
pawn Код:
#if !defined MAX_STRING
    #define MAX_STRING 0xff
#endif

stock function_r[MAX_STRING][MAX_STRING];
stock ff_calcule[MAX_STRING][MAX_STRING];
really?


Respuesta: AW: similar_text.inc - ipsBruno - 07.10.2013

Quote:
Originally Posted by BigETI
Посмотреть сообщение
really?
:/

I thought improve this

Thanks.


Re: similar_text.inc - Jankingston - 07.10.2013

nice release +rep


Respuesta: Re: similar_text.inc - ipsBruno - 07.10.2013

Quote:
Originally Posted by Jankingston
Посмотреть сообщение
nice release +rep
Thanks Jankingston


Re: similar_text.inc - Vinnyy - 07.10.2013

is it Levenshtein distance ?


Respuesta: Re: similar_text.inc - ipsBruno - 07.10.2013

Quote:
Originally Posted by Vinnyy
Посмотреть сообщение
is it Levenshtein distance ?
Yeap! I tried using the method with recursion, but in Pawn failed

Thanks.


Re: similar_text.inc - Jochemd - 08.10.2013

Well that's actually quite nice, but could you tell me a practical usage for this?


Respuesta: Re: similar_text.inc - ipsBruno - 08.10.2013

Quote:
Originally Posted by Jochemd
Посмотреть сообщение
Well that's actually quite nice, but could you tell me a practical usage for this?
Thanks

pawn Код:
// only example
stock SimilarPlayerID(name[]) {

    if(name[0]) {
        static szName[MAX_PLAYER_NAME]. i;
       
        for(i = 0; i != MAX_PLAYERS; ++i) {
            if(IsPlayerConnected(i)) {
                GetPlayerName(i, szName, sizeof szName);
               
                if( similar_text(name, szName) > 50.0 ) { // 50% of similarity
                    return i;
                }
            }
        }
    }
    return -1;
}
if have a player with name kill_machine and you search kill mach similar_text return the id. Strfind not


Respuesta: similar_text.inc - ipsBruno - 29.10.2013

Simple example. '******' in PAWN:




https://sampforum.blast.hk/showthread.php?tid=472589