Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using C#, how can I replace similar words?

Tags:

string

c#

Assuming these two strings:

string s1="control";
string s2="conrol"; (or "ocntrol", "onrtol", "lcontro" etc.)

How can I programatically find that s2 is similar with s1 and replace the s2 string with the s1 string?

Thanks.

Jeff

like image 392
Jeff Norman Avatar asked Oct 12 '10 08:10

Jeff Norman


2 Answers

You could try to check the Levenshtein distance between your two words and if the distance is beyond a threshold, replace the word.

The hard part is defining the threshold, in your examples a threshold of 2 could work.

(Implementation of Levenshtein distance in C#)

like image 92
Julien Hoarau Avatar answered Nov 05 '22 03:11

Julien Hoarau


You can use Levenshtein Distance which would give you a rank on how close the two words are. You need to decide at which rank you do the replace .

like image 44
Shay Erlichmen Avatar answered Nov 05 '22 03:11

Shay Erlichmen