Ver Mensaje Individual
  #2 (permalink)  
Antiguo 12/12/2013, 03:30
marioml13
 
Fecha de Ingreso: diciembre-2013
Mensajes: 3
Antigüedad: 10 años, 4 meses
Puntos: 0
Respuesta: Algoritmo de búsqueda palabras similares (Fuzzy Search)

Al final creo que lo he solucionado modificando el código del algoritmo de Levensthein:

Código:
public static double LevenshteinDistance(string src, string dest)
		{
			double[,] d = new double[src.Length + 1, dest.Length + 1];
            int i, j;
            double cost;
			char[] str1 = src.ToCharArray();
			char[] str2 = dest.ToCharArray();

			for (i = 0; i <= str1.Length; i++)
			{
				d[i, 0] = i*1.0;
			}
			for (j = 0; j <= str2.Length; j++)
			{
				d[0, j] = j*1.0;
			}
			for (i = 1; i <= str1.Length; i++)
			{
				for (j = 1; j <= str2.Length; j++)
				{

                    if (str1[i - 1] == str2[j - 1]){
                        cost = 0.0;
                    }else
                    {
                        cost = 1.0;
                        //El 1 con la i
                        if (str1[i - 1] == 'i' && str2[j - 1] == '1')
                            cost = 0.4;
                        if (str1[i - 1] == '1' && str2[j - 1] == 'i')
                            cost = 0.4;
                        //La l con la i
                        if (str1[i - 1] == 'l' && str2[j - 1] == 'i')
                            cost = 0.4;
                        if (str1[i - 1] == 'i' && str2[j - 1] == 'l')
                            cost = 0.4;
                        //La L con la I
                        if (str1[i - 1] == 'L' && str2[j - 1] == 'I')
                            cost = 0.4;
                        if (str1[i - 1] == 'I' && str2[j - 1] == 'L')
                            cost = 0.4;
                        //El 0 con la o
                        if (str1[i - 1] == '0' && str2[j - 1] == 'o')
                            cost = 0.4;
                        if (str1[i - 1] == 'o' && str2[j - 1] == '0')
                            cost = 0.4;
                        //El 0 con la O
                        if (str1[i - 1] == '0' && str2[j - 1] == 'O')
                            cost = 0.4;
                        if (str1[i - 1] == 'O' && str2[j - 1] == '0')
                            cost = 0.4;
                        //La o con la O
                        if (str1[i - 1] == 'o' && str2[j - 1] == 'O')
                            cost = 0.4;
                        if (str1[i - 1] == 'O' && str2[j - 1] == 'o')
                            cost = 0.4;  
                        
                    }
                        

					d[i, j] =
						Math.Min(
							d[i - 1, j] + 1,					// Deletion
							Math.Min(
								d[i, j - 1] + 1,				// Insertion
								d[i - 1, j - 1] + cost));		// Substitution

					if ((i > 1) && (j > 1) && (str1[i - 1] == str2[j - 2]) && (str1[i - 2] == str2[j - 1]))
					{
						d[i, j] = Math.Min(d[i, j], d[i - 2, j - 2] + cost);
					}
				}
			}
            Console.WriteLine("Distancia entre: "+src+" y "+dest+" es: "+d[str1.Length, str2.Length]);
			return d[str1.Length, str2.Length];
		}