如何有效地比较两个句子,并说两者都是平等的

时间:2013-10-23 08:56:17

标签: pattern-recognition

我想比较两个句子。我想要模式匹配技术

例如:

  The first thing you will do is choose a topic
  Vs
  The first thing you will do is choose a topic

预期结果是:Patten matching

  The first thing  will do is choose a topic
  Vs
  The first thing you will do is choose  topic

在这种情况下,patten匹配,但有一些错误。

这是一个简单的例子,我必须匹配复杂句子的模式。

我谷歌这个并得到点矩阵方法。这是正确的申请吗?任何其他方法都可以找出两个句子是否相互匹配。

2 个答案:

答案 0 :(得分:0)

我在之前的一个项目中遇到过类似的问题,我通过以下算法解决了这个问题。

I applied the "Longest Common Substring" algorithm and founded the longest common substring between the two strings.

Then I used "Levenshtein Distance algorithm" to compare my String A with the "Longest Common Substring" found from step 1.

If the result available from the algorithm mentioned in step 2 is above certain threshold, then it implies that the string A and String B matches.

答案 1 :(得分:0)

假设你已经有办法解析句子,而你只关心句子是否相同而不是 它们可能有什么不同,那么你可以简单地寻找字符串相等。考虑一下这个Ruby方法:

def sentences_eql? sentence_one, sentence_two
  sentence_one == sentence_two
end

当您为此方法提供一对句子时,您将获得基于字符串比较的布尔结果。例如:

sentences_eql? 'The first thing you will do is choose a topic.',
               'The first thing you will do is choose a topic.'
#=> true

sentences_eql? 'The first thing you will do is choose a topic.',
               'The first thing you will do is choose   topic.'
#=> false

如果您关心差异的实际细节,可以使用Levenshtein Distance或使用最长公共子串算法创建word-diff。作为后者的示例,请参阅diff-lcs gem。