Reputation: 135
I have a list of over a thousand different cryptocurrencies alphabetized. The issue is here if a user queries for, say, "BTC" they will get BTCA (Bitair) out of the list, since THAT comes before Bitcoin. Alternatively, typing Bitcoin will give you "AntiBitcoin" instead, since THAT comes before Bitcoin. Originally, this occurred because I used .Contains()
which is just not correct, so now I switched to Levenshtein for delimination.
I am setting up a simple loop like this:
foreach (String s in crypto.currencies)
{
if (Crypto.LevenshteinDistance(s,currency) <= (Stuck on the right way to do this now))
{
//foo
}
Console.WriteLine(s + " is not a match. Continuing.");
}
But I am very stuck on how I should actually go upon getting the proper item the user wants. It may seem completely dumb, what I am trying to do, but it is imperative I can properly query through this list (plus, I had typed it out by hand, ouch)
Upvotes: 2
Views: 586
Reputation: 726479
You could order the list on the Levenshtein distance from the target currency, and pick the top one:
var closestMatch = crypto.currencies
.OrderBy(s => Crypto.LevenshteinDistance(s, currency))
.First();
Upvotes: 2