Find the closest match string

C # WinApp: let's say I'm looking for dtg, but the elements I'm looking for are dvg, dz, dxg ... so I want it to find dvg for me because it is closer to the dtg I was looking for. I know that there are some NP-hard algorithms for this, but in fact I do not want to spend a lot of time on this. are there any String methods that do something close to this? or can it do this with a few extra code pledges?

+4
source share
3 answers

You want to use Soundex. If I can find a link to some code, I will get you. I did a spell check with Soundexes, and that is exactly what you are looking for.

In the meantime, this google search should help:

http://www.google.com/search?q=C%23+soundex&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a

Here's a good C # implementation: http://www.builderau.com.au/program/csharp/print.htm?TYPE=story&AT=339291792-339028385t-320002002c

+1
source

You will need a metric that describes the difference between the two lines. The general approach is to use Levenshtein distance , which is quickly implemented using several lines of C # code (code files are available on the Internet).

+8
source
public static string ClosestWord(string word, string[] terms) { string term = word.ToLower(); List<string> list = terms.ToList(); if (list.Contains(term)) return list.Find(t => t.ToLower() == term); else { int[] counter = new int[terms.Length]; for (int i = 0; i < terms.Length; i++) { for (int x = 0; x < Math.Min(term.Length, terms[i].Length); x++) { int difference = Math.Abs(term[x] - terms[i][x]); counter[i] += difference; } } int min = counter.Min(); int index = counter.ToList().FindIndex(t => t == min); return terms[index]; } } 
0
source

All Articles