【问题标题】:Similarity of two ORDERED collections [duplicate]两个 ORDERED 集合的相似性 [重复]
【发布时间】:2012-10-10 18:53:21
【问题描述】:

我有一个有序的集合:

[Doc1, Doc2, Doc3, Doc4, Doc5] 

Doc1 排在Doc2 之前(想象一下搜索查询情况,这个有序集合是搜索的结果。

现在,假设我有第二个有序集合:

[Doc1, Doc2, Doc3, Doc5, Doc4]

我需要一种将这种差异量化为数字分数的方法。还必须考虑权重,这样[Doc1, Doc2, Doc3, Doc5, Doc4]更接近原始集合,然后[Doc2, Doc1, Doc3, Doc4, Doc5]更接近,因为差异发生在更接近顶部。

我已经考虑了 Levenshtein 差异,但不知道如何考虑顺序。

【问题讨论】:

  • 在什么基础上确定这种紧密的排序方式?
  • 不太清楚你的意思抱歉。
  • 您可能正在寻找类似DCG 或nDCG 的内容。我在this thread 中详细阐述过,我相信这基本上是一个相同的问题。

标签: algorithm language-agnostic levenshtein-distance


【解决方案1】:

According to Wikipedia Levenshtein 距离可以使用以下伪代码计算。

int LevenshteinDistance(string s, string t)
{
  int len_s = length(s), len_t = length(t), cost = 0;
  if (s[0] != t[0])
    cost = 1;
  if (len_s == 0)
    return len_t;
  else if (len_t == 0)
    return len_s;
  else
    return minimum(
        LevenshteinDistance(s[1..len_s], t) + 1,
        LevenshteinDistance(s, t[1..len_t]) + 1,
        LevenshteinDistance(s[1..len_s], t[1..len_t]) + cost);
}

如果我正确理解您的要求,您希望集合开始时的差异比结束时的差异更重要。让我们调整这个递归函数来反映这种需求。

float LevenshteinDistance(string s, string t, float decay)
{
  int len_s = length(s), len_t = length(t), cost = 0;
  if (s[0] != t[0])
    cost = 1;
  if (len_s == 0)
    return len_t;
  else if (len_t == 0)
    return len_s;
  else
    return decay * minimum(
        LevenshteinDistance(s[1..len_s], t, decay) + 1,
        LevenshteinDistance(s, t[1..len_t], decay) + 1,
        LevenshteinDistance(s[1..len_s], t[1..len_t], decay) + cost);
}

当decay 是属于区间 (0,1) 的参数时,较大索引上的差异变得不如以前的差异显着。

这是decay=0.9 的示例。

s       t       dist
"1234"  "1234"  0.0000
"1234"  "1243"  1.3851
"1234"  "2134"  1.6290

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-26
    • 2020-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-17
    相关资源
    最近更新 更多