public static void Main()
string textoLI = "Veiculo simples de roda livre - Matéria Prima Polipropileno - Dimensões 19x11x10 - Indicação +3 anos - Restrição 0-3 anos - Modo de Produção Injeção";
string textoReg = "DIMENSÕES 19X11X20 - VEICULO SIMPLES DE RODA LIVRE - MATÉRIA PRIMA PP - INDICAÇÃO MAIOR DE 3 ANOS - RESTRIÇÃO 0-3 ANOS - MODO DE PRODUÇÃO INJEÇÃO";
var similaridade = CalculateSimilarity(textoLI, textoReg);
Console.WriteLine(similaridade);
public static float CalculateSimilarity(string source, string target)
if (string.IsNullOrEmpty(source))
if (string.IsNullOrEmpty(target))
if (string.IsNullOrEmpty(target))
int stepsToSame = ComputeLevenshteinDistance(source, target);
return (1.0f - ((float)stepsToSame / (float)Math.Max(source.Length, target.Length)));
public static int ComputeLevenshteinDistance(string source, string target)
if ((source == null) || (target == null))
if ((source.Length == 0) || (target.Length == 0))
int sourceWordCount = source.Length;
int targetWordCount = target.Length;
if (sourceWordCount == 0)
if (targetWordCount == 0)
int[, ] distance = new int[sourceWordCount + 1, targetWordCount + 1];
for (int i = 0; i <= sourceWordCount; distance[i, 0] = i++)
for (int j = 0; j <= targetWordCount; distance[0, j] = j++)
for (int i = 1; i <= sourceWordCount; i++)
for (int j = 1; j <= targetWordCount; j++)
int cost = (target[j - 1] == source[i - 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[sourceWordCount, targetWordCount];