Skip to content

fix(visibility): count names that start or end outside ASCII letters - #890

Merged
gkhngyk merged 1 commit into
mainfrom
fix/unicode-mention-matching
Oct 3, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
fix/unicode-mention-matching

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

Summary

Brand and competitor mentions were matched with \b${name}\b. JavaScript's \b only treats ASCII [A-Za-z0-9_] as word characters, so a name that starts or ends with anything else was never counted. Affected names include accented or dotted letters ("Ümit", "İpek"), non-Latin scripts, and trailing punctuation ("C++", "Acme, Inc."). Every answer naming such a brand was stored with zero mentions and no mention position, so its visibility read close to zero.

In production this affects 9 of 183 brands and 47 of 1,231 competitors. For the most affected active brand, the name appears in about 9,200 of 25,000 stored answers, but only about 800 were counted as mentions.

Change

  • nameRegex() in response-parser.js replaces \b with Unicode letter/number lookarounds (u flag). Both countOccurrences and firstOccurrenceIndex use it. A boundary is only required on a side where the name ends in a word character, so names ending in punctuation match.
  • Scripts written without spaces between words (Chinese, Japanese, Thai, Lao, Khmer, Myanmar), and Korean, whose particles attach to the noun, get no boundary. A name in these scripts is always flanked by other letters, so the lookaround suggested in the issue would still never match it.
  • scripts/backfill-mention-counts.js re-runs parseResponse over stored answers. It rewrites mention_count, visibility_score, mention_position, mentioned_entity_count and the matching fields of existing competitor_mentions entries.
    • Scope: brands in active or trialing orgs whose own name or a competitor's name is affected. --brand <id> narrows it, --dry-run writes nothing.
    • Rows whose recomputed values equal the stored ones are skipped, so the script is idempotent.
    • Stored sentiment is kept. Answers that now count as mentions keep the neutral they were stored with; re-analysing them is a separate decision.
    • At the end the script prints the backfill-insights-daily.js command for each changed brand.

Dry run against production (read only)

  • On a brand that only has affected competitors: 3 of 1,220 rows change, and the other 1,217 recompute to exactly their stored values.
  • On the most affected brand: 9,751 of 25,236 rows change.

Related issue

Closes #871

Type of change

  • Bug fix

How to test

  1. yarn test from server/. The new names outside ASCII word characters (#871) cases cover accented and dotted names, trailing punctuation, CJK and Korean names, names inside longer words (no match), and mention positions.
  2. Optional: node src/scripts/backfill-mention-counts.js --brand <id> --dry-run against a brand with an affected name.

Rollout

  1. Merge and deploy the server, so new answers are counted correctly.
  2. Run node src/scripts/backfill-mention-counts.js.
  3. Run the printed INSIGHTS_BACKFILL_BRAND=<id> node src/scripts/backfill-insights-daily.js commands to refresh the rollups.

Checklist

  • yarn lint, yarn format:check and yarn test (897 passed) pass from server/

🤖 Generated with Claude Code

Mentions were matched with `\b${name}\b`. JavaScript's `\b` only knows
ASCII word characters, so a brand or competitor whose name starts or ends
with an accented letter, a non-Latin script or punctuation ("Émile",
"C++", "Acme, Inc.") was never counted. Every answer naming it was stored
with zero mentions and no position.

- nameRegex() replaces `\b` with Unicode letter/number lookarounds, used
  by both countOccurrences and firstOccurrenceIndex. A boundary is only
  required on a side where the name ends in a word character.
- Scripts written without spaces (Chinese, Japanese, Thai...) and Korean,
  whose particles attach to the noun, get no boundary: the name is always
  flanked by other letters there.
- backfill-mention-counts.js re-runs parseResponse over stored answers for
  brands whose own or competitor names are affected, and rewrites the
  mention counts, visibility scores and positions. Stored sentiment is
  kept. Rows whose values do not change are skipped.

Closes #871
@gkhngyk
gkhngyk merged commit d0b88b3 into main Oct 3, 2026
6 checks passed
@gkhngyk
gkhngyk deleted the fix/unicode-mention-matching branch October 3, 2026 19:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: brand and competitor names that start or end with a non-ASCII letter or punctuation are never counted as mentions

1 participant