找到两个字符串之间的不同单词

Spe*_*dge 5 c# string comparison

我有很多短语,例如

"Nola jumped off the cliff"
"Loroy jumped off the cliff"
"Nola jumped off the couch"
"Leroy lept off the couch"
Run Code Online (Sandbox Code Playgroud)

我需要找到一个不同单词的短语中的每个点,并将该单词添加到节点,该节点是可以在短语中的该位置使用的单词列表.所以我们最终会结束.

"Node1(1) Node2(1) off the Node3(1)"
"Node1(2) Node2(1) off the Node3(1)"
...etc
Run Code Online (Sandbox Code Playgroud)

其中节点1表示名称列表(Nola,Leroy),node2表示动作列表(跳跃,lept),node3最终表示位置列表(悬崖,沙发)

我们的想法是获取短语列表,并让它自动创建节点,并用短语中可以在该节点上使用的单词填充它.

那么,我将如何生成短语节点列表?我无法弄清楚如何比较两个句子,看看它们是否完全相同,减去一个单词.

第二,一旦我设置了节点,比较所有节点组合以获得新匹配的最佳方法是什么?(希望有道理)

hot*_*S85 5

好的,我喜欢它.既然你用C#标记了你的问题,我也用C#写了答案.

快速获取两个短语之间的不同单词:

string phrase1 = "Nola jumped off the cliff";
string phrase2 = "Juri jumped off the coach";

//Split phrases into word arrays
var phrase1Words = phrase1.Split(' ');
var phrase2Words = phrase2.Split(' ');

//Find the intersection of the two arrays (find the matching words)
var wordsInPhrase1and2 = phrase1Words.Intersect(phrase2Words);

//The number of words that differ 
int wordDelta = phrase1Words.Count() - wordsInPhrase1and2.Count();

//Find the differing words
var wordsOnlyInPhrase1 = phrase1Words.Except(wordsInPhrase1and2);
var wordsOnlyInPhrase2 = phrase2Words.Except(wordsInPhrase1and2);
Run Code Online (Sandbox Code Playgroud)

不是通过循环和检查每个元素来自己匹配元素,而是可以节省自己的时间并使用内置的LINQ函数Intersect,Except等...

如需随机创建短语,请参阅NominSim的答案.