3

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

例如 :

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

预期结果是:Patten 匹配

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

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

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

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

4

2 回答 2

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或使用最长公共子字符串算法创建单词差异。作为后者的示例,请参阅diff-lcs gem。

于 2013-10-24T05:12:57.620 回答
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.
于 2013-10-24T04:55:32.413 回答