From eb5ca1125ba043fb83c3a5ae8182ec48aff85c1b Mon Sep 17 00:00:00 2001 From: kotontrion Date: Fri, 11 Oct 2024 11:22:26 +0200 Subject: apps: better fuzzy search algorithm --- lib/apps/application.vala | 50 ++++++++--------------------------------------- 1 file changed, 8 insertions(+), 42 deletions(-) (limited to 'lib/apps/application.vala') diff --git a/lib/apps/application.vala b/lib/apps/application.vala index 5748fc6..75ff6b2 100644 --- a/lib/apps/application.vala +++ b/lib/apps/application.vala @@ -32,13 +32,13 @@ public class Application : Object { public Score fuzzy_match(string term) { var score = Score(); if (name != null) - score.name = levenshtein(term, name); + score.name = fuzzy_match_string(term, name); if (entry != null) - score.entry = levenshtein(term, entry); + score.entry = fuzzy_match_string(term, entry); if (executable != null) - score.executable = levenshtein(term, executable); + score.executable = fuzzy_match_string(term, executable); if (description != null) - score.description = levenshtein(term, description); + score.description = fuzzy_match_string(term, description); return score; } @@ -75,44 +75,10 @@ int min3(int a, int b, int c) { return (a < b) ? ((a < c) ? a : c) : ((b < c) ? b : c); } -double levenshtein(string s1, string s2) { - int len1 = s1.length; - int len2 = s2.length; - - int[, ] d = new int[len1 + 1, len2 + 1]; - - for (int i = 0; i <= len1; i++) { - d[i, 0] = i; - } - for (int j = 0; j <= len2; j++) { - d[0, j] = j; - } - - for (int i = 1; i <= len1; i++) { - for (int j = 1; j <= len2; j++) { - int cost = (s1[i - 1] == s2[j - 1]) ? 0 : 1; - d[i, j] = min3( - d[i - 1, j] + 1, // deletion - d[i, j - 1] + 1, // insertion - d[i - 1, j - 1] + cost // substitution - ); - } - } - - var distance = d[len1, len2]; - int max_len = len1 > len2 ? len1 : len2; - - if (max_len == 0) { - return 1.0; - } - - return 1.0 - ((double)distance / max_len); -} - public struct Score { - double name; - double entry; - double executable; - double description; + int name; + int entry; + int executable; + int description; } } -- cgit v1.2.3 From 0cb8733f31defcf2f7158d23c058fbb92a580215 Mon Sep 17 00:00:00 2001 From: Aylur Date: Tue, 15 Oct 2024 18:58:12 +0000 Subject: docs: apps doc comments docs: apps doc comments --- lib/apps/application.vala | 55 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 10 deletions(-) (limited to 'lib/apps/application.vala') diff --git a/lib/apps/application.vala b/lib/apps/application.vala index 75ff6b2..7e20b06 100644 --- a/lib/apps/application.vala +++ b/lib/apps/application.vala @@ -1,12 +1,45 @@ -namespace AstalApps { -public class Application : Object { +public class AstalApps.Application : Object { + /** + * The underlying DesktopAppInfo. + */ public DesktopAppInfo app { get; construct set; } + + /** + * The number of times [func@AstalApps.Application.launch] was called on this Application. + */ public int frequency { get; set; default = 0; } + + /** + * The name of this Application. + */ public string name { get { return app.get_name(); } } + + /** + * Name of the .desktop of this Application. + */ public string entry { get { return app.get_id(); } } + + /** + * Description of this Application. + */ public string description { get { return app.get_description(); } } + + /** + * The StartupWMClass field from the desktop file. + * This represents the WM_CLASS property of the main window of the application. + */ public string wm_class { get { return app.get_startup_wm_class(); } } + + /** + * `Exec` field from the desktop file. + * Note that if you want to launch this Application you should use the [func@AstalApps.Application.launch] method. + */ public string executable { owned get { return app.get_string("Exec"); } } + + /** + * `Icon` field from the desktop file. + * This is usually a named icon or a path to a file. + */ public string icon_name { owned get { return app.get_string("Icon"); } } internal Application(string id, int? frequency = 0) { @@ -14,10 +47,17 @@ public class Application : Object { this.frequency = frequency; } + /** + * Get a value from the .desktop file by its key. + */ public string get_key(string key) { return app.get_string(key); } + /** + * Launches this application. + * The launched application inherits the environment of the launching process + */ public bool launch() { try { var s = app.launch(null, null); @@ -29,7 +69,7 @@ public class Application : Object { } } - public Score fuzzy_match(string term) { + internal Score fuzzy_match(string term) { var score = Score(); if (name != null) score.name = fuzzy_match_string(term, name); @@ -43,7 +83,7 @@ public class Application : Object { return score; } - public Score exact_match(string term) { + internal Score exact_match(string term) { var score = Score(); if (name != null) score.name = name.down().contains(term.down()) ? 1 : 0; @@ -71,14 +111,9 @@ public class Application : Object { } } -int min3(int a, int b, int c) { - return (a < b) ? ((a < c) ? a : c) : ((b < c) ? b : c); -} - -public struct Score { +public struct AstalApps.Score { int name; int entry; int executable; int description; } -} -- cgit v1.2.3 From 02fdcbe57155bd62c632a75e08087177b7c66eb9 Mon Sep 17 00:00:00 2001 From: Aylur Date: Tue, 22 Oct 2024 22:27:42 +0000 Subject: docs: fixup notifd and apps --- lib/apps/application.vala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/apps/application.vala') diff --git a/lib/apps/application.vala b/lib/apps/application.vala index 7e20b06..ea22e7a 100644 --- a/lib/apps/application.vala +++ b/lib/apps/application.vala @@ -5,7 +5,7 @@ public class AstalApps.Application : Object { public DesktopAppInfo app { get; construct set; } /** - * The number of times [func@AstalApps.Application.launch] was called on this Application. + * The number of times [method@AstalApps.Application.launch] was called on this Application. */ public int frequency { get; set; default = 0; } @@ -32,7 +32,7 @@ public class AstalApps.Application : Object { /** * `Exec` field from the desktop file. - * Note that if you want to launch this Application you should use the [func@AstalApps.Application.launch] method. + * Note that if you want to launch this Application you should use the [method@AstalApps.Application.launch] method. */ public string executable { owned get { return app.get_string("Exec"); } } -- cgit v1.2.3 From e6250c7a4366da033d98a67b426ae5e08aa6f712 Mon Sep 17 00:00:00 2001 From: Aylur Date: Thu, 24 Oct 2024 20:25:33 +0000 Subject: lib(apps): make scoring mechanism public resolves #54 --- lib/apps/application.vala | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'lib/apps/application.vala') diff --git a/lib/apps/application.vala b/lib/apps/application.vala index ea22e7a..4b4fc44 100644 --- a/lib/apps/application.vala +++ b/lib/apps/application.vala @@ -69,7 +69,10 @@ public class AstalApps.Application : Object { } } - internal Score fuzzy_match(string term) { + /** + * Calculate a score for an application using fuzzy matching algorithm. + */ + public Score fuzzy_match(string term) { var score = Score(); if (name != null) score.name = fuzzy_match_string(term, name); @@ -83,7 +86,10 @@ public class AstalApps.Application : Object { return score; } - internal Score exact_match(string term) { + /** + * Calculate a score using exact string algorithm. + */ + public Score exact_match(string term) { var score = Score(); if (name != null) score.name = name.down().contains(term.down()) ? 1 : 0; -- cgit v1.2.3 From 61727b01ba06f876664addca83e774cb2bad9ce9 Mon Sep 17 00:00:00 2001 From: matteo4375 <160355435+matteo4375@users.noreply.github.com> Date: Thu, 24 Oct 2024 17:39:38 -0400 Subject: apps: add option to include keywords in queries (#56) --- lib/apps/application.vala | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) (limited to 'lib/apps/application.vala') diff --git a/lib/apps/application.vala b/lib/apps/application.vala index 4b4fc44..c137183 100644 --- a/lib/apps/application.vala +++ b/lib/apps/application.vala @@ -42,6 +42,11 @@ public class AstalApps.Application : Object { */ public string icon_name { owned get { return app.get_string("Icon"); } } + /** + * `Keywords` field from the desktop file. + */ + public string[] keywords { owned get { return app.get_keywords(); } } + internal Application(string id, int? frequency = 0) { Object(app: new DesktopAppInfo(id)); this.frequency = frequency; @@ -74,6 +79,7 @@ public class AstalApps.Application : Object { */ public Score fuzzy_match(string term) { var score = Score(); + if (name != null) score.name = fuzzy_match_string(term, name); if (entry != null) @@ -82,6 +88,12 @@ public class AstalApps.Application : Object { score.executable = fuzzy_match_string(term, executable); if (description != null) score.description = fuzzy_match_string(term, description); + foreach (var keyword in keywords) { + var s = fuzzy_match_string(term, keyword); + if (s > score.keywords) { + score.keywords = s; + } + } return score; } @@ -91,6 +103,7 @@ public class AstalApps.Application : Object { */ public Score exact_match(string term) { var score = Score(); + if (name != null) score.name = name.down().contains(term.down()) ? 1 : 0; if (entry != null) @@ -99,12 +112,17 @@ public class AstalApps.Application : Object { score.executable = executable.down().contains(term.down()) ? 1 : 0; if (description != null) score.description = description.down().contains(term.down()) ? 1 : 0; + foreach (var keyword in keywords) { + if (score.keywords == 0) { + score.keywords = keyword.down().contains(term.down()) ? 1 : 0; + } + } return score; } internal Json.Node to_json() { - return new Json.Builder() + var builder = new Json.Builder() .begin_object() .set_member_name("name").add_string_value(name) .set_member_name("entry").add_string_value(entry) @@ -112,6 +130,15 @@ public class AstalApps.Application : Object { .set_member_name("description").add_string_value(description) .set_member_name("icon_name").add_string_value(icon_name) .set_member_name("frequency").add_int_value(frequency) + .set_member_name("keywords") + .begin_array(); + + foreach (string keyword in keywords) { + builder.add_string_value(keyword); + } + + return builder + .end_array() .end_object() .get_root(); } @@ -122,4 +149,5 @@ public struct AstalApps.Score { int entry; int executable; int description; + int keywords; } -- cgit v1.2.3 From e8715aec5e05e0438192e611afea2fe6f10cb80f Mon Sep 17 00:00:00 2001 From: Aylur Date: Fri, 25 Oct 2024 14:09:04 +0000 Subject: docs: battery doc comments --- lib/apps/application.vala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/apps/application.vala') diff --git a/lib/apps/application.vala b/lib/apps/application.vala index c137183..0a2f73c 100644 --- a/lib/apps/application.vala +++ b/lib/apps/application.vala @@ -25,8 +25,8 @@ public class AstalApps.Application : Object { public string description { get { return app.get_description(); } } /** - * The StartupWMClass field from the desktop file. - * This represents the WM_CLASS property of the main window of the application. + * `StartupWMClass` field from the desktop file. + * This represents the `WM_CLASS` property of the main window of the application. */ public string wm_class { get { return app.get_startup_wm_class(); } } -- cgit v1.2.3