473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

fuzzy string compare in .Net ?

hi,
when a user reaches a 404 page not found on my web site, i want to give them
one or more 'best guess' links to the page they are looking for (MS do this
on their site...). i have a list of all the available pages to compare the
requested url against.
i've hunted around on google and newsgroups like alt.comp.fuzzy, but i can't
find a nice .NET API that i can just call in my code to give me a closest
match. i would define closest match as the one with the smallest 'edit
distance' to the original, i.e. the one that requires the least amount of
insertions, deletions and edits to match it exactly. i can't see how a
regular expression could help (but i may of course be wrong) since i want to
be quite tolerant of variances in the strings.
if there isn't such an API, i would have the time to implement one, if
someone gave me a few pointers in the right direction. it would want to be
efficient also, because i can easily see an algorithm going into n cubed or
worse here.

thanks for reading this
tim

blog: http://tim.mackey.ie
67d0ebfec70e8db3
Jul 21 '05 #1
4 3724
You should try searching for "Levenshtein" on Google.
This should get you a number of good resources on String Edit Distance
methodologies.
At one point, I came across a great dotNet implementation. However, I cannot
locate it at the moment, but I will keep looking.
While this is not really what you want, it is a good read and has some very
informative and helpful links:
http://www.codeproject.com/string/dmetaphone6.asp
it might also give you some ideas on maybe implementing a less resource
intensive version of what you want.

Gerald

"Tim Mackey" <ti*@scootasp.net> wrote in message
news:31*************@individual.net...
hi,
when a user reaches a 404 page not found on my web site, i want to give them one or more 'best guess' links to the page they are looking for (MS do this on their site...). i have a list of all the available pages to compare the requested url against.
i've hunted around on google and newsgroups like alt.comp.fuzzy, but i can't find a nice .NET API that i can just call in my code to give me a closest
match. i would define closest match as the one with the smallest 'edit
distance' to the original, i.e. the one that requires the least amount of
insertions, deletions and edits to match it exactly. i can't see how a
regular expression could help (but i may of course be wrong) since i want to be quite tolerant of variances in the strings.
if there isn't such an API, i would have the time to implement one, if
someone gave me a few pointers in the right direction. it would want to be efficient also, because i can easily see an algorithm going into n cubed or worse here.

thanks for reading this
tim

blog: http://tim.mackey.ie
67d0ebfec70e8db3

Jul 21 '05 #2
hi Gerald
many thanks for the pointer. i've got a working prototype using a c#
levenshtein implementation, which is a good start. i think i need more
flexibility than what it provides though.
for example, i'm searching for a best guess for 'consortium', the results of
EditDistance() are shown below.
result format: <EditDistance> <Compared With>:
6 contact
6 conor ryan
7 motorola
7 joe morris
8 bocse 2004
8 consortium members

it's clear that contact does require fewer character changes to change to
consortium, but the fact that "consortium members" has the exact word match
in it should put it ahead of all the others. i can obviously start catering
for manual cases like this, but it would be buggy and it probably take a
long time to get a generic solution.

i'm very happy with the efficiency of this algorithm, i calculated the edit
distance of a 25 character string with 165 other strings of variable length
in .06 seconds. are there other alternatives that are a little more clever,
without getting into genetic programming?!

many thanks
tim
Jul 21 '05 #3
You could perhaps take into account the length by dividing the edit distance
by the length to get an "mean edit distance per letter" result.

Patrice

--

"Tim Mackey" <ti*@scootasp.net> a écrit dans le message de
news:31*************@individual.net...
hi Gerald
many thanks for the pointer. i've got a working prototype using a c#
levenshtein implementation, which is a good start. i think i need more
flexibility than what it provides though.
for example, i'm searching for a best guess for 'consortium', the results of EditDistance() are shown below.
result format: <EditDistance> <Compared With>:
6 contact
6 conor ryan
7 motorola
7 joe morris
8 bocse 2004
8 consortium members

it's clear that contact does require fewer character changes to change to
consortium, but the fact that "consortium members" has the exact word match in it should put it ahead of all the others. i can obviously start catering for manual cases like this, but it would be buggy and it probably take a
long time to get a generic solution.

i'm very happy with the efficiency of this algorithm, i calculated the edit distance of a 25 character string with 165 other strings of variable length in .06 seconds. are there other alternatives that are a little more clever, without getting into genetic programming?!

many thanks
tim

Jul 21 '05 #4
hi Patrice
i was skeptical to go starting my own extensions to the algorithm, because i
could keep adding special cases till the cows come home, but i did what you
suggested and it works great! for the record, in my situation, a value below
0.60 is quite good. thanks for giving me the tip.
i've included the algorithm here and a sample usage below it to save anyone
looking it up.
many thanks
tim
public class Levenshtein
{
/// <summary>
/// Compute Levenshtein distance.
/// http://www.merriampark.com/ld.htm
/// </summary>
/// <returns>Distance between the two strings.
/// The larger the number, the bigger the difference.
/// </returns>
public static int CalcEditDistance (string s, string t)
{
int n = s.Length; //length of s
int m = t.Length; //length of t
int[,] d = new int[n + 1, m + 1]; // matrix
int cost; // cost
// Step 1
if(n == 0) return m;
if(m == 0) return n;
// Step 2
for(int i = 0; i <= n; d[i, 0] = i++);
for(int j = 0; j <= m; d[0, j] = j++);
// Step 3
for(int i = 1; i <= n;i++)
{
//Step 4
for(int j = 1; j <= m;j++)
{
// Step 5
cost = (t.Substring(j - 1, 1) == s.Substring(i - 1, 1) ? 0 : 1);
// Step 6
d[i, j] = System.Math.Min(System.Math.Min(d[i - 1, j] + 1, d[i, j - 1] +
1),
d[i - 1, j - 1] + cost);
}
}
// Step 7
return d[n, m];
}
}

// sample usage:
double distance = Levenshtein.CalcEditDistance(s, input);
double meanDistancePerLetter = (distance / s.Length);
Jul 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: DNKMCA | last post by:
Hi, i was building a internet based search engine and heard of fuzzy search algorithms. Can anybody give fuzzy logic search algorithm for string search -dnk
3
by: michael.f.morrison | last post by:
Hi all, Does anyone now if there is a way to do a fuzzy string compare in c#? For example, comparing 'cat' to 'cats' would return something like a 90% match ratio. I was told that this type of ...
4
by: Tim Mackey | last post by:
hi, when a user reaches a 404 page not found on my web site, i want to give them one or more 'best guess' links to the page they are looking for (MS do this on their site...). i have a list of...
24
by: BBands | last post by:
I have some CDs and have been archiving them on a PC. I wrote a Python script that spans the archive and returns a list of its contents: ...]. I wanted to add a search function to locate all the...
24
by: cassetti | last post by:
Here's the issue: I have roughly 20 MS excel spreadsheets, each row contains a record. These records were hand entered by people in call centers. The problem is, there can and are duplicate...
11
by: John Henry | last post by:
I am just wondering what's with get_close_matches() in difflib. What's the magic? How fuzzy do I need to get in order to get a match?
14
by: Steve Bergman | last post by:
I'm looking for a module to do fuzzy comparison of strings. I have 2 item master files which are supposed to be identical, but they have thousands of records where the item numbers don't match in...
2
by: Pilcrow | last post by:
This problem was raised in comp.lang.perl.misc, and the poster was concerned, among other things, by the speed of execution. Since C is faster than perl, I wonder how a C coder would solve it? ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.