Skip to content

Commit ef4d433

Browse files
committed
strip pound sign
1 parent e63e8e3 commit ef4d433

File tree

1 file changed

+16
-30
lines changed

1 file changed

+16
-30
lines changed

src/components/dictionary/Dictionary.tsx

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,14 @@ const WithTgtLang = ({
116116
return children({ tgtLang, setTgtLang, recentTgtLangs });
117117
};
118118

119-
const stripTagsLower = (s: string) =>
119+
const stripTagsAndHashes = (s: string) =>
120120
s
121121
.replace(/<[^>]+>/g, '')
122-
.trim()
123-
.toLowerCase();
122+
.replace(/#/g, '')
123+
.replace(/\s{2,}/g, ' ')
124+
.trim();
125+
126+
const stripTagsLower = (s: string) => stripTagsAndHashes(s).toLowerCase();
124127

125128
const dedupeEmbeddingEntries = (entries: Entry[]): Entry[] => {
126129
const byKey = new Map<string, { head: string; def: string; sims: Set<string>; extraTags?: string[] }>();
@@ -460,31 +463,14 @@ const Dictionary: React.FC = () => {
460463
const enriched: Record<string, string[]> = {};
461464
headResponses.forEach((arr, i) => {
462465
enriched[uniqueHeads[i]] = Array.from(
463-
new Set(
464-
arr.flatMap((item) => item.defs.map((d) => d.replace(/<[^>]+>/g, '').trim())).filter(Boolean),
465-
),
466+
new Set(arr.flatMap((item) => item.defs.map((d) => stripTagsAndHashes(d))).filter(Boolean)),
466467
);
467468
});
468469
setReverseResults(uniqueHeads.map((h) => ({ head: h, defs: enriched[h] } as Entry)));
469470

470-
const cleanedWord = rawWord
471-
.replace(/<[^>]+>/g, '')
472-
.trim()
473-
.toLowerCase();
474-
const exactForward = fwdParsed.find(
475-
(item) =>
476-
item.head
477-
.replace(/<[^>]+>/g, '')
478-
.trim()
479-
.toLowerCase() === cleanedWord,
480-
);
481-
const exactReverse = reverseRaw.find(
482-
(item) =>
483-
item.head
484-
.replace(/<[^>]+>/g, '')
485-
.trim()
486-
.toLowerCase() === cleanedWord,
487-
);
471+
const cleanedWord = stripTagsLower(rawWord);
472+
const exactForward = fwdParsed.find((item) => stripTagsLower(item.head) === cleanedWord);
473+
const exactReverse = reverseRaw.find((item) => stripTagsLower(item.head) === cleanedWord);
488474

489475
let headerTerm: string | null = null;
490476
let translations: string[] = [];
@@ -493,14 +479,14 @@ const Dictionary: React.FC = () => {
493479
let exactMatchFound = false;
494480

495481
if (exactForward) {
496-
headerTerm = exactForward.head.replace(/<[^>]+>/g, '').trim();
497-
translations = exactForward.defs.map((d) => d.replace(/<[^>]+>/g, '').trim());
482+
headerTerm = stripTagsAndHashes(exactForward.head);
483+
translations = exactForward.defs.map((d) => stripTagsAndHashes(d));
498484
headLang = srcOverride;
499485
translationLang = tgtOverride;
500486
exactMatchFound = true;
501487
} else if (exactReverse) {
502-
headerTerm = exactReverse.head.replace(/<[^>]+>/g, '').trim();
503-
translations = exactReverse.defs.map((d) => d.replace(/<[^>]+>/g, '').trim());
488+
headerTerm = stripTagsAndHashes(exactReverse.head);
489+
translations = exactReverse.defs.map((d) => stripTagsAndHashes(d));
504490
headLang = tgtOverride;
505491
translationLang = srcOverride;
506492
exactMatchFound = true;
@@ -788,11 +774,11 @@ const Dictionary: React.FC = () => {
788774
const all: Entry[] = [...results, ...reverseResults, ...embeddingResults];
789775
const map: Record<string, Entry[]> = {};
790776
all.forEach((e) => {
791-
const surface = e.head.replace(/<[^>]+>/g, '');
777+
const surface = stripTagsAndHashes(e.head);
792778
if (!map[surface]) map[surface] = [];
793779
map[surface].push({
794780
head: e.head,
795-
defs: e.defs.map((d) => d.replace(/<[^>]+>/g, '')),
781+
defs: e.defs.map((d) => stripTagsAndHashes(d)),
796782
...(e.similarTo ? { similarTo: e.similarTo } : {}),
797783
...(e.extraTags ? { extraTags: e.extraTags } : {}),
798784
} as Entry);

0 commit comments

Comments
 (0)