diff --git a/src/d2m/converters/find-mentions.js b/src/d2m/converters/find-mentions.js index 0f52992..9db6355 100644 --- a/src/d2m/converters/find-mentions.js +++ b/src/d2m/converters/find-mentions.js @@ -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 } diff --git a/src/d2m/converters/find-mentions.test.js b/src/d2m/converters/find-mentions.test.js index fc950e3..0d02285 100644 --- a/src/d2m/converters/find-mentions.test.js +++ b/src/d2m/converters/find-mentions.test.js @@ -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") +})