我有一个字符串,其中包含我需要匹配的单词,如下所示,它是常量,另一个可以更改的字符串可能包含来自该字符串“s”的单词。
string s = "run fun play game do work click type";
string g = "he will be running. He is playing a game by clicking the mouse";
我希望能够匹配这些词并在 Windows 窗体应用程序文本框中将它们加粗。像 run 和 Running 匹配,所以 running 会变成粗体等等。 :-/有什么想法吗??
最佳答案
我会做的是使用词干分析器(例如 Porter 词干分析器),使用 split(' ') 拆分字符串并遍历每个字符串。比较两个词的词干版本,然后将匹配的词加粗。
foreach (string t1 in term1.split(' '){
foreach (string t2 in term2.split(' '){
if (Stemmer.Stem(t1).equals(Stemmer.Stem(t2)){
//do whatever here
}
}
}
关于包含源代码的 porter 词干分析器:
关于c# - C#中如何在两个字符串中查找相似的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8105370/