Fix U+FE0F and tweak decay to fix tie result

This commit is contained in:
Cadence Ember
2026-02-03 22:58:42 +13:00
parent 238e911d13
commit c73800f785
2 changed files with 16 additions and 4 deletions

View File

@@ -52,7 +52,7 @@ function scoreLocalpart(localpart, input, displayname) {
return {score, matchedInputTokens: [fakeToken]}
}
const decayDistance = 10
const decayDistance = 20
const decayValue = 0.33
/**
* Score by how many tokens in sequence (not necessarily back to back) at the start of input are in display name tokens. Score each token on its length. 2x if it matches at the start. +1 tiebreaker bonus if it matches all
@@ -90,11 +90,12 @@ function scoreName(displaynameTokens, inputTokens) {
* @returns {Token[]}
*/
function tokenise(name) {
name = name.replaceAll("\ufe0f", "").normalize().toLowerCase()
let index = 0
let result = []
for (const part of name.split(/(_|\s|\b)/g)) {
if (part.trim()) {
result.push({text: part.toLowerCase(), index, end: index + part.length})
result.push({text: part, index, end: index + part.length})
}
index += part.length
}

View File

@@ -1,7 +1,7 @@
// @ts-check
const {test} = require("supertape")
const {scoreLocalpart, scoreName, tokenise} = require("./find-mentions")
const {processJoined, scoreLocalpart, scoreName, tokenise, findMention} = require("./find-mentions")
test("score localpart: score against cadence", t => {
const localparts = [
@@ -87,7 +87,7 @@ test("score name: prefers earlier match", t => {
test("score name: matches lots of tokens", t => {
t.deepEqual(
Math.round(scoreName(tokenise("Cadence, Maid of Creation, Eye of Clarity, Empress of Hope ☆"), tokenise("cadence maid of creation eye of clarity empress of hope")).score),
50
65
)
})
@@ -116,3 +116,14 @@ test("score name: finds match location", t => {
const endLocation = result.matchedInputTokens.at(-1).end
t.equal(message.slice(startLocation, endLocation), "evil lillith")
})
test("find mention: test various tiebreakers", t => {
const found = findMention(processJoined([{
mxid: "@emma:conduit.rory.gay",
displayname: "Emma [it/its] ⚡️"
}, {
mxid: "@emma:rory.gay",
displayname: "Emma [it/its]"
}]), "emma ⚡ curious which one this prefers", 0, "@", "@emma ⚡ curious which one this prefers")
t.equal(found.mxid, "@emma:conduit.rory.gay")
})