【发布时间】: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