对我的列表歌曲搜索结果进行相似度排序

pull/277/head
lyswhut 2020-07-25 19:36:46 +08:00
parent e985e70303
commit 85c3a812da
1 changed files with 63 additions and 1 deletions

View File

@ -28,6 +28,68 @@ div(:class="$style.container" ref="dom_container" v-show="isShow")
import { clipboardReadText, debounce, scrollTo } from '../../utils' import { clipboardReadText, debounce, scrollTo } from '../../utils'
let canceleFn let canceleFn
// https://blog.csdn.net/xcxy2015/article/details/77164126#comments
const similar = (a, b) => {
if (!a || !b) return 0
if (a.length > b.length) { // a <= b
let t = b
b = a
a = t
}
let al = a.length
let bl = b.length
let mp = [] //
let i, j, ai, lt, tmp // aiai lt tmp
for (i = 0; i <= bl; i++) mp[i] = i
for (i = 1; i <= al; i++) {
ai = a.charAt(i - 1)
lt = mp[0]
mp[0] = mp[0] + 1
for (j = 1; j <= bl; j++) {
tmp = Math.min(mp[j] + 1, mp[j - 1] + 1, lt + (ai == b.charAt(j - 1) ? 0 : 1))
lt = mp[j]
mp[j] = tmp
}
}
return 1 - (mp[bl] / bl)
}
const sortInsert = (arr, data) => {
let key = data.num
let left = 0
let right = arr.length - 1
while (left <= right) {
let middle = parseInt((left + right) / 2)
if (key == arr[middle]) {
left = middle
break
} else if (key < arr[middle].num) {
right = middle - 1
} else {
left = middle + 1
}
}
while (left > 0) {
if (arr[left - 1].num != key) break
left--
}
arr.splice(left, 0, data)
}
const handleSortList = (list, keyword) => {
let arr = []
for (const item of list) {
sortInsert(arr, {
num: similar(keyword, `${item.name} ${item.singer} ${item.albumName || ''}`),
data: item,
})
}
return arr.map(item => item.data).reverse()
}
export default { export default {
props: { props: {
placeholder: { placeholder: {
@ -181,7 +243,7 @@ export default {
for (const item of this.list) { for (const item of this.list) {
if (rxp.test(`${item.name}${item.singer}${item.albumName ? item.albumName : ''}`)) list.push(item) if (rxp.test(`${item.name}${item.singer}${item.albumName ? item.albumName : ''}`)) list.push(item)
} }
this.resultList = list this.resultList = handleSortList(list, this.text)
}, },
}, },
} }