开发者

Using C#, how can I replace similar words?

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


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#)


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 .


I'll Suggest a simpler answer. Compare the length of the 2 strings & also compare the sum of ASCII values of the both strings.


I'd use matlab to run some tests on this. I would do the follow

CONTROL 1111111

OCNTROL 0011111

ONRCTOL 0000111

So I have all 1s for original word, than I have five 1s in a second case and three 1s in a third. You can say that 70% is acceptable and if 70% match than I will use this word. OCNTROL will get accepted, but ONRCTOL won't.

I say Matlab because you can easily load a lot of data into vectors and do vector comparissons.


Linq Method: Try to store the chars in both the strings in two List<chars> or List<String> and compare (SequenceEqual or Except) the samller one with the bigger one.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜