mirror of
https://github.com/AvengeMedia/DankMaterialShell.git
synced 2026-01-24 21:42:51 -05:00
launcher: new search algo
- replace fzf.js with custom levenshtein distance matching - tweak scoring system - more graceful fuzzy, more weight to prefixes - basic tokenization
This commit is contained in:
@@ -1,10 +1,9 @@
|
|||||||
pragma Singleton
|
pragma Singleton
|
||||||
|
|
||||||
pragma ComponentBehavior: Bound
|
pragma ComponentBehavior
|
||||||
|
|
||||||
import QtQuick
|
import QtQuick
|
||||||
import Quickshell
|
import Quickshell
|
||||||
import "../Common/fzf.js" as Fzf
|
|
||||||
import qs.Common
|
import qs.Common
|
||||||
|
|
||||||
Singleton {
|
Singleton {
|
||||||
@@ -12,7 +11,141 @@ Singleton {
|
|||||||
|
|
||||||
property var applications: DesktopEntries.applications.values.filter(app => !app.noDisplay && !app.runInTerminal)
|
property var applications: DesktopEntries.applications.values.filter(app => !app.noDisplay && !app.runInTerminal)
|
||||||
|
|
||||||
|
readonly property int maxResults: 10
|
||||||
|
readonly property int frecencySampleSize: 10
|
||||||
|
|
||||||
|
readonly property var timeBuckets: [{
|
||||||
|
"maxDays": 4,
|
||||||
|
"weight": 100
|
||||||
|
}, {
|
||||||
|
"maxDays": 14,
|
||||||
|
"weight": 70
|
||||||
|
}, {
|
||||||
|
"maxDays": 31,
|
||||||
|
"weight": 50
|
||||||
|
}, {
|
||||||
|
"maxDays": 90,
|
||||||
|
"weight": 30
|
||||||
|
}, {
|
||||||
|
"maxDays": 99999,
|
||||||
|
"weight": 10
|
||||||
|
}]
|
||||||
|
|
||||||
|
function tokenize(text) {
|
||||||
|
return text.toLowerCase().trim().split(/[\s\-_]+/).filter(w => w.length > 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
function wordBoundaryMatch(text, query) {
|
||||||
|
const textWords = tokenize(text)
|
||||||
|
const queryWords = tokenize(query)
|
||||||
|
|
||||||
|
if (queryWords.length === 0)
|
||||||
|
return false
|
||||||
|
if (queryWords.length > textWords.length)
|
||||||
|
return false
|
||||||
|
|
||||||
|
for (var i = 0; i <= textWords.length - queryWords.length; i++) {
|
||||||
|
let allMatch = true
|
||||||
|
for (var j = 0; j < queryWords.length; j++) {
|
||||||
|
if (!textWords[i + j].startsWith(queryWords[j])) {
|
||||||
|
allMatch = false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (allMatch)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
function levenshteinDistance(s1, s2) {
|
||||||
|
const len1 = s1.length
|
||||||
|
const len2 = s2.length
|
||||||
|
const matrix = []
|
||||||
|
|
||||||
|
for (var i = 0; i <= len1; i++) {
|
||||||
|
matrix[i] = [i]
|
||||||
|
}
|
||||||
|
for (var j = 0; j <= len2; j++) {
|
||||||
|
matrix[0][j] = j
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var i = 1; i <= len1; i++) {
|
||||||
|
for (var j = 1; j <= len2; j++) {
|
||||||
|
const cost = s1[i - 1] === s2[j - 1] ? 0 : 1
|
||||||
|
matrix[i][j] = Math.min(matrix[i - 1][j] + 1, matrix[i][j - 1] + 1, matrix[i - 1][j - 1] + cost)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return matrix[len1][len2]
|
||||||
|
}
|
||||||
|
|
||||||
|
function fuzzyMatchScore(text, query) {
|
||||||
|
const queryLower = query.toLowerCase()
|
||||||
|
const maxDistance = query.length <= 2 ? 0 : query.length === 3 ? 1 : query.length <= 6 ? 2 : 3
|
||||||
|
|
||||||
|
let bestScore = 0
|
||||||
|
|
||||||
|
const distance = levenshteinDistance(text.toLowerCase(), queryLower)
|
||||||
|
if (distance <= maxDistance) {
|
||||||
|
const maxLen = Math.max(text.length, query.length)
|
||||||
|
bestScore = 1 - (distance / maxLen)
|
||||||
|
}
|
||||||
|
|
||||||
|
const words = tokenize(text)
|
||||||
|
for (const word of words) {
|
||||||
|
const wordDistance = levenshteinDistance(word, queryLower)
|
||||||
|
if (wordDistance <= maxDistance) {
|
||||||
|
const maxLen = Math.max(word.length, query.length)
|
||||||
|
const score = 1 - (wordDistance / maxLen)
|
||||||
|
bestScore = Math.max(bestScore, score)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return bestScore
|
||||||
|
}
|
||||||
|
|
||||||
|
function calculateFrecency(app) {
|
||||||
|
const usageRanking = AppUsageHistoryData.appUsageRanking || {}
|
||||||
|
const appId = app.id || (app.execString || app.exec || "")
|
||||||
|
const idVariants = [appId, appId.replace(".desktop", ""), app.id, app.id ? app.id.replace(".desktop", "") : null].filter(id => id)
|
||||||
|
|
||||||
|
let usageData = null
|
||||||
|
for (const variant of idVariants) {
|
||||||
|
if (usageRanking[variant]) {
|
||||||
|
usageData = usageRanking[variant]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!usageData || !usageData.usageCount) {
|
||||||
|
return {
|
||||||
|
"frecency": 0,
|
||||||
|
"daysSinceUsed": 999999
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const usageCount = usageData.usageCount || 0
|
||||||
|
const lastUsed = usageData.lastUsed || 0
|
||||||
|
const now = Date.now()
|
||||||
|
const daysSinceUsed = (now - lastUsed) / (1000 * 60 * 60 * 24)
|
||||||
|
|
||||||
|
let timeBucketWeight = 10
|
||||||
|
for (const bucket of timeBuckets) {
|
||||||
|
if (daysSinceUsed <= bucket.maxDays) {
|
||||||
|
timeBucketWeight = bucket.weight
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const contextBonus = 100
|
||||||
|
const sampleSize = Math.min(usageCount, frecencySampleSize)
|
||||||
|
const frecency = (timeBucketWeight * contextBonus * sampleSize) / 100
|
||||||
|
|
||||||
|
return {
|
||||||
|
"frecency": frecency,
|
||||||
|
"daysSinceUsed": daysSinceUsed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function searchApplications(query) {
|
function searchApplications(query) {
|
||||||
if (!query || query.length === 0) {
|
if (!query || query.length === 0) {
|
||||||
@@ -23,7 +156,7 @@ Singleton {
|
|||||||
|
|
||||||
const queryLower = query.toLowerCase().trim()
|
const queryLower = query.toLowerCase().trim()
|
||||||
const scoredApps = []
|
const scoredApps = []
|
||||||
const usageRanking = AppUsageHistoryData.appUsageRanking || {}
|
const results = []
|
||||||
|
|
||||||
for (const app of applications) {
|
for (const app of applications) {
|
||||||
const name = (app.name || "").toLowerCase()
|
const name = (app.name || "").toLowerCase()
|
||||||
@@ -31,123 +164,83 @@ Singleton {
|
|||||||
const comment = (app.comment || "").toLowerCase()
|
const comment = (app.comment || "").toLowerCase()
|
||||||
const keywords = app.keywords ? app.keywords.map(k => k.toLowerCase()) : []
|
const keywords = app.keywords ? app.keywords.map(k => k.toLowerCase()) : []
|
||||||
|
|
||||||
let score = 0
|
let textScore = 0
|
||||||
let matched = false
|
let matchType = "none"
|
||||||
|
|
||||||
const nameWords = name.trim().split(/\s+/).filter(w => w.length > 0)
|
|
||||||
const containsAsWord = nameWords.includes(queryLower)
|
|
||||||
const startsWithAsWord = nameWords.some(word => word.startsWith(queryLower))
|
|
||||||
|
|
||||||
if (name === queryLower) {
|
if (name === queryLower) {
|
||||||
score = 10000
|
textScore = 10000
|
||||||
matched = true
|
matchType = "exact"
|
||||||
} else if (containsAsWord) {
|
|
||||||
score = 9500 + (100 - Math.min(name.length, 100))
|
|
||||||
matched = true
|
|
||||||
} else if (name.startsWith(queryLower)) {
|
} else if (name.startsWith(queryLower)) {
|
||||||
score = 9000 + (100 - Math.min(name.length, 100))
|
textScore = 5000
|
||||||
matched = true
|
matchType = "prefix"
|
||||||
} else if (startsWithAsWord) {
|
} else if (wordBoundaryMatch(name, queryLower)) {
|
||||||
score = 8500 + (100 - Math.min(name.length, 100))
|
textScore = 1000
|
||||||
matched = true
|
matchType = "word_boundary"
|
||||||
} else if (name.includes(queryLower)) {
|
} else if (name.includes(queryLower)) {
|
||||||
score = 8000 + (100 - Math.min(name.length, 100))
|
textScore = 500
|
||||||
matched = true
|
matchType = "substring"
|
||||||
} else if (keywords.length > 0) {
|
} else if (genericName && genericName.startsWith(queryLower)) {
|
||||||
|
textScore = 800
|
||||||
|
matchType = "generic_prefix"
|
||||||
|
} else if (genericName && genericName.includes(queryLower)) {
|
||||||
|
textScore = 400
|
||||||
|
matchType = "generic"
|
||||||
|
}
|
||||||
|
|
||||||
|
if (matchType === "none" && keywords.length > 0) {
|
||||||
for (const keyword of keywords) {
|
for (const keyword of keywords) {
|
||||||
if (keyword === queryLower) {
|
if (keyword.startsWith(queryLower)) {
|
||||||
score = 6000
|
textScore = 300
|
||||||
matched = true
|
matchType = "keyword_prefix"
|
||||||
break
|
|
||||||
} else if (keyword.startsWith(queryLower)) {
|
|
||||||
score = 5500
|
|
||||||
matched = true
|
|
||||||
break
|
break
|
||||||
} else if (keyword.includes(queryLower)) {
|
} else if (keyword.includes(queryLower)) {
|
||||||
score = 5000
|
textScore = 150
|
||||||
matched = true
|
matchType = "keyword"
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!matched && genericName.includes(queryLower)) {
|
|
||||||
if (genericName === queryLower) {
|
if (matchType === "none" && comment && comment.includes(queryLower)) {
|
||||||
score = 9000
|
textScore = 50
|
||||||
} else if (genericName.startsWith(queryLower)) {
|
matchType = "comment"
|
||||||
score = 8500
|
}
|
||||||
} else {
|
|
||||||
const genericWords = genericName.trim().split(/\s+/).filter(w => w.length > 0)
|
if (matchType === "none") {
|
||||||
if (genericWords.includes(queryLower)) {
|
const fuzzyScore = fuzzyMatchScore(name, queryLower)
|
||||||
score = 8000
|
if (fuzzyScore > 0) {
|
||||||
} else if (genericWords.some(word => word.startsWith(queryLower))) {
|
textScore = fuzzyScore * 100
|
||||||
score = 7500
|
matchType = "fuzzy"
|
||||||
} else {
|
|
||||||
score = 7000
|
|
||||||
}
|
|
||||||
}
|
|
||||||
matched = true
|
|
||||||
} else if (!matched && comment.includes(queryLower)) {
|
|
||||||
score = 3000
|
|
||||||
matched = true
|
|
||||||
} else if (!matched) {
|
|
||||||
const nameFinder = new Fzf.Finder([app], {
|
|
||||||
"selector": a => a.name || "",
|
|
||||||
"casing": "case-insensitive",
|
|
||||||
"fuzzy": "v2"
|
|
||||||
})
|
|
||||||
const fuzzyResults = nameFinder.find(query)
|
|
||||||
if (fuzzyResults.length > 0 && fuzzyResults[0].score > 0) {
|
|
||||||
score = Math.min(fuzzyResults[0].score, 2000)
|
|
||||||
matched = true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (matched) {
|
if (matchType !== "none") {
|
||||||
const appId = app.id || (app.execString || app.exec || "")
|
const frecencyData = calculateFrecency(app)
|
||||||
const idVariants = [
|
|
||||||
appId,
|
|
||||||
appId.replace(".desktop", ""),
|
|
||||||
app.id,
|
|
||||||
app.id ? app.id.replace(".desktop", "") : null
|
|
||||||
].filter(id => id)
|
|
||||||
|
|
||||||
let usageData = null
|
results.push({
|
||||||
for (const variant of idVariants) {
|
"app": app,
|
||||||
if (usageRanking[variant]) {
|
"textScore": textScore,
|
||||||
usageData = usageRanking[variant]
|
"frecency": frecencyData.frecency,
|
||||||
break
|
"daysSinceUsed": frecencyData.daysSinceUsed,
|
||||||
}
|
"matchType": matchType
|
||||||
}
|
})
|
||||||
|
|
||||||
if (usageData) {
|
|
||||||
const usageCount = usageData.usageCount || 0
|
|
||||||
const lastUsed = usageData.lastUsed || 0
|
|
||||||
const now = Date.now()
|
|
||||||
const daysSinceUsed = (now - lastUsed) / (1000 * 60 * 60 * 24)
|
|
||||||
|
|
||||||
let usageBonus = 0
|
|
||||||
usageBonus += Math.min(usageCount * 100, 2000)
|
|
||||||
|
|
||||||
if (daysSinceUsed < 1) {
|
|
||||||
usageBonus += 1500
|
|
||||||
} else if (daysSinceUsed < 7) {
|
|
||||||
usageBonus += 1000
|
|
||||||
} else if (daysSinceUsed < 30) {
|
|
||||||
usageBonus += 500
|
|
||||||
}
|
|
||||||
|
|
||||||
score += usageBonus
|
|
||||||
}
|
|
||||||
|
|
||||||
scoredApps.push({
|
|
||||||
"app": app,
|
|
||||||
"score": score
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (const result of results) {
|
||||||
|
const frecencyBonus = result.frecency > 0 ? Math.min(result.frecency / 10, 2000) : 0
|
||||||
|
const recencyBonus = result.daysSinceUsed < 1 ? 1500 : result.daysSinceUsed < 7 ? 1000 : result.daysSinceUsed < 30 ? 500 : 0
|
||||||
|
|
||||||
|
const finalScore = result.textScore + frecencyBonus + recencyBonus
|
||||||
|
|
||||||
|
scoredApps.push({
|
||||||
|
"app": result.app,
|
||||||
|
"score": finalScore
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
scoredApps.sort((a, b) => b.score - a.score)
|
scoredApps.sort((a, b) => b.score - a.score)
|
||||||
return scoredApps.slice(0, 50).map(item => item.app)
|
return scoredApps.slice(0, maxResults).map(item => item.app)
|
||||||
}
|
}
|
||||||
|
|
||||||
function getCategoriesForApp(app) {
|
function getCategoriesForApp(app) {
|
||||||
@@ -265,7 +358,8 @@ Singleton {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getPluginCategoryIcon(category) {
|
function getPluginCategoryIcon(category) {
|
||||||
if (typeof PluginService === "undefined") return null
|
if (typeof PluginService === "undefined")
|
||||||
|
return null
|
||||||
|
|
||||||
const launchers = PluginService.getLauncherPlugins()
|
const launchers = PluginService.getLauncherPlugins()
|
||||||
for (const pluginId in launchers) {
|
for (const pluginId in launchers) {
|
||||||
@@ -295,7 +389,8 @@ Singleton {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getPluginItems(category, query) {
|
function getPluginItems(category, query) {
|
||||||
if (typeof PluginService === "undefined") return []
|
if (typeof PluginService === "undefined")
|
||||||
|
return []
|
||||||
|
|
||||||
const launchers = PluginService.getLauncherPlugins()
|
const launchers = PluginService.getLauncherPlugins()
|
||||||
for (const pluginId in launchers) {
|
for (const pluginId in launchers) {
|
||||||
@@ -313,12 +408,13 @@ Singleton {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const component = PluginService.pluginLauncherComponents[pluginId]
|
const component = PluginService.pluginLauncherComponents[pluginId]
|
||||||
if (!component) return []
|
if (!component)
|
||||||
|
return []
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const instance = component.createObject(root, {
|
const instance = component.createObject(root, {
|
||||||
"pluginService": PluginService
|
"pluginService": PluginService
|
||||||
})
|
})
|
||||||
|
|
||||||
if (instance && typeof instance.getItems === "function") {
|
if (instance && typeof instance.getItems === "function") {
|
||||||
const items = instance.getItems(query || "")
|
const items = instance.getItems(query || "")
|
||||||
@@ -337,15 +433,17 @@ Singleton {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function executePluginItem(item, pluginId) {
|
function executePluginItem(item, pluginId) {
|
||||||
if (typeof PluginService === "undefined") return false
|
if (typeof PluginService === "undefined")
|
||||||
|
return false
|
||||||
|
|
||||||
const component = PluginService.pluginLauncherComponents[pluginId]
|
const component = PluginService.pluginLauncherComponents[pluginId]
|
||||||
if (!component) return false
|
if (!component)
|
||||||
|
return false
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const instance = component.createObject(root, {
|
const instance = component.createObject(root, {
|
||||||
"pluginService": PluginService
|
"pluginService": PluginService
|
||||||
})
|
})
|
||||||
|
|
||||||
if (instance && typeof instance.executeItem === "function") {
|
if (instance && typeof instance.executeItem === "function") {
|
||||||
instance.executeItem(item)
|
instance.executeItem(item)
|
||||||
@@ -364,7 +462,8 @@ Singleton {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function searchPluginItems(query) {
|
function searchPluginItems(query) {
|
||||||
if (typeof PluginService === "undefined") return []
|
if (typeof PluginService === "undefined")
|
||||||
|
return []
|
||||||
|
|
||||||
let allItems = []
|
let allItems = []
|
||||||
const launchers = PluginService.getLauncherPlugins()
|
const launchers = PluginService.getLauncherPlugins()
|
||||||
|
|||||||
Reference in New Issue
Block a user