[Include] similar_text.inc
#1

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.
Reply
#2

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?
Reply
#3

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

I thought improve this

Thanks.
Reply
#4

nice release +rep
Reply
#5

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

is it Levenshtein distance ?
Reply
#7

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

Thanks.
Reply
#8

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

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
Reply
#10

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




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


Forum Jump:


Users browsing this thread: 2 Guest(s)