【问题标题】:Keystroke aggregation for live-search phrases实时搜索短语的击键聚合
【发布时间】:2020-04-05 03:09:52
【问题描述】:

作为网站上实时搜索的结果,我有一组字符串,例如:

[
  'how',
  'how do i',
  'how do i cancel my',
  'how do i cancel my account',
  'where is',
  'where is the',
  'where is the analytics',
  'where is the analytics page'
]

我需要应用一个编辑距离算法,只剩下两个“最终”短语:

[
  'how do i cancel my account',
  'where is the analytics page'
]

如果有任何关于实施的建议,我将不胜感激。

UPD:这将用于搜索分析,因此可能需要处理数万条记录。

UPD2:我最终采用了这种方法,它为我提供了稳定的>0.8 分数来过滤最终查询。我很想知道替代方案。 Jaro-Winkler similarity 算法似乎最合适,因为它优先考虑前导字符而不是尾随。

require 'edits'

values = [
  'how',
  'how do i',
  'how do i cancel my',
  'how do i cancel my account',
  'where is',
  'where is the',
  'where is the analytics',
  'where is the analytics page'
]

values.map(&:strip).uniq
  .each_cons(2)
  .map do |seq|
    [
      seq.first,
      seq.last,
      Edits::JaroWinkler.similarity(seq.first, seq.last)
    ]
  end
["how", "how do i", 0.8541666666666666]
["how do i", "how do i cancel my", 0.888888888888889]
["how do i cancel my", "how do i cancel my account", 0.9384615384615385]
["how do i cancel my account", "where is", 0.47243589743589737]
["where is", "where is the", 0.9333333333333333]
["where is the", "where is the analytics", 0.9090909090909091]
["where is the analytics", "where is the analytics page", 0.962962962962963]

【问题讨论】:

    标签: ruby record n-gram edit-distance record-linkage


    【解决方案1】:

    以下代码应删除前缀。

    require 'set'
    
    suggestions = Set.new([
      'how',
      'how do i',
      'how do i cancel my',
      'how do i cancel my account',
      'where is',
      'where is the',
      'where is the analytics',
      'where is the analytics page'
    ])
    phrases = suggestions.each do |a|
      suggestions.delete_if {|b| a != b && a.start_with?(b) }
    end
    
    phrases.to_a
    

    请注意,上述代码不适用于大型数组。但我想您不会从您的应用中获得超过 15 或 20 条建议(带前缀)。

    参考:Set#delete_if

    希望这会有所帮助。

    【讨论】:

    • 这将用于搜索分析,因此可能需要处理数万条记录,而数组过滤方法无法高效处理。
    • @Adam 啊,我刚刚看到您对输入大小的问题进行了编辑。让我看看我能不能想出一个性能更好的算法。
    猜你喜欢
    • 2012-10-29
    • 2012-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-29
    • 1970-01-01
    • 1970-01-01
    • 2016-03-31
    相关资源
    最近更新 更多