Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions packages/orama/src/components/tokenizer/diacritics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,19 @@ function replaceChar(charCode: number): number {
return CHARCODE_REPLACE_MAPPING[charCode - DIACRITICS_CHARCODE_START] || charCode
}

// Spreading a very large array into String.fromCharCode(...) throws a RangeError (the arguments
// don't fit in the call stack), so long strings are converted in chunks.
const REPLACE_CHUNK_SIZE = 8192

export function replaceDiacritics(str: string): string {
const stringCharCode: number[] = []
for (let idx = 0; idx < str.length; idx++) {
stringCharCode[idx] = replaceChar(str.charCodeAt(idx))
let result = ''
for (let start = 0; start < str.length; start += REPLACE_CHUNK_SIZE) {
const end = Math.min(start + REPLACE_CHUNK_SIZE, str.length)
const stringCharCode: number[] = []
for (let idx = start; idx < end; idx++) {
stringCharCode.push(replaceChar(str.charCodeAt(idx)))
}
result += String.fromCharCode(...stringCharCode)
}
return String.fromCharCode(...stringCharCode)
return result
}
36 changes: 36 additions & 0 deletions packages/orama/tests/diacritics.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import t from 'tap'
import { replaceDiacritics } from '../src/components/tokenizer/diacritics.js'
import { create, insert, search } from '../src/index.js'

t.test('Diacritics Replacer', async (t) => {
t.test('should replace diacritics', async (t) => {
Expand All @@ -16,3 +17,38 @@ t.test('Diacritics Replacer', async (t) => {
t.equal(O3, `aaaaeeeiiooooucnAAAAEEIIOOOOUCN`)
})
})

t.test('Diacritics Replacer with long strings', async (t) => {
t.test('should replace diacritics in a string of 500000 characters', async (t) => {
const length = 500_000
// A pattern of 3 characters does not align with the chunk boundaries
const input = 'aéü'.repeat(Math.ceil(length / 3)).slice(0, length)
const expected = 'aeu'.repeat(Math.ceil(length / 3)).slice(0, length)

const output = replaceDiacritics(input)

t.equal(output.length, length)
t.ok(output === expected, 'every character is replaced')
})

t.test('should handle strings around the chunk size', async (t) => {
for (const length of [0, 1, 8191, 8192, 8193, 16384, 16385]) {
t.equal(replaceDiacritics('é'.repeat(length)), 'e'.repeat(length), `length ${length}`)
}
})

t.test('should leave a long string without diacritics unchanged', async (t) => {
const input = 'x'.repeat(150_000)
t.ok(replaceDiacritics(input) === input)
})

t.test('should be able to index and find a document with a very long token', async (t) => {
const db = create({ schema: { text: 'string' } as const })
const token = 'x'.repeat(200_000)

await insert(db, { text: token })
const results = await search(db, { term: token })

t.equal(results.count, 1)
})
})