在两个字符串之间找到不同的词 [英] Find different word between two strings

查看:101
本文介绍了在两个字符串之间找到不同的词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大的列表,如

"Nola jumped off the cliff"
"Loroy jumped off the cliff"
"Nola jumped off the couch"
"Leroy lept off the couch"

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

I need to find each point in a phrase that is a different word and add that word to a node, which is a list of words that can be used in that position in a phrase. So we would end up with.

"Node1(1) Node2(1) off the Node3(1)"
"Node1(2) Node2(1) off the Node3(1)"
...etc


$ b b

其中节点1代表名称列表(Nola,Leroy),node2代表动作列表(jumped,lept),node3代表位置列表(cliff,couch)

Where node 1 represents a list of the names(Nola,Leroy), node2 represents a list of the actions(jumped,lept) and node3 ends up representing the list of locations(cliff,couch)

这个想法是获取一个短语列表,并自动创建节点并用短语中该节点上可以使用的单词填充。

The idea is to take a list of the phrases, and have it automatically create the nodes and fill it with the words that can be used at that node in a phrase.

那么,第一个如何生成短语节点的列表?我没能找出如何比较两个句子,看看他们是否完全相同减去一个字。

So, 1st how would I generate the list of phrase nodes? I haven't been able to figure out how to compare two sentences and see if they are exactly alike minus one word.

第二次一旦我设置了节点,什么将是最好的方法来比较所有的节点组合以得出新的匹配? (希望有意义)

2nd once I have the nodes set up, what would be the best way to compare all the combinations of the nodes to come up with new matches? (hope that made sense)

推荐答案

很好的一个,我喜欢。

Nice one, I like it. Since you tagged your question with C#, I wrote the answer also in 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);

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

Instead of matching the elements yourself by looping over and checking each element, you can save yourself time and use the built-in LINQ functions Intersect, Except, etc...

要随机创建短语,请参考NominSim的答案。

For creating phrases by random, please refer to the answer of NominSim.

这篇关于在两个字符串之间找到不同的词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆