feat: fixed tokenization strategy

This commit is contained in:
=
2025-08-08 12:29:19 +05:30
parent 676ebaf3c2
commit 34eb9f475a

View File

@@ -20,10 +20,16 @@ const vowelRegex = new RE2(/^[aeiou]/i);
export const startsWithVowel = (str: string) => vowelRegex.test(str);
const pickWordsRegex = new RE2(/(\W+)/);
export const sanitizeString = (dto: { unsanitizedString: string; tokens: string[] }) => {
let sanitizedString = dto.unsanitizedString;
dto.tokens.filter(Boolean).forEach((el) => {
sanitizedString = sanitizedString.replaceAll(el, "[REDACTED]");
const words = dto.unsanitizedString.split(pickWordsRegex);
const redactionSet = new Set(dto.tokens.filter(Boolean));
const sanitizedWords = words.map((el) => {
if (redactionSet.has(el)) {
return "[REDACTED]";
}
return el;
});
return sanitizedString;
return sanitizedWords.join("");
};