Skip to content

Commit

Permalink
feat(search): Sort search results by levenshtein distance
Browse files Browse the repository at this point in the history
  • Loading branch information
mmstick committed Jan 6, 2021
1 parent 3b05ab5 commit c0e4226
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 12 deletions.
30 changes: 18 additions & 12 deletions src/dialog_launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as result from 'result';
import * as search from 'dialog_search';
import * as launch from 'launcher_service';
import * as plugins from 'launcher_plugins';
import * as levenshtein from 'levenshtein';

import type { ShellWindow } from 'window';
import type { Ext } from 'extension';
Expand Down Expand Up @@ -90,15 +91,13 @@ export class Launcher extends search.Search {
return needles.every((n) => hay.includes(n));
};

let apps: Array<launch.SearchOption> = new Array();

// Filter matching windows
for (const window of ext.tab_list(Meta.TabList.NORMAL, null)) {
const retain = contains_pattern(window.name(ext), needles)
|| contains_pattern(window.meta.get_title(), needles);

if (retain) {
apps.push(window_selection(ext, window, this.icon_size()))
this.options.push(window_selection(ext, window, this.icon_size()))
}
}

Expand All @@ -113,7 +112,7 @@ export class Launcher extends search.Search {
if (retain) {
const generic = app.generic_name();

apps.push(new launch.SearchOption(
this.options.push(new launch.SearchOption(
app.name(),
generic ? generic + " — " + where : where,
'application-default-symbolic',
Expand All @@ -125,19 +124,26 @@ export class Launcher extends search.Search {
}

// Sort the list of matched selections
apps.sort((a, b) => {
const a_name = a.title.toLowerCase();
const b_name = b.title.toLowerCase();
this.options.sort((a, b) => {
const a_name = a.title.toLowerCase()
const b_name = b.title.toLowerCase()

const pattern_lower = pattern.toLowerCase()

const a_includes = a_name.includes(pattern_lower);
const b_includes = b_name.includes(pattern_lower);
let a_name_weight = levenshtein.compare(pattern_lower, a_name)

return ((a_includes && b_includes) || (!a_includes && !b_includes)) ? (a_name > b_name ? 1 : 0) : a_includes ? -1 : b_includes ? 1 : 0;
});
let b_name_weight = levenshtein.compare(pattern_lower, b_name)

if (a.description) {
a_name_weight = Math.min(a_name_weight, levenshtein.compare(pattern_lower, a.description.toLowerCase()))
}

for (const app of apps) this.options.push(app)
if (b.description) {
b_name_weight = Math.min(b_name_weight, levenshtein.compare(pattern_lower, b.description.toLowerCase()))
}

return a_name_weight > b_name_weight ? 1 : 0
});

// Truncate excess items from the list
this.options.splice(this.list_max());
Expand Down
100 changes: 100 additions & 0 deletions src/levenshtein.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* Based on https://github.com/gustf/js-levenshtein/blob/master/index.js
* MIT License
* Copyright (c) 2017 Gustaf Andersson
*/

function _min(d0: number, d1: number, d2: number, bx: number, ay: number): number {
return d0 < d1 || d2 < d1
? d0 > d2
? d2 + 1
: d0 + 1
: bx === ay
? d1
: d1 + 1
}

export function compare(a: string, b: string): number {
if (a === b) return 0

if (a.length > b.length) {
const tmp = a
a = b
b = tmp
}

let la = a.length
let lb = b.length

while (la > 0 && (a.charCodeAt(la - 1) === b.charCodeAt(lb - 1))) {
la--
lb--
}

let offset = 0

while (offset < la && (a.charCodeAt(offset) === b.charCodeAt(offset))) {
offset++
}

la -= offset
lb -= offset

if (la === 0 || lb < 3) return lb

let x = 0,
y = 0,
d0 = 0,
d1 = 0,
d2 = 0,
d3 = 0,
dd = 0,
dy = 0,
ay = 0,
bx0 = 0,
bx1 = 0,
bx2 = 0,
bx3 = 0

const vector = []

for (y = 0; y < la; y++) {
vector.push(y + 1)
vector.push(a.charCodeAt(offset + y))
}

let len = vector.length - 1

for (; x < lb - 3;) {
bx0 = b.charCodeAt(offset + (d0 = x))
bx1 = b.charCodeAt(offset + (d1 = x + 1))
bx2 = b.charCodeAt(offset + (d2 = x + 2))
bx3 = b.charCodeAt(offset + (d3 = x + 3))
dd = (x += 4)
for (y = 0; y < len; y += 2) {
dy = vector[y]
ay = vector[y + 1]
d0 = _min(dy, d0, d1, bx0, ay)
d1 = _min(d0, d1, d2, bx1, ay)
d2 = _min(d1, d2, d3, bx2, ay)
dd = _min(d2, d3, dd, bx3, ay)
vector[y] = dd
d3 = d2
d2 = d1
d1 = d0
d0 = dy
}
}

for (; x < lb;) {
bx0 = b.charCodeAt(offset + (d0 = x))
dd = ++x
for (y = 0; y < len; y += 2) {
dy = vector[y]
vector[y] = dd = _min(dy, d0, dd, bx0, vector[y + 1])
d0 = dy
}
}

return dd
}

0 comments on commit c0e4226

Please sign in to comment.