1

我基本上需要一些数学来根据以下指标对短输入句子进行排名:

1)术语相对于句子开头的距离(注意:相对术语距离,没有编辑距离!)。例如,搜索“a”应该给句子“a b”比“b a”更高的排名,因为 a 更接近句子的开头。

2)术语之间的距离。例如,搜索“a”和“b”应该将“ccc a b”的排名高于“a ccc b”,因为a 和b 彼此更接近。

3)基于术语的顺序排名。例如,搜索 a AND b 的“a b”排名应该高于“b a”,因为它是正确的顺序。尽管如此,ba 也应该在结果集中,因此它也必须按较低的权重进行排名。

4) 单词本身是未加权的。这是与广泛常见的内容以及我可以轻松找到信息的主要区别。但在我的情况下,所有术语都具有相同的权重,无论它们在文档中的出现/计数如何。

我已经完成了我的研究,但没有找到匹配项。你知道什么排名算法会匹配,或者至少接近这个吗?

4

1 回答 1

1
  1. 计算每个搜索词在主题字符串中的位置。
  2. 计算搜索字符串中所有词条的平均位置。
  3. 计算主题字符串和搜索词列表中的平均位置之间的绝对差。
  4. 计算术语位置相对于平均值的绝对差。
decimal Rank(string subject, IList<string> terms)
{
    // Isolate all the words in the subject.
    var words = Regex.Matches(subject, @"\w+")
        .Cast<Match>()
        .Select(m => m.Value.ToLower())
        .ToList();

    // Calculate the positions
    var positions = new List<int>();
    var sumPositions = 0;
    foreach (var term in terms)
    {
        int pos = words.IndexOf(term.ToLower());
        if (pos < 0) return decimal.MaxValue;
        positions.Add(pos);
        sumPositions += pos;
    }

    // Calculate the difference in average positions
    decimal averageSubject = (decimal) sumPositions / terms.Count;
    decimal averageTerms = (terms.Count - 1) / 2m; // average(0..n-1)
    decimal rank = Math.Abs(averageSubject - averageTerms);

    for (int i = 0; i < terms.Count; i++)
    {
        decimal relativePos1 = positions[i] - averageSubject;
        decimal relativePos2 = i - averageTerms;
        rank += Math.Abs(relativePos2 - relativePos1);
    }

    return rank;
}

我使用较低的值以获得更好的匹配,因为它比每次匹配的得分更容易测量与完美匹配的距离。

例子

Subject     Terms       Rank
"a b"       "a"         0.0
"b a"       "a"         1.0
"ccc a b"   "a", "b"    1.0
"a ccc b"   "a", "b"    1.5
"a b"       "a", "b"    0.0
"b a"       "a", "b"    2.0
于 2013-06-19T09:09:02.207 回答