Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

typeahead: Prefer word boundary matches to arbitrary substring matches.

N-Shar-maafba77300aac
diff --git a/web/shared/src/typeahead.ts b/web/shared/src/typeahead.ts
index 53a3ca7554c..3d293c57748 100644
--- a/web/shared/src/typeahead.ts
+++ b/web/shared/src/typeahead.ts
@@ -1,3 +1,5 @@
+import _ from "lodash";
+
/*
We hand selected the following emojis a few years
ago to be given extra precedence in our typeahead
@@ -110,27 +112,37 @@ export function get_emoji_matcher(query: string): (emoji: Emoji) => boolean {
};
}
-export function triage<T>(
+// space, hyphen, underscore and slash characters are considered word
+// boundaries for now, but we might want to consider the characters
+// from BEFORE_MENTION_ALLOWED_REGEX in zerver/lib/mention.py later.
+export const word_boundary_chars = " _/-";
+
+export function triage_raw<T>(
query: string,
objs: T[],
get_item: (x: T) => string,
- sorting_comparator?: (a: T, b: T) => number,
-): {matches: T[]; rest: T[]} {
+): {
+ exact_matches: T[];
+ begins_with_case_sensitive_matches: T[];
+ begins_with_case_insensitive_matches: T[];
+ word_boundary_matches: T[];
+ no_matches: T[];
+} {
/*
- We split objs into four groups:
+ We split objs into five groups:
- entire string exact match
- match prefix exactly with `query`
- match prefix case-insensitively
+ - match word boundary prefix case-insensitively
- other
- Then we concat the first three groups into
- `matches` and then call the rest `rest`.
+ and return an object of these.
*/
-
const exact_matches = [];
const begins_with_case_sensitive_matches = [];
const begins_with_case_insensitive_matches = [];
+ const word_boundary_matches = [];
const no_matches = [];
const lower_query = query ? query.toLowerCase() : "";
@@ -144,26 +156,59 @@ export function triage<T>(
begins_with_case_sensitive_matches.push(obj);
} else if (lower_item.startsWith(lower_query)) {
begins_with_case_insensitive_matches.push(obj);
+ } else if (
+ new RegExp(`[${word_boundary_chars}]${_.escapeRegExp(lower_query)}`).test(lower_item)
+ ) {
+ word_boundary_matches.push(obj);
} else {
no_matches.push(obj);
}
}
+ return {
+ exact_matches,
+ begins_with_case_sensitive_matches,
+ begins_with_case_insensitive_matches,
+ word_boundary_matches,
+ no_matches,
+ };
+}
+
+export function triage<T>(
+ query: string,
+ objs: T[],
+ get_item: (x: T) => string,
+ sorting_comparator?: (a: T, b: T) => number,
+): {matches: T[]; rest: T[]} {
+ const {
+ exact_matches,
+ begins_with_case_sensitive_matches,
+ begins_with_case_insensitive_matches,
+ word_boundary_matches,
+ no_matches,
+ } = triage_raw(query, objs, get_item);
+
if (sorting_comparator) {
- const non_exact_sorted_matches = [
+ const beginning_matches_sorted = [
...begins_with_case_sensitive_matches,
...begins_with_case_insensitive_matches,
].sort(sorting_comparator);
return {
- matches: [...exact_matches.sort(sorting_comparator), ...non_exact_sorted_matches],
+ matches: [
+ ...exact_matches.sort(sorting_comparator),
+ ...beginning_matches_sorted,
+ ...word_boundary_matches.sort(sorting_comparator),
+ ],
rest: no_matches.sort(sorting_comparator),
};
}
+
return {
matches: [
...exact_matches,
...begins_with_case_sensitive_matches,
...begins_with_case_insensitive_matches,
+ ...word_boundary_matches,
],
rest: no_matches,
};
diff --git a/web/src/typeahead_helper.ts b/web/src/typeahead_helper.ts
index 62238d0fba4..b1650825bb9 100644
--- a/web/src/typeahead_helper.ts
+++ b/web/src/typeahead_helper.ts
@@ -421,17 +421,45 @@ export function sort_recipients({
return sort_people_for_relevance(items, current_stream_id, current_topic);
}
- const users_name_results = typeahead.triage(query, users, (p) => p.full_name);
+ const users_name_results = typeahead.triage_raw(query, users, (p) => p.full_name);
+ const users_name_good_matches = [
+ ...users_name_results.exact_matches,
+ ...users_name_results.begins_with_case_sensitive_matches,
+ ...users_name_results.begins_with_case_insensitive_matches,
+ ];
+ const users_name_okay_matches = [...users_name_results.word_boundary_matches];
- const email_results = typeahead.triage(query, users_name_results.rest, (p) => p.email);
+ const email_results = typeahead.triage_raw(
+ query,
+ users_name_results.no_matches,
+ (p) => p.email,
+ );
+ const email_good_matches = [
+ ...email_results.exact_matches,
+ ...email_results.begins_with_case_sensitive_matches,
+ ...email_results.begins_with_case_insensitive_matches,
+ ];
+ const email_okay_matches = [...email_results.word_boundary_matches];
- const groups_results = typeahead.triage(query, groups, (g) => g.name);
+ const groups_results = typeahead.triage_raw(query, groups, (g) => g.name);
+ const groups_good_matches = [
+ ...groups_results.exact_matches,
+ ...groups_results.begins_with_case_sensitive_matches,
+ ...groups_results.begins_with_case_insensitive_matches,
+ ];
+ const groups_okay_matches = [...groups_results.word_boundary_matches];
- const best_users = (): UserOrMention[] => sort_relevance(users_name_results.matches);
- const best_groups = (): UserGroup[] => groups_results.matches;
- const ok_users = (): UserOrMention[] => sort_relevance(email_results.matches);
- const worst_users = (): UserOrMention[] => sort_relevance(email_results.rest);
- const worst_groups = (): UserGroup[] => groups_results.rest;
+ const best_users = (): UserOrMention[] => [
+ ...sort_relevance(users_name_good_matches),
+ ...sort_relevance(users_name_okay_matches),
+ ];
+ const best_groups = (): UserGroup[] => [...groups_good_matches, ...groups_okay_matches];
+ const ok_users = (): UserOrMention[] => [
+ ...sort_relevance(email_good_matches),
+ ...sort_relevance(email_okay_matches),
+ ];
+ const worst_users = (): UserOrMention[] => sort_relevance(email_results.no_matches);
+ const worst_groups = (): UserGroup[] => groups_results.no_matches;
const getters: (
| {
diff --git a/web/tests/composebox_typeahead.test.js b/web/tests/composebox_typeahead.test.js
index f99af30eabb..d4b95d56c22 100644
--- a/web/tests/composebox_typeahead.test.js
+++ b/web/tests/composebox_typeahead.test.js
@@ -1805,11 +1805,9 @@ test("message people", ({override, override_rewire}) => {
let results;
/*
- We will simulate that we talk to Hal and Harry,
- while we don't talk to King Hamlet. This will
- knock King Hamlet out of consideration in the
- filtering pass. Then Hal will be truncated in
- the sorting step.
+ We will initially simulate that we talk to Hal and Harry, while
+ we don't talk to King Hamlet or Characters of Hamlet. This
+ will knock these 2 out of consideration in the filtering pass.
*/
let user_ids = [hal.user_id, harry.user_id];
@@ -1823,23 +1821,20 @@ test("message people", ({override, override_rewire}) => {
};
results = ct.get_person_suggestions("Ha", opts);
- assert.deepEqual(results, [harry, hamletcharacters]);
+ assert.deepEqual(results, [harry, hal]);
- // Now let's exclude Hal.
+ // Now let's exclude Hal and include King Hamlet.
user_ids = [hamlet.user_id, harry.user_id];
results = ct.get_person_suggestions("Ha", opts);
- assert.deepEqual(results, [harry, hamletcharacters]);
+ assert.deepEqual(results, [harry, hamlet]);
+ // Reincluding Hal and deactivating harry
user_ids = [hamlet.user_id, harry.user_id, hal.user_id];
-
- results = ct.get_person_suggestions("Ha", opts);
- assert.deepEqual(results, [harry, hamletcharacters]);
-
people.deactivate(harry);
results = ct.get_person_suggestions("Ha", opts);
// harry is excluded since it has been deactivated.
- assert.deepEqual(results, [hamletcharacters, hal]);
+ assert.deepEqual(results, [hal, hamlet]);
});
test("muted users excluded from results", () => {
diff --git a/web/tests/typeahead.test.js b/web/tests/typeahead.test.js
index 4a622611023..f5e22a8c585 100644
--- a/web/tests/typeahead.test.js
+++ b/web/tests/typeahead.test.js
@@ -150,6 +150,26 @@ run_test("triage", () => {
);
});
+run_test("triage: prioritise word boundary matches to arbitrary substring matches", () => {
+ const book = {name: "book"};
+ const hyphen_ok = {name: "hyphen_ok"};
+ const space_ok = {name: "space ok"};
+ const no_space_ok = {name: "nospaceok"};
+ const number_ok = {name: "number1ok"};
+ const okay = {name: "okay"};
+ const ok = {name: "ok"};
+
+ const emojis = [book, hyphen_ok, space_ok, no_space_ok, number_ok, okay, ok];
+
+ assert.deepEqual(
+ typeahead.triage("ok", emojis, (r) => r.name),
+ {
+ matches: [ok, okay, hyphen_ok, space_ok],
+ rest: [book, no_space_ok, number_ok],
+ },
+ );
+});
+
function sort_emojis(emojis, query) {
return typeahead.sort_emojis(emojis, query).map((emoji) => emoji.emoji_name);
}
diff --git a/web/tests/typeahead_helper.test.js b/web/tests/typeahead_helper.test.js
index 068e9674569..938f6d8f8ff 100644
--- a/web/tests/typeahead_helper.test.js
+++ b/web/tests/typeahead_helper.test.js
@@ -339,9 +339,9 @@ test("sort_recipients", () => {
]);
// Typeahead for direct message [query, "", ""]
@@ -394,9 +394,9 @@ test("sort_recipients", () => {
subscriber_email_2,
subscriber_email_1,
]);
recent_senders.process_stream_message({
@@ -507,10 +507,10 @@ test("sort_recipients dup bots", () => {
];
assert.deepEqual(recipients_email, expected);
});