baburchi 1.5.0 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +166 -7
- package/dist/index.d.ts +179 -60
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/package.json +11 -21
package/README.md
CHANGED
|
@@ -62,7 +62,9 @@ const noiseText = isArabicTextNoise('---'); // true
|
|
|
62
62
|
|
|
63
63
|
## API Reference
|
|
64
64
|
|
|
65
|
-
###
|
|
65
|
+
### Core Text Processing
|
|
66
|
+
|
|
67
|
+
#### `fixTypo(original, correction, options)`
|
|
66
68
|
|
|
67
69
|
The main function for correcting typos using text alignment.
|
|
68
70
|
|
|
@@ -80,7 +82,7 @@ The main function for correcting typos using text alignment.
|
|
|
80
82
|
|
|
81
83
|
**Returns:** Corrected text string
|
|
82
84
|
|
|
83
|
-
|
|
85
|
+
#### `processTextAlignment(originalText, altText, options)`
|
|
84
86
|
|
|
85
87
|
Low-level function for advanced text processing with full configuration control.
|
|
86
88
|
|
|
@@ -90,6 +92,167 @@ Low-level function for advanced text processing with full configuration control.
|
|
|
90
92
|
- `altText` (string): Reference text for alignment
|
|
91
93
|
- `options` (FixTypoOptions): Complete configuration object
|
|
92
94
|
|
|
95
|
+
### Fuzzy Text Matching
|
|
96
|
+
|
|
97
|
+
#### `findMatches(pages, excerpts, policy?)`
|
|
98
|
+
|
|
99
|
+
Finds the best matching page for each excerpt using exact and fuzzy matching algorithms.
|
|
100
|
+
|
|
101
|
+
**Parameters:**
|
|
102
|
+
|
|
103
|
+
- `pages` (string[]): Array of page texts to search within
|
|
104
|
+
- `excerpts` (string[]): Array of text excerpts to find
|
|
105
|
+
- `policy` (MatchPolicy, optional): Matching configuration
|
|
106
|
+
|
|
107
|
+
**Returns:** `number[]` - Array of page indices (0-based) where each excerpt was found, or -1 if not found
|
|
108
|
+
|
|
109
|
+
**Example:**
|
|
110
|
+
|
|
111
|
+
```typescript
|
|
112
|
+
import { findMatches } from 'baburchi';
|
|
113
|
+
|
|
114
|
+
const pages = [
|
|
115
|
+
'هذا النص في الصفحة الأولى مع محتوى إضافي',
|
|
116
|
+
'النص الثاني يظهر هنا في الصفحة الثانية',
|
|
117
|
+
'الصفحة الثالثة تحتوي على نص مختلف'
|
|
118
|
+
];
|
|
119
|
+
|
|
120
|
+
const excerpts = [
|
|
121
|
+
'النص في الصفحة الأولى',
|
|
122
|
+
'النص الثاني يظهر',
|
|
123
|
+
'نص غير موجود'
|
|
124
|
+
];
|
|
125
|
+
|
|
126
|
+
const matches = findMatches(pages, excerpts);
|
|
127
|
+
console.log(matches); // [0, 1, -1]
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
#### `findMatchesAll(pages, excerpts, policy?)`
|
|
131
|
+
|
|
132
|
+
Finds all potential matches for each excerpt, ranked by match quality.
|
|
133
|
+
|
|
134
|
+
**Parameters:**
|
|
135
|
+
|
|
136
|
+
- `pages` (string[]): Array of page texts to search within
|
|
137
|
+
- `excerpts` (string[]): Array of text excerpts to find
|
|
138
|
+
- `policy` (MatchPolicy, optional): Matching configuration
|
|
139
|
+
|
|
140
|
+
**Returns:** `number[][]` - Array where each element is an array of page indices ranked by match quality (exact matches first, then fuzzy matches by score)
|
|
141
|
+
|
|
142
|
+
**Example:**
|
|
143
|
+
|
|
144
|
+
```typescript
|
|
145
|
+
import { findMatchesAll } from 'baburchi';
|
|
146
|
+
|
|
147
|
+
const pages = [
|
|
148
|
+
'النص الأول مع محتوى مشابه',
|
|
149
|
+
'محتوى مشابه في النص الثاني',
|
|
150
|
+
'النص الأول بصيغة مختلفة قليلاً'
|
|
151
|
+
];
|
|
152
|
+
|
|
153
|
+
const excerpts = ['النص الأول'];
|
|
154
|
+
|
|
155
|
+
const allMatches = findMatchesAll(pages, excerpts);
|
|
156
|
+
console.log(allMatches); // [[0, 2]] - excerpt matches page 0 exactly, page 2 fuzzily
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
#### Match Policy Configuration
|
|
160
|
+
|
|
161
|
+
The `MatchPolicy` interface allows fine-tuning of the matching algorithm:
|
|
162
|
+
|
|
163
|
+
```typescript
|
|
164
|
+
interface MatchPolicy {
|
|
165
|
+
enableFuzzy?: boolean; // Enable fuzzy matching (default: true)
|
|
166
|
+
maxEditAbs?: number; // Max absolute edit distance (default: 3)
|
|
167
|
+
maxEditRel?: number; // Max relative edit distance (default: 0.1)
|
|
168
|
+
q?: number; // Q-gram size for indexing (default: 4)
|
|
169
|
+
gramsPerExcerpt?: number; // Q-grams to sample per excerpt (default: 5)
|
|
170
|
+
maxCandidatesPerExcerpt?: number; // Max candidates to evaluate (default: 40)
|
|
171
|
+
seamLen?: number; // Cross-page seam length (default: 512)
|
|
172
|
+
}
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
**Example with custom policy:**
|
|
176
|
+
|
|
177
|
+
```typescript
|
|
178
|
+
import { findMatches } from 'baburchi';
|
|
179
|
+
|
|
180
|
+
const customPolicy: MatchPolicy = {
|
|
181
|
+
enableFuzzy: true,
|
|
182
|
+
maxEditAbs: 6, // Allow more character differences
|
|
183
|
+
maxEditRel: 0.3, // Allow 30% character differences
|
|
184
|
+
q: 4, // Use 4-grams for better precision
|
|
185
|
+
gramsPerExcerpt: 30, // Sample more Q-grams
|
|
186
|
+
maxCandidatesPerExcerpt: 150
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
const matches = findMatches(pages, excerpts, customPolicy);
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### Arabic Text Normalization
|
|
193
|
+
|
|
194
|
+
#### `sanitizeArabic(input, optionsOrPreset)`
|
|
195
|
+
|
|
196
|
+
Unified Arabic text sanitizer that provides fast, configurable cleanup for Arabic text.
|
|
197
|
+
|
|
198
|
+
**Parameters:**
|
|
199
|
+
|
|
200
|
+
- `input` (string): The Arabic text to sanitize
|
|
201
|
+
- `optionsOrPreset` (string | object): Either a preset name or custom options
|
|
202
|
+
|
|
203
|
+
**Presets:**
|
|
204
|
+
|
|
205
|
+
- `"light"`: Basic cleanup for display (strips zero-width chars, collapses whitespace)
|
|
206
|
+
- `"search"`: Tolerant search normalization (removes diacritics, normalizes letters)
|
|
207
|
+
- `"aggressive"`: Indexing-friendly (letters and spaces only, removes everything else)
|
|
208
|
+
|
|
209
|
+
**Custom Options:**
|
|
210
|
+
|
|
211
|
+
```typescript
|
|
212
|
+
interface SanitizeOptions {
|
|
213
|
+
base?: 'light' | 'search' | 'aggressive' | 'none';
|
|
214
|
+
stripDiacritics?: boolean;
|
|
215
|
+
stripTatweel?: boolean;
|
|
216
|
+
normalizeAlif?: boolean;
|
|
217
|
+
replaceAlifMaqsurah?: boolean;
|
|
218
|
+
replaceTaMarbutahWithHa?: boolean;
|
|
219
|
+
stripZeroWidth?: boolean;
|
|
220
|
+
zeroWidthToSpace?: boolean;
|
|
221
|
+
stripLatinAndSymbols?: boolean;
|
|
222
|
+
lettersAndSpacesOnly?: boolean;
|
|
223
|
+
keepOnlyArabicLetters?: boolean;
|
|
224
|
+
collapseWhitespace?: boolean;
|
|
225
|
+
trim?: boolean;
|
|
226
|
+
removeHijriMarker?: boolean;
|
|
227
|
+
}
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
**Examples:**
|
|
231
|
+
|
|
232
|
+
```typescript
|
|
233
|
+
import { sanitizeArabic } from 'baburchi';
|
|
234
|
+
|
|
235
|
+
// Light display cleanup
|
|
236
|
+
sanitizeArabic(' مرحبا\u200C\u200D بالعالم ', 'light'); // → 'مرحبا بالعالم'
|
|
237
|
+
|
|
238
|
+
// Tolerant search normalization
|
|
239
|
+
sanitizeArabic('اَلسَّلَامُ عَلَيْكُمْ', 'search'); // → 'السلام عليكم'
|
|
240
|
+
|
|
241
|
+
// Indexing-friendly text (letters + spaces only)
|
|
242
|
+
sanitizeArabic('اَلسَّلَامُ 1435/3/29 هـ — www', 'aggressive'); // → 'السلام'
|
|
243
|
+
|
|
244
|
+
// Custom: Tatweel-only, preserving dates/list markers
|
|
245
|
+
sanitizeArabic('أبـــتِـــكَةُ', { base: 'none', stripTatweel: true }); // → 'أبتِكَةُ'
|
|
246
|
+
|
|
247
|
+
// Zero-width controls → spaces
|
|
248
|
+
sanitizeArabic('يَخْلُوَ . قَالَ غَرِيبٌ . ', {
|
|
249
|
+
base: 'none',
|
|
250
|
+
stripZeroWidth: true,
|
|
251
|
+
zeroWidthToSpace: true
|
|
252
|
+
});
|
|
253
|
+
// → 'يَخْلُوَ . قَالَ غَرِيبٌ . '
|
|
254
|
+
```
|
|
255
|
+
|
|
93
256
|
## Usage Examples
|
|
94
257
|
|
|
95
258
|
### Basic Arabic Text Correction
|
|
@@ -190,7 +353,7 @@ Baburchi uses the **Needleman-Wunsch global sequence alignment algorithm** to op
|
|
|
190
353
|
Baburchi works in all modern environments:
|
|
191
354
|
|
|
192
355
|
- ✅ Node.js 22+
|
|
193
|
-
- ✅ Bun 1.2.
|
|
356
|
+
- ✅ Bun 1.2.21+
|
|
194
357
|
- ✅ Modern browsers (ES2023+)
|
|
195
358
|
- ✅ Deno (with npm compatibility)
|
|
196
359
|
|
|
@@ -367,7 +530,6 @@ The library also exports utility functions for advanced use cases:
|
|
|
367
530
|
```typescript
|
|
368
531
|
import {
|
|
369
532
|
calculateSimilarity,
|
|
370
|
-
normalizeArabicText,
|
|
371
533
|
tokenizeText,
|
|
372
534
|
alignTokenSequences,
|
|
373
535
|
hasInvalidFootnotes,
|
|
@@ -380,9 +542,6 @@ import {
|
|
|
380
542
|
// Calculate similarity between two strings
|
|
381
543
|
const similarity = calculateSimilarity('hello', 'helo'); // 0.8
|
|
382
544
|
|
|
383
|
-
// Normalize Arabic text
|
|
384
|
-
const normalized = normalizeArabicText('اَلسَّلَامُ'); // 'السلام'
|
|
385
|
-
|
|
386
545
|
// Tokenize with symbol preservation
|
|
387
546
|
const tokens = tokenizeText('محمد ﷺ رسول', ['ﷺ']); // ['محمد', 'ﷺ', 'رسول']
|
|
388
547
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,32 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Configuration options for fixing typos in OCR text using alignment algorithms.
|
|
3
|
-
* These options control how text tokens are compared, aligned, and merged during typo correction.
|
|
4
|
-
*/
|
|
5
|
-
type FixTypoOptions = {
|
|
6
|
-
/**
|
|
7
|
-
* High similarity threshold (0.0 to 1.0) for detecting and removing duplicate tokens.
|
|
8
|
-
* Used in post-processing to eliminate redundant tokens that are nearly identical.
|
|
9
|
-
* Should typically be higher than similarityThreshold to catch only very similar duplicates.
|
|
10
|
-
* @default 0.9
|
|
11
|
-
* @example 0.95 // Removes tokens that are 95% or more similar
|
|
12
|
-
*/
|
|
13
|
-
readonly highSimilarityThreshold: number;
|
|
14
|
-
/**
|
|
15
|
-
* Similarity threshold (0.0 to 1.0) for determining if two tokens should be aligned.
|
|
16
|
-
* Higher values require closer matches, lower values are more permissive.
|
|
17
|
-
* Used in the Needleman-Wunsch alignment algorithm for token matching.
|
|
18
|
-
* @default 0.7
|
|
19
|
-
* @example 0.8 // Requires 80% similarity for token alignment
|
|
20
|
-
*/
|
|
21
|
-
readonly similarityThreshold: number;
|
|
22
|
-
/**
|
|
23
|
-
* Array of special symbols that should be preserved during typo correction.
|
|
24
|
-
* These symbols (like honorifics or religious markers) take precedence in token selection.
|
|
25
|
-
* @example ['ﷺ', '﷽', 'ﷻ'] // Common Arabic religious symbols
|
|
26
|
-
*/
|
|
27
|
-
readonly typoSymbols: string[];
|
|
28
|
-
};
|
|
29
|
-
|
|
30
1
|
/**
|
|
31
2
|
* Aligns split text segments to match target lines by finding the best order.
|
|
32
3
|
*
|
|
@@ -214,6 +185,88 @@ type TextLine = {
|
|
|
214
185
|
*/
|
|
215
186
|
declare const correctReferences: <T extends TextLine>(lines: T[]) => T[];
|
|
216
187
|
|
|
188
|
+
/**
|
|
189
|
+
* Configuration options for fixing typos in OCR text using alignment algorithms.
|
|
190
|
+
* These options control how text tokens are compared, aligned, and merged during typo correction.
|
|
191
|
+
*/
|
|
192
|
+
type FixTypoOptions = {
|
|
193
|
+
/**
|
|
194
|
+
* High similarity threshold (0.0 to 1.0) for detecting and removing duplicate tokens.
|
|
195
|
+
* Used in post-processing to eliminate redundant tokens that are nearly identical.
|
|
196
|
+
* Should typically be higher than similarityThreshold to catch only very similar duplicates.
|
|
197
|
+
* @default 0.9
|
|
198
|
+
* @example 0.95 // Removes tokens that are 95% or more similar
|
|
199
|
+
*/
|
|
200
|
+
readonly highSimilarityThreshold: number;
|
|
201
|
+
/**
|
|
202
|
+
* Similarity threshold (0.0 to 1.0) for determining if two tokens should be aligned.
|
|
203
|
+
* Higher values require closer matches, lower values are more permissive.
|
|
204
|
+
* Used in the Needleman-Wunsch alignment algorithm for token matching.
|
|
205
|
+
* @default 0.7
|
|
206
|
+
* @example 0.8 // Requires 80% similarity for token alignment
|
|
207
|
+
*/
|
|
208
|
+
readonly similarityThreshold: number;
|
|
209
|
+
/**
|
|
210
|
+
* Array of special symbols that should be preserved during typo correction.
|
|
211
|
+
* These symbols (like honorifics or religious markers) take precedence in token selection.
|
|
212
|
+
* @example ['ﷺ', '﷽', 'ﷻ'] // Common Arabic religious symbols
|
|
213
|
+
*/
|
|
214
|
+
readonly typoSymbols: string[];
|
|
215
|
+
};
|
|
216
|
+
type MatchPolicy = {
|
|
217
|
+
/** Try approximate matches for leftovers (default true). */
|
|
218
|
+
enableFuzzy?: boolean;
|
|
219
|
+
/** Max absolute edit distance accepted in fuzzy (default 3). */
|
|
220
|
+
maxEditAbs?: number;
|
|
221
|
+
/** Max relative edit distance (fraction of excerpt length). Default 0.1 (10%). */
|
|
222
|
+
maxEditRel?: number;
|
|
223
|
+
/** q-gram length for candidate generation (default 4). */
|
|
224
|
+
q?: number;
|
|
225
|
+
/** Max rare grams to seed candidates per excerpt (default 5). */
|
|
226
|
+
gramsPerExcerpt?: number;
|
|
227
|
+
/** Max candidate windows verified per excerpt (default 40). */
|
|
228
|
+
maxCandidatesPerExcerpt?: number;
|
|
229
|
+
/** Seam length for bleed windows (default 512). */
|
|
230
|
+
seamLen?: number;
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Main function to find the single best match per excerpt.
|
|
235
|
+
* Combines exact matching with fuzzy matching for comprehensive text search.
|
|
236
|
+
*
|
|
237
|
+
* @param pages - Array of page texts to search within
|
|
238
|
+
* @param excerpts - Array of text excerpts to find matches for
|
|
239
|
+
* @param policy - Optional matching policy configuration
|
|
240
|
+
* @returns Array of page indices (one per excerpt, -1 if no match found)
|
|
241
|
+
*
|
|
242
|
+
* @example
|
|
243
|
+
* ```typescript
|
|
244
|
+
* const pages = ['Hello world', 'Goodbye world'];
|
|
245
|
+
* const excerpts = ['Hello', 'Good bye']; // Note the typo
|
|
246
|
+
* const matches = findMatches(pages, excerpts, { enableFuzzy: true });
|
|
247
|
+
* // Returns [0, 1] - exact match on page 0, fuzzy match on page 1
|
|
248
|
+
* ```
|
|
249
|
+
*/
|
|
250
|
+
declare function findMatches(pages: string[], excerpts: string[], policy?: MatchPolicy): number[];
|
|
251
|
+
/**
|
|
252
|
+
* Main function to find all matches per excerpt, ranked by quality.
|
|
253
|
+
* Returns comprehensive results with both exact and fuzzy matches for each excerpt.
|
|
254
|
+
*
|
|
255
|
+
* @param pages - Array of page texts to search within
|
|
256
|
+
* @param excerpts - Array of text excerpts to find matches for
|
|
257
|
+
* @param policy - Optional matching policy configuration
|
|
258
|
+
* @returns Array of page index arrays (one array per excerpt, sorted by match quality)
|
|
259
|
+
*
|
|
260
|
+
* @example
|
|
261
|
+
* ```typescript
|
|
262
|
+
* const pages = ['Hello world', 'Hello there', 'Goodbye world'];
|
|
263
|
+
* const excerpts = ['Hello'];
|
|
264
|
+
* const matches = findMatchesAll(pages, excerpts);
|
|
265
|
+
* // Returns [[0, 1]] - both pages 0 and 1 contain "Hello", sorted by page order
|
|
266
|
+
* ```
|
|
267
|
+
*/
|
|
268
|
+
declare function findMatchesAll(pages: string[], excerpts: string[], policy?: MatchPolicy): number[][];
|
|
269
|
+
|
|
217
270
|
/**
|
|
218
271
|
* Character statistics for analyzing text content and patterns
|
|
219
272
|
*/
|
|
@@ -382,6 +435,19 @@ declare function isSpacingNoise(charStats: CharacterStats, contentChars: number,
|
|
|
382
435
|
*/
|
|
383
436
|
declare function isValidArabicContent(charStats: CharacterStats, textLength: number): boolean;
|
|
384
437
|
|
|
438
|
+
/**
|
|
439
|
+
* Processes text alignment between original and alternate OCR results to fix typos.
|
|
440
|
+
* Uses the Needleman-Wunsch sequence alignment algorithm to align tokens,
|
|
441
|
+
* then selects the best tokens and performs post-processing.
|
|
442
|
+
*
|
|
443
|
+
* @param originalText - Original OCR text that may contain typos
|
|
444
|
+
* @param altText - Reference text from alternate OCR for comparison
|
|
445
|
+
* @param options - Configuration options for alignment and selection
|
|
446
|
+
* @returns Corrected text with typos fixed
|
|
447
|
+
*/
|
|
448
|
+
declare const processTextAlignment: (originalText: string, altText: string, options: FixTypoOptions) => string;
|
|
449
|
+
declare const fixTypo: (original: string, correction: string, { highSimilarityThreshold, similarityThreshold, typoSymbols, }: Partial<FixTypoOptions> & Pick<FixTypoOptions, "typoSymbols">) => string;
|
|
450
|
+
|
|
385
451
|
/**
|
|
386
452
|
* Calculates Levenshtein distance between two strings using space-optimized dynamic programming.
|
|
387
453
|
* The Levenshtein distance is the minimum number of single-character edits (insertions,
|
|
@@ -396,6 +462,87 @@ declare function isValidArabicContent(charStats: CharacterStats, textLength: num
|
|
|
396
462
|
* calculateLevenshteinDistance('', 'hello') // Returns 5
|
|
397
463
|
*/
|
|
398
464
|
declare const calculateLevenshteinDistance: (textA: string, textB: string) => number;
|
|
465
|
+
/**
|
|
466
|
+
* Calculates bounded Levenshtein distance with early termination.
|
|
467
|
+
* More efficient when you only care about distances up to a threshold.
|
|
468
|
+
*/
|
|
469
|
+
declare const boundedLevenshtein: (a: string, b: string, maxDist: number) => number;
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* Ultra-fast Arabic text sanitizer for search/indexing/display.
|
|
473
|
+
* Optimized for very high call rates: avoids per-call object spreads and minimizes allocations.
|
|
474
|
+
* Options can merge over a base preset or `'none'` to apply exactly the rules you request.
|
|
475
|
+
*/
|
|
476
|
+
type SanitizePreset = 'light' | 'search' | 'aggressive';
|
|
477
|
+
type SanitizeBase = 'none' | SanitizePreset;
|
|
478
|
+
/**
|
|
479
|
+
* Public options for {@link sanitizeArabic}. When you pass an options object, it overlays the chosen
|
|
480
|
+
* `base` (default `'light'`) without allocating merged objects on the hot path; flags are resolved
|
|
481
|
+
* directly into local booleans for speed.
|
|
482
|
+
*/
|
|
483
|
+
type SanitizeOptions = {
|
|
484
|
+
/** Base to merge over. `'none'` applies only the options you specify. Default when passing an object: `'light'`. */
|
|
485
|
+
base?: SanitizeBase;
|
|
486
|
+
/** Unicode NFC normalization. Default: `true` in all presets. */
|
|
487
|
+
nfc?: boolean;
|
|
488
|
+
/** Strip zero-width controls (U+200B–U+200F, U+202A–U+202E, U+2060–U+2064, U+FEFF). Default: `true` in presets. */
|
|
489
|
+
stripZeroWidth?: boolean;
|
|
490
|
+
/** If stripping zero-width, replace them with a space instead of removing. Default: `false`. */
|
|
491
|
+
zeroWidthToSpace?: boolean;
|
|
492
|
+
/** Remove Arabic diacritics (tashkīl). Default: `true` in `'search'`/`'aggressive'`. */
|
|
493
|
+
stripDiacritics?: boolean;
|
|
494
|
+
/**
|
|
495
|
+
* Remove tatweel (ـ).
|
|
496
|
+
* - `true` is treated as `'safe'` (preserves tatweel after digits or 'ه' for dates/list markers)
|
|
497
|
+
* - `'safe'` or `'all'` explicitly
|
|
498
|
+
* - `false` to keep tatweel
|
|
499
|
+
* Default: `'all'` in `'search'`/`'aggressive'`, `false` in `'light'`.
|
|
500
|
+
*/
|
|
501
|
+
stripTatweel?: boolean | 'safe' | 'all';
|
|
502
|
+
/** Normalize آ/أ/إ → ا. Default: `true` in `'search'`/`'aggressive'`. */
|
|
503
|
+
normalizeAlif?: boolean;
|
|
504
|
+
/** Replace ى → ي. Default: `true` in `'search'`/`'aggressive'`. */
|
|
505
|
+
replaceAlifMaqsurah?: boolean;
|
|
506
|
+
/** Replace ة → ه (lossy). Default: `true` in `'aggressive'` only. */
|
|
507
|
+
replaceTaMarbutahWithHa?: boolean;
|
|
508
|
+
/** Strip Latin letters/digits and common OCR noise into spaces. Default: `true` in `'aggressive'`. */
|
|
509
|
+
stripLatinAndSymbols?: boolean;
|
|
510
|
+
/** Keep only Arabic letters (no whitespace). Use for compact keys, not FTS. */
|
|
511
|
+
keepOnlyArabicLetters?: boolean;
|
|
512
|
+
/** Keep Arabic letters + spaces (drops digits/punct/symbols). Great for FTS. Default: `true` in `'aggressive'`. */
|
|
513
|
+
lettersAndSpacesOnly?: boolean;
|
|
514
|
+
/** Collapse runs of whitespace to a single space. Default: `true`. */
|
|
515
|
+
collapseWhitespace?: boolean;
|
|
516
|
+
/** Trim leading/trailing whitespace. Default: `true`. */
|
|
517
|
+
trim?: boolean;
|
|
518
|
+
/**
|
|
519
|
+
* Remove the Hijri date marker ("هـ" or bare "ه" if tatweel already removed) when it follows a date-like token
|
|
520
|
+
* (digits/slashes/hyphens/spaces). Example: `1435/3/29 هـ` → `1435/3/29`.
|
|
521
|
+
* Default: `true` in `'search'`/`'aggressive'`, `false` in `'light'`.
|
|
522
|
+
*/
|
|
523
|
+
removeHijriMarker?: boolean;
|
|
524
|
+
};
|
|
525
|
+
/**
|
|
526
|
+
* Sanitizes Arabic text according to a preset or custom options.
|
|
527
|
+
*
|
|
528
|
+
* Presets:
|
|
529
|
+
* - `'light'`: NFC, zero-width removal, collapse/trim spaces.
|
|
530
|
+
* - `'search'`: removes diacritics and tatweel, normalizes Alif and ى→ي, removes Hijri marker.
|
|
531
|
+
* - `'aggressive'`: ideal for FTS; keeps letters+spaces only and strips common noise.
|
|
532
|
+
*
|
|
533
|
+
* Custom options:
|
|
534
|
+
* - Passing an options object overlays the selected `base` preset (default `'light'`).
|
|
535
|
+
* - Use `base: 'none'` to apply **only** the rules you specify (e.g., tatweel only).
|
|
536
|
+
*
|
|
537
|
+
* Examples:
|
|
538
|
+
* ```ts
|
|
539
|
+
* sanitizeArabic('أبـــتِـــكَةُ', { base: 'none', stripTatweel: true }); // 'أبتِكَةُ'
|
|
540
|
+
* sanitizeArabic('1435/3/29 هـ', 'aggressive'); // '1435 3 29'
|
|
541
|
+
* sanitizeArabic('اَلسَّلَامُ عَلَيْكُمْ', 'search'); // 'السلام عليكم'
|
|
542
|
+
* ```
|
|
543
|
+
*/
|
|
544
|
+
declare const sanitizeArabic: (input: string, optionsOrPreset?: SanitizePreset | SanitizeOptions) => string;
|
|
545
|
+
|
|
399
546
|
/**
|
|
400
547
|
* Calculates similarity ratio between two strings as a value between 0.0 and 1.0.
|
|
401
548
|
* Uses Levenshtein distance normalized by the length of the longer string.
|
|
@@ -487,8 +634,6 @@ declare const PATTERNS: {
|
|
|
487
634
|
arabicPunctuationAndWhitespace: RegExp;
|
|
488
635
|
/** Matches footnote references with Arabic-Indic digits in parentheses: \([\u0660-\u0669]+\) */
|
|
489
636
|
arabicReferenceRegex: RegExp;
|
|
490
|
-
/** Matches Arabic diacritical marks (harakat, tanween, etc.) */
|
|
491
|
-
diacritics: RegExp;
|
|
492
637
|
/** Matches embedded footnotes within text: \([0-9\u0660-\u0669]+\) */
|
|
493
638
|
footnoteEmbedded: RegExp;
|
|
494
639
|
/** Matches standalone footnote markers at line start/end: ^\(?[0-9\u0660-\u0669]+\)?[،.]?$ */
|
|
@@ -499,22 +644,9 @@ declare const PATTERNS: {
|
|
|
499
644
|
ocrConfusedFootnoteReferenceRegex: RegExp;
|
|
500
645
|
/** Matches OCR-confused footnote references with characters commonly misread as Arabic digits */
|
|
501
646
|
ocrConfusedReferenceRegex: RegExp;
|
|
502
|
-
/** Matches Arabic tatweel (kashida) character used for text stretching */
|
|
503
|
-
tatweel: RegExp;
|
|
504
647
|
/** Matches one or more whitespace characters */
|
|
505
648
|
whitespace: RegExp;
|
|
506
649
|
};
|
|
507
|
-
/**
|
|
508
|
-
* Normalizes Arabic text by removing diacritics, and tatweel marks.
|
|
509
|
-
* This normalization enables better text comparison by focusing on core characters
|
|
510
|
-
* while ignoring decorative elements that don't affect meaning.
|
|
511
|
-
*
|
|
512
|
-
* @param text - Arabic text to normalize
|
|
513
|
-
* @returns Normalized text with diacritics, tatweel, and basic tags removed
|
|
514
|
-
* @example
|
|
515
|
-
* normalizeArabicText('اَلسَّلَامُ عَلَيْكُمْ') // Returns 'السلام عليكم'
|
|
516
|
-
*/
|
|
517
|
-
declare const normalizeArabicText: (text: string) => string;
|
|
518
650
|
/**
|
|
519
651
|
* Extracts the first sequence of Arabic or Western digits from text.
|
|
520
652
|
* Used primarily for footnote number comparison to match related footnote elements.
|
|
@@ -528,8 +660,8 @@ declare const normalizeArabicText: (text: string) => string;
|
|
|
528
660
|
declare const extractDigits: (text: string) => string;
|
|
529
661
|
/**
|
|
530
662
|
* Tokenizes text into individual words while preserving special symbols.
|
|
531
|
-
*
|
|
532
|
-
*
|
|
663
|
+
* Adds spacing around preserved symbols to ensure they are tokenized separately,
|
|
664
|
+
* then splits on whitespace.
|
|
533
665
|
*
|
|
534
666
|
* @param text - Text to tokenize
|
|
535
667
|
* @param preserveSymbols - Array of symbols that should be tokenized as separate tokens
|
|
@@ -591,17 +723,4 @@ declare const standardizeHijriSymbol: (text: string) => string;
|
|
|
591
723
|
*/
|
|
592
724
|
declare const standardizeIntahaSymbol: (text: string) => string;
|
|
593
725
|
|
|
594
|
-
|
|
595
|
-
* Processes text alignment between original and alternate OCR results to fix typos.
|
|
596
|
-
* Uses the Needleman-Wunsch sequence alignment algorithm to align tokens,
|
|
597
|
-
* then selects the best tokens and performs post-processing.
|
|
598
|
-
*
|
|
599
|
-
* @param originalText - Original OCR text that may contain typos
|
|
600
|
-
* @param altText - Reference text from alternate OCR for comparison
|
|
601
|
-
* @param options - Configuration options for alignment and selection
|
|
602
|
-
* @returns Corrected text with typos fixed
|
|
603
|
-
*/
|
|
604
|
-
declare const processTextAlignment: (originalText: string, altText: string, options: FixTypoOptions) => string;
|
|
605
|
-
declare const fixTypo: (original: string, correction: string, { highSimilarityThreshold, similarityThreshold, typoSymbols, }: Partial<FixTypoOptions> & Pick<FixTypoOptions, "typoSymbols">) => string;
|
|
606
|
-
|
|
607
|
-
export { BRACKETS, CLOSE_BRACKETS, type CharacterError, INTAHA_ACTUAL, OPEN_BRACKETS, PATTERNS, alignTextSegments, alignTokenSequences, analyzeCharacterStats, areBracketsBalanced, areQuotesBalanced, areSimilarAfterNormalization, backtrackAlignment, calculateAlignmentScore, calculateLevenshteinDistance, calculateSimilarity, checkBalance, correctReferences, extractDigits, fixTypo, getUnbalancedErrors, handleFootnoteFusion, handleFootnoteSelection, handleStandaloneFootnotes, hasExcessiveRepetition, hasInvalidFootnotes, isArabicTextNoise, isBalanced, isBasicNoisePattern, isNonArabicNoise, isSpacingNoise, isValidArabicContent, normalizeArabicText, processTextAlignment, standardizeHijriSymbol, standardizeIntahaSymbol, tokenizeText };
|
|
726
|
+
export { BRACKETS, CLOSE_BRACKETS, type CharacterError, INTAHA_ACTUAL, OPEN_BRACKETS, PATTERNS, type SanitizeBase, type SanitizeOptions, type SanitizePreset, alignTextSegments, alignTokenSequences, analyzeCharacterStats, areBracketsBalanced, areQuotesBalanced, areSimilarAfterNormalization, backtrackAlignment, boundedLevenshtein, calculateAlignmentScore, calculateLevenshteinDistance, calculateSimilarity, checkBalance, correctReferences, extractDigits, findMatches, findMatchesAll, fixTypo, getUnbalancedErrors, handleFootnoteFusion, handleFootnoteSelection, handleStandaloneFootnotes, hasExcessiveRepetition, hasInvalidFootnotes, isArabicTextNoise, isBalanced, isBasicNoisePattern, isNonArabicNoise, isSpacingNoise, isValidArabicContent, processTextAlignment, sanitizeArabic, standardizeHijriSymbol, standardizeIntahaSymbol, tokenizeText };
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
var
|
|
2
|
-
`),n=0;return r.forEach((o,s)=>{if(o.length>10){let a=O(o);a.isBalanced||a.errors.forEach(i=>{t.push({absoluteIndex:n+i.index,char:i.char,reason:i.reason,type:i.type})})}n+=o.length+(s<r.length-1?1:0)}),t},me=e=>P(e).isBalanced,pe=e=>M(e).isBalanced,be=e=>O(e).isBalanced;var j="()",Y=e=>u.invalidReferenceRegex.test(e),G=new Intl.NumberFormat("ar-SA"),K=e=>G.format(e),F=e=>({1:"\u0661",9:"\u0669",".":"\u0660",O:"\u0665",o:"\u0665",V:"\u0667",v:"\u0667"})[e]||e,Z=e=>{let t={"\u0660":"0","\u0661":"1","\u0662":"2","\u0663":"3","\u0664":"4","\u0665":"5","\u0666":"6","\u0667":"7","\u0668":"8","\u0669":"9"},r=e.replace(/[()]/g,""),n="";for(let s of r)n+=t[s];let o=parseInt(n,10);return isNaN(o)?0:o},w=e=>{let t=e.filter(i=>!i.isFootnote).flatMap(i=>i.text.match(u.arabicReferenceRegex)||[]),r=e.filter(i=>!i.isFootnote).flatMap(i=>i.text.match(u.ocrConfusedReferenceRegex)||[]),n=e.filter(i=>i.isFootnote).flatMap(i=>i.text.match(u.arabicFootnoteReferenceRegex)||[]),o=e.filter(i=>i.isFootnote).flatMap(i=>i.text.match(u.ocrConfusedFootnoteReferenceRegex)||[]),s=r.map(i=>i.replace(/[.1OV9]/g,c=>F(c))),a=o.map(i=>i.replace(/[.1OV9]/g,c=>F(c)));return{bodyReferences:[...t,...s],footnoteReferences:[...n,...a],ocrConfusedInBody:r,ocrConfusedInFootnotes:o}},Q=(e,t)=>{if(e.some(s=>Y(s.text)))return!0;let n=new Set(t.bodyReferences),o=new Set(t.footnoteReferences);if(n.size!==o.size)return!0;for(let s of n)if(!o.has(s))return!0;return!1},Ce=e=>{let t=w(e);if(!Q(e,t))return e;let r=e.map(l=>{let m=l.text,y=/\([.1OV9]+\)/g;return m=m.replace(y,b=>b.replace(/[.1OV9]/g,z=>F(z))),{...l,text:m}}),n=w(r),o=new Set(n.bodyReferences),s=new Set(n.footnoteReferences),a=[...new Set(n.bodyReferences)],i=[...new Set(n.footnoteReferences)],c=a.filter(l=>!s.has(l)),f=i.filter(l=>!o.has(l)),d=[...o,...s],C={count:(d.length>0?Math.max(0,...d.map(l=>Z(l))):0)+1};return r.map(l=>{if(!l.text.includes(j))return l;let m=l.text;return m=m.replace(/\(\)/g,()=>{if(l.isFootnote){let b=c.shift();if(b)return b}else{let b=f.shift();if(b)return b}let y=`(${K(C.count)})`;return C.count++,y}),{...l,text:m}})};var Ae=e=>{if(!e||e.trim().length===0)return!0;let t=e.trim(),r=t.length;if(r<2||J(t))return!0;let n=U(t);if(W(n,r))return!0;let o=u.arabicCharacters.test(t);return!o&&/[a-zA-Z]/.test(t)?!0:o?!ee(n,r):X(n,r,t)};function U(e){let t={arabicCount:0,charFreq:new Map,digitCount:0,latinCount:0,punctuationCount:0,spaceCount:0,symbolCount:0},r=Array.from(e);for(let n of r)t.charFreq.set(n,(t.charFreq.get(n)||0)+1),u.arabicCharacters.test(n)?t.arabicCount++:/\d/.test(n)?t.digitCount++:/[a-zA-Z]/.test(n)?t.latinCount++:/\s/.test(n)?t.spaceCount++:/[.,;:()[\]{}"""''`]/.test(n)?t.punctuationCount++:t.symbolCount++;return t}function W(e,t){let r=0,n=["!",".","-","=","_"];for(let[o,s]of e.charFreq)s>=5&&n.includes(o)&&(r+=s);return r/t>.4}function J(e){return[/^[-=_━≺≻\s]*$/,/^[.\s]*$/,/^[!\s]*$/,/^[A-Z\s]*$/,/^[-\d\s]*$/,/^\d+\s*$/,/^[A-Z]\s*$/,/^[—\s]*$/,/^[्र\s-]*$/].some(r=>r.test(e))}function X(e,t,r){let n=e.arabicCount+e.latinCount+e.digitCount;return n===0||k(e,n,t)?!0:/[٠-٩]/.test(r)&&e.digitCount>=3?!1:(e.symbolCount+Math.max(0,e.punctuationCount-5))/Math.max(n,1)>2||t<=5&&e.arabicCount===0&&!(/^\d+$/.test(r)&&e.digitCount>=3)?!0:/^\d{3,4}$/.test(r)?!1:t<=10}function k(e,t,r){let{arabicCount:n,spaceCount:o}=e;return o>0&&t===o+1&&t<=5||r<=10&&o>=2&&n===0||o/r>.6}function ee(e,t){return e.arabicCount>=3||e.arabicCount>=1&&e.digitCount>0&&t<=20||e.arabicCount>=2&&e.punctuationCount<=2&&t<=10||e.arabicCount>=1&&t<=5&&e.punctuationCount<=1}var te=(e,t,{similarityThreshold:r,typoSymbols:n})=>{if(e===null)return[t];if(t===null)return[e];if(g(e)===g(t))return[e];let o=E(e,t);if(o)return o;let s=N(e,t);if(s)return s;if(n.includes(e)||n.includes(t)){let f=n.find(d=>d===e||d===t);return f?[f]:[e]}let a=g(e),i=g(t);return[x(a,i)>r?e:t]},ne=(e,t)=>{if(e.length===0)return e;let r=[];for(let n of e){if(r.length===0){r.push(n);continue}let o=r.at(-1);if(R(o,n,t)){n.length<o.length&&(r[r.length-1]=n);continue}S(r,o,n)||r.push(n)}return r},re=(e,t,r)=>{let n=A(e,r.typoSymbols),o=A(t,r.typoSymbols),a=B(n,o,r.typoSymbols,r.similarityThreshold).flatMap(([c,f])=>te(c,f,r));return ne(a,r.highSimilarityThreshold).join(" ")},Ee=(e,t,{highSimilarityThreshold:r=.8,similarityThreshold:n=.6,typoSymbols:o})=>re(e,t,{highSimilarityThreshold:r,similarityThreshold:n,typoSymbols:o});export{H as BRACKETS,V as CLOSE_BRACKETS,I as INTAHA_ACTUAL,L as OPEN_BRACKETS,u as PATTERNS,fe as alignTextSegments,B as alignTokenSequences,U as analyzeCharacterStats,pe as areBracketsBalanced,me as areQuotesBalanced,R as areSimilarAfterNormalization,_ as backtrackAlignment,v as calculateAlignmentScore,$ as calculateLevenshteinDistance,x as calculateSimilarity,O as checkBalance,Ce as correctReferences,T as extractDigits,Ee as fixTypo,ge as getUnbalancedErrors,S as handleFootnoteFusion,E as handleFootnoteSelection,N as handleStandaloneFootnotes,W as hasExcessiveRepetition,Y as hasInvalidFootnotes,Ae as isArabicTextNoise,be as isBalanced,J as isBasicNoisePattern,X as isNonArabicNoise,k as isSpacingNoise,ee as isValidArabicContent,g as normalizeArabicText,re as processTextAlignment,oe as standardizeHijriSymbol,se as standardizeIntahaSymbol,A as tokenizeText};
|
|
1
|
+
var ne=/\s+/g,_=/\u0640/g,re=/[\u0610-\u061A\u064B-\u065F\u0670\u06D6-\u06ED]/g,oe=/[أإآٱ]/g,se=/\u0649/g,ae=/\u0629/g,ie=/[\u200B-\u200F\u202A-\u202E\u2060-\u2064\uFEFF]/g,ce=/[A-Za-z]+[0-9]*|[0-9]+|[¬§`=]|[/]{2,}|[&]|[ﷺ]/g,le=/[^\u0621-\u063A\u0641-\u064A\u0671\u067E\u0686\u06A4-\u06AF\u06CC\u06D2\u06D3]/g,ue=/[^\u0621-\u063A\u0641-\u064A\u0671\u067E\u0686\u06A4-\u06AF\u06CC\u06D2\u06D3\s]/g,fe=e=>e===32,pe=e=>e>=48&&e<=57||e>=1632&&e<=1641,me=e=>e.replace(_,(t,r,n)=>{let o=r-1;for(;o>=0&&fe(n.charCodeAt(o));)o--;if(o>=0){let s=n.charCodeAt(o);if(pe(s)||s===1607)return"\u0640"}return""}),ge=e=>e.replace(/([0-9\u0660-\u0669][0-9\u0660-\u0669/\-\s]*?)\s*ه(?:ـ)?(?=(?:\s|$|[^\p{L}\p{N}]))/gu,"$1"),de=(e,t)=>t&&e.normalize?e.normalize("NFC"):e,he=(e,t,r)=>t?e.replace(ie,r?" ":""):e,be=(e,t,r)=>(t&&(e=e.replace(re,"")),r==="safe"?me(e):r==="all"?e.replace(_,""):e),Ae=(e,t,r,n)=>(t&&(e=e.replace(oe,"\u0627")),r&&(e=e.replace(se,"\u064A")),n&&(e=e.replace(ae,"\u0647")),e),ye=(e,t)=>t?e.replace(ce," "):e,xe=(e,t,r)=>t?e.replace(ue," "):r?e.replace(le,""):e,Se=(e,t,r)=>(t&&(e=e.replace(ne," ")),r&&(e=e.trim()),e),A=(e,t)=>t===void 0?e:!!t,Ce=(e,t)=>t===void 0?e:t===!0?"safe":t===!1?!1:t,L={light:{nfc:!0,stripZeroWidth:!0,zeroWidthToSpace:!1,stripDiacritics:!1,stripTatweel:!1,normalizeAlif:!1,replaceAlifMaqsurah:!1,replaceTaMarbutahWithHa:!1,stripLatinAndSymbols:!1,keepOnlyArabicLetters:!1,lettersAndSpacesOnly:!1,collapseWhitespace:!0,trim:!0,removeHijriMarker:!1},search:{nfc:!0,stripZeroWidth:!0,zeroWidthToSpace:!1,stripDiacritics:!0,stripTatweel:"all",normalizeAlif:!0,replaceAlifMaqsurah:!0,replaceTaMarbutahWithHa:!1,stripLatinAndSymbols:!1,keepOnlyArabicLetters:!1,lettersAndSpacesOnly:!1,collapseWhitespace:!0,trim:!0,removeHijriMarker:!0},aggressive:{nfc:!0,stripZeroWidth:!0,zeroWidthToSpace:!1,stripDiacritics:!0,stripTatweel:"all",normalizeAlif:!0,replaceAlifMaqsurah:!0,replaceTaMarbutahWithHa:!0,stripLatinAndSymbols:!0,keepOnlyArabicLetters:!1,lettersAndSpacesOnly:!0,collapseWhitespace:!0,trim:!0,removeHijriMarker:!0}},Te={nfc:!1,stripZeroWidth:!1,zeroWidthToSpace:!1,stripDiacritics:!1,stripTatweel:!1,normalizeAlif:!1,replaceAlifMaqsurah:!1,replaceTaMarbutahWithHa:!1,stripLatinAndSymbols:!1,keepOnlyArabicLetters:!1,lettersAndSpacesOnly:!1,collapseWhitespace:!1,trim:!1,removeHijriMarker:!1},g=(e,t="search")=>{if(!e)return"";let r,n=null;if(typeof t=="string")r=L[t];else{let M=t.base??"light";r=M==="none"?Te:L[M],n=t}let o=A(r.nfc,n?.nfc),s=A(r.stripZeroWidth,n?.stripZeroWidth),a=A(r.zeroWidthToSpace,n?.zeroWidthToSpace),i=A(r.stripDiacritics,n?.stripDiacritics),c=A(r.normalizeAlif,n?.normalizeAlif),u=A(r.replaceAlifMaqsurah,n?.replaceAlifMaqsurah),l=A(r.replaceTaMarbutahWithHa,n?.replaceTaMarbutahWithHa),f=A(r.stripLatinAndSymbols,n?.stripLatinAndSymbols),p=A(r.lettersAndSpacesOnly,n?.lettersAndSpacesOnly),m=A(r.keepOnlyArabicLetters,n?.keepOnlyArabicLetters),b=A(r.collapseWhitespace,n?.collapseWhitespace),C=A(r.trim,n?.trim),y=A(r.removeHijriMarker,n?.removeHijriMarker),T=Ce(r.stripTatweel,n?.stripTatweel),d=e;return d=de(d,o),d=he(d,s,a),y&&(d=ge(d)),d=be(d,i,T),d=Ae(d,c,u,l),p||(d=ye(d,f)),d=xe(d,p,m),d=Se(d,b,C),d};var N=(e,t)=>{let r=e.length,n=t.length;if(r===0)return n;if(n===0)return r;let[o,s]=r<=n?[e,t]:[t,e],a=o.length,i=s.length,c=Array.from({length:a+1},(u,l)=>l);for(let u=1;u<=i;u++){let l=[u];for(let f=1;f<=a;f++){let p=s[u-1]===o[f-1]?0:1,m=Math.min(c[f]+1,l[f-1]+1,c[f-1]+p);l.push(m)}c=l}return c[a]},Me=(e,t,r)=>Math.abs(e.length-t.length)>r?r+1:e.length===0?t.length<=r?t.length:r+1:t.length===0?e.length<=r?e.length:r+1:null,Re=e=>{let t=new Int16Array(e+1),r=new Int16Array(e+1);for(let n=0;n<=e;n++)t[n]=n;return[t,r]},Fe=(e,t,r)=>({from:Math.max(1,e-t),to:Math.min(r,e+t)}),Ee=(e,t,r,n,o,s)=>{let a=e[r-1]===t[n-1]?0:1,i=o[n]+1,c=s[n-1]+1,u=o[n-1]+a;return Math.min(i,c,u)},ze=(e,t,r,n,o,s)=>{let a=t.length,i=n+1,{from:c,to:u}=Fe(r,n,a);s[0]=r;let l=r;for(let f=1;f<c;f++)s[f]=i;for(let f=u+1;f<=a;f++)s[f]=i;for(let f=c;f<=u;f++){let p=Ee(e,t,r,f,o,s);s[f]=p,p<l&&(l=p)}return l},P=(e,t,r)=>{let n=r+1,o=Me(e,t,r);if(o!==null)return o;if(e.length>t.length)return P(t,e,r);let[s,a]=Re(t.length);for(let i=1;i<=e.length;i++){if(ze(e,t,i,r,s,a)>r)return n;for(let u=0;u<=t.length;u++)s[u]=a[u]}return s[t.length]<=r?s[t.length]:n};var x={GAP_PENALTY:-1,MISMATCH_PENALTY:-2,PERFECT_MATCH:2,SOFT_MATCH:1},S=(e,t)=>{let r=Math.max(e.length,t.length)||1,n=N(e,t);return(r-n)/r},R=(e,t,r=.6)=>{let n=g(e),o=g(t);return S(n,o)>=r},ft=(e,t,r,n)=>{let o=g(e),s=g(t);if(o===s)return x.PERFECT_MATCH;let a=r.includes(e)||r.includes(t),i=S(o,s)>=n;return a||i?x.SOFT_MATCH:x.MISMATCH_PENALTY},Pe=(e,t,r)=>{let n=[],o=t.length,s=r.length;for(;o>0||s>0;)switch(e[o][s].direction){case"diagonal":n.push([t[--o],r[--s]]);break;case"left":n.push([null,r[--s]]);break;case"up":n.push([t[--o],null]);break;default:throw new Error("Invalid alignment direction")}return n.reverse()},ve=(e,t)=>{let r=Array.from({length:e+1},()=>Array.from({length:t+1},()=>({direction:null,score:0})));for(let n=1;n<=e;n++)r[n][0]={direction:"up",score:n*x.GAP_PENALTY};for(let n=1;n<=t;n++)r[0][n]={direction:"left",score:n*x.GAP_PENALTY};return r},Ie=(e,t,r)=>{let n=Math.max(e,t,r);return n===e?{direction:"diagonal",score:n}:n===t?{direction:"up",score:n}:{direction:"left",score:n}},W=(e,t,r,n)=>{let o=e.length,s=t.length,a=ve(o,s),i=new Set(r),c=e.map(l=>g(l)),u=t.map(l=>g(l));for(let l=1;l<=o;l++)for(let f=1;f<=s;f++){let p=c[l-1],m=u[f-1],b;if(p===m)b=x.PERFECT_MATCH;else{let ee=i.has(e[l-1])||i.has(t[f-1]),te=S(p,m)>=n;b=ee||te?x.SOFT_MATCH:x.MISMATCH_PENALTY}let C=a[l-1][f-1].score+b,y=a[l-1][f].score+x.GAP_PENALTY,T=a[l][f-1].score+x.GAP_PENALTY,{direction:d,score:M}=Ie(C,y,T);a[l][f]={direction:d,score:M}}return Pe(a,e,t)};var dt=(e,t)=>{let r=[],n=0;for(let o of e){if(n>=t.length)break;if(o){let{result:s,segmentsConsumed:a}=Oe(o,t,n);s&&r.push(s),n+=a}else r.push(t[n]),n++}return n<t.length&&r.push(...t.slice(n)),r},we=(e,t,r)=>{let n=`${t} ${r}`,o=`${r} ${t}`,s=g(e),a=S(s,g(n)),i=S(s,g(o));return a>=i?n:o},Oe=(e,t,r)=>{let n=t[r];if(R(e,n))return{result:n,segmentsConsumed:1};let o=t[r],s=t[r+1];return!o||!s?o?{result:o,segmentsConsumed:1}:{result:"",segmentsConsumed:0}:{result:we(e,o,s),segmentsConsumed:2}};var k=e=>{let t=[],r=0,n=-1;for(let s=0;s<e.length;s++)e[s]==='"'&&(r++,n=s);let o=r%2===0;return!o&&n!==-1&&t.push({char:'"',index:n,reason:"unmatched",type:"quote"}),{errors:t,isBalanced:o}},qe={"\xAB":"\xBB","(":")","[":"]","{":"}"},Be=new Set(["\xAB","(","[","{"]),He=new Set(["\xBB",")","]","}"]),$=e=>{let t=[],r=[];for(let n=0;n<e.length;n++){let o=e[n];if(Be.has(o))r.push({char:o,index:n});else if(He.has(o)){let s=r.pop();s?qe[s.char]!==o&&(t.push({char:s.char,index:s.index,reason:"mismatched",type:"bracket"}),t.push({char:o,index:n,reason:"mismatched",type:"bracket"})):t.push({char:o,index:n,reason:"unmatched",type:"bracket"})}}return r.forEach(({char:n,index:o})=>{t.push({char:n,index:o,reason:"unclosed",type:"bracket"})}),{errors:t,isBalanced:t.length===0}},D=e=>{let t=k(e),r=$(e);return{errors:[...t.errors,...r.errors].sort((n,o)=>n.index-o.index),isBalanced:t.isBalanced&&r.isBalanced}},bt=e=>{let t=[],r=e.split(`
|
|
2
|
+
`),n=0;return r.forEach((o,s)=>{if(o.length>10){let a=D(o);a.isBalanced||a.errors.forEach(i=>{t.push({absoluteIndex:n+i.index,char:i.char,reason:i.reason,type:i.type})})}n+=o.length+(s<r.length-1?1:0)}),t},At=e=>k(e).isBalanced,yt=e=>$(e).isBalanced,xt=e=>D(e).isBalanced;var Ct="\u0627\u0647\u0640",h={arabicCharacters:/[\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF\uFB50-\uFDFF\uFE70-\uFEFF]/,arabicDigits:/[0-9\u0660-\u0669]+/,arabicFootnoteReferenceRegex:/^\([\u0660-\u0669]+\)/g,arabicLettersAndDigits:/[0-9\u0621-\u063A\u0641-\u064A\u0660-\u0669]+/g,arabicPunctuationAndWhitespace:/[\s\u060C\u061B\u061F\u06D4]+/,arabicReferenceRegex:/\([\u0660-\u0669]+\)/g,footnoteEmbedded:/\([0-9\u0660-\u0669]+\)/,footnoteStandalone:/^\(?[0-9\u0660-\u0669]+\)?[،.]?$/,invalidReferenceRegex:/\(\)|\([.1OV9]+\)/g,ocrConfusedFootnoteReferenceRegex:/^\([.1OV9]+\)/g,ocrConfusedReferenceRegex:/\([.1OV9]+\)/g,whitespace:/\s+/},G=e=>{let t=e.match(h.arabicDigits);return t?t[0]:""},v=(e,t=[])=>{let r=e;for(let n of t){let o=new RegExp(n,"g");r=r.replace(o,` ${n} `)}return r.trim().split(h.whitespace).filter(Boolean)},j=(e,t,r)=>{let n=h.footnoteStandalone.test(t),o=h.footnoteEmbedded.test(r),s=h.footnoteStandalone.test(r),a=h.footnoteEmbedded.test(t),i=G(t),c=G(r);return n&&o&&i===c?(e[e.length-1]=r,!0):!!(a&&s&&i===c)},Z=(e,t)=>{let r=h.footnoteEmbedded.test(e),n=h.footnoteEmbedded.test(t);return r&&!n?[e]:n&&!r?[t]:r&&n?[e.length<=t.length?e:t]:null},U=(e,t)=>{let r=h.footnoteStandalone.test(e),n=h.footnoteStandalone.test(t);return r&&!n?[e,t]:n&&!r?[t,e]:r&&n?[e.length<=t.length?e:t]:null},Tt=e=>e.replace(/([0-9\u0660-\u0669])\s*ه(?=\s|$|[^\u0621-\u063A\u0641-\u064A\u0660-\u0669])/gu,"$1 \u0647\u0640"),Mt=e=>e.replace(/(^|\s|[^\u0600-\u06FF])اه(?=\s|$|[^\u0600-\u06FF])/gu,"$1\u0627\u0647\u0640");var Le="()",_e=e=>h.invalidReferenceRegex.test(e),Ne=new Intl.NumberFormat("ar-SA"),We=e=>Ne.format(e),I=e=>({1:"\u0661",9:"\u0669",".":"\u0660",O:"\u0665",o:"\u0665",V:"\u0667",v:"\u0667"})[e]||e,ke=e=>{let t={"\u0660":"0","\u0661":"1","\u0662":"2","\u0663":"3","\u0664":"4","\u0665":"5","\u0666":"6","\u0667":"7","\u0668":"8","\u0669":"9"},r=e.replace(/[()]/g,""),n="";for(let s of r)n+=t[s];let o=parseInt(n,10);return isNaN(o)?0:o},Y=e=>{let t=e.filter(i=>!i.isFootnote).flatMap(i=>i.text.match(h.arabicReferenceRegex)||[]),r=e.filter(i=>!i.isFootnote).flatMap(i=>i.text.match(h.ocrConfusedReferenceRegex)||[]),n=e.filter(i=>i.isFootnote).flatMap(i=>i.text.match(h.arabicFootnoteReferenceRegex)||[]),o=e.filter(i=>i.isFootnote).flatMap(i=>i.text.match(h.ocrConfusedFootnoteReferenceRegex)||[]),s=r.map(i=>i.replace(/[.1OV9]/g,c=>I(c))),a=o.map(i=>i.replace(/[.1OV9]/g,c=>I(c)));return{bodyReferences:[...t,...s],footnoteReferences:[...n,...a],ocrConfusedInBody:r,ocrConfusedInFootnotes:o}},$e=(e,t)=>{if(e.some(s=>_e(s.text)))return!0;let n=new Set(t.bodyReferences),o=new Set(t.footnoteReferences);if(n.size!==o.size)return!0;for(let s of n)if(!o.has(s))return!0;return!1},Et=e=>{let t=Y(e);if(!$e(e,t))return e;let r=e.map(m=>{let b=m.text,C=/\([.1OV9]+\)/g;return b=b.replace(C,y=>y.replace(/[.1OV9]/g,T=>I(T))),{...m,text:b}}),n=Y(r),o=new Set(n.bodyReferences),s=new Set(n.footnoteReferences),a=[...new Set(n.bodyReferences)],i=[...new Set(n.footnoteReferences)],c=a.filter(m=>!s.has(m)),u=i.filter(m=>!o.has(m)),l=[...o,...s],p={count:(l.length>0?Math.max(0,...l.map(m=>ke(m))):0)+1};return r.map(m=>{if(!m.text.includes(Le))return m;let b=m.text;return b=b.replace(/\(\)/g,()=>{if(m.isFootnote){let y=c.shift();if(y)return y}else{let y=u.shift();if(y)return y}let C=`(${We(p.count)})`;return p.count++,C}),{...m,text:b}})};var F=class{next=new Map;link=0;out=[]},w=class{nodes=[new F];add(t,r){let n=0;for(let o=0;o<t.length;o++){let s=t[o],a=this.nodes[n].next.get(s);a===void 0&&(a=this.nodes.length,this.nodes[n].next.set(s,a),this.nodes.push(new F)),n=a}this.nodes[n].out.push(r)}build(){let t=[];for(let[,r]of this.nodes[0].next)this.nodes[r].link=0,t.push(r);for(let r=0;r<t.length;r++){let n=t[r];for(let[o,s]of this.nodes[n].next){t.push(s);let a=this.nodes[n].link;for(;a!==0&&!this.nodes[a].next.has(o);)a=this.nodes[a].link;let i=this.nodes[a].next.get(o);this.nodes[s].link=i===void 0?0:i;let c=this.nodes[this.nodes[s].link].out;c.length&&this.nodes[s].out.push(...c)}}}find(t,r){let n=0;for(let o=0;o<t.length;o++){let s=t[o];for(;n!==0&&!this.nodes[n].next.has(s);)n=this.nodes[n].link;let a=this.nodes[n].next.get(s);if(n=a===void 0?0:a,this.nodes[n].out.length)for(let i of this.nodes[n].out)r(i,o+1)}}},E=e=>{let t=new w;for(let r=0;r<e.length;r++){let n=e[r];n.length>0&&t.add(n,r)}return t.build(),t};var O={enableFuzzy:!0,maxEditAbs:3,maxEditRel:.1,q:4,gramsPerExcerpt:5,maxCandidatesPerExcerpt:40,seamLen:512};function q(e){let t=[],r=[],n=[],o=0;for(let s=0;s<e.length;s++){let a=e[s];r.push(o),n.push(a.length),t.push(a),o+=a.length,s+1<e.length&&(t.push(" "),o+=1)}return{book:t.join(""),starts:r,lens:n}}function B(e,t){let r=0,n=t.length-1,o=0;for(;r<=n;){let s=r+n>>1;t[s]<=e?(o=s,r=s+1):n=s-1}return o}function V(e,t,r,n,o){let s=E(r),a=new Int32Array(o).fill(-1),i=new Uint8Array(o);return s.find(e,(c,u)=>{let l=r[c],f=u-l.length,p=B(f,t);for(let m of n[c])i[m]||(a[m]=p,i[m]=1)}),{result:a,seenExact:i}}function H(e){let t=new Map,r=[],n=[];for(let o=0;o<e.length;o++){let s=e[o],a=t.get(s);a===void 0?(a=n.length,t.set(s,a),n.push(s),r.push([o])):r[a].push(o)}return{keyToPatId:t,patIdToOrigIdxs:r,patterns:n}}var De=(e,t,r)=>{let n=[];for(let o of t)if(e.has(o.gram)&&(n.push({gram:o.gram,offset:o.offset}),n.length>=r))break;return n},Ge=(e,t,r)=>{let n=[];for(let o=t.length-1;o>=0&&n.length<r;o--){let s=t[o];e.has(s.gram)&&n.push({gram:s.gram,offset:s.offset})}return n},z=class{q;map=new Map;gramFreq=new Map;constructor(t){this.q=t}addText(t,r,n){this.addGramsToMap(t,r,n),this.updateGramFrequencies(r)}addGramsToMap(t,r,n){for(let o=0;o+this.q<=r.length;o++){let s=r.slice(o,o+this.q),a=this.map.get(s);a||(a=[],this.map.set(s,a)),a.push({page:t,pos:o,seam:n})}}updateGramFrequencies(t){for(let r=0;r+this.q<=t.length;r++){let n=t.slice(r,r+this.q);this.gramFreq.set(n,(this.gramFreq.get(n)??0)+1)}}extractUniqueGrams(t){let r=[],n=new Set;for(let o=0;o+this.q<=t.length;o++){let s=t.slice(o,o+this.q);if(n.has(s))continue;n.add(s);let a=this.gramFreq.get(s)??2147483647;r.push({gram:s,offset:o,freq:a})}return r.sort((o,s)=>o.freq-s.freq)}pickRare(t,r){r=Math.max(1,Math.floor(r));let n=this.extractUniqueGrams(t),o=De(this.map,n,r);return o.length>0?o:Ge(this.map,n,r)}getPostings(t){return this.map.get(t)}};function X(e,t){let r=[];for(let n=0;n+1<e.length;n++){let o=e[n].slice(-t),s=e[n+1].slice(0,t),a=`${o} ${s}`;r.push({text:a,startPage:n})}return r}function Q(e,t,r){let n=new z(r);for(let o=0;o<e.length;o++)n.addText(o,e[o],!1);for(let o=0;o<t.length;o++)n.addText(o,t[o].text,!0);return n}function K(e,t,r){let n=t.pickRare(e,r.gramsPerExcerpt);if(n.length===0)return[];let o=[],s=e.length;for(let{gram:a,offset:i}of n){let c=t.getPostings(a);if(c){for(let u of c){let l=u.pos-i;if(!(l<-Math.floor(s*.25))&&(o.push({page:u.page,start:Math.max(0,l),seam:u.seam}),o.length>=r.maxCandidatesPerExcerpt))break}if(o.length>=r.maxCandidatesPerExcerpt)break}}return o}function J(e,t,r,n,o){let s=t.seam?n[t.page]?.text:r[t.page];if(!s)return null;let a=e.length,i=Math.min(o,Math.max(6,Math.ceil(a*.12))),c=Math.max(0,t.start-Math.floor(i/2)),u=Math.min(s.length,c+a+i);if(u<=c)return null;let l=s.slice(c,u),f=P(e,l,o);return f<=o?f:null}function je(e,t,r,n,o){if(e.length===0)return null;let s=Math.max(o.maxEditAbs,Math.ceil(o.maxEditRel*e.length)),a=new Set,i=null;for(let c of t){let u=`${c.page}:${c.start}:${c.seam?1:0}`;if(a.has(u))continue;a.add(u);let l=J(e,c,r,n,s);if(l!==null&&(!i||l<i.dist||l===i.dist&&c.page<i.page)&&(i={page:c.page,dist:l},l===0))break}return i}function Ze(e,t,r,n,o){if(!o.enableFuzzy)return;let s=X(t,o.seamLen),a=Q(t,s,o.q);for(let i=0;i<e.length;i++){if(r[i])continue;let c=e[i];if(!c||c.length<o.q)continue;let u=K(c,a,o);if(u.length===0)continue;let l=je(c,u,t,s,o);l&&(n[i]=l.page,r[i]=1)}}function Wt(e,t,r={}){let n={...O,...r},o=e.map(p=>g(p,"aggressive")),s=t.map(p=>g(p,"aggressive")),{patIdToOrigIdxs:a,patterns:i}=H(s),{book:c,starts:u}=q(o),{result:l,seenExact:f}=V(c,u,i,a,t.length);return f.every(p=>p===1)||Ze(s,o,f,l,n),Array.from(l)}function Ue(e,t,r,n,o){E(r).find(e,(a,i)=>{let c=r[a],u=i-c.length,l=B(u,t);for(let f of n[a]){let p=o[f],m=p.get(l);(!m||!m.exact)&&p.set(l,{score:1,exact:!0})}})}function Ye(e,t,r,n,o,s,a){let i=`${e.page}:${e.start}:${e.seam?1:0}`;if(a.has(i))return;a.add(i);let c=J(t,e,r,n,o);if(c===null)return;let u=1-c/o,l=s.get(e.page);(!l||!l.exact&&u>l.score)&&s.set(e.page,{score:u,exact:!1})}function Ve(e,t,r,n,o,s,a){if(Array.from(s[e].values()).some(p=>p.exact)||!t||t.length<a.q)return;let c=K(t,o,a);if(c.length===0)return;let u=Math.max(a.maxEditAbs,Math.ceil(a.maxEditRel*t.length)),l=new Set,f=s[e];for(let p of c)Ye(p,t,r,n,u,f,l)}function Xe(e,t,r,n){let o=X(t,n.seamLen),s=Q(t,o,n.q);for(let a=0;a<e.length;a++)Ve(a,e[a],t,o,s,r,n)}function Qe(e){if(e.size===0)return[];let t=[],r=[];for(let n of e.entries())n[1].exact?t.push(n):r.push(n);return t.sort((n,o)=>n[0]-o[0]),r.sort((n,o)=>o[1].score-n[1].score||n[0]-o[0]),[...t,...r].map(n=>n[0])}function kt(e,t,r={}){let n={...O,...r},o=e.map(f=>g(f,"aggressive")),s=t.map(f=>g(f,"aggressive")),{patIdToOrigIdxs:a,patterns:i}=H(s),{book:c,starts:u}=q(o),l=Array.from({length:t.length},()=>new Map);return Ue(c,u,i,a,l),n.enableFuzzy&&Xe(s,o,l,n),l.map(f=>Qe(f))}var Gt=e=>{if(!e||e.trim().length===0)return!0;let t=e.trim(),r=t.length;if(r<2||et(t))return!0;let n=Ke(t);if(Je(n,r))return!0;let o=h.arabicCharacters.test(t);return!o&&/[a-zA-Z]/.test(t)?!0:o?!rt(n,r):tt(n,r,t)};function Ke(e){let t={arabicCount:0,charFreq:new Map,digitCount:0,latinCount:0,punctuationCount:0,spaceCount:0,symbolCount:0},r=Array.from(e);for(let n of r)t.charFreq.set(n,(t.charFreq.get(n)||0)+1),h.arabicCharacters.test(n)?t.arabicCount++:/\d/.test(n)?t.digitCount++:/[a-zA-Z]/.test(n)?t.latinCount++:/\s/.test(n)?t.spaceCount++:/[.,;:()[\]{}"""''`]/.test(n)?t.punctuationCount++:t.symbolCount++;return t}function Je(e,t){let r=0,n=["!",".","-","=","_"];for(let[o,s]of e.charFreq)s>=5&&n.includes(o)&&(r+=s);return r/t>.4}function et(e){return[/^[-=_━≺≻\s]*$/,/^[.\s]*$/,/^[!\s]*$/,/^[A-Z\s]*$/,/^[-\d\s]*$/,/^\d+\s*$/,/^[A-Z]\s*$/,/^[—\s]*$/,/^[्र\s-]*$/].some(r=>r.test(e))}function tt(e,t,r){let n=e.arabicCount+e.latinCount+e.digitCount;return n===0||nt(e,n,t)?!0:/[٠-٩]/.test(r)&&e.digitCount>=3?!1:(e.symbolCount+Math.max(0,e.punctuationCount-5))/Math.max(n,1)>2||t<=5&&e.arabicCount===0&&!(/^\d+$/.test(r)&&e.digitCount>=3)?!0:/^\d{3,4}$/.test(r)?!1:t<=10}function nt(e,t,r){let{arabicCount:n,spaceCount:o}=e;return o>0&&t===o+1&&t<=5||r<=10&&o>=2&&n===0||o/r>.6}function rt(e,t){return e.arabicCount>=3||e.arabicCount>=1&&e.digitCount>0&&t<=20||e.arabicCount>=2&&e.punctuationCount<=2&&t<=10||e.arabicCount>=1&&t<=5&&e.punctuationCount<=1}var ot=(e,t,{similarityThreshold:r,typoSymbols:n})=>{if(e===null)return[t];if(t===null)return[e];if(g(e)===g(t))return[e];let o=Z(e,t);if(o)return o;let s=U(e,t);if(s)return s;if(n.includes(e)||n.includes(t)){let u=n.find(l=>l===e||l===t);return u?[u]:[e]}let a=g(e),i=g(t);return[S(a,i)>r?e:t]},st=(e,t)=>{if(e.length===0)return e;let r=[];for(let n of e){if(r.length===0){r.push(n);continue}let o=r.at(-1);if(R(o,n,t)){n.length<o.length&&(r[r.length-1]=n);continue}j(r,o,n)||r.push(n)}return r},at=(e,t,r)=>{let n=v(e,r.typoSymbols),o=v(t,r.typoSymbols),a=W(n,o,r.typoSymbols,r.similarityThreshold).flatMap(([c,u])=>ot(c,u,r));return st(a,r.highSimilarityThreshold).join(" ")},Vt=(e,t,{highSimilarityThreshold:r=.8,similarityThreshold:n=.6,typoSymbols:o})=>at(e,t,{highSimilarityThreshold:r,similarityThreshold:n,typoSymbols:o});export{qe as BRACKETS,He as CLOSE_BRACKETS,Ct as INTAHA_ACTUAL,Be as OPEN_BRACKETS,h as PATTERNS,dt as alignTextSegments,W as alignTokenSequences,Ke as analyzeCharacterStats,yt as areBracketsBalanced,At as areQuotesBalanced,R as areSimilarAfterNormalization,Pe as backtrackAlignment,P as boundedLevenshtein,ft as calculateAlignmentScore,N as calculateLevenshteinDistance,S as calculateSimilarity,D as checkBalance,Et as correctReferences,G as extractDigits,Wt as findMatches,kt as findMatchesAll,Vt as fixTypo,bt as getUnbalancedErrors,j as handleFootnoteFusion,Z as handleFootnoteSelection,U as handleStandaloneFootnotes,Je as hasExcessiveRepetition,_e as hasInvalidFootnotes,Gt as isArabicTextNoise,xt as isBalanced,et as isBasicNoisePattern,tt as isNonArabicNoise,nt as isSpacingNoise,rt as isValidArabicContent,at as processTextAlignment,g as sanitizeArabic,Tt as standardizeHijriSymbol,Mt as standardizeIntahaSymbol,v as tokenizeText};
|
|
3
3
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/textUtils.ts","../src/similarity.ts","../src/alignment.ts","../src/balance.ts","../src/footnotes.ts","../src/noise.ts","../src/index.ts"],"sourcesContent":["export const INTAHA_ACTUAL = 'اهـ';\n\n/**\n * Collection of regex patterns used throughout the library for text processing\n */\nexport const PATTERNS = {\n /** Matches Arabic characters across all Unicode blocks */\n arabicCharacters: /[\\u0600-\\u06FF\\u0750-\\u077F\\u08A0-\\u08FF\\uFB50-\\uFDFF\\uFE70-\\uFEFF]/,\n\n /** Matches Arabic-Indic digits (٠-٩) and Western digits (0-9) */\n arabicDigits: /[0-9\\u0660-\\u0669]+/,\n\n /** Matches footnote references at the start of a line with Arabic-Indic digits: ^\\([\\u0660-\\u0669]+\\) */\n arabicFootnoteReferenceRegex: /^\\([\\u0660-\\u0669]+\\)/g,\n\n /** Matches Arabic letters and digits (both Western 0-9 and Arabic-Indic ٠-٩) */\n arabicLettersAndDigits: /[0-9\\u0621-\\u063A\\u0641-\\u064A\\u0660-\\u0669]+/g,\n\n /** Matches Arabic punctuation marks and whitespace characters */\n arabicPunctuationAndWhitespace: /[\\s\\u060C\\u061B\\u061F\\u06D4]+/,\n\n /** Matches footnote references with Arabic-Indic digits in parentheses: \\([\\u0660-\\u0669]+\\) */\n arabicReferenceRegex: /\\([\\u0660-\\u0669]+\\)/g,\n\n /** Matches Arabic diacritical marks (harakat, tanween, etc.) */\n diacritics: /[\\u0610-\\u061A\\u064B-\\u065F\\u0670\\u06D6-\\u06ED]/g,\n\n /** Matches embedded footnotes within text: \\([0-9\\u0660-\\u0669]+\\) */\n footnoteEmbedded: /\\([0-9\\u0660-\\u0669]+\\)/,\n\n /** Matches standalone footnote markers at line start/end: ^\\(?[0-9\\u0660-\\u0669]+\\)?[،.]?$ */\n footnoteStandalone: /^\\(?[0-9\\u0660-\\u0669]+\\)?[،.]?$/,\n\n /** Matches invalid/problematic footnote references: empty \"()\" or OCR-confused endings */\n invalidReferenceRegex: /\\(\\)|\\([.1OV9]+\\)/g, // Combined pattern for detecting any invalid/problematic references\n\n /** Matches OCR-confused footnote references at line start with characters like .1OV9 */\n ocrConfusedFootnoteReferenceRegex: /^\\([.1OV9]+\\)/g,\n\n /** Matches OCR-confused footnote references with characters commonly misread as Arabic digits */\n ocrConfusedReferenceRegex: /\\([.1OV9]+\\)/g,\n\n /** Matches Arabic tatweel (kashida) character used for text stretching */\n tatweel: /\\u0640/g,\n\n /** Matches one or more whitespace characters */\n whitespace: /\\s+/,\n};\n\n/**\n * Normalizes Arabic text by removing diacritics, and tatweel marks.\n * This normalization enables better text comparison by focusing on core characters\n * while ignoring decorative elements that don't affect meaning.\n *\n * @param text - Arabic text to normalize\n * @returns Normalized text with diacritics, tatweel, and basic tags removed\n * @example\n * normalizeArabicText('اَلسَّلَامُ عَلَيْكُمْ') // Returns 'السلام عليكم'\n */\nexport const normalizeArabicText = (text: string): string => {\n return text.replace(PATTERNS.tatweel, '').replace(PATTERNS.diacritics, '').trim();\n};\n\n/**\n * Extracts the first sequence of Arabic or Western digits from text.\n * Used primarily for footnote number comparison to match related footnote elements.\n *\n * @param text - Text containing digits to extract\n * @returns First digit sequence found, or empty string if none found\n * @example\n * extractDigits('(٥)أخرجه البخاري') // Returns '٥'\n * extractDigits('See note (123)') // Returns '123'\n */\nexport const extractDigits = (text: string): string => {\n const match = text.match(PATTERNS.arabicDigits);\n return match ? match[0] : '';\n};\n\n/**\n * Tokenizes text into individual words while preserving special symbols.\n * Removes HTML tags, adds spacing around preserved symbols to ensure they\n * are tokenized separately, then splits on whitespace.\n *\n * @param text - Text to tokenize\n * @param preserveSymbols - Array of symbols that should be tokenized as separate tokens\n * @returns Array of tokens, or empty array if input is empty/whitespace\n * @example\n * tokenizeText('Hello ﷺ world', ['ﷺ']) // Returns ['Hello', 'ﷺ', 'world']\n */\nexport const tokenizeText = (text: string, preserveSymbols: string[] = []): string[] => {\n let processedText = text;\n\n // Add spaces around each preserve symbol to ensure they're tokenized separately\n for (const symbol of preserveSymbols) {\n const symbolRegex = new RegExp(symbol, 'g');\n processedText = processedText.replace(symbolRegex, ` ${symbol} `);\n }\n\n return processedText.trim().split(PATTERNS.whitespace).filter(Boolean);\n};\n\n/**\n * Handles fusion of standalone and embedded footnotes during token processing.\n * Detects patterns where standalone footnotes should be merged with embedded ones\n * or where trailing standalone footnotes should be skipped.\n *\n * @param result - Current result array being built\n * @param previousToken - The previous token in the sequence\n * @param currentToken - The current token being processed\n * @returns True if the current token was handled (fused or skipped), false otherwise\n * @example\n * // (٥) + (٥)أخرجه → result gets (٥)أخرجه\n * // (٥)أخرجه + (٥) → (٥) is skipped\n */\nexport const handleFootnoteFusion = (result: string[], previousToken: string, currentToken: string): boolean => {\n const prevIsStandalone = PATTERNS.footnoteStandalone.test(previousToken);\n const currHasEmbedded = PATTERNS.footnoteEmbedded.test(currentToken);\n const currIsStandalone = PATTERNS.footnoteStandalone.test(currentToken);\n const prevHasEmbedded = PATTERNS.footnoteEmbedded.test(previousToken);\n\n const prevDigits = extractDigits(previousToken);\n const currDigits = extractDigits(currentToken);\n\n // Replace standalone with fused version: (٥) + (٥)أخرجه → (٥)أخرجه\n if (prevIsStandalone && currHasEmbedded && prevDigits === currDigits) {\n result[result.length - 1] = currentToken;\n return true;\n }\n\n // Skip trailing standalone: (٥)أخرجه + (٥) → (٥)أخرجه\n if (prevHasEmbedded && currIsStandalone && prevDigits === currDigits) {\n return true;\n }\n\n return false;\n};\n\n/**\n * Handles selection logic for tokens with embedded footnotes during alignment.\n * Prefers tokens that contain embedded footnotes over plain text, and among\n * tokens with embedded footnotes, prefers the shorter one.\n *\n * @param tokenA - First token to compare\n * @param tokenB - Second token to compare\n * @returns Array containing selected token(s), or null if no special handling needed\n * @example\n * handleFootnoteSelection('text', '(١)text') // Returns ['(١)text']\n * handleFootnoteSelection('(١)longtext', '(١)text') // Returns ['(١)text']\n */\nexport const handleFootnoteSelection = (tokenA: string, tokenB: string): null | string[] => {\n const aHasEmbedded = PATTERNS.footnoteEmbedded.test(tokenA);\n const bHasEmbedded = PATTERNS.footnoteEmbedded.test(tokenB);\n\n if (aHasEmbedded && !bHasEmbedded) return [tokenA];\n if (bHasEmbedded && !aHasEmbedded) return [tokenB];\n if (aHasEmbedded && bHasEmbedded) {\n return [tokenA.length <= tokenB.length ? tokenA : tokenB];\n }\n\n return null;\n};\n\n/**\n * Handles selection logic for standalone footnote tokens during alignment.\n * Manages cases where one or both tokens are standalone footnotes, preserving\n * both tokens when one is a footnote and the other is regular text.\n *\n * @param tokenA - First token to compare\n * @param tokenB - Second token to compare\n * @returns Array containing selected token(s), or null if no special handling needed\n * @example\n * handleStandaloneFootnotes('(١)', 'text') // Returns ['(١)', 'text']\n * handleStandaloneFootnotes('(١)', '(٢)') // Returns ['(١)'] (shorter one)\n */\nexport const handleStandaloneFootnotes = (tokenA: string, tokenB: string): null | string[] => {\n const aIsFootnote = PATTERNS.footnoteStandalone.test(tokenA);\n const bIsFootnote = PATTERNS.footnoteStandalone.test(tokenB);\n\n if (aIsFootnote && !bIsFootnote) return [tokenA, tokenB];\n if (bIsFootnote && !aIsFootnote) return [tokenB, tokenA];\n if (aIsFootnote && bIsFootnote) {\n return [tokenA.length <= tokenB.length ? tokenA : tokenB];\n }\n\n return null;\n};\n\n/**\n * Standardizes standalone Hijri symbol ه to هـ when following Arabic digits\n * @param text - Input text to process\n * @returns Text with standardized Hijri symbols\n */\nexport const standardizeHijriSymbol = (text: string): string => {\n // Replace standalone ه with هـ when it appears after Arabic digits (0-9 or ٠-٩)\n // Allow any amount of whitespace between the digit and ه, and consider Arabic punctuation as a boundary.\n // Boundary rule: only Arabic letters/digits should block replacement; punctuation should not.\n return text.replace(/([0-9\\u0660-\\u0669])\\s*ه(?=\\s|$|[^\\u0621-\\u063A\\u0641-\\u064A\\u0660-\\u0669])/gu, '$1 هـ');\n};\n\n/**\n * Standardizes standalone اه to اهـ when appearing as whole word\n * @param text - Input text to process\n * @returns Text with standardized AH Hijri symbols\n */\nexport const standardizeIntahaSymbol = (text: string) => {\n // Replace standalone اه with اهـ when it appears as a whole word\n // Ensures it's preceded by start/whitespace/non-Arabic AND followed by end/whitespace/non-Arabic\n return text.replace(/(^|\\s|[^\\u0600-\\u06FF])اه(?=\\s|$|[^\\u0600-\\u06FF])/g, `$1${INTAHA_ACTUAL}`);\n};\n","import { normalizeArabicText } from './textUtils';\n\n// Alignment scoring constants\nconst ALIGNMENT_SCORES = {\n GAP_PENALTY: -1,\n MISMATCH_PENALTY: -2,\n PERFECT_MATCH: 2,\n SOFT_MATCH: 1,\n};\n\n/**\n * Calculates Levenshtein distance between two strings using space-optimized dynamic programming.\n * The Levenshtein distance is the minimum number of single-character edits (insertions,\n * deletions, or substitutions) required to change one string into another.\n *\n * @param textA - First string to compare\n * @param textB - Second string to compare\n * @returns Minimum edit distance between the two strings\n * @complexity Time: O(m*n), Space: O(min(m,n)) where m,n are string lengths\n * @example\n * calculateLevenshteinDistance('kitten', 'sitting') // Returns 3\n * calculateLevenshteinDistance('', 'hello') // Returns 5\n */\nexport const calculateLevenshteinDistance = (textA: string, textB: string): number => {\n const lengthA = textA.length;\n const lengthB = textB.length;\n\n if (lengthA === 0) {\n return lengthB;\n }\n\n if (lengthB === 0) {\n return lengthA;\n }\n\n // Use shorter string for the array to optimize space\n const [shorter, longer] = lengthA <= lengthB ? [textA, textB] : [textB, textA];\n const shortLen = shorter.length;\n const longLen = longer.length;\n\n let previousRow = Array.from({ length: shortLen + 1 }, (_, index) => index);\n\n for (let i = 1; i <= longLen; i++) {\n const currentRow = [i];\n\n for (let j = 1; j <= shortLen; j++) {\n const substitutionCost = longer[i - 1] === shorter[j - 1] ? 0 : 1;\n const minCost = Math.min(\n previousRow[j] + 1, // deletion\n currentRow[j - 1] + 1, // insertion\n previousRow[j - 1] + substitutionCost, // substitution\n );\n currentRow.push(minCost);\n }\n\n previousRow = currentRow;\n }\n\n return previousRow[shortLen];\n};\n\n/**\n * Calculates similarity ratio between two strings as a value between 0.0 and 1.0.\n * Uses Levenshtein distance normalized by the length of the longer string.\n * A ratio of 1.0 indicates identical strings, 0.0 indicates completely different strings.\n *\n * @param textA - First string to compare\n * @param textB - Second string to compare\n * @returns Similarity ratio from 0.0 (completely different) to 1.0 (identical)\n * @example\n * calculateSimilarity('hello', 'hello') // Returns 1.0\n * calculateSimilarity('hello', 'help') // Returns 0.6\n */\nexport const calculateSimilarity = (textA: string, textB: string): number => {\n const maxLength = Math.max(textA.length, textB.length) || 1;\n const distance = calculateLevenshteinDistance(textA, textB);\n return (maxLength - distance) / maxLength;\n};\n\n/**\n * Checks if two texts are similar after Arabic normalization.\n * Normalizes both texts by removing diacritics and decorative elements,\n * then compares their similarity against the provided threshold.\n *\n * @param textA - First text to compare\n * @param textB - Second text to compare\n * @param threshold - Similarity threshold (0.0 to 1.0)\n * @returns True if normalized texts meet the similarity threshold\n * @example\n * areSimilarAfterNormalization('السَّلام', 'السلام', 0.9) // Returns true\n */\nexport const areSimilarAfterNormalization = (textA: string, textB: string, threshold: number = 0.6): boolean => {\n const normalizedA = normalizeArabicText(textA);\n const normalizedB = normalizeArabicText(textB);\n return calculateSimilarity(normalizedA, normalizedB) >= threshold;\n};\n\n/**\n * Calculates alignment score for two tokens in sequence alignment.\n * Uses different scoring criteria: perfect match after normalization gets highest score,\n * typo symbols or highly similar tokens get soft match score, mismatches get penalty.\n *\n * @param tokenA - First token to score\n * @param tokenB - Second token to score\n * @param typoSymbols - Array of special symbols that get preferential treatment\n * @param similarityThreshold - Threshold for considering tokens highly similar\n * @returns Alignment score (higher is better match)\n * @example\n * calculateAlignmentScore('hello', 'hello', [], 0.8) // Returns 2 (perfect match)\n * calculateAlignmentScore('hello', 'help', [], 0.8) // Returns 1 or -2 based on similarity\n */\nexport const calculateAlignmentScore = (\n tokenA: string,\n tokenB: string,\n typoSymbols: string[],\n similarityThreshold: number,\n): number => {\n const normalizedA = normalizeArabicText(tokenA);\n const normalizedB = normalizeArabicText(tokenB);\n\n // Perfect match after normalization\n if (normalizedA === normalizedB) {\n return ALIGNMENT_SCORES.PERFECT_MATCH;\n }\n\n // Check if either token is a typo symbol or high similarity\n const isTypoSymbol = typoSymbols.includes(tokenA) || typoSymbols.includes(tokenB);\n const isHighlySimilar = calculateSimilarity(normalizedA, normalizedB) >= similarityThreshold;\n\n if (isTypoSymbol || isHighlySimilar) {\n return ALIGNMENT_SCORES.SOFT_MATCH;\n }\n\n return ALIGNMENT_SCORES.MISMATCH_PENALTY;\n};\n\ntype AlignedTokenPair = [null | string, null | string];\n\ntype AlignmentCell = {\n direction: 'diagonal' | 'left' | 'up' | null;\n score: number;\n};\n\n/**\n * Backtracks through the scoring matrix to reconstruct optimal sequence alignment.\n * Follows the directional indicators in the matrix to build the sequence of aligned\n * token pairs from the Needleman-Wunsch algorithm.\n *\n * @param matrix - Scoring matrix with directional information from alignment\n * @param tokensA - First sequence of tokens\n * @param tokensB - Second sequence of tokens\n * @returns Array of aligned token pairs, where null indicates a gap\n * @throws Error if invalid alignment direction is encountered\n */\nexport const backtrackAlignment = (\n matrix: AlignmentCell[][],\n tokensA: string[],\n tokensB: string[],\n): AlignedTokenPair[] => {\n const alignment: AlignedTokenPair[] = [];\n let i = tokensA.length;\n let j = tokensB.length;\n\n while (i > 0 || j > 0) {\n const currentCell = matrix[i][j];\n\n switch (currentCell.direction) {\n case 'diagonal':\n alignment.push([tokensA[--i], tokensB[--j]]);\n break;\n case 'left':\n alignment.push([null, tokensB[--j]]);\n break;\n case 'up':\n alignment.push([tokensA[--i], null]);\n break;\n default:\n throw new Error('Invalid alignment direction');\n }\n }\n\n return alignment.reverse();\n};\n\n/**\n * Performs global sequence alignment using the Needleman-Wunsch algorithm.\n * Aligns two token sequences to find the optimal pairing that maximizes\n * the total alignment score, handling insertions, deletions, and substitutions.\n *\n * @param tokensA - First sequence of tokens to align\n * @param tokensB - Second sequence of tokens to align\n * @param typoSymbols - Special symbols that affect scoring\n * @param similarityThreshold - Threshold for high similarity scoring\n * @returns Array of aligned token pairs, with null indicating gaps\n * @example\n * alignTokenSequences(['a', 'b'], ['a', 'c'], [], 0.8)\n * // Returns [['a', 'a'], ['b', 'c']]\n */\nexport const alignTokenSequences = (\n tokensA: string[],\n tokensB: string[],\n typoSymbols: string[],\n similarityThreshold: number,\n): AlignedTokenPair[] => {\n const lengthA = tokensA.length;\n const lengthB = tokensB.length;\n\n // Initialize scoring matrix\n const scoringMatrix: AlignmentCell[][] = Array.from({ length: lengthA + 1 }, () =>\n Array.from({ length: lengthB + 1 }, () => ({ direction: null, score: 0 })),\n );\n\n // Initialize first row and column\n for (let i = 1; i <= lengthA; i++) {\n scoringMatrix[i][0] = { direction: 'up', score: i * ALIGNMENT_SCORES.GAP_PENALTY };\n }\n for (let j = 1; j <= lengthB; j++) {\n scoringMatrix[0][j] = { direction: 'left', score: j * ALIGNMENT_SCORES.GAP_PENALTY };\n }\n\n // Fill scoring matrix\n for (let i = 1; i <= lengthA; i++) {\n for (let j = 1; j <= lengthB; j++) {\n const alignmentScore = calculateAlignmentScore(\n tokensA[i - 1],\n tokensB[j - 1],\n typoSymbols,\n similarityThreshold,\n );\n\n const diagonalScore = scoringMatrix[i - 1][j - 1].score + alignmentScore;\n const upScore = scoringMatrix[i - 1][j].score + ALIGNMENT_SCORES.GAP_PENALTY;\n const leftScore = scoringMatrix[i][j - 1].score + ALIGNMENT_SCORES.GAP_PENALTY;\n\n const bestScore = Math.max(diagonalScore, upScore, leftScore);\n let bestDirection: 'diagonal' | 'left' | 'up' = 'left';\n\n if (bestScore === diagonalScore) {\n bestDirection = 'diagonal';\n } else if (bestScore === upScore) {\n bestDirection = 'up';\n }\n\n scoringMatrix[i][j] = { direction: bestDirection, score: bestScore };\n }\n }\n\n // Backtrack to build alignment\n return backtrackAlignment(scoringMatrix, tokensA, tokensB);\n};\n","import { areSimilarAfterNormalization, calculateSimilarity } from './similarity';\nimport { normalizeArabicText } from './textUtils';\n\n/**\n * Aligns split text segments to match target lines by finding the best order.\n *\n * This function handles cases where text lines have been split into segments\n * and need to be merged back together in the correct order. It compares\n * different arrangements of the segments against target lines to find the\n * best match based on similarity scores.\n *\n * @param targetLines - Array where each element is either a string to align against, or falsy to skip alignment\n * @param segmentLines - Array of text segments that may represent split versions of target lines.\n * @returns Array of aligned text lines\n */\nexport const alignTextSegments = (targetLines: string[], segmentLines: string[]) => {\n const alignedLines: string[] = [];\n let segmentIndex = 0;\n\n for (const targetLine of targetLines) {\n if (segmentIndex >= segmentLines.length) {\n break;\n }\n\n if (targetLine) {\n // Process line that needs alignment\n const { result, segmentsConsumed } = processAlignmentTarget(targetLine, segmentLines, segmentIndex);\n\n if (result) {\n alignedLines.push(result);\n }\n segmentIndex += segmentsConsumed;\n } else {\n // For lines that don't need alignment, use one-to-one correspondence\n alignedLines.push(segmentLines[segmentIndex]);\n segmentIndex++;\n }\n }\n\n // Add any remaining segments that were not processed\n if (segmentIndex < segmentLines.length) {\n alignedLines.push(...segmentLines.slice(segmentIndex));\n }\n\n return alignedLines;\n};\n\n/**\n * Tries to merge two segments in both possible orders and returns the best match.\n */\nconst findBestSegmentMerge = (targetLine: string, partA: string, partB: string) => {\n const mergedForward = `${partA} ${partB}`;\n const mergedReversed = `${partB} ${partA}`;\n\n const normalizedTarget = normalizeArabicText(targetLine);\n const scoreForward = calculateSimilarity(normalizedTarget, normalizeArabicText(mergedForward));\n const scoreReversed = calculateSimilarity(normalizedTarget, normalizeArabicText(mergedReversed));\n\n return scoreForward >= scoreReversed ? mergedForward : mergedReversed;\n};\n\n/**\n * Processes a single target line that needs alignment.\n */\nconst processAlignmentTarget = (targetLine: string, segmentLines: string[], segmentIndex: number) => {\n const currentSegment = segmentLines[segmentIndex];\n\n // First, check if the current segment is already a good match\n if (areSimilarAfterNormalization(targetLine, currentSegment)) {\n return { result: currentSegment, segmentsConsumed: 1 };\n }\n\n // If not a direct match, try to merge two segments\n const partA = segmentLines[segmentIndex];\n const partB = segmentLines[segmentIndex + 1];\n\n // Ensure we have two parts to merge\n if (!partA || !partB) {\n return partA ? { result: partA, segmentsConsumed: 1 } : { result: '', segmentsConsumed: 0 };\n }\n\n const bestMerge = findBestSegmentMerge(targetLine, partA, partB);\n return { result: bestMerge, segmentsConsumed: 2 };\n};\n","/**\n * Represents an error found when checking balance of quotes or brackets in text.\n */\ntype BalanceError = {\n /** The character that caused the error */\n char: string;\n /** The position of the character in the string */\n index: number;\n /** The reason for the error */\n reason: 'mismatched' | 'unclosed' | 'unmatched';\n /** The type of character that caused the error */\n type: 'bracket' | 'quote';\n};\n\n/**\n * Result of a balance check operation.\n */\ntype BalanceResult = {\n /** Array of errors found during balance checking */\n errors: BalanceError[];\n /** Whether the text is properly balanced */\n isBalanced: boolean;\n};\n\n/**\n * Checks if all double quotes in a string are balanced and returns detailed error information.\n *\n * A string has balanced quotes when every opening quote has a corresponding closing quote.\n * This function counts all quote characters and determines if there's an even number of them.\n * If there's an odd number, the last quote is marked as unmatched.\n *\n * @param str - The string to check for quote balance\n * @returns An object containing balance status and any errors found\n *\n * @example\n * ```typescript\n * checkQuoteBalance('Hello \"world\"') // { errors: [], isBalanced: true }\n * checkQuoteBalance('Hello \"world') // { errors: [{ char: '\"', index: 6, reason: 'unmatched', type: 'quote' }], isBalanced: false }\n * ```\n */\nconst checkQuoteBalance = (str: string): BalanceResult => {\n const errors: BalanceError[] = [];\n let quoteCount = 0;\n let lastQuoteIndex = -1;\n\n for (let i = 0; i < str.length; i++) {\n if (str[i] === '\"') {\n quoteCount++;\n lastQuoteIndex = i;\n }\n }\n\n const isBalanced = quoteCount % 2 === 0;\n\n if (!isBalanced && lastQuoteIndex !== -1) {\n errors.push({\n char: '\"',\n index: lastQuoteIndex,\n reason: 'unmatched',\n type: 'quote',\n });\n }\n\n return { errors, isBalanced };\n};\n\n/** Mapping of opening brackets to their corresponding closing brackets */\nexport const BRACKETS = { '«': '»', '(': ')', '[': ']', '{': '}' };\n\n/** Set of all opening bracket characters */\nexport const OPEN_BRACKETS = new Set(['«', '(', '[', '{']);\n\n/** Set of all closing bracket characters */\nexport const CLOSE_BRACKETS = new Set(['»', ')', ']', '}']);\n\n/**\n * Checks if all brackets in a string are properly balanced and returns detailed error information.\n *\n * A string has balanced brackets when:\n * - Every opening bracket has a corresponding closing bracket\n * - Brackets are properly nested (no crossing pairs)\n * - Each closing bracket matches the most recent unmatched opening bracket\n *\n * Supports the following bracket pairs: (), [], {}, «»\n *\n * @param str - The string to check for bracket balance\n * @returns An object containing balance status and any errors found\n *\n * @example\n * ```typescript\n * checkBracketBalance('(hello [world])') // { errors: [], isBalanced: true }\n * checkBracketBalance('(hello [world)') // { errors: [{ char: '[', index: 7, reason: 'unclosed', type: 'bracket' }], isBalanced: false }\n * checkBracketBalance('(hello ]world[') // { errors: [...], isBalanced: false }\n * ```\n */\nconst checkBracketBalance = (str: string): BalanceResult => {\n const errors: BalanceError[] = [];\n const stack: Array<{ char: string; index: number }> = [];\n\n for (let i = 0; i < str.length; i++) {\n const char = str[i];\n\n if (OPEN_BRACKETS.has(char)) {\n stack.push({ char, index: i });\n } else if (CLOSE_BRACKETS.has(char)) {\n const lastOpen = stack.pop();\n\n if (!lastOpen) {\n errors.push({\n char,\n index: i,\n reason: 'unmatched',\n type: 'bracket',\n });\n } else if (BRACKETS[lastOpen.char as keyof typeof BRACKETS] !== char) {\n errors.push({\n char: lastOpen.char,\n index: lastOpen.index,\n reason: 'mismatched',\n type: 'bracket',\n });\n errors.push({\n char,\n index: i,\n reason: 'mismatched',\n type: 'bracket',\n });\n }\n }\n }\n\n stack.forEach(({ char, index }) => {\n errors.push({\n char,\n index,\n reason: 'unclosed',\n type: 'bracket',\n });\n });\n\n return { errors, isBalanced: errors.length === 0 };\n};\n\n/**\n * Checks if both quotes and brackets are balanced in a string and returns detailed error information.\n *\n * This function combines the results of both quote and bracket balance checking,\n * providing a comprehensive analysis of all balance issues in the text.\n * The errors are sorted by their position in the string for easier debugging.\n *\n * @param str - The string to check for overall balance\n * @returns An object containing combined balance status and all errors found, sorted by position\n *\n * @example\n * ```typescript\n * checkBalance('Hello \"world\" and (test)') // { errors: [], isBalanced: true }\n * checkBalance('Hello \"world and (test') // { errors: [...], isBalanced: false }\n * ```\n */\nexport const checkBalance = (str: string): BalanceResult => {\n const quoteResult = checkQuoteBalance(str);\n const bracketResult = checkBracketBalance(str);\n\n return {\n errors: [...quoteResult.errors, ...bracketResult.errors].sort((a, b) => a.index - b.index),\n isBalanced: quoteResult.isBalanced && bracketResult.isBalanced,\n };\n};\n\n/**\n * Enhanced error detection that returns absolute character positions for use with HighlightableTextarea.\n *\n * This interface extends the basic BalanceError to include absolute positioning\n * across multiple lines of text, making it suitable for text editors and\n * syntax highlighters that need precise character positioning.\n */\nexport interface CharacterError {\n /** Absolute character position from the start of the entire text */\n absoluteIndex: number;\n /** The character that caused the error */\n char: string;\n /** The reason for the error */\n reason: 'mismatched' | 'unclosed' | 'unmatched';\n /** The type of character that caused the error */\n type: 'bracket' | 'quote';\n}\n\n/**\n * Gets detailed character-level errors for unbalanced quotes and brackets in multi-line text.\n *\n * This function processes text line by line, but only checks lines longer than 10 characters\n * for balance issues. It returns absolute positions that can be used with text editors\n * or highlighting components that need precise character positioning across the entire text.\n *\n * The absolute index accounts for newline characters between lines, providing accurate\n * positioning for the original text string.\n *\n * @param text - The multi-line text to analyze for balance errors\n * @returns Array of character errors with absolute positioning information\n *\n * @example\n * ```typescript\n * const text = 'Line 1 with \"quote\\nLine 2 with (bracket';\n * const errors = getUnbalancedErrors(text);\n * // Returns errors with absoluteIndex pointing to exact character positions\n * ```\n */\nexport const getUnbalancedErrors = (text: string): CharacterError[] => {\n const characterErrors: CharacterError[] = [];\n const lines = text.split('\\n');\n let absoluteIndex = 0;\n\n lines.forEach((line, lineIndex) => {\n if (line.length > 10) {\n const balanceResult = checkBalance(line);\n if (!balanceResult.isBalanced) {\n balanceResult.errors.forEach((error) => {\n characterErrors.push({\n absoluteIndex: absoluteIndex + error.index,\n char: error.char,\n reason: error.reason,\n type: error.type,\n });\n });\n }\n }\n // Add 1 for the newline character (except for the last line)\n absoluteIndex += line.length + (lineIndex < lines.length - 1 ? 1 : 0);\n });\n\n return characterErrors;\n};\n\n/**\n * Checks if all double quotes in a string are balanced.\n *\n * This is a convenience function that returns only the boolean result\n * without detailed error information.\n *\n * @param str - The string to check for quote balance\n * @returns True if quotes are balanced, false otherwise\n *\n * @example\n * ```typescript\n * areQuotesBalanced('Hello \"world\"') // true\n * areQuotesBalanced('Hello \"world') // false\n * ```\n */\nexport const areQuotesBalanced = (str: string): boolean => {\n return checkQuoteBalance(str).isBalanced;\n};\n\n/**\n * Checks if all brackets in a string are properly balanced.\n *\n * This is a convenience function that returns only the boolean result\n * without detailed error information.\n *\n * @param str - The string to check for bracket balance\n * @returns True if brackets are balanced, false otherwise\n *\n * @example\n * ```typescript\n * areBracketsBalanced('(hello [world])') // true\n * areBracketsBalanced('(hello [world') // false\n * ```\n */\nexport const areBracketsBalanced = (str: string): boolean => {\n return checkBracketBalance(str).isBalanced;\n};\n\n/**\n * Checks if both quotes and brackets are balanced in a string.\n *\n * This is a convenience function that returns only the boolean result\n * without detailed error information.\n *\n * @param str - The string to check for overall balance\n * @returns True if both quotes and brackets are balanced, false otherwise\n *\n * @example\n * ```typescript\n * isBalanced('Hello \"world\" and (test)') // true\n * isBalanced('Hello \"world and (test') // false\n * ```\n */\nexport const isBalanced = (str: string): boolean => {\n return checkBalance(str).isBalanced;\n};\n","import { PATTERNS } from './textUtils';\n\nconst INVALID_FOOTNOTE = '()';\n\n/**\n * Checks if the given text contains invalid footnote references.\n * Invalid footnotes include empty parentheses \"()\" or OCR-confused characters\n * like \".1OV9\" that were misrecognized instead of Arabic numerals.\n *\n * @param text - Text to check for invalid footnote patterns\n * @returns True if text contains invalid footnote references, false otherwise\n * @example\n * hasInvalidFootnotes('This text has ()') // Returns true\n * hasInvalidFootnotes('This text has (١)') // Returns false\n * hasInvalidFootnotes('OCR mistake (O)') // Returns true\n */\nexport const hasInvalidFootnotes = (text: string): boolean => {\n return PATTERNS.invalidReferenceRegex.test(text);\n};\n\n// Arabic number formatter instance\nconst arabicFormatter = new Intl.NumberFormat('ar-SA');\n\n/**\n * Converts a number to Arabic-Indic numerals using the Intl.NumberFormat API.\n * Uses the 'ar-SA' locale to ensure proper Arabic numeral formatting.\n *\n * @param num - The number to convert to Arabic numerals\n * @returns String representation using Arabic-Indic digits (٠-٩)\n * @example\n * numberToArabic(123) // Returns '١٢٣'\n * numberToArabic(5) // Returns '٥'\n */\nconst numberToArabic = (num: number): string => {\n return arabicFormatter.format(num);\n};\n\n/**\n * Converts OCR-confused characters to their corresponding Arabic-Indic numerals.\n * Handles common OCR misrecognitions where Latin characters are mistaken for Arabic digits.\n *\n * @param char - Single character that may be an OCR mistake\n * @returns Corresponding Arabic-Indic numeral or original character if no mapping exists\n * @example\n * ocrToArabic('O') // Returns '٥' (O often confused with ٥)\n * ocrToArabic('1') // Returns '١' (1 often confused with ١)\n * ocrToArabic('.') // Returns '٠' (dot often confused with ٠)\n */\nconst ocrToArabic = (char: string): string => {\n const ocrToArabicMap: { [key: string]: string } = {\n '1': '١',\n '9': '٩',\n '.': '٠',\n O: '٥',\n o: '٥',\n V: '٧',\n v: '٧',\n };\n return ocrToArabicMap[char] || char;\n};\n\n/**\n * Parses Arabic-Indic numerals from a reference string and converts to a JavaScript number.\n * Removes parentheses and converts each Arabic-Indic digit to its Western equivalent.\n *\n * @param arabicStr - String containing Arabic-Indic numerals, typically in format '(١٢٣)'\n * @returns Parsed number, or 0 if parsing fails\n * @example\n * arabicToNumber('(١٢٣)') // Returns 123\n * arabicToNumber('(٥)') // Returns 5\n * arabicToNumber('invalid') // Returns 0\n */\nconst arabicToNumber = (arabicStr: string): number => {\n const lookup: { [key: string]: string } = {\n '٠': '0',\n '١': '1',\n '٢': '2',\n '٣': '3',\n '٤': '4',\n '٥': '5',\n '٦': '6',\n '٧': '7',\n '٨': '8',\n '٩': '9',\n };\n const digits = arabicStr.replace(/[()]/g, '');\n let numStr = '';\n for (const char of digits) {\n numStr += lookup[char];\n }\n const parsed = parseInt(numStr, 10);\n return isNaN(parsed) ? 0 : parsed;\n};\n\ntype TextLine = {\n isFootnote?: boolean;\n text: string;\n};\n\n/**\n * Extracts all footnote references from text lines, categorizing them by type and location.\n * Handles both Arabic-Indic numerals and OCR-confused characters in body text and footnotes.\n *\n * @param lines - Array of text line objects with optional isFootnote flag\n * @returns Object containing categorized reference arrays:\n * - bodyReferences: All valid references found in body text\n * - footnoteReferences: All valid references found in footnotes\n * - ocrConfusedInBody: OCR-confused references in body text (for tracking)\n * - ocrConfusedInFootnotes: OCR-confused references in footnotes (for tracking)\n * @example\n * const lines = [\n * { text: 'Body with (١) and (O)', isFootnote: false },\n * { text: '(١) Footnote text', isFootnote: true }\n * ];\n * const refs = extractReferences(lines);\n * // refs.bodyReferences contains ['(١)', '(٥)'] - OCR 'O' converted to '٥'\n */\nconst extractReferences = (lines: TextLine[]) => {\n const arabicReferencesInBody = lines\n .filter((b) => !b.isFootnote)\n .flatMap((b) => b.text.match(PATTERNS.arabicReferenceRegex) || []);\n\n const ocrConfusedReferencesInBody = lines\n .filter((b) => !b.isFootnote)\n .flatMap((b) => b.text.match(PATTERNS.ocrConfusedReferenceRegex) || []);\n\n const arabicReferencesInFootnotes = lines\n .filter((b) => b.isFootnote)\n .flatMap((b) => b.text.match(PATTERNS.arabicFootnoteReferenceRegex) || []);\n\n const ocrConfusedReferencesInFootnotes = lines\n .filter((b) => b.isFootnote)\n .flatMap((b) => b.text.match(PATTERNS.ocrConfusedFootnoteReferenceRegex) || []);\n\n const convertedOcrBodyRefs = ocrConfusedReferencesInBody.map((ref) =>\n ref.replace(/[.1OV9]/g, (char) => ocrToArabic(char)),\n );\n\n const convertedOcrFootnoteRefs = ocrConfusedReferencesInFootnotes.map((ref) =>\n ref.replace(/[.1OV9]/g, (char) => ocrToArabic(char)),\n );\n\n return {\n bodyReferences: [...arabicReferencesInBody, ...convertedOcrBodyRefs],\n footnoteReferences: [...arabicReferencesInFootnotes, ...convertedOcrFootnoteRefs],\n ocrConfusedInBody: ocrConfusedReferencesInBody,\n ocrConfusedInFootnotes: ocrConfusedReferencesInFootnotes,\n };\n};\n\n/**\n * Determines if footnote reference correction is needed by checking for:\n * 1. Invalid footnote patterns (empty parentheses, OCR mistakes)\n * 2. Mismatched sets of references between body text and footnotes\n * 3. Different counts of references in body vs footnotes\n *\n * @param lines - Array of text line objects to analyze\n * @param references - Extracted reference data from extractReferences()\n * @returns True if correction is needed, false if references are already correct\n * @example\n * const lines = [{ text: 'Text with ()', isFootnote: false }];\n * const refs = extractReferences(lines);\n * needsCorrection(lines, refs) // Returns true due to invalid \"()\" reference\n */\nconst needsCorrection = (lines: TextLine[], references: ReturnType<typeof extractReferences>) => {\n const mistakenReferences = lines.some((line) => hasInvalidFootnotes(line.text));\n if (mistakenReferences) return true;\n\n const bodySet = new Set(references.bodyReferences);\n const footnoteSet = new Set(references.footnoteReferences);\n if (bodySet.size !== footnoteSet.size) return true;\n\n // Check if the sets contain the same elements\n for (const ref of bodySet) {\n if (!footnoteSet.has(ref)) {\n return true;\n }\n }\n\n return false;\n};\n\n/**\n * Corrects footnote references in an array of text lines by:\n * 1. Converting OCR-confused characters to proper Arabic numerals\n * 2. Filling in empty \"()\" references with appropriate numbers\n * 3. Ensuring footnote references in body text match those in footnotes\n * 4. Generating new reference numbers when needed\n *\n * @param lines - Array of text line objects, each with optional isFootnote flag\n * @returns Array of corrected text lines with proper footnote references\n * @example\n * const lines = [\n * { text: 'Main text with ()', isFootnote: false },\n * { text: '() This is a footnote', isFootnote: true }\n * ];\n * const corrected = correctReferences(lines);\n * // Returns lines with \"()\" replaced by proper Arabic numerals like \"(١)\"\n */\nexport const correctReferences = <T extends TextLine>(lines: T[]): T[] => {\n const initialReferences = extractReferences(lines);\n\n if (!needsCorrection(lines, initialReferences)) {\n return lines;\n }\n\n // Pass 1: Sanitize lines by correcting only OCR characters inside reference markers.\n const sanitizedLines = lines.map((line) => {\n let updatedText = line.text;\n // This regex finds the full reference, e.g., \"(O)\" or \"(1)\"\n const ocrRegex = /\\([.1OV9]+\\)/g;\n updatedText = updatedText.replace(ocrRegex, (match) => {\n // This replace acts *inside* the found match, e.g., on \"O\" or \"1\"\n return match.replace(/[.1OV9]/g, (char) => ocrToArabic(char));\n });\n return { ...line, text: updatedText };\n });\n\n // Pass 2: Analyze the sanitized lines to get a clear and accurate picture of references.\n const cleanReferences = extractReferences(sanitizedLines);\n\n // Step 3: Create queues of \"unmatched\" references for two-way pairing.\n const bodyRefSet = new Set(cleanReferences.bodyReferences);\n const footnoteRefSet = new Set(cleanReferences.footnoteReferences);\n\n const uniqueBodyRefs = [...new Set(cleanReferences.bodyReferences)];\n const uniqueFootnoteRefs = [...new Set(cleanReferences.footnoteReferences)];\n\n // Queue 1: Body references available for footnotes.\n const bodyRefsForFootnotes = uniqueBodyRefs.filter((ref) => !footnoteRefSet.has(ref));\n // Queue 2: Footnote references available for the body.\n const footnoteRefsForBody = uniqueFootnoteRefs.filter((ref) => !bodyRefSet.has(ref));\n\n // Step 4: Determine the starting point for any completely new reference numbers.\n const allRefs = [...bodyRefSet, ...footnoteRefSet];\n const maxRefNum = allRefs.length > 0 ? Math.max(0, ...allRefs.map((ref) => arabicToNumber(ref))) : 0;\n const referenceCounter = { count: maxRefNum + 1 };\n\n // Step 5: Map over the sanitized lines, filling in '()' using the queues.\n return sanitizedLines.map((line) => {\n if (!line.text.includes(INVALID_FOOTNOTE)) {\n return line;\n }\n let updatedText = line.text;\n\n updatedText = updatedText.replace(/\\(\\)/g, () => {\n if (line.isFootnote) {\n const availableRef = bodyRefsForFootnotes.shift();\n if (availableRef) return availableRef;\n } else {\n // It's body text\n const availableRef = footnoteRefsForBody.shift();\n if (availableRef) return availableRef;\n }\n\n // If no available partner reference exists, generate a new one.\n const newRef = `(${numberToArabic(referenceCounter.count)})`;\n referenceCounter.count++;\n return newRef;\n });\n\n return { ...line, text: updatedText };\n });\n};\n","import { PATTERNS } from './textUtils';\n\n/**\n * Character statistics for analyzing text content and patterns\n */\ntype CharacterStats = {\n /** Number of Arabic script characters in the text */\n arabicCount: number;\n /** Map of character frequencies for repetition analysis */\n charFreq: Map<string, number>;\n /** Number of digit characters (0-9) in the text */\n digitCount: number;\n /** Number of Latin alphabet characters (a-z, A-Z) in the text */\n latinCount: number;\n /** Number of punctuation characters in the text */\n punctuationCount: number;\n /** Number of whitespace characters in the text */\n spaceCount: number;\n /** Number of symbol characters (non-alphanumeric, non-punctuation) in the text */\n symbolCount: number;\n};\n\n/**\n * Determines if a given Arabic text string is likely to be noise or unwanted OCR artifacts.\n * This function performs comprehensive analysis to identify patterns commonly associated\n * with OCR errors, formatting artifacts, or meaningless content in Arabic text processing.\n *\n * @param text - The input string to analyze for noise patterns\n * @returns true if the text is likely noise or unwanted content, false if it appears to be valid Arabic content\n *\n * @example\n * ```typescript\n * import { isArabicTextNoise } from 'baburchi';\n *\n * console.log(isArabicTextNoise('---')); // true (formatting artifact)\n * console.log(isArabicTextNoise('السلام عليكم')); // false (valid Arabic)\n * console.log(isArabicTextNoise('ABC')); // true (uppercase pattern)\n * ```\n */\nexport const isArabicTextNoise = (text: string): boolean => {\n // Early return for empty or very short strings\n if (!text || text.trim().length === 0) {\n return true;\n }\n\n const trimmed = text.trim();\n const length = trimmed.length;\n\n // Very short strings are likely noise unless they're meaningful Arabic\n if (length < 2) {\n return true;\n }\n\n // Check for basic noise patterns first\n if (isBasicNoisePattern(trimmed)) {\n return true;\n }\n\n const charStats = analyzeCharacterStats(trimmed);\n\n // Check for excessive repetition\n if (hasExcessiveRepetition(charStats, length)) {\n return true;\n }\n\n // Check if text contains Arabic characters\n const hasArabic = PATTERNS.arabicCharacters.test(trimmed);\n\n // Handle non-Arabic text\n if (!hasArabic && /[a-zA-Z]/.test(trimmed)) {\n return true;\n }\n\n // Arabic-specific validation\n if (hasArabic) {\n return !isValidArabicContent(charStats, length);\n }\n\n // Non-Arabic content validation\n return isNonArabicNoise(charStats, length, trimmed);\n};\n\n/**\n * Analyzes character composition and frequency statistics for the input text.\n * Categorizes characters by type (Arabic, Latin, digits, spaces, punctuation, symbols)\n * and tracks character frequency for pattern analysis.\n *\n * @param text - The text string to analyze\n * @returns CharacterStats object containing detailed character analysis\n *\n * @example\n * ```typescript\n * import { analyzeCharacterStats } from 'baburchi';\n *\n * const stats = analyzeCharacterStats('مرحبا 123!');\n * console.log(stats.arabicCount); // 5\n * console.log(stats.digitCount); // 3\n * console.log(stats.symbolCount); // 1\n * ```\n */\nexport function analyzeCharacterStats(text: string): CharacterStats {\n const stats: CharacterStats = {\n arabicCount: 0,\n charFreq: new Map<string, number>(),\n digitCount: 0,\n latinCount: 0,\n punctuationCount: 0,\n spaceCount: 0,\n symbolCount: 0,\n };\n\n const chars = Array.from(text);\n\n for (const char of chars) {\n // Count character frequency for repetition detection\n stats.charFreq.set(char, (stats.charFreq.get(char) || 0) + 1);\n\n if (PATTERNS.arabicCharacters.test(char)) {\n stats.arabicCount++;\n } else if (/\\d/.test(char)) {\n stats.digitCount++;\n } else if (/[a-zA-Z]/.test(char)) {\n stats.latinCount++;\n } else if (/\\s/.test(char)) {\n stats.spaceCount++;\n } else if (/[.,;:()[\\]{}\"\"\"''`]/.test(char)) {\n stats.punctuationCount++;\n } else {\n stats.symbolCount++;\n }\n }\n\n return stats;\n}\n\n/**\n * Detects excessive repetition of specific characters that commonly indicate noise.\n * Focuses on repetitive characters like exclamation marks, dots, dashes, equals signs,\n * and underscores that often appear in OCR artifacts or formatting elements.\n *\n * @param charStats - Character statistics from analyzeCharacterStats\n * @param textLength - Total length of the original text\n * @returns true if excessive repetition is detected, false otherwise\n *\n * @example\n * ```typescript\n * import { hasExcessiveRepetition, analyzeCharacterStats } from 'baburchi';\n *\n * const stats = analyzeCharacterStats('!!!!!');\n * console.log(hasExcessiveRepetition(stats, 5)); // true\n *\n * const normalStats = analyzeCharacterStats('hello world');\n * console.log(hasExcessiveRepetition(normalStats, 11)); // false\n * ```\n */\nexport function hasExcessiveRepetition(charStats: CharacterStats, textLength: number): boolean {\n let repeatCount = 0;\n const repetitiveChars = ['!', '.', '-', '=', '_'];\n\n for (const [char, count] of charStats.charFreq) {\n if (count >= 5 && repetitiveChars.includes(char)) {\n repeatCount += count;\n }\n }\n\n // High repetition ratio indicates noise\n return repeatCount / textLength > 0.4;\n}\n\n/**\n * Identifies text that matches common noise patterns using regular expressions.\n * Detects patterns like repeated dashes, dot sequences, uppercase-only text,\n * digit-dash combinations, and other formatting artifacts commonly found in OCR output.\n *\n * @param text - The text string to check against noise patterns\n * @returns true if the text matches a basic noise pattern, false otherwise\n *\n * @example\n * ```typescript\n * import { isBasicNoisePattern } from 'baburchi';\n *\n * console.log(isBasicNoisePattern('---')); // true\n * console.log(isBasicNoisePattern('...')); // true\n * console.log(isBasicNoisePattern('ABC')); // true\n * console.log(isBasicNoisePattern('- 77')); // true\n * console.log(isBasicNoisePattern('hello world')); // false\n * ```\n */\nexport function isBasicNoisePattern(text: string): boolean {\n const noisePatterns = [\n /^[-=_━≺≻\\s]*$/, // Only dashes, equals, underscores, special chars, or spaces\n /^[.\\s]*$/, // Only dots and spaces\n /^[!\\s]*$/, // Only exclamation marks and spaces\n /^[A-Z\\s]*$/, // Only uppercase letters and spaces (like \"Ap Ap Ap\")\n /^[-\\d\\s]*$/, // Only dashes, digits and spaces (like \"- 77\", \"- 4\")\n /^\\d+\\s*$/, // Only digits and spaces (like \"1\", \" 1 \")\n /^[A-Z]\\s*$/, // Single uppercase letter with optional spaces\n /^[—\\s]*$/, // Only em-dashes and spaces\n /^[्र\\s-]*$/, // Devanagari characters (likely OCR errors)\n ];\n\n return noisePatterns.some((pattern) => pattern.test(text));\n}\n\n/**\n * Determines if non-Arabic content should be classified as noise based on various heuristics.\n * Analyzes symbol-to-content ratios, text length, spacing patterns, and content composition\n * to identify unwanted OCR artifacts or meaningless content.\n *\n * @param charStats - Character statistics from analyzeCharacterStats\n * @param textLength - Total length of the original text\n * @param text - The original text string for additional pattern matching\n * @returns true if the content is likely noise, false if it appears to be valid content\n *\n * @example\n * ```typescript\n * import { isNonArabicNoise, analyzeCharacterStats } from 'baburchi';\n *\n * const stats = analyzeCharacterStats('!!!');\n * console.log(isNonArabicNoise(stats, 3, '!!!')); // true\n *\n * const validStats = analyzeCharacterStats('2023');\n * console.log(isNonArabicNoise(validStats, 4, '2023')); // false\n * ```\n */\nexport function isNonArabicNoise(charStats: CharacterStats, textLength: number, text: string): boolean {\n const contentChars = charStats.arabicCount + charStats.latinCount + charStats.digitCount;\n\n // Text that's mostly symbols or punctuation is likely noise\n if (contentChars === 0) {\n return true;\n }\n\n // Check for specific spacing patterns that indicate noise\n if (isSpacingNoise(charStats, contentChars, textLength)) {\n return true;\n }\n\n // Special handling for Arabic numerals in parentheses (like \"(٦٠١٠).\")\n const hasArabicNumerals = /[٠-٩]/.test(text);\n if (hasArabicNumerals && charStats.digitCount >= 3) {\n return false;\n }\n\n // High symbol-to-content ratio indicates noise, but be more lenient with punctuation\n // Allow more punctuation for valid content like references, citations, etc.\n const adjustedNonContentChars = charStats.symbolCount + Math.max(0, charStats.punctuationCount - 5);\n if (adjustedNonContentChars / Math.max(contentChars, 1) > 2) {\n return true;\n }\n\n // Very short strings with no Arabic are likely noise (except substantial numbers)\n if (textLength <= 5 && charStats.arabicCount === 0 && !(/^\\d+$/.test(text) && charStats.digitCount >= 3)) {\n return true;\n }\n\n // Allow pure numbers if they're substantial (like years)\n if (/^\\d{3,4}$/.test(text)) {\n return false;\n }\n\n // Default to not noise for longer content\n return textLength <= 10;\n}\n\n/**\n * Detects problematic spacing patterns that indicate noise or OCR artifacts.\n * Identifies cases where spacing is excessive relative to content, or where\n * single characters are surrounded by spaces in a way that suggests OCR errors.\n *\n * @param charStats - Character statistics from analyzeCharacterStats\n * @param contentChars - Number of meaningful content characters (Arabic + Latin + digits)\n * @param textLength - Total length of the original text\n * @returns true if spacing patterns indicate noise, false otherwise\n *\n * @example\n * ```typescript\n * import { isSpacingNoise, analyzeCharacterStats } from 'baburchi';\n *\n * const stats = analyzeCharacterStats(' a ');\n * const contentChars = stats.arabicCount + stats.latinCount + stats.digitCount;\n * console.log(isSpacingNoise(stats, contentChars, 3)); // true\n *\n * const normalStats = analyzeCharacterStats('hello world');\n * const normalContent = normalStats.arabicCount + normalStats.latinCount + normalStats.digitCount;\n * console.log(isSpacingNoise(normalStats, normalContent, 11)); // false\n * ```\n */\nexport function isSpacingNoise(charStats: CharacterStats, contentChars: number, textLength: number): boolean {\n const { arabicCount, spaceCount } = charStats;\n\n // Too many spaces relative to content\n if (spaceCount > 0 && contentChars === spaceCount + 1 && contentChars <= 5) {\n return true;\n }\n\n // Short text with multiple spaces and no Arabic\n if (textLength <= 10 && spaceCount >= 2 && arabicCount === 0) {\n return true;\n }\n\n // Excessive spacing ratio\n if (spaceCount / textLength > 0.6) {\n return true;\n }\n\n return false;\n}\n\n/**\n * Validates whether Arabic content is substantial enough to be considered meaningful.\n * Uses character counts and text length to determine if Arabic text contains\n * sufficient content or if it's likely to be a fragment or OCR artifact.\n *\n * @param charStats - Character statistics from analyzeCharacterStats\n * @param textLength - Total length of the original text\n * @returns true if the Arabic content appears valid, false if it's likely noise\n *\n * @example\n * ```typescript\n * import { isValidArabicContent, analyzeCharacterStats } from 'baburchi';\n *\n * const validStats = analyzeCharacterStats('السلام عليكم');\n * console.log(isValidArabicContent(validStats, 12)); // true\n *\n * const shortStats = analyzeCharacterStats('ص');\n * console.log(isValidArabicContent(shortStats, 1)); // false\n *\n * const withDigitsStats = analyzeCharacterStats('ص 5');\n * console.log(isValidArabicContent(withDigitsStats, 3)); // true\n * ```\n */\nexport function isValidArabicContent(charStats: CharacterStats, textLength: number): boolean {\n // Arabic text with reasonable content length is likely valid\n if (charStats.arabicCount >= 3) {\n return true;\n }\n\n // Short Arabic snippets with numbers might be valid (like dates, references)\n if (charStats.arabicCount >= 1 && charStats.digitCount > 0 && textLength <= 20) {\n return true;\n }\n\n // Allow short Arabic words with punctuation (like \"له.\" - \"for him/it.\")\n if (charStats.arabicCount >= 2 && charStats.punctuationCount <= 2 && textLength <= 10) {\n return true;\n }\n\n // Allow single meaningful Arabic words that are common standalone terms\n // This handles cases like pronouns, prepositions, common short words\n if (charStats.arabicCount >= 1 && textLength <= 5 && charStats.punctuationCount <= 1) {\n return true;\n }\n\n return false;\n}\n","import type { FixTypoOptions } from './types';\n\nimport { alignTokenSequences, areSimilarAfterNormalization, calculateSimilarity } from './similarity';\nimport {\n handleFootnoteFusion,\n handleFootnoteSelection,\n handleStandaloneFootnotes,\n normalizeArabicText,\n tokenizeText,\n} from './textUtils';\n\n/**\n * Selects the best token(s) from an aligned pair during typo correction.\n * Uses various heuristics including normalization, footnote handling, typo symbols,\n * and similarity scores to determine which token(s) to keep.\n *\n * @param originalToken - Token from the original OCR text (may be null)\n * @param altToken - Token from the alternative OCR text (may be null)\n * @param options - Configuration options including typo symbols and similarity threshold\n * @returns Array of selected tokens (usually contains one token, but may contain multiple)\n */\nconst selectBestTokens = (\n originalToken: null | string,\n altToken: null | string,\n { similarityThreshold, typoSymbols }: FixTypoOptions,\n): string[] => {\n // Handle missing tokens\n if (originalToken === null) {\n return [altToken!];\n }\n if (altToken === null) {\n return [originalToken];\n }\n\n // Preserve original if same after normalization (keeps diacritics)\n if (normalizeArabicText(originalToken) === normalizeArabicText(altToken)) {\n return [originalToken];\n }\n\n // Handle embedded footnotes\n const result = handleFootnoteSelection(originalToken, altToken);\n if (result) return result;\n\n // Handle standalone footnotes\n const footnoteResult = handleStandaloneFootnotes(originalToken, altToken);\n if (footnoteResult) return footnoteResult;\n\n // Handle typo symbols - prefer the symbol itself\n if (typoSymbols.includes(originalToken) || typoSymbols.includes(altToken)) {\n const typoSymbol = typoSymbols.find((symbol) => symbol === originalToken || symbol === altToken);\n return typoSymbol ? [typoSymbol] : [originalToken];\n }\n\n // Choose based on similarity\n const normalizedOriginal = normalizeArabicText(originalToken);\n const normalizedAlt = normalizeArabicText(altToken);\n const similarity = calculateSimilarity(normalizedOriginal, normalizedAlt);\n\n return [similarity > similarityThreshold ? originalToken : altToken];\n};\n\n/**\n * Removes duplicate tokens and handles footnote fusion in post-processing.\n * Identifies and removes tokens that are highly similar while preserving\n * important variations. Also handles special cases like footnote merging.\n *\n * @param tokens - Array of tokens to process\n * @param highSimilarityThreshold - Threshold for detecting duplicates (0.0 to 1.0)\n * @returns Array of tokens with duplicates removed and footnotes fused\n */\nconst removeDuplicateTokens = (tokens: string[], highSimilarityThreshold: number): string[] => {\n if (tokens.length === 0) {\n return tokens;\n }\n\n const result: string[] = [];\n\n for (const currentToken of tokens) {\n if (result.length === 0) {\n result.push(currentToken);\n continue;\n }\n\n const previousToken = result.at(-1)!;\n\n // Handle ordinary echoes (similar tokens)\n if (areSimilarAfterNormalization(previousToken, currentToken, highSimilarityThreshold)) {\n // Keep the shorter version\n if (currentToken.length < previousToken.length) {\n result[result.length - 1] = currentToken;\n }\n continue;\n }\n\n // Handle footnote fusion cases\n if (handleFootnoteFusion(result, previousToken, currentToken)) {\n continue;\n }\n\n result.push(currentToken);\n }\n\n return result;\n};\n\n/**\n * Processes text alignment between original and alternate OCR results to fix typos.\n * Uses the Needleman-Wunsch sequence alignment algorithm to align tokens,\n * then selects the best tokens and performs post-processing.\n *\n * @param originalText - Original OCR text that may contain typos\n * @param altText - Reference text from alternate OCR for comparison\n * @param options - Configuration options for alignment and selection\n * @returns Corrected text with typos fixed\n */\nexport const processTextAlignment = (originalText: string, altText: string, options: FixTypoOptions): string => {\n const originalTokens = tokenizeText(originalText, options.typoSymbols);\n const altTokens = tokenizeText(altText, options.typoSymbols);\n\n // Align token sequences\n const alignedPairs = alignTokenSequences(\n originalTokens,\n altTokens,\n options.typoSymbols,\n options.similarityThreshold,\n );\n\n // Select best tokens from each aligned pair\n const mergedTokens = alignedPairs.flatMap(([original, alt]) => selectBestTokens(original, alt, options));\n\n // Remove duplicates and handle post-processing\n const finalTokens = removeDuplicateTokens(mergedTokens, options.highSimilarityThreshold);\n\n return finalTokens.join(' ');\n};\n\nexport const fixTypo = (\n original: string,\n correction: string,\n {\n highSimilarityThreshold = 0.8,\n similarityThreshold = 0.6,\n typoSymbols,\n }: Partial<FixTypoOptions> & Pick<FixTypoOptions, 'typoSymbols'>,\n) => {\n return processTextAlignment(original, correction, { highSimilarityThreshold, similarityThreshold, typoSymbols });\n};\n\nexport * from './alignment';\nexport * from './balance';\nexport * from './footnotes';\nexport * from './noise';\nexport * from './similarity';\nexport * from './textUtils';\n"],"mappings":"AAAO,IAAMA,EAAgB,qBAKhBC,EAAW,CAEpB,iBAAkB,sEAGlB,aAAc,sBAGd,6BAA8B,yBAG9B,uBAAwB,iDAGxB,+BAAgC,gCAGhC,qBAAsB,wBAGtB,WAAY,mDAGZ,iBAAkB,0BAGlB,mBAAoB,mCAGpB,sBAAuB,qBAGvB,kCAAmC,iBAGnC,0BAA2B,gBAG3B,QAAS,UAGT,WAAY,KAChB,EAYaC,EAAuBC,GACzBA,EAAK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gBb,EAAO,GAAG,EAAE,EAGlC,GAAIc,EAA6BD,EAAeD,EAAcD,CAAuB,EAAG,CAEhFC,EAAa,OAASC,EAAc,SACpCb,EAAOA,EAAO,OAAS,CAAC,EAAIY,GAEhC,QACJ,CAGIG,EAAqBf,EAAQa,EAAeD,CAAY,GAI5DZ,EAAO,KAAKY,CAAY,CAC5B,CAEA,OAAOZ,CACX,EAYagB,GAAuB,CAACC,EAAsBC,EAAiBC,IAAoC,CAC5G,IAAMC,EAAiBC,EAAaJ,EAAcE,EAAQ,WAAW,EAC/DG,EAAYD,EAAaH,EAASC,EAAQ,WAAW,EAWrDI,EAReC,EACjBJ,EACAE,EACAH,EAAQ,YACRA,EAAQ,mBACZ,EAGkC,QAAQ,CAAC,CAACM,EAAUC,CAAG,IAAMhC,GAAiB+B,EAAUC,EAAKP,CAAO,CAAC,EAKvG,OAFoBV,GAAsBc,EAAcJ,EAAQ,uBAAuB,EAEpE,KAAK,GAAG,CAC/B,EAEaQ,GAAU,CACnBF,EACAG,EACA,CACI,wBAAAjB,EAA0B,GAC1B,oBAAAd,EAAsB,GACtB,YAAAC,CACJ,IAEOkB,GAAqBS,EAAUG,EAAY,CAAE,wBAAAjB,EAAyB,oBAAAd,EAAqB,YAAAC,CAAY,CAAC","names":["INTAHA_ACTUAL","PATTERNS","normalizeArabicText","text","extractDigits","match","tokenizeText","preserveSymbols","processedText","symbol","symbolRegex","handleFootnoteFusion","result","previousToken","currentToken","prevIsStandalone","currHasEmbedded","currIsStandalone","prevHasEmbedded","prevDigits","currDigits","handleFootnoteSelection","tokenA","tokenB","aHasEmbedded","bHasEmbedded","handleStandaloneFootnotes","aIsFootnote","bIsFootnote","standardizeHijriSymbol","standardizeIntahaSymbol","ALIGNMENT_SCORES","calculateLevenshteinDistance","textA","textB","lengthA","lengthB","shorter","longer","shortLen","longLen","previousRow","_","index","i","currentRow","j","substitutionCost","minCost","calculateSimilarity","maxLength","distance","areSimilarAfterNormalization","threshold","normalizedA","normalizeArabicText","normalizedB","calculateAlignmentScore","tokenA","tokenB","typoSymbols","similarityThreshold","isTypoSymbol","isHighlySimilar","backtrackAlignment","matrix","tokensA","tokensB","alignment","alignTokenSequences","scoringMatrix","alignmentScore","diagonalScore","upScore","leftScore","bestScore","bestDirection","alignTextSegments","targetLines","segmentLines","alignedLines","segmentIndex","targetLine","result","segmentsConsumed","processAlignmentTarget","findBestSegmentMerge","partA","partB","mergedForward","mergedReversed","normalizedTarget","normalizeArabicText","scoreForward","calculateSimilarity","scoreReversed","currentSegment","areSimilarAfterNormalization","checkQuoteBalance","str","errors","quoteCount","lastQuoteIndex","i","isBalanced","BRACKETS","OPEN_BRACKETS","CLOSE_BRACKETS","checkBracketBalance","stack","char","lastOpen","index","checkBalance","quoteResult","bracketResult","a","b","getUnbalancedErrors","text","characterErrors","lines","absoluteIndex","line","lineIndex","balanceResult","error","areQuotesBalanced","areBracketsBalanced","INVALID_FOOTNOTE","hasInvalidFootnotes","text","PATTERNS","arabicFormatter","numberToArabic","num","ocrToArabic","char","arabicToNumber","arabicStr","lookup","digits","numStr","parsed","extractReferences","lines","arabicReferencesInBody","b","ocrConfusedReferencesInBody","arabicReferencesInFootnotes","ocrConfusedReferencesInFootnotes","convertedOcrBodyRefs","ref","convertedOcrFootnoteRefs","needsCorrection","references","line","bodySet","footnoteSet","correctReferences","initialReferences","sanitizedLines","updatedText","ocrRegex","match","cleanReferences","bodyRefSet","footnoteRefSet","uniqueBodyRefs","uniqueFootnoteRefs","bodyRefsForFootnotes","footnoteRefsForBody","allRefs","referenceCounter","availableRef","newRef","isArabicTextNoise","text","trimmed","length","isBasicNoisePattern","charStats","analyzeCharacterStats","hasExcessiveRepetition","hasArabic","PATTERNS","isValidArabicContent","isNonArabicNoise","stats","chars","char","textLength","repeatCount","repetitiveChars","count","pattern","contentChars","isSpacingNoise","arabicCount","spaceCount","selectBestTokens","originalToken","altToken","similarityThreshold","typoSymbols","normalizeArabicText","result","handleFootnoteSelection","footnoteResult","handleStandaloneFootnotes","typoSymbol","symbol","normalizedOriginal","normalizedAlt","calculateSimilarity","removeDuplicateTokens","tokens","highSimilarityThreshold","currentToken","previousToken","areSimilarAfterNormalization","handleFootnoteFusion","processTextAlignment","originalText","altText","options","originalTokens","tokenizeText","altTokens","mergedTokens","alignTokenSequences","original","alt","fixTypo","correction"]}
|
|
1
|
+
{"version":3,"sources":["../src/utils/sanitize.ts","../src/utils/levenshthein.ts","../src/utils/similarity.ts","../src/alignment.ts","../src/balance.ts","../src/utils/textUtils.ts","../src/footnotes.ts","../src/utils/ahocorasick.ts","../src/utils/constants.ts","../src/utils/fuzzyUtils.ts","../src/utils/qgram.ts","../src/fuzzy.ts","../src/noise.ts","../src/typos.ts"],"sourcesContent":["/**\n * Ultra-fast Arabic text sanitizer for search/indexing/display.\n * Optimized for very high call rates: avoids per-call object spreads and minimizes allocations.\n * Options can merge over a base preset or `'none'` to apply exactly the rules you request.\n */\nexport type SanitizePreset = 'light' | 'search' | 'aggressive';\nexport type SanitizeBase = 'none' | SanitizePreset;\n\n/**\n * Public options for {@link sanitizeArabic}. When you pass an options object, it overlays the chosen\n * `base` (default `'light'`) without allocating merged objects on the hot path; flags are resolved\n * directly into local booleans for speed.\n */\nexport type SanitizeOptions = {\n /** Base to merge over. `'none'` applies only the options you specify. Default when passing an object: `'light'`. */\n base?: SanitizeBase;\n\n /** Unicode NFC normalization. Default: `true` in all presets. */\n nfc?: boolean;\n\n /** Strip zero-width controls (U+200B–U+200F, U+202A–U+202E, U+2060–U+2064, U+FEFF). Default: `true` in presets. */\n stripZeroWidth?: boolean;\n\n /** If stripping zero-width, replace them with a space instead of removing. Default: `false`. */\n zeroWidthToSpace?: boolean;\n\n /** Remove Arabic diacritics (tashkīl). Default: `true` in `'search'`/`'aggressive'`. */\n stripDiacritics?: boolean;\n\n /**\n * Remove tatweel (ـ).\n * - `true` is treated as `'safe'` (preserves tatweel after digits or 'ه' for dates/list markers)\n * - `'safe'` or `'all'` explicitly\n * - `false` to keep tatweel\n * Default: `'all'` in `'search'`/`'aggressive'`, `false` in `'light'`.\n */\n stripTatweel?: boolean | 'safe' | 'all';\n\n /** Normalize آ/أ/إ → ا. Default: `true` in `'search'`/`'aggressive'`. */\n normalizeAlif?: boolean;\n\n /** Replace ى → ي. Default: `true` in `'search'`/`'aggressive'`. */\n replaceAlifMaqsurah?: boolean;\n\n /** Replace ة → ه (lossy). Default: `true` in `'aggressive'` only. */\n replaceTaMarbutahWithHa?: boolean;\n\n /** Strip Latin letters/digits and common OCR noise into spaces. Default: `true` in `'aggressive'`. */\n stripLatinAndSymbols?: boolean;\n\n /** Keep only Arabic letters (no whitespace). Use for compact keys, not FTS. */\n keepOnlyArabicLetters?: boolean;\n\n /** Keep Arabic letters + spaces (drops digits/punct/symbols). Great for FTS. Default: `true` in `'aggressive'`. */\n lettersAndSpacesOnly?: boolean;\n\n /** Collapse runs of whitespace to a single space. Default: `true`. */\n collapseWhitespace?: boolean;\n\n /** Trim leading/trailing whitespace. Default: `true`. */\n trim?: boolean;\n\n /**\n * Remove the Hijri date marker (\"هـ\" or bare \"ه\" if tatweel already removed) when it follows a date-like token\n * (digits/slashes/hyphens/spaces). Example: `1435/3/29 هـ` → `1435/3/29`.\n * Default: `true` in `'search'`/`'aggressive'`, `false` in `'light'`.\n */\n removeHijriMarker?: boolean;\n};\n\n/** Fully-resolved internal preset options (no `base`, and tatweel as a mode). */\ntype PresetOptions = {\n nfc: boolean;\n stripZeroWidth: boolean;\n zeroWidthToSpace: boolean;\n stripDiacritics: boolean;\n stripTatweel: false | 'safe' | 'all';\n normalizeAlif: boolean;\n replaceAlifMaqsurah: boolean;\n replaceTaMarbutahWithHa: boolean;\n stripLatinAndSymbols: boolean;\n keepOnlyArabicLetters: boolean;\n lettersAndSpacesOnly: boolean;\n collapseWhitespace: boolean;\n trim: boolean;\n removeHijriMarker: boolean;\n};\n\nconst RX_SPACES = /\\s+/g;\nconst RX_TATWEEL = /\\u0640/g;\nconst RX_DIACRITICS = /[\\u0610-\\u061A\\u064B-\\u065F\\u0670\\u06D6-\\u06ED]/g;\nconst RX_ALIF_VARIANTS = /[أإآٱ]/g;\nconst RX_ALIF_MAQSURAH = /\\u0649/g;\nconst RX_TA_MARBUTAH = /\\u0629/g;\nconst RX_ZERO_WIDTH = /[\\u200B-\\u200F\\u202A-\\u202E\\u2060-\\u2064\\uFEFF]/g;\nconst RX_LATIN_AND_SYMBOLS = /[A-Za-z]+[0-9]*|[0-9]+|[¬§`=]|[/]{2,}|[&]|[ﷺ]/g;\nconst RX_NON_ARABIC_LETTERS = /[^\\u0621-\\u063A\\u0641-\\u064A\\u0671\\u067E\\u0686\\u06A4-\\u06AF\\u06CC\\u06D2\\u06D3]/g;\nconst RX_NOT_LETTERS_OR_SPACE = /[^\\u0621-\\u063A\\u0641-\\u064A\\u0671\\u067E\\u0686\\u06A4-\\u06AF\\u06CC\\u06D2\\u06D3\\s]/g;\n\n/**\n * Returns `true` if the code point is ASCII space.\n */\nconst isAsciiSpace = (code: number): boolean => code === 32;\n\n/**\n * Returns `true` if the code point is a Western digit or Arabic-Indic digit.\n */\nconst isDigitCodePoint = (code: number): boolean => (code >= 48 && code <= 57) || (code >= 0x0660 && code <= 0x0669);\n\n/**\n * Removes tatweel while preserving a tatweel that immediately follows a digit or 'ه'.\n * This protects list markers and Hijri date forms.\n */\nconst removeTatweelSafely = (s: string): string =>\n s.replace(RX_TATWEEL, (_m, i: number, str: string) => {\n let j = i - 1;\n while (j >= 0 && isAsciiSpace(str.charCodeAt(j))) {\n j--;\n }\n if (j >= 0) {\n const prev = str.charCodeAt(j);\n if (isDigitCodePoint(prev) || prev === 0x0647) {\n return 'ـ';\n }\n }\n return '';\n });\n\n/**\n * Removes the Hijri date marker when it immediately follows a date-like token.\n */\nconst removeHijriDateMarker = (s: string): string =>\n s.replace(/([0-9\\u0660-\\u0669][0-9\\u0660-\\u0669/\\-\\s]*?)\\s*ه(?:ـ)?(?=(?:\\s|$|[^\\p{L}\\p{N}]))/gu, '$1');\n\n/**\n * Applies NFC normalization if available and requested.\n */\nconst applyNfcNormalization = (s: string, enable: boolean): string => (enable && s.normalize ? s.normalize('NFC') : s);\n\n/**\n * Removes zero-width controls, optionally replacing them with spaces.\n */\nconst removeZeroWidthControls = (s: string, enable: boolean, asSpace: boolean): string =>\n enable ? s.replace(RX_ZERO_WIDTH, asSpace ? ' ' : '') : s;\n\n/**\n * Removes diacritics and tatweel according to the selected mode.\n */\nconst removeDiacriticsAndTatweel = (\n s: string,\n removeDiacritics: boolean,\n tatweelMode: false | 'safe' | 'all',\n): string => {\n if (removeDiacritics) {\n s = s.replace(RX_DIACRITICS, '');\n }\n if (tatweelMode === 'safe') {\n return removeTatweelSafely(s);\n }\n if (tatweelMode === 'all') {\n return s.replace(RX_TATWEEL, '');\n }\n return s;\n};\n\n/**\n * Applies canonical character mappings: Alif variants, alif maqṣūrah, tāʾ marbūṭa.\n */\nconst applyCharacterMappings = (\n s: string,\n normalizeAlif: boolean,\n maqsurahToYa: boolean,\n taMarbutahToHa: boolean,\n): string => {\n if (normalizeAlif) {\n s = s.replace(RX_ALIF_VARIANTS, 'ا');\n }\n if (maqsurahToYa) {\n s = s.replace(RX_ALIF_MAQSURAH, 'ي');\n }\n if (taMarbutahToHa) {\n s = s.replace(RX_TA_MARBUTAH, 'ه');\n }\n return s;\n};\n\n/**\n * Removes Latin letters/digits and common OCR noise by converting them to spaces.\n */\nconst removeLatinAndSymbolNoise = (s: string, enable: boolean): string =>\n enable ? s.replace(RX_LATIN_AND_SYMBOLS, ' ') : s;\n\n/**\n * Applies letter filters:\n * - `lettersAndSpacesOnly`: keep Arabic letters and whitespace, drop everything else to spaces.\n * - `lettersOnly`: keep only Arabic letters, drop everything else.\n */\nconst applyLetterFilters = (s: string, lettersAndSpacesOnly: boolean, lettersOnly: boolean): string => {\n if (lettersAndSpacesOnly) {\n return s.replace(RX_NOT_LETTERS_OR_SPACE, ' ');\n }\n if (lettersOnly) {\n return s.replace(RX_NON_ARABIC_LETTERS, '');\n }\n return s;\n};\n\n/**\n * Collapses whitespace runs and trims if requested.\n */\nconst normalizeWhitespace = (s: string, collapse: boolean, doTrim: boolean): string => {\n if (collapse) {\n s = s.replace(RX_SPACES, ' ');\n }\n if (doTrim) {\n s = s.trim();\n }\n return s;\n};\n\n/**\n * Resolves a boolean by taking an optional override over a preset value.\n */\nconst resolveBoolean = (presetValue: boolean, override?: boolean): boolean =>\n override === undefined ? presetValue : !!override;\n\n/**\n * Resolves the tatweel mode by taking an optional override over a preset mode.\n * An override of `true` maps to `'safe'` for convenience.\n */\nconst resolveTatweelMode = (\n presetValue: false | 'safe' | 'all',\n override?: boolean | 'safe' | 'all',\n): false | 'safe' | 'all' => {\n if (override === undefined) {\n return presetValue;\n }\n if (override === true) {\n return 'safe';\n }\n if (override === false) {\n return false;\n }\n return override;\n};\n\nconst PRESETS: Record<SanitizePreset, PresetOptions> = {\n light: {\n nfc: true,\n stripZeroWidth: true,\n zeroWidthToSpace: false,\n stripDiacritics: false,\n stripTatweel: false,\n normalizeAlif: false,\n replaceAlifMaqsurah: false,\n replaceTaMarbutahWithHa: false,\n stripLatinAndSymbols: false,\n keepOnlyArabicLetters: false,\n lettersAndSpacesOnly: false,\n collapseWhitespace: true,\n trim: true,\n removeHijriMarker: false,\n },\n search: {\n nfc: true,\n stripZeroWidth: true,\n zeroWidthToSpace: false,\n stripDiacritics: true,\n stripTatweel: 'all',\n normalizeAlif: true,\n replaceAlifMaqsurah: true,\n replaceTaMarbutahWithHa: false,\n stripLatinAndSymbols: false,\n keepOnlyArabicLetters: false,\n lettersAndSpacesOnly: false,\n collapseWhitespace: true,\n trim: true,\n removeHijriMarker: true,\n },\n aggressive: {\n nfc: true,\n stripZeroWidth: true,\n zeroWidthToSpace: false,\n stripDiacritics: true,\n stripTatweel: 'all',\n normalizeAlif: true,\n replaceAlifMaqsurah: true,\n replaceTaMarbutahWithHa: true,\n stripLatinAndSymbols: true,\n keepOnlyArabicLetters: false,\n lettersAndSpacesOnly: true,\n collapseWhitespace: true,\n trim: true,\n removeHijriMarker: true,\n },\n} as const;\n\nconst PRESET_NONE: PresetOptions = {\n nfc: false,\n stripZeroWidth: false,\n zeroWidthToSpace: false,\n stripDiacritics: false,\n stripTatweel: false,\n normalizeAlif: false,\n replaceAlifMaqsurah: false,\n replaceTaMarbutahWithHa: false,\n stripLatinAndSymbols: false,\n keepOnlyArabicLetters: false,\n lettersAndSpacesOnly: false,\n collapseWhitespace: false,\n trim: false,\n removeHijriMarker: false,\n};\n\n/**\n * Sanitizes Arabic text according to a preset or custom options.\n *\n * Presets:\n * - `'light'`: NFC, zero-width removal, collapse/trim spaces.\n * - `'search'`: removes diacritics and tatweel, normalizes Alif and ى→ي, removes Hijri marker.\n * - `'aggressive'`: ideal for FTS; keeps letters+spaces only and strips common noise.\n *\n * Custom options:\n * - Passing an options object overlays the selected `base` preset (default `'light'`).\n * - Use `base: 'none'` to apply **only** the rules you specify (e.g., tatweel only).\n *\n * Examples:\n * ```ts\n * sanitizeArabic('أبـــتِـــكَةُ', { base: 'none', stripTatweel: true }); // 'أبتِكَةُ'\n * sanitizeArabic('1435/3/29 هـ', 'aggressive'); // '1435 3 29'\n * sanitizeArabic('اَلسَّلَامُ عَلَيْكُمْ', 'search'); // 'السلام عليكم'\n * ```\n */\nexport const sanitizeArabic = (input: string, optionsOrPreset: SanitizePreset | SanitizeOptions = 'search'): string => {\n if (!input) {\n return '';\n }\n\n let preset: PresetOptions;\n let opts: SanitizeOptions | null = null;\n\n if (typeof optionsOrPreset === 'string') {\n preset = PRESETS[optionsOrPreset];\n } else {\n const base = optionsOrPreset.base ?? 'light';\n preset = base === 'none' ? PRESET_NONE : PRESETS[base];\n opts = optionsOrPreset;\n }\n\n const nfc = resolveBoolean(preset.nfc, opts?.nfc);\n const stripZW = resolveBoolean(preset.stripZeroWidth, opts?.stripZeroWidth);\n const zwAsSpace = resolveBoolean(preset.zeroWidthToSpace, opts?.zeroWidthToSpace);\n const removeDia = resolveBoolean(preset.stripDiacritics, opts?.stripDiacritics);\n const normAlif = resolveBoolean(preset.normalizeAlif, opts?.normalizeAlif);\n const maqToYa = resolveBoolean(preset.replaceAlifMaqsurah, opts?.replaceAlifMaqsurah);\n const taToHa = resolveBoolean(preset.replaceTaMarbutahWithHa, opts?.replaceTaMarbutahWithHa);\n const stripNoise = resolveBoolean(preset.stripLatinAndSymbols, opts?.stripLatinAndSymbols);\n const lettersSpacesOnly = resolveBoolean(preset.lettersAndSpacesOnly, opts?.lettersAndSpacesOnly);\n const lettersOnly = resolveBoolean(preset.keepOnlyArabicLetters, opts?.keepOnlyArabicLetters);\n const collapseWS = resolveBoolean(preset.collapseWhitespace, opts?.collapseWhitespace);\n const doTrim = resolveBoolean(preset.trim, opts?.trim);\n const removeHijri = resolveBoolean(preset.removeHijriMarker, opts?.removeHijriMarker);\n const tatweelMode = resolveTatweelMode(preset.stripTatweel, opts?.stripTatweel);\n\n let s = input;\n s = applyNfcNormalization(s, nfc);\n s = removeZeroWidthControls(s, stripZW, zwAsSpace);\n if (removeHijri) {\n s = removeHijriDateMarker(s);\n }\n s = removeDiacriticsAndTatweel(s, removeDia, tatweelMode);\n s = applyCharacterMappings(s, normAlif, maqToYa, taToHa);\n\n if (!lettersSpacesOnly) {\n s = removeLatinAndSymbolNoise(s, stripNoise);\n }\n s = applyLetterFilters(s, lettersSpacesOnly, lettersOnly);\n\n s = normalizeWhitespace(s, collapseWS, doTrim);\n\n return s;\n};\n","/**\n * Calculates Levenshtein distance between two strings using space-optimized dynamic programming.\n * The Levenshtein distance is the minimum number of single-character edits (insertions,\n * deletions, or substitutions) required to change one string into another.\n *\n * @param textA - First string to compare\n * @param textB - Second string to compare\n * @returns Minimum edit distance between the two strings\n * @complexity Time: O(m*n), Space: O(min(m,n)) where m,n are string lengths\n * @example\n * calculateLevenshteinDistance('kitten', 'sitting') // Returns 3\n * calculateLevenshteinDistance('', 'hello') // Returns 5\n */\nexport const calculateLevenshteinDistance = (textA: string, textB: string): number => {\n const lengthA = textA.length;\n const lengthB = textB.length;\n\n if (lengthA === 0) {\n return lengthB;\n }\n\n if (lengthB === 0) {\n return lengthA;\n }\n\n // Use shorter string for the array to optimize space\n const [shorter, longer] = lengthA <= lengthB ? [textA, textB] : [textB, textA];\n const shortLen = shorter.length;\n const longLen = longer.length;\n\n let previousRow = Array.from({ length: shortLen + 1 }, (_, index) => index);\n\n for (let i = 1; i <= longLen; i++) {\n const currentRow = [i];\n\n for (let j = 1; j <= shortLen; j++) {\n const substitutionCost = longer[i - 1] === shorter[j - 1] ? 0 : 1;\n const minCost = Math.min(\n previousRow[j] + 1, // deletion\n currentRow[j - 1] + 1, // insertion\n previousRow[j - 1] + substitutionCost, // substitution\n );\n currentRow.push(minCost);\n }\n\n previousRow = currentRow;\n }\n\n return previousRow[shortLen];\n};\n\n/**\n * Early exit check for bounded Levenshtein distance.\n */\nconst shouldEarlyExit = (a: string, b: string, maxDist: number): number | null => {\n if (Math.abs(a.length - b.length) > maxDist) {\n return maxDist + 1;\n }\n if (a.length === 0) {\n return b.length <= maxDist ? b.length : maxDist + 1;\n }\n if (b.length === 0) {\n return a.length <= maxDist ? a.length : maxDist + 1;\n }\n return null;\n};\n\n/**\n * Initializes arrays for bounded Levenshtein calculation.\n */\nconst initializeBoundedArrays = (m: number): [Int16Array, Int16Array] => {\n const prev = new Int16Array(m + 1);\n const curr = new Int16Array(m + 1);\n for (let j = 0; j <= m; j++) {\n prev[j] = j;\n }\n return [prev, curr];\n};\n\n/**\n * Calculates the bounds for the current row in bounded Levenshtein.\n */\nconst getRowBounds = (i: number, maxDist: number, m: number) => ({\n from: Math.max(1, i - maxDist),\n to: Math.min(m, i + maxDist),\n});\n\n/**\n * Processes a single cell in the bounded Levenshtein matrix.\n */\nconst processBoundedCell = (a: string, b: string, i: number, j: number, prev: Int16Array, curr: Int16Array): number => {\n const cost = a[i - 1] === b[j - 1] ? 0 : 1;\n const del = prev[j] + 1;\n const ins = curr[j - 1] + 1;\n const sub = prev[j - 1] + cost;\n return Math.min(del, ins, sub);\n};\n\n/**\n * Processes a single row in bounded Levenshtein calculation.\n */\nconst processBoundedRow = (\n a: string,\n b: string,\n i: number,\n maxDist: number,\n prev: Int16Array,\n curr: Int16Array,\n): number => {\n const m = b.length;\n const big = maxDist + 1;\n const { from, to } = getRowBounds(i, maxDist, m);\n\n curr[0] = i;\n let rowMin = i;\n\n // Fill out-of-bounds cells\n for (let j = 1; j < from; j++) {\n curr[j] = big;\n }\n for (let j = to + 1; j <= m; j++) {\n curr[j] = big;\n }\n\n // Process valid range\n for (let j = from; j <= to; j++) {\n const val = processBoundedCell(a, b, i, j, prev, curr);\n curr[j] = val;\n if (val < rowMin) {\n rowMin = val;\n }\n }\n\n return rowMin;\n};\n\n/**\n * Calculates bounded Levenshtein distance with early termination.\n * More efficient when you only care about distances up to a threshold.\n */\nexport const boundedLevenshtein = (a: string, b: string, maxDist: number): number => {\n const big = maxDist + 1;\n\n // Early exit checks\n const earlyResult = shouldEarlyExit(a, b, maxDist);\n if (earlyResult !== null) {\n return earlyResult;\n }\n\n // Ensure a is shorter for optimization\n if (a.length > b.length) {\n return boundedLevenshtein(b, a, maxDist);\n }\n\n const [prev, curr] = initializeBoundedArrays(b.length);\n\n for (let i = 1; i <= a.length; i++) {\n const rowMin = processBoundedRow(a, b, i, maxDist, prev, curr);\n\n if (rowMin > maxDist) {\n return big;\n }\n\n // Swap arrays\n for (let j = 0; j <= b.length; j++) {\n prev[j] = curr[j];\n }\n }\n\n return prev[b.length] <= maxDist ? prev[b.length] : big;\n};\n","import { calculateLevenshteinDistance } from './levenshthein';\nimport { sanitizeArabic } from './sanitize';\n\n// Alignment scoring constants\nconst ALIGNMENT_SCORES = {\n GAP_PENALTY: -1,\n MISMATCH_PENALTY: -2,\n PERFECT_MATCH: 2,\n SOFT_MATCH: 1,\n} as const;\n\n/**\n * Calculates similarity ratio between two strings as a value between 0.0 and 1.0.\n * Uses Levenshtein distance normalized by the length of the longer string.\n * A ratio of 1.0 indicates identical strings, 0.0 indicates completely different strings.\n *\n * @param textA - First string to compare\n * @param textB - Second string to compare\n * @returns Similarity ratio from 0.0 (completely different) to 1.0 (identical)\n * @example\n * calculateSimilarity('hello', 'hello') // Returns 1.0\n * calculateSimilarity('hello', 'help') // Returns 0.6\n */\nexport const calculateSimilarity = (textA: string, textB: string): number => {\n const maxLength = Math.max(textA.length, textB.length) || 1;\n const distance = calculateLevenshteinDistance(textA, textB);\n return (maxLength - distance) / maxLength;\n};\n\n/**\n * Checks if two texts are similar after Arabic normalization.\n * Normalizes both texts by removing diacritics and decorative elements,\n * then compares their similarity against the provided threshold.\n *\n * @param textA - First text to compare\n * @param textB - Second text to compare\n * @param threshold - Similarity threshold (0.0 to 1.0)\n * @returns True if normalized texts meet the similarity threshold\n * @example\n * areSimilarAfterNormalization('السَّلام', 'السلام', 0.9) // Returns true\n */\nexport const areSimilarAfterNormalization = (textA: string, textB: string, threshold: number = 0.6): boolean => {\n const normalizedA = sanitizeArabic(textA);\n const normalizedB = sanitizeArabic(textB);\n return calculateSimilarity(normalizedA, normalizedB) >= threshold;\n};\n\n/**\n * Calculates alignment score for two tokens in sequence alignment.\n * Uses different scoring criteria: perfect match after normalization gets highest score,\n * typo symbols or highly similar tokens get soft match score, mismatches get penalty.\n *\n * @param tokenA - First token to score\n * @param tokenB - Second token to score\n * @param typoSymbols - Array of special symbols that get preferential treatment\n * @param similarityThreshold - Threshold for considering tokens highly similar\n * @returns Alignment score (higher is better match)\n * @example\n * calculateAlignmentScore('hello', 'hello', [], 0.8) // Returns 2 (perfect match)\n * calculateAlignmentScore('hello', 'help', [], 0.8) // Returns 1 or -2 based on similarity\n */\nexport const calculateAlignmentScore = (\n tokenA: string,\n tokenB: string,\n typoSymbols: string[],\n similarityThreshold: number,\n): number => {\n const normalizedA = sanitizeArabic(tokenA);\n const normalizedB = sanitizeArabic(tokenB);\n\n if (normalizedA === normalizedB) {\n return ALIGNMENT_SCORES.PERFECT_MATCH;\n }\n\n const isTypoSymbol = typoSymbols.includes(tokenA) || typoSymbols.includes(tokenB);\n const isHighlySimilar = calculateSimilarity(normalizedA, normalizedB) >= similarityThreshold;\n\n return isTypoSymbol || isHighlySimilar ? ALIGNMENT_SCORES.SOFT_MATCH : ALIGNMENT_SCORES.MISMATCH_PENALTY;\n};\n\ntype AlignedTokenPair = [null | string, null | string];\n\ntype AlignmentCell = {\n direction: 'diagonal' | 'left' | 'up' | null;\n score: number;\n};\n\n/**\n * Backtracks through the scoring matrix to reconstruct optimal sequence alignment.\n * Follows the directional indicators in the matrix to build the sequence of aligned\n * token pairs from the Needleman-Wunsch algorithm.\n *\n * @param matrix - Scoring matrix with directional information from alignment\n * @param tokensA - First sequence of tokens\n * @param tokensB - Second sequence of tokens\n * @returns Array of aligned token pairs, where null indicates a gap\n * @throws Error if invalid alignment direction is encountered\n */\nexport const backtrackAlignment = (\n matrix: AlignmentCell[][],\n tokensA: string[],\n tokensB: string[],\n): AlignedTokenPair[] => {\n const alignment: AlignedTokenPair[] = [];\n let i = tokensA.length;\n let j = tokensB.length;\n\n while (i > 0 || j > 0) {\n const currentCell = matrix[i][j];\n\n switch (currentCell.direction) {\n case 'diagonal':\n alignment.push([tokensA[--i], tokensB[--j]]);\n break;\n case 'left':\n alignment.push([null, tokensB[--j]]);\n break;\n case 'up':\n alignment.push([tokensA[--i], null]);\n break;\n default:\n throw new Error('Invalid alignment direction');\n }\n }\n\n return alignment.reverse();\n};\n\n/**\n * Initializes the scoring matrix with gap penalties.\n */\nconst initializeScoringMatrix = (lengthA: number, lengthB: number): AlignmentCell[][] => {\n const matrix: AlignmentCell[][] = Array.from({ length: lengthA + 1 }, () =>\n Array.from({ length: lengthB + 1 }, () => ({ direction: null, score: 0 })),\n );\n\n // Initialize first row and column with gap penalties\n for (let i = 1; i <= lengthA; i++) {\n matrix[i][0] = { direction: 'up', score: i * ALIGNMENT_SCORES.GAP_PENALTY };\n }\n for (let j = 1; j <= lengthB; j++) {\n matrix[0][j] = { direction: 'left', score: j * ALIGNMENT_SCORES.GAP_PENALTY };\n }\n\n return matrix;\n};\n\n/**\n * Determines the best alignment direction and score for a cell.\n */\nconst getBestAlignment = (\n diagonalScore: number,\n upScore: number,\n leftScore: number,\n): { direction: 'diagonal' | 'up' | 'left'; score: number } => {\n const maxScore = Math.max(diagonalScore, upScore, leftScore);\n\n if (maxScore === diagonalScore) {\n return { direction: 'diagonal', score: maxScore };\n }\n if (maxScore === upScore) {\n return { direction: 'up', score: maxScore };\n }\n return { direction: 'left', score: maxScore };\n};\n\n/**\n * Performs global sequence alignment using the Needleman-Wunsch algorithm.\n * Aligns two token sequences to find the optimal pairing that maximizes\n * the total alignment score, handling insertions, deletions, and substitutions.\n *\n * @param tokensA - First sequence of tokens to align\n * @param tokensB - Second sequence of tokens to align\n * @param typoSymbols - Special symbols that affect scoring\n * @param similarityThreshold - Threshold for high similarity scoring\n * @returns Array of aligned token pairs, with null indicating gaps\n * @example\n * alignTokenSequences(['a', 'b'], ['a', 'c'], [], 0.8)\n * // Returns [['a', 'a'], ['b', 'c']]\n */\nexport const alignTokenSequences = (\n tokensA: string[],\n tokensB: string[],\n typoSymbols: string[],\n similarityThreshold: number,\n): AlignedTokenPair[] => {\n const lengthA = tokensA.length;\n const lengthB = tokensB.length;\n\n const matrix = initializeScoringMatrix(lengthA, lengthB);\n const typoSymbolsSet = new Set(typoSymbols);\n const normalizedA = tokensA.map((t) => sanitizeArabic(t));\n const normalizedB = tokensB.map((t) => sanitizeArabic(t));\n\n // Fill scoring matrix\n for (let i = 1; i <= lengthA; i++) {\n for (let j = 1; j <= lengthB; j++) {\n const aNorm = normalizedA[i - 1];\n const bNorm = normalizedB[j - 1];\n let alignmentScore: number;\n if (aNorm === bNorm) {\n alignmentScore = ALIGNMENT_SCORES.PERFECT_MATCH;\n } else {\n const isTypo = typoSymbolsSet.has(tokensA[i - 1]) || typoSymbolsSet.has(tokensB[j - 1]);\n const highSim = calculateSimilarity(aNorm, bNorm) >= similarityThreshold;\n alignmentScore = isTypo || highSim ? ALIGNMENT_SCORES.SOFT_MATCH : ALIGNMENT_SCORES.MISMATCH_PENALTY;\n }\n\n const diagonalScore = matrix[i - 1][j - 1].score + alignmentScore;\n const upScore = matrix[i - 1][j].score + ALIGNMENT_SCORES.GAP_PENALTY;\n const leftScore = matrix[i][j - 1].score + ALIGNMENT_SCORES.GAP_PENALTY;\n\n const { direction, score } = getBestAlignment(diagonalScore, upScore, leftScore);\n matrix[i][j] = { direction, score };\n }\n }\n\n return backtrackAlignment(matrix, tokensA, tokensB);\n};\n","import { sanitizeArabic } from './utils/sanitize';\nimport { areSimilarAfterNormalization, calculateSimilarity } from './utils/similarity';\n\n/**\n * Aligns split text segments to match target lines by finding the best order.\n *\n * This function handles cases where text lines have been split into segments\n * and need to be merged back together in the correct order. It compares\n * different arrangements of the segments against target lines to find the\n * best match based on similarity scores.\n *\n * @param targetLines - Array where each element is either a string to align against, or falsy to skip alignment\n * @param segmentLines - Array of text segments that may represent split versions of target lines.\n * @returns Array of aligned text lines\n */\nexport const alignTextSegments = (targetLines: string[], segmentLines: string[]) => {\n const alignedLines: string[] = [];\n let segmentIndex = 0;\n\n for (const targetLine of targetLines) {\n if (segmentIndex >= segmentLines.length) {\n break;\n }\n\n if (targetLine) {\n // Process line that needs alignment\n const { result, segmentsConsumed } = processAlignmentTarget(targetLine, segmentLines, segmentIndex);\n\n if (result) {\n alignedLines.push(result);\n }\n segmentIndex += segmentsConsumed;\n } else {\n // For lines that don't need alignment, use one-to-one correspondence\n alignedLines.push(segmentLines[segmentIndex]);\n segmentIndex++;\n }\n }\n\n // Add any remaining segments that were not processed\n if (segmentIndex < segmentLines.length) {\n alignedLines.push(...segmentLines.slice(segmentIndex));\n }\n\n return alignedLines;\n};\n\n/**\n * Tries to merge two segments in both possible orders and returns the best match.\n */\nconst findBestSegmentMerge = (targetLine: string, partA: string, partB: string) => {\n const mergedForward = `${partA} ${partB}`;\n const mergedReversed = `${partB} ${partA}`;\n\n const normalizedTarget = sanitizeArabic(targetLine);\n const scoreForward = calculateSimilarity(normalizedTarget, sanitizeArabic(mergedForward));\n const scoreReversed = calculateSimilarity(normalizedTarget, sanitizeArabic(mergedReversed));\n\n return scoreForward >= scoreReversed ? mergedForward : mergedReversed;\n};\n\n/**\n * Processes a single target line that needs alignment.\n */\nconst processAlignmentTarget = (targetLine: string, segmentLines: string[], segmentIndex: number) => {\n const currentSegment = segmentLines[segmentIndex];\n\n // First, check if the current segment is already a good match\n if (areSimilarAfterNormalization(targetLine, currentSegment)) {\n return { result: currentSegment, segmentsConsumed: 1 };\n }\n\n // If not a direct match, try to merge two segments\n const partA = segmentLines[segmentIndex];\n const partB = segmentLines[segmentIndex + 1];\n\n // Ensure we have two parts to merge\n if (!partA || !partB) {\n return partA ? { result: partA, segmentsConsumed: 1 } : { result: '', segmentsConsumed: 0 };\n }\n\n const bestMerge = findBestSegmentMerge(targetLine, partA, partB);\n return { result: bestMerge, segmentsConsumed: 2 };\n};\n","/**\n * Represents an error found when checking balance of quotes or brackets in text.\n */\ntype BalanceError = {\n /** The character that caused the error */\n char: string;\n /** The position of the character in the string */\n index: number;\n /** The reason for the error */\n reason: 'mismatched' | 'unclosed' | 'unmatched';\n /** The type of character that caused the error */\n type: 'bracket' | 'quote';\n};\n\n/**\n * Result of a balance check operation.\n */\ntype BalanceResult = {\n /** Array of errors found during balance checking */\n errors: BalanceError[];\n /** Whether the text is properly balanced */\n isBalanced: boolean;\n};\n\n/**\n * Checks if all double quotes in a string are balanced and returns detailed error information.\n *\n * A string has balanced quotes when every opening quote has a corresponding closing quote.\n * This function counts all quote characters and determines if there's an even number of them.\n * If there's an odd number, the last quote is marked as unmatched.\n *\n * @param str - The string to check for quote balance\n * @returns An object containing balance status and any errors found\n *\n * @example\n * ```typescript\n * checkQuoteBalance('Hello \"world\"') // { errors: [], isBalanced: true }\n * checkQuoteBalance('Hello \"world') // { errors: [{ char: '\"', index: 6, reason: 'unmatched', type: 'quote' }], isBalanced: false }\n * ```\n */\nconst checkQuoteBalance = (str: string): BalanceResult => {\n const errors: BalanceError[] = [];\n let quoteCount = 0;\n let lastQuoteIndex = -1;\n\n for (let i = 0; i < str.length; i++) {\n if (str[i] === '\"') {\n quoteCount++;\n lastQuoteIndex = i;\n }\n }\n\n const isBalanced = quoteCount % 2 === 0;\n\n if (!isBalanced && lastQuoteIndex !== -1) {\n errors.push({\n char: '\"',\n index: lastQuoteIndex,\n reason: 'unmatched',\n type: 'quote',\n });\n }\n\n return { errors, isBalanced };\n};\n\n/** Mapping of opening brackets to their corresponding closing brackets */\nexport const BRACKETS = { '«': '»', '(': ')', '[': ']', '{': '}' };\n\n/** Set of all opening bracket characters */\nexport const OPEN_BRACKETS = new Set(['«', '(', '[', '{']);\n\n/** Set of all closing bracket characters */\nexport const CLOSE_BRACKETS = new Set(['»', ')', ']', '}']);\n\n/**\n * Checks if all brackets in a string are properly balanced and returns detailed error information.\n *\n * A string has balanced brackets when:\n * - Every opening bracket has a corresponding closing bracket\n * - Brackets are properly nested (no crossing pairs)\n * - Each closing bracket matches the most recent unmatched opening bracket\n *\n * Supports the following bracket pairs: (), [], {}, «»\n *\n * @param str - The string to check for bracket balance\n * @returns An object containing balance status and any errors found\n *\n * @example\n * ```typescript\n * checkBracketBalance('(hello [world])') // { errors: [], isBalanced: true }\n * checkBracketBalance('(hello [world)') // { errors: [{ char: '[', index: 7, reason: 'unclosed', type: 'bracket' }], isBalanced: false }\n * checkBracketBalance('(hello ]world[') // { errors: [...], isBalanced: false }\n * ```\n */\nconst checkBracketBalance = (str: string): BalanceResult => {\n const errors: BalanceError[] = [];\n const stack: Array<{ char: string; index: number }> = [];\n\n for (let i = 0; i < str.length; i++) {\n const char = str[i];\n\n if (OPEN_BRACKETS.has(char)) {\n stack.push({ char, index: i });\n } else if (CLOSE_BRACKETS.has(char)) {\n const lastOpen = stack.pop();\n\n if (!lastOpen) {\n errors.push({\n char,\n index: i,\n reason: 'unmatched',\n type: 'bracket',\n });\n } else if (BRACKETS[lastOpen.char as keyof typeof BRACKETS] !== char) {\n errors.push({\n char: lastOpen.char,\n index: lastOpen.index,\n reason: 'mismatched',\n type: 'bracket',\n });\n errors.push({\n char,\n index: i,\n reason: 'mismatched',\n type: 'bracket',\n });\n }\n }\n }\n\n stack.forEach(({ char, index }) => {\n errors.push({\n char,\n index,\n reason: 'unclosed',\n type: 'bracket',\n });\n });\n\n return { errors, isBalanced: errors.length === 0 };\n};\n\n/**\n * Checks if both quotes and brackets are balanced in a string and returns detailed error information.\n *\n * This function combines the results of both quote and bracket balance checking,\n * providing a comprehensive analysis of all balance issues in the text.\n * The errors are sorted by their position in the string for easier debugging.\n *\n * @param str - The string to check for overall balance\n * @returns An object containing combined balance status and all errors found, sorted by position\n *\n * @example\n * ```typescript\n * checkBalance('Hello \"world\" and (test)') // { errors: [], isBalanced: true }\n * checkBalance('Hello \"world and (test') // { errors: [...], isBalanced: false }\n * ```\n */\nexport const checkBalance = (str: string): BalanceResult => {\n const quoteResult = checkQuoteBalance(str);\n const bracketResult = checkBracketBalance(str);\n\n return {\n errors: [...quoteResult.errors, ...bracketResult.errors].sort((a, b) => a.index - b.index),\n isBalanced: quoteResult.isBalanced && bracketResult.isBalanced,\n };\n};\n\n/**\n * Enhanced error detection that returns absolute character positions for use with HighlightableTextarea.\n *\n * This interface extends the basic BalanceError to include absolute positioning\n * across multiple lines of text, making it suitable for text editors and\n * syntax highlighters that need precise character positioning.\n */\nexport interface CharacterError {\n /** Absolute character position from the start of the entire text */\n absoluteIndex: number;\n /** The character that caused the error */\n char: string;\n /** The reason for the error */\n reason: 'mismatched' | 'unclosed' | 'unmatched';\n /** The type of character that caused the error */\n type: 'bracket' | 'quote';\n}\n\n/**\n * Gets detailed character-level errors for unbalanced quotes and brackets in multi-line text.\n *\n * This function processes text line by line, but only checks lines longer than 10 characters\n * for balance issues. It returns absolute positions that can be used with text editors\n * or highlighting components that need precise character positioning across the entire text.\n *\n * The absolute index accounts for newline characters between lines, providing accurate\n * positioning for the original text string.\n *\n * @param text - The multi-line text to analyze for balance errors\n * @returns Array of character errors with absolute positioning information\n *\n * @example\n * ```typescript\n * const text = 'Line 1 with \"quote\\nLine 2 with (bracket';\n * const errors = getUnbalancedErrors(text);\n * // Returns errors with absoluteIndex pointing to exact character positions\n * ```\n */\nexport const getUnbalancedErrors = (text: string): CharacterError[] => {\n const characterErrors: CharacterError[] = [];\n const lines = text.split('\\n');\n let absoluteIndex = 0;\n\n lines.forEach((line, lineIndex) => {\n if (line.length > 10) {\n const balanceResult = checkBalance(line);\n if (!balanceResult.isBalanced) {\n balanceResult.errors.forEach((error) => {\n characterErrors.push({\n absoluteIndex: absoluteIndex + error.index,\n char: error.char,\n reason: error.reason,\n type: error.type,\n });\n });\n }\n }\n // Add 1 for the newline character (except for the last line)\n absoluteIndex += line.length + (lineIndex < lines.length - 1 ? 1 : 0);\n });\n\n return characterErrors;\n};\n\n/**\n * Checks if all double quotes in a string are balanced.\n *\n * This is a convenience function that returns only the boolean result\n * without detailed error information.\n *\n * @param str - The string to check for quote balance\n * @returns True if quotes are balanced, false otherwise\n *\n * @example\n * ```typescript\n * areQuotesBalanced('Hello \"world\"') // true\n * areQuotesBalanced('Hello \"world') // false\n * ```\n */\nexport const areQuotesBalanced = (str: string): boolean => {\n return checkQuoteBalance(str).isBalanced;\n};\n\n/**\n * Checks if all brackets in a string are properly balanced.\n *\n * This is a convenience function that returns only the boolean result\n * without detailed error information.\n *\n * @param str - The string to check for bracket balance\n * @returns True if brackets are balanced, false otherwise\n *\n * @example\n * ```typescript\n * areBracketsBalanced('(hello [world])') // true\n * areBracketsBalanced('(hello [world') // false\n * ```\n */\nexport const areBracketsBalanced = (str: string): boolean => {\n return checkBracketBalance(str).isBalanced;\n};\n\n/**\n * Checks if both quotes and brackets are balanced in a string.\n *\n * This is a convenience function that returns only the boolean result\n * without detailed error information.\n *\n * @param str - The string to check for overall balance\n * @returns True if both quotes and brackets are balanced, false otherwise\n *\n * @example\n * ```typescript\n * isBalanced('Hello \"world\" and (test)') // true\n * isBalanced('Hello \"world and (test') // false\n * ```\n */\nexport const isBalanced = (str: string): boolean => {\n return checkBalance(str).isBalanced;\n};\n","export const INTAHA_ACTUAL = 'اهـ';\n\n/**\n * Collection of regex patterns used throughout the library for text processing\n */\nexport const PATTERNS = {\n /** Matches Arabic characters across all Unicode blocks */\n arabicCharacters: /[\\u0600-\\u06FF\\u0750-\\u077F\\u08A0-\\u08FF\\uFB50-\\uFDFF\\uFE70-\\uFEFF]/,\n\n /** Matches Arabic-Indic digits (٠-٩) and Western digits (0-9) */\n arabicDigits: /[0-9\\u0660-\\u0669]+/,\n\n /** Matches footnote references at the start of a line with Arabic-Indic digits: ^\\([\\u0660-\\u0669]+\\) */\n arabicFootnoteReferenceRegex: /^\\([\\u0660-\\u0669]+\\)/g,\n\n /** Matches Arabic letters and digits (both Western 0-9 and Arabic-Indic ٠-٩) */\n arabicLettersAndDigits: /[0-9\\u0621-\\u063A\\u0641-\\u064A\\u0660-\\u0669]+/g,\n\n /** Matches Arabic punctuation marks and whitespace characters */\n arabicPunctuationAndWhitespace: /[\\s\\u060C\\u061B\\u061F\\u06D4]+/,\n\n /** Matches footnote references with Arabic-Indic digits in parentheses: \\([\\u0660-\\u0669]+\\) */\n arabicReferenceRegex: /\\([\\u0660-\\u0669]+\\)/g,\n\n /** Matches embedded footnotes within text: \\([0-9\\u0660-\\u0669]+\\) */\n footnoteEmbedded: /\\([0-9\\u0660-\\u0669]+\\)/,\n\n /** Matches standalone footnote markers at line start/end: ^\\(?[0-9\\u0660-\\u0669]+\\)?[،.]?$ */\n footnoteStandalone: /^\\(?[0-9\\u0660-\\u0669]+\\)?[،.]?$/,\n\n /** Matches invalid/problematic footnote references: empty \"()\" or OCR-confused endings */\n invalidReferenceRegex: /\\(\\)|\\([.1OV9]+\\)/g, // Combined pattern for detecting any invalid/problematic references\n\n /** Matches OCR-confused footnote references at line start with characters like .1OV9 */\n ocrConfusedFootnoteReferenceRegex: /^\\([.1OV9]+\\)/g,\n\n /** Matches OCR-confused footnote references with characters commonly misread as Arabic digits */\n ocrConfusedReferenceRegex: /\\([.1OV9]+\\)/g,\n\n /** Matches one or more whitespace characters */\n whitespace: /\\s+/,\n};\n\n/**\n * Extracts the first sequence of Arabic or Western digits from text.\n * Used primarily for footnote number comparison to match related footnote elements.\n *\n * @param text - Text containing digits to extract\n * @returns First digit sequence found, or empty string if none found\n * @example\n * extractDigits('(٥)أخرجه البخاري') // Returns '٥'\n * extractDigits('See note (123)') // Returns '123'\n */\nexport const extractDigits = (text: string): string => {\n const match = text.match(PATTERNS.arabicDigits);\n return match ? match[0] : '';\n};\n\n/**\n * Tokenizes text into individual words while preserving special symbols.\n * Adds spacing around preserved symbols to ensure they are tokenized separately,\n * then splits on whitespace.\n *\n * @param text - Text to tokenize\n * @param preserveSymbols - Array of symbols that should be tokenized as separate tokens\n * @returns Array of tokens, or empty array if input is empty/whitespace\n * @example\n * tokenizeText('Hello ﷺ world', ['ﷺ']) // Returns ['Hello', 'ﷺ', 'world']\n */\nexport const tokenizeText = (text: string, preserveSymbols: string[] = []): string[] => {\n let processedText = text;\n\n // Add spaces around each preserve symbol to ensure they're tokenized separately\n for (const symbol of preserveSymbols) {\n const symbolRegex = new RegExp(symbol, 'g');\n processedText = processedText.replace(symbolRegex, ` ${symbol} `);\n }\n\n return processedText.trim().split(PATTERNS.whitespace).filter(Boolean);\n};\n\n/**\n * Handles fusion of standalone and embedded footnotes during token processing.\n * Detects patterns where standalone footnotes should be merged with embedded ones\n * or where trailing standalone footnotes should be skipped.\n *\n * @param result - Current result array being built\n * @param previousToken - The previous token in the sequence\n * @param currentToken - The current token being processed\n * @returns True if the current token was handled (fused or skipped), false otherwise\n * @example\n * // (٥) + (٥)أخرجه → result gets (٥)أخرجه\n * // (٥)أخرجه + (٥) → (٥) is skipped\n */\nexport const handleFootnoteFusion = (result: string[], previousToken: string, currentToken: string): boolean => {\n const prevIsStandalone = PATTERNS.footnoteStandalone.test(previousToken);\n const currHasEmbedded = PATTERNS.footnoteEmbedded.test(currentToken);\n const currIsStandalone = PATTERNS.footnoteStandalone.test(currentToken);\n const prevHasEmbedded = PATTERNS.footnoteEmbedded.test(previousToken);\n\n const prevDigits = extractDigits(previousToken);\n const currDigits = extractDigits(currentToken);\n\n // Replace standalone with fused version: (٥) + (٥)أخرجه → (٥)أخرجه\n if (prevIsStandalone && currHasEmbedded && prevDigits === currDigits) {\n result[result.length - 1] = currentToken;\n return true;\n }\n\n // Skip trailing standalone: (٥)أخرجه + (٥) → (٥)أخرجه\n if (prevHasEmbedded && currIsStandalone && prevDigits === currDigits) {\n return true;\n }\n\n return false;\n};\n\n/**\n * Handles selection logic for tokens with embedded footnotes during alignment.\n * Prefers tokens that contain embedded footnotes over plain text, and among\n * tokens with embedded footnotes, prefers the shorter one.\n *\n * @param tokenA - First token to compare\n * @param tokenB - Second token to compare\n * @returns Array containing selected token(s), or null if no special handling needed\n * @example\n * handleFootnoteSelection('text', '(١)text') // Returns ['(١)text']\n * handleFootnoteSelection('(١)longtext', '(١)text') // Returns ['(١)text']\n */\nexport const handleFootnoteSelection = (tokenA: string, tokenB: string): null | string[] => {\n const aHasEmbedded = PATTERNS.footnoteEmbedded.test(tokenA);\n const bHasEmbedded = PATTERNS.footnoteEmbedded.test(tokenB);\n\n if (aHasEmbedded && !bHasEmbedded) {\n return [tokenA];\n }\n if (bHasEmbedded && !aHasEmbedded) {\n return [tokenB];\n }\n if (aHasEmbedded && bHasEmbedded) {\n return [tokenA.length <= tokenB.length ? tokenA : tokenB];\n }\n\n return null;\n};\n\n/**\n * Handles selection logic for standalone footnote tokens during alignment.\n * Manages cases where one or both tokens are standalone footnotes, preserving\n * both tokens when one is a footnote and the other is regular text.\n *\n * @param tokenA - First token to compare\n * @param tokenB - Second token to compare\n * @returns Array containing selected token(s), or null if no special handling needed\n * @example\n * handleStandaloneFootnotes('(١)', 'text') // Returns ['(١)', 'text']\n * handleStandaloneFootnotes('(١)', '(٢)') // Returns ['(١)'] (shorter one)\n */\nexport const handleStandaloneFootnotes = (tokenA: string, tokenB: string): null | string[] => {\n const aIsFootnote = PATTERNS.footnoteStandalone.test(tokenA);\n const bIsFootnote = PATTERNS.footnoteStandalone.test(tokenB);\n\n if (aIsFootnote && !bIsFootnote) {\n return [tokenA, tokenB];\n }\n if (bIsFootnote && !aIsFootnote) {\n return [tokenB, tokenA];\n }\n if (aIsFootnote && bIsFootnote) {\n return [tokenA.length <= tokenB.length ? tokenA : tokenB];\n }\n\n return null;\n};\n\n/**\n * Standardizes standalone Hijri symbol ه to هـ when following Arabic digits\n * @param text - Input text to process\n * @returns Text with standardized Hijri symbols\n */\nexport const standardizeHijriSymbol = (text: string): string => {\n // Replace standalone ه with هـ when it appears after Arabic digits (0-9 or ٠-٩)\n // Allow any amount of whitespace between the digit and ه, and consider Arabic punctuation as a boundary.\n // Boundary rule: only Arabic letters/digits should block replacement; punctuation should not.\n return text.replace(/([0-9\\u0660-\\u0669])\\s*ه(?=\\s|$|[^\\u0621-\\u063A\\u0641-\\u064A\\u0660-\\u0669])/gu, '$1 هـ');\n};\n\n/**\n * Standardizes standalone اه to اهـ when appearing as whole word\n * @param text - Input text to process\n * @returns Text with standardized AH Hijri symbols\n */\nexport const standardizeIntahaSymbol = (text: string) => {\n // Replace standalone اه with اهـ when it appears as a whole word\n // Ensures it's preceded by start/whitespace/non-Arabic AND followed by end/whitespace/non-Arabic\n return text.replace(/(^|\\s|[^\\u0600-\\u06FF])اه(?=\\s|$|[^\\u0600-\\u06FF])/gu, `$1${INTAHA_ACTUAL}`);\n};\n","import { PATTERNS } from './utils/textUtils';\n\nconst INVALID_FOOTNOTE = '()';\n\n/**\n * Checks if the given text contains invalid footnote references.\n * Invalid footnotes include empty parentheses \"()\" or OCR-confused characters\n * like \".1OV9\" that were misrecognized instead of Arabic numerals.\n *\n * @param text - Text to check for invalid footnote patterns\n * @returns True if text contains invalid footnote references, false otherwise\n * @example\n * hasInvalidFootnotes('This text has ()') // Returns true\n * hasInvalidFootnotes('This text has (١)') // Returns false\n * hasInvalidFootnotes('OCR mistake (O)') // Returns true\n */\nexport const hasInvalidFootnotes = (text: string): boolean => {\n return PATTERNS.invalidReferenceRegex.test(text);\n};\n\n// Arabic number formatter instance\nconst arabicFormatter = new Intl.NumberFormat('ar-SA');\n\n/**\n * Converts a number to Arabic-Indic numerals using the Intl.NumberFormat API.\n * Uses the 'ar-SA' locale to ensure proper Arabic numeral formatting.\n *\n * @param num - The number to convert to Arabic numerals\n * @returns String representation using Arabic-Indic digits (٠-٩)\n * @example\n * numberToArabic(123) // Returns '١٢٣'\n * numberToArabic(5) // Returns '٥'\n */\nconst numberToArabic = (num: number): string => {\n return arabicFormatter.format(num);\n};\n\n/**\n * Converts OCR-confused characters to their corresponding Arabic-Indic numerals.\n * Handles common OCR misrecognitions where Latin characters are mistaken for Arabic digits.\n *\n * @param char - Single character that may be an OCR mistake\n * @returns Corresponding Arabic-Indic numeral or original character if no mapping exists\n * @example\n * ocrToArabic('O') // Returns '٥' (O often confused with ٥)\n * ocrToArabic('1') // Returns '١' (1 often confused with ١)\n * ocrToArabic('.') // Returns '٠' (dot often confused with ٠)\n */\nconst ocrToArabic = (char: string): string => {\n const ocrToArabicMap: { [key: string]: string } = {\n '1': '١',\n '9': '٩',\n '.': '٠',\n O: '٥',\n o: '٥',\n V: '٧',\n v: '٧',\n };\n return ocrToArabicMap[char] || char;\n};\n\n/**\n * Parses Arabic-Indic numerals from a reference string and converts to a JavaScript number.\n * Removes parentheses and converts each Arabic-Indic digit to its Western equivalent.\n *\n * @param arabicStr - String containing Arabic-Indic numerals, typically in format '(١٢٣)'\n * @returns Parsed number, or 0 if parsing fails\n * @example\n * arabicToNumber('(١٢٣)') // Returns 123\n * arabicToNumber('(٥)') // Returns 5\n * arabicToNumber('invalid') // Returns 0\n */\nconst arabicToNumber = (arabicStr: string): number => {\n const lookup: { [key: string]: string } = {\n '٠': '0',\n '١': '1',\n '٢': '2',\n '٣': '3',\n '٤': '4',\n '٥': '5',\n '٦': '6',\n '٧': '7',\n '٨': '8',\n '٩': '9',\n };\n const digits = arabicStr.replace(/[()]/g, '');\n let numStr = '';\n for (const char of digits) {\n numStr += lookup[char];\n }\n const parsed = parseInt(numStr, 10);\n return isNaN(parsed) ? 0 : parsed;\n};\n\ntype TextLine = {\n isFootnote?: boolean;\n text: string;\n};\n\n/**\n * Extracts all footnote references from text lines, categorizing them by type and location.\n * Handles both Arabic-Indic numerals and OCR-confused characters in body text and footnotes.\n *\n * @param lines - Array of text line objects with optional isFootnote flag\n * @returns Object containing categorized reference arrays:\n * - bodyReferences: All valid references found in body text\n * - footnoteReferences: All valid references found in footnotes\n * - ocrConfusedInBody: OCR-confused references in body text (for tracking)\n * - ocrConfusedInFootnotes: OCR-confused references in footnotes (for tracking)\n * @example\n * const lines = [\n * { text: 'Body with (١) and (O)', isFootnote: false },\n * { text: '(١) Footnote text', isFootnote: true }\n * ];\n * const refs = extractReferences(lines);\n * // refs.bodyReferences contains ['(١)', '(٥)'] - OCR 'O' converted to '٥'\n */\nconst extractReferences = (lines: TextLine[]) => {\n const arabicReferencesInBody = lines\n .filter((b) => !b.isFootnote)\n .flatMap((b) => b.text.match(PATTERNS.arabicReferenceRegex) || []);\n\n const ocrConfusedReferencesInBody = lines\n .filter((b) => !b.isFootnote)\n .flatMap((b) => b.text.match(PATTERNS.ocrConfusedReferenceRegex) || []);\n\n const arabicReferencesInFootnotes = lines\n .filter((b) => b.isFootnote)\n .flatMap((b) => b.text.match(PATTERNS.arabicFootnoteReferenceRegex) || []);\n\n const ocrConfusedReferencesInFootnotes = lines\n .filter((b) => b.isFootnote)\n .flatMap((b) => b.text.match(PATTERNS.ocrConfusedFootnoteReferenceRegex) || []);\n\n const convertedOcrBodyRefs = ocrConfusedReferencesInBody.map((ref) =>\n ref.replace(/[.1OV9]/g, (char) => ocrToArabic(char)),\n );\n\n const convertedOcrFootnoteRefs = ocrConfusedReferencesInFootnotes.map((ref) =>\n ref.replace(/[.1OV9]/g, (char) => ocrToArabic(char)),\n );\n\n return {\n bodyReferences: [...arabicReferencesInBody, ...convertedOcrBodyRefs],\n footnoteReferences: [...arabicReferencesInFootnotes, ...convertedOcrFootnoteRefs],\n ocrConfusedInBody: ocrConfusedReferencesInBody,\n ocrConfusedInFootnotes: ocrConfusedReferencesInFootnotes,\n };\n};\n\n/**\n * Determines if footnote reference correction is needed by checking for:\n * 1. Invalid footnote patterns (empty parentheses, OCR mistakes)\n * 2. Mismatched sets of references between body text and footnotes\n * 3. Different counts of references in body vs footnotes\n *\n * @param lines - Array of text line objects to analyze\n * @param references - Extracted reference data from extractReferences()\n * @returns True if correction is needed, false if references are already correct\n * @example\n * const lines = [{ text: 'Text with ()', isFootnote: false }];\n * const refs = extractReferences(lines);\n * needsCorrection(lines, refs) // Returns true due to invalid \"()\" reference\n */\nconst needsCorrection = (lines: TextLine[], references: ReturnType<typeof extractReferences>) => {\n const mistakenReferences = lines.some((line) => hasInvalidFootnotes(line.text));\n if (mistakenReferences) {\n return true;\n }\n\n const bodySet = new Set(references.bodyReferences);\n const footnoteSet = new Set(references.footnoteReferences);\n if (bodySet.size !== footnoteSet.size) {\n return true;\n }\n\n // Check if the sets contain the same elements\n for (const ref of bodySet) {\n if (!footnoteSet.has(ref)) {\n return true;\n }\n }\n\n return false;\n};\n\n/**\n * Corrects footnote references in an array of text lines by:\n * 1. Converting OCR-confused characters to proper Arabic numerals\n * 2. Filling in empty \"()\" references with appropriate numbers\n * 3. Ensuring footnote references in body text match those in footnotes\n * 4. Generating new reference numbers when needed\n *\n * @param lines - Array of text line objects, each with optional isFootnote flag\n * @returns Array of corrected text lines with proper footnote references\n * @example\n * const lines = [\n * { text: 'Main text with ()', isFootnote: false },\n * { text: '() This is a footnote', isFootnote: true }\n * ];\n * const corrected = correctReferences(lines);\n * // Returns lines with \"()\" replaced by proper Arabic numerals like \"(١)\"\n */\nexport const correctReferences = <T extends TextLine>(lines: T[]): T[] => {\n const initialReferences = extractReferences(lines);\n\n if (!needsCorrection(lines, initialReferences)) {\n return lines;\n }\n\n // Pass 1: Sanitize lines by correcting only OCR characters inside reference markers.\n const sanitizedLines = lines.map((line) => {\n let updatedText = line.text;\n // This regex finds the full reference, e.g., \"(O)\" or \"(1)\"\n const ocrRegex = /\\([.1OV9]+\\)/g;\n updatedText = updatedText.replace(ocrRegex, (match) => {\n // This replace acts *inside* the found match, e.g., on \"O\" or \"1\"\n return match.replace(/[.1OV9]/g, (char) => ocrToArabic(char));\n });\n return { ...line, text: updatedText };\n });\n\n // Pass 2: Analyze the sanitized lines to get a clear and accurate picture of references.\n const cleanReferences = extractReferences(sanitizedLines);\n\n // Step 3: Create queues of \"unmatched\" references for two-way pairing.\n const bodyRefSet = new Set(cleanReferences.bodyReferences);\n const footnoteRefSet = new Set(cleanReferences.footnoteReferences);\n\n const uniqueBodyRefs = [...new Set(cleanReferences.bodyReferences)];\n const uniqueFootnoteRefs = [...new Set(cleanReferences.footnoteReferences)];\n\n // Queue 1: Body references available for footnotes.\n const bodyRefsForFootnotes = uniqueBodyRefs.filter((ref) => !footnoteRefSet.has(ref));\n // Queue 2: Footnote references available for the body.\n const footnoteRefsForBody = uniqueFootnoteRefs.filter((ref) => !bodyRefSet.has(ref));\n\n // Step 4: Determine the starting point for any completely new reference numbers.\n const allRefs = [...bodyRefSet, ...footnoteRefSet];\n const maxRefNum = allRefs.length > 0 ? Math.max(0, ...allRefs.map((ref) => arabicToNumber(ref))) : 0;\n const referenceCounter = { count: maxRefNum + 1 };\n\n // Step 5: Map over the sanitized lines, filling in '()' using the queues.\n return sanitizedLines.map((line) => {\n if (!line.text.includes(INVALID_FOOTNOTE)) {\n return line;\n }\n let updatedText = line.text;\n\n updatedText = updatedText.replace(/\\(\\)/g, () => {\n if (line.isFootnote) {\n const availableRef = bodyRefsForFootnotes.shift();\n if (availableRef) {\n return availableRef;\n }\n } else {\n // It's body text\n const availableRef = footnoteRefsForBody.shift();\n if (availableRef) {\n return availableRef;\n }\n }\n\n // If no available partner reference exists, generate a new one.\n const newRef = `(${numberToArabic(referenceCounter.count)})`;\n referenceCounter.count++;\n return newRef;\n });\n\n return { ...line, text: updatedText };\n });\n};\n","/**\n * Node in the Aho-Corasick automaton trie structure.\n * Each node represents a state in the pattern matching automaton.\n */\nclass ACNode {\n /** Transition map from characters to next node indices */\n next: Map<string, number> = new Map();\n /** Failure link for efficient pattern matching */\n link = 0;\n /** Pattern IDs that end at this node */\n out: number[] = [];\n}\n\n/**\n * Aho-Corasick automaton for efficient multi-pattern string matching.\n * Provides O(n + m + z) time complexity where n is text length,\n * m is total pattern length, and z is number of matches.\n */\nexport class AhoCorasick {\n /** Array of nodes forming the automaton */\n private nodes: ACNode[] = [new ACNode()];\n\n /**\n * Adds a pattern to the automaton trie.\n *\n * @param pattern - Pattern string to add\n * @param id - Unique identifier for this pattern\n */\n add(pattern: string, id: number): void {\n let v = 0;\n for (let i = 0; i < pattern.length; i++) {\n const ch = pattern[i];\n let to = this.nodes[v].next.get(ch);\n if (to === undefined) {\n to = this.nodes.length;\n this.nodes[v].next.set(ch, to);\n this.nodes.push(new ACNode());\n }\n v = to;\n }\n this.nodes[v].out.push(id);\n }\n\n /**\n * Builds failure links for the automaton using BFS.\n * Must be called after adding all patterns and before searching.\n */\n build(): void {\n const q: number[] = [];\n for (const [, to] of this.nodes[0].next) {\n this.nodes[to].link = 0;\n q.push(to);\n }\n for (let qi = 0; qi < q.length; qi++) {\n const v = q[qi]!;\n\n for (const [ch, to] of this.nodes[v].next) {\n q.push(to);\n let link = this.nodes[v].link;\n while (link !== 0 && !this.nodes[link].next.has(ch)) {\n link = this.nodes[link].link;\n }\n const nxt = this.nodes[link].next.get(ch);\n this.nodes[to].link = nxt === undefined ? 0 : nxt;\n const linkOut = this.nodes[this.nodes[to].link].out;\n if (linkOut.length) {\n this.nodes[to].out.push(...linkOut);\n }\n }\n }\n }\n\n /**\n * Finds all pattern matches in the given text.\n *\n * @param text - Text to search in\n * @param onMatch - Callback function called for each match found\n * Receives pattern ID and end position of the match\n */\n find(text: string, onMatch: (patternId: number, endPos: number) => void): void {\n let v = 0;\n for (let i = 0; i < text.length; i++) {\n const ch = text[i];\n while (v !== 0 && !this.nodes[v].next.has(ch)) {\n v = this.nodes[v].link;\n }\n const to = this.nodes[v].next.get(ch);\n v = to === undefined ? 0 : to;\n if (this.nodes[v].out.length) {\n for (const pid of this.nodes[v].out) {\n onMatch(pid, i + 1);\n }\n }\n }\n }\n}\n\n/**\n * Builds Aho-Corasick automaton for exact pattern matching.\n *\n * @param patterns - Array of patterns to search for\n * @returns Constructed and built Aho-Corasick automaton ready for searching\n *\n * @example\n * ```typescript\n * const patterns = ['hello', 'world', 'hell'];\n * const ac = buildAhoCorasick(patterns);\n * ac.find('hello world', (patternId, endPos) => {\n * console.log(`Found pattern ${patternId} ending at position ${endPos}`);\n * });\n * ```\n */\nexport const buildAhoCorasick = (patterns: string[]) => {\n const ac = new AhoCorasick();\n for (let pid = 0; pid < patterns.length; pid++) {\n const pat = patterns[pid];\n if (pat.length > 0) {\n ac.add(pat, pid);\n }\n }\n ac.build();\n return ac;\n};\n","import type { MatchPolicy } from '@/types';\n\nexport const DEFAULT_POLICY: Required<MatchPolicy> = {\n enableFuzzy: true,\n maxEditAbs: 3,\n maxEditRel: 0.1,\n q: 4,\n gramsPerExcerpt: 5,\n maxCandidatesPerExcerpt: 40,\n seamLen: 512,\n};\n","import { buildAhoCorasick } from './ahocorasick';\n\n/**\n * Builds a concatenated book from pages with position tracking\n */\nexport function buildBook(pagesN: string[]) {\n const parts: string[] = [];\n const starts: number[] = [];\n const lens: number[] = [];\n let off = 0;\n\n for (let i = 0; i < pagesN.length; i++) {\n const p = pagesN[i];\n starts.push(off);\n lens.push(p.length);\n parts.push(p);\n off += p.length;\n\n if (i + 1 < pagesN.length) {\n parts.push(' '); // single space to allow cross-page substring matches\n off += 1;\n }\n }\n return { book: parts.join(''), starts, lens };\n}\n\n/**\n * Binary search to find which page contains a given position\n */\nexport function posToPage(pos: number, pageStarts: number[]): number {\n let lo = 0;\n let hi = pageStarts.length - 1;\n let ans = 0;\n\n while (lo <= hi) {\n const mid = (lo + hi) >> 1;\n if (pageStarts[mid] <= pos) {\n ans = mid;\n lo = mid + 1;\n } else {\n hi = mid - 1;\n }\n }\n return ans;\n}\n\n/**\n * Performs exact matching using Aho-Corasick algorithm to find all occurrences\n * of patterns in the concatenated book text.\n *\n * @param book - Concatenated text from all pages\n * @param pageStarts - Array of starting positions for each page in the book\n * @param patterns - Array of deduplicated patterns to search for\n * @param patIdToOrigIdxs - Mapping from pattern IDs to original excerpt indices\n * @param excerpts - Original array of excerpts (used for length reference)\n * @returns Object containing result array and exact match flags\n */\nexport function findExactMatches(\n book: string,\n pageStarts: number[],\n patterns: string[],\n patIdToOrigIdxs: number[][],\n excerptsCount: number,\n): { result: Int32Array; seenExact: Uint8Array } {\n const ac = buildAhoCorasick(patterns);\n const result = new Int32Array(excerptsCount).fill(-1);\n const seenExact = new Uint8Array(excerptsCount);\n\n ac.find(book, (pid, endPos) => {\n const pat = patterns[pid];\n const startPos = endPos - pat.length;\n const startPage = posToPage(startPos, pageStarts);\n\n for (const origIdx of patIdToOrigIdxs[pid]) {\n if (!seenExact[origIdx]) {\n result[origIdx] = startPage;\n seenExact[origIdx] = 1;\n }\n }\n });\n\n return { result, seenExact };\n}\n\n/**\n * Deduplicates excerpts and creates pattern mapping\n */\nexport function deduplicateExcerpts(excerptsN: string[]) {\n const keyToPatId = new Map<string, number>();\n const patIdToOrigIdxs: number[][] = [];\n const patterns: string[] = [];\n\n for (let i = 0; i < excerptsN.length; i++) {\n const k = excerptsN[i];\n let pid = keyToPatId.get(k);\n\n if (pid === undefined) {\n pid = patterns.length;\n keyToPatId.set(k, pid);\n patterns.push(k);\n patIdToOrigIdxs.push([i]);\n } else {\n patIdToOrigIdxs[pid].push(i);\n }\n }\n\n return { keyToPatId, patIdToOrigIdxs, patterns };\n}\n","/**\n * Represents a posting in the inverted index, storing position information.\n */\ntype Posting = {\n /** Page number where this gram occurs */\n page: number;\n /** Position within the page where this gram starts */\n pos: number;\n /** Whether this posting is from a seam (cross-page boundary) */\n seam: boolean;\n};\n\n/**\n * Basic gram information with position offset.\n */\ntype GramBase = {\n /** The q-gram string */\n gram: string;\n /** Offset position of this gram in the original text */\n offset: number;\n};\n\n/**\n * Extended gram information including frequency data for selection.\n */\ntype GramItem = GramBase & {\n /** Frequency count of this gram in the corpus */\n freq: number;\n};\n\n/**\n * Selects grams that exist in the index from sorted items by frequency.\n * Prioritizes rarer grams for better discrimination.\n *\n * @param map - Inverted index mapping grams to postings\n * @param sortedItems - Gram items sorted by frequency (rarest first)\n * @param gramsPerExcerpt - Maximum number of grams to select\n * @returns Array of selected grams that exist in the index\n */\nconst selectExistingGrams = (map: Map<string, Posting[]>, sortedItems: GramItem[], gramsPerExcerpt: number) => {\n const result: GramBase[] = [];\n\n for (const item of sortedItems) {\n if (map.has(item.gram)) {\n result.push({ gram: item.gram, offset: item.offset });\n if (result.length >= gramsPerExcerpt) {\n break;\n }\n }\n }\n\n return result;\n};\n\n/**\n * Fallback selection when no indexed grams are found in rare items.\n * Selects from the most common grams as a last resort.\n *\n * @param map - Inverted index mapping grams to postings\n * @param sortedItems - Gram items sorted by frequency\n * @param gramsPerExcerpt - Maximum number of grams to select\n * @returns Array of fallback grams from most common items\n */\nconst selectFallbackGrams = (map: Map<string, Posting[]>, sortedItems: GramItem[], gramsPerExcerpt: number) => {\n const result: GramBase[] = [];\n\n for (let i = sortedItems.length - 1; i >= 0 && result.length < gramsPerExcerpt; i--) {\n const item = sortedItems[i];\n if (map.has(item.gram)) {\n result.push({ gram: item.gram, offset: item.offset });\n }\n }\n\n return result;\n};\n\n/**\n * Q-gram index for efficient fuzzy string matching candidate generation.\n * Maintains an inverted index of q-grams to their occurrence positions.\n */\nexport class QGramIndex {\n /** Length of q-grams to index */\n private q: number;\n /** Inverted index mapping q-grams to their postings */\n private map = new Map<string, Posting[]>();\n /** Frequency count for each q-gram in the corpus */\n private gramFreq = new Map<string, number>();\n\n /**\n * Creates a new Q-gram index with the specified gram length.\n *\n * @param q - Length of q-grams to index (typically 3-5)\n */\n constructor(q: number) {\n this.q = q;\n }\n\n /**\n * Adds text to the index, extracting q-grams and building postings.\n *\n * @param page - Page number or identifier for this text\n * @param text - Text content to index\n * @param seam - Whether this text represents a seam (cross-page boundary)\n */\n addText(page: number, text: string, seam: boolean): void {\n this.addGramsToMap(page, text, seam);\n this.updateGramFrequencies(text);\n }\n\n /**\n * Adds q-grams from text to the inverted index with position information.\n *\n * @param page - Page number for the text\n * @param text - Text to extract grams from\n * @param seam - Whether this is seam text\n */\n private addGramsToMap(page: number, text: string, seam: boolean): void {\n for (let i = 0; i + this.q <= text.length; i++) {\n const gram = text.slice(i, i + this.q);\n let postings = this.map.get(gram);\n if (!postings) {\n postings = [];\n this.map.set(gram, postings);\n }\n postings.push({ page, pos: i, seam });\n }\n }\n\n private updateGramFrequencies(text: string): void {\n for (let i = 0; i + this.q <= text.length; i++) {\n const gram = text.slice(i, i + this.q);\n this.gramFreq.set(gram, (this.gramFreq.get(gram) ?? 0) + 1);\n }\n }\n\n /**\n * Extracts unique grams from excerpt with their frequencies.\n */\n private extractUniqueGrams(excerpt: string): GramItem[] {\n const items: GramItem[] = [];\n const seen = new Set<string>();\n\n for (let i = 0; i + this.q <= excerpt.length; i++) {\n const gram = excerpt.slice(i, i + this.q);\n if (seen.has(gram)) {\n continue;\n }\n\n seen.add(gram);\n const freq = this.gramFreq.get(gram) ?? 0x7fffffff;\n items.push({ gram, offset: i, freq });\n }\n\n return items.sort((a, b) => a.freq - b.freq);\n }\n\n /**\n * Picks the rarest grams from an excerpt that exist in the index.\n */\n pickRare(excerpt: string, gramsPerExcerpt: number): { gram: string; offset: number }[] {\n gramsPerExcerpt = Math.max(1, Math.floor(gramsPerExcerpt));\n const sortedItems = this.extractUniqueGrams(excerpt);\n const selected = selectExistingGrams(this.map, sortedItems, gramsPerExcerpt);\n\n return selected.length > 0 ? selected : selectFallbackGrams(this.map, sortedItems, gramsPerExcerpt);\n }\n\n getPostings(gram: string): Posting[] | undefined {\n return this.map.get(gram);\n }\n}\n","import type { MatchPolicy } from './types';\nimport { buildAhoCorasick } from './utils/ahocorasick';\nimport { DEFAULT_POLICY } from './utils/constants';\nimport { buildBook, deduplicateExcerpts, findExactMatches, posToPage } from './utils/fuzzyUtils';\nimport { boundedLevenshtein } from './utils/levenshthein';\nimport { QGramIndex } from './utils/qgram';\nimport { sanitizeArabic } from './utils/sanitize';\n\n/**\n * Represents a candidate match position for fuzzy matching.\n */\ntype Candidate = {\n /** Page number where the candidate match is found */\n page: number;\n /** Starting position within the page or seam */\n start: number;\n /** Whether this candidate is from a seam (cross-page boundary) */\n seam: boolean;\n};\n\n/**\n * Data structure for cross-page text seams used in fuzzy matching.\n */\ntype SeamData = {\n /** Combined text from adjacent page boundaries */\n text: string;\n /** Starting page number for this seam */\n startPage: number;\n};\n\n/**\n * Represents a fuzzy match result with quality score.\n */\ntype FuzzyMatch = {\n /** Page number where the match was found */\n page: number;\n /** Edit distance (lower is better) */\n dist: number;\n};\n\n/**\n * Represents a page hit with quality metrics for ranking matches.\n */\ntype PageHit = {\n /** Quality score (0-1, higher is better) */\n score: number;\n /** Whether this is an exact match */\n exact: boolean;\n};\n\n/**\n * Creates seam data for cross-page matching by combining text from adjacent page boundaries.\n * Seams help find matches that span across page breaks.\n *\n * @param pagesN - Array of normalized page texts\n * @param seamLen - Length of text to take from each page boundary\n * @returns Array of seam data structures\n */\nfunction createSeams(pagesN: string[], seamLen: number): SeamData[] {\n const seams: SeamData[] = [];\n for (let p = 0; p + 1 < pagesN.length; p++) {\n const left = pagesN[p].slice(-seamLen);\n const right = pagesN[p + 1].slice(0, seamLen);\n const text = `${left} ${right}`;\n seams.push({ text, startPage: p });\n }\n return seams;\n}\n\n/**\n * Builds Q-gram index for efficient fuzzy matching candidate generation.\n * The index contains both regular pages and cross-page seams.\n *\n * @param pagesN - Array of normalized page texts\n * @param seams - Array of seam data for cross-page matching\n * @param q - Length of q-grams to index\n * @returns Constructed Q-gram index\n */\nfunction buildQGramIndex(pagesN: string[], seams: SeamData[], q: number): QGramIndex {\n const qidx = new QGramIndex(q);\n\n for (let p = 0; p < pagesN.length; p++) {\n qidx.addText(p, pagesN[p], false);\n }\n\n for (let p = 0; p < seams.length; p++) {\n qidx.addText(p, seams[p].text, true);\n }\n\n return qidx;\n}\n\n/**\n * Generates fuzzy matching candidates using rare q-grams from the excerpt.\n * Uses frequency-based selection to find the most discriminative grams.\n *\n * @param excerpt - Text excerpt to find candidates for\n * @param qidx - Q-gram index containing page and seam data\n * @param cfg - Match policy configuration\n * @returns Array of candidate match positions\n */\nfunction generateCandidates(excerpt: string, qidx: QGramIndex, cfg: Required<MatchPolicy>) {\n const seeds = qidx.pickRare(excerpt, cfg.gramsPerExcerpt);\n if (seeds.length === 0) {\n return [];\n }\n\n const candidates: Candidate[] = [];\n const excerptLen = excerpt.length;\n\n for (const { gram, offset } of seeds) {\n const posts = qidx.getPostings(gram);\n if (!posts) {\n continue;\n }\n\n for (const p of posts) {\n const startPos = p.pos - offset;\n if (startPos < -Math.floor(excerptLen * 0.25)) {\n continue;\n }\n\n candidates.push({\n page: p.page,\n start: Math.max(0, startPos),\n seam: p.seam,\n });\n\n if (candidates.length >= cfg.maxCandidatesPerExcerpt) {\n break;\n }\n }\n\n if (candidates.length >= cfg.maxCandidatesPerExcerpt) {\n break;\n }\n }\n\n return candidates;\n}\n\n/**\n * Calculates fuzzy match score for a candidate using bounded Levenshtein distance.\n * Extracts a window around the candidate position and computes edit distance.\n *\n * @param excerpt - Text excerpt to match\n * @param candidate - Candidate position to evaluate\n * @param pagesN - Array of normalized page texts\n * @param seams - Array of seam data\n * @param maxDist - Maximum edit distance to consider\n * @returns Edit distance if within bounds, null otherwise\n */\nfunction calculateFuzzyScore(\n excerpt: string,\n candidate: Candidate,\n pagesN: string[],\n seams: SeamData[],\n maxDist: number,\n): number | null {\n const src = candidate.seam ? seams[candidate.page]?.text : pagesN[candidate.page];\n if (!src) {\n return null;\n }\n\n const L = excerpt.length;\n const extra = Math.min(maxDist, Math.max(6, Math.ceil(L * 0.12)));\n const start0 = Math.max(0, candidate.start - Math.floor(extra / 2));\n const end0 = Math.min(src.length, start0 + L + extra);\n\n if (end0 <= start0) {\n return null;\n }\n\n const window = src.slice(start0, end0);\n const dist = boundedLevenshtein(excerpt, window, maxDist);\n\n return dist <= maxDist ? dist : null;\n}\n\n/**\n * Finds the best fuzzy match among candidates by comparing edit distances.\n * Prioritizes lower edit distance, then earlier page number for tie-breaking.\n *\n * @param excerpt - Text excerpt to match\n * @param candidates - Array of candidate positions to evaluate\n * @param pagesN - Array of normalized page texts\n * @param seams - Array of seam data\n * @param cfg - Match policy configuration\n * @returns Best fuzzy match or null if none found\n */\nfunction findBestFuzzyMatch(\n excerpt: string,\n candidates: Candidate[],\n pagesN: string[],\n seams: SeamData[],\n cfg: Required<MatchPolicy>,\n): FuzzyMatch | null {\n if (excerpt.length === 0) {\n return null;\n }\n\n const maxDist = Math.max(cfg.maxEditAbs, Math.ceil(cfg.maxEditRel * excerpt.length));\n const keyset = new Set<string>();\n let best: FuzzyMatch | null = null;\n\n for (const candidate of candidates) {\n const key = `${candidate.page}:${candidate.start}:${candidate.seam ? 1 : 0}`;\n if (keyset.has(key)) {\n continue;\n }\n keyset.add(key);\n\n const dist = calculateFuzzyScore(excerpt, candidate, pagesN, seams, maxDist);\n if (dist === null) {\n continue;\n }\n\n if (!best || dist < best.dist || (dist === best.dist && candidate.page < best.page)) {\n best = { page: candidate.page, dist };\n if (dist === 0) {\n break;\n }\n }\n }\n\n return best;\n}\n\n/**\n * Performs fuzzy matching for excerpts that didn't have exact matches.\n * Uses Q-gram indexing and bounded Levenshtein distance for efficiency.\n *\n * @param excerptsN - Array of normalized excerpts\n * @param pagesN - Array of normalized page texts\n * @param seenExact - Flags indicating which excerpts had exact matches\n * @param result - Result array to update with fuzzy match pages\n * @param cfg - Match policy configuration\n */\nfunction performFuzzyMatching(\n excerptsN: string[],\n pagesN: string[],\n seenExact: Uint8Array,\n result: Int32Array,\n cfg: Required<MatchPolicy>,\n): void {\n if (!cfg.enableFuzzy) {\n return;\n }\n\n const seams = createSeams(pagesN, cfg.seamLen);\n const qidx = buildQGramIndex(pagesN, seams, cfg.q);\n\n for (let i = 0; i < excerptsN.length; i++) {\n if (seenExact[i]) {\n continue;\n }\n\n const excerpt = excerptsN[i];\n if (!excerpt || excerpt.length < cfg.q) {\n continue;\n }\n\n const candidates = generateCandidates(excerpt, qidx, cfg);\n if (candidates.length === 0) {\n continue;\n }\n\n const best = findBestFuzzyMatch(excerpt, candidates, pagesN, seams, cfg);\n if (best) {\n result[i] = best.page;\n seenExact[i] = 1;\n }\n }\n}\n\n/**\n * Main function to find the single best match per excerpt.\n * Combines exact matching with fuzzy matching for comprehensive text search.\n *\n * @param pages - Array of page texts to search within\n * @param excerpts - Array of text excerpts to find matches for\n * @param policy - Optional matching policy configuration\n * @returns Array of page indices (one per excerpt, -1 if no match found)\n *\n * @example\n * ```typescript\n * const pages = ['Hello world', 'Goodbye world'];\n * const excerpts = ['Hello', 'Good bye']; // Note the typo\n * const matches = findMatches(pages, excerpts, { enableFuzzy: true });\n * // Returns [0, 1] - exact match on page 0, fuzzy match on page 1\n * ```\n */\nexport function findMatches(pages: string[], excerpts: string[], policy: MatchPolicy = {}) {\n const cfg = { ...DEFAULT_POLICY, ...policy };\n\n const pagesN = pages.map((p) => sanitizeArabic(p, 'aggressive'));\n const excerptsN = excerpts.map((e) => sanitizeArabic(e, 'aggressive'));\n\n const { patIdToOrigIdxs, patterns } = deduplicateExcerpts(excerptsN);\n const { book, starts: pageStarts } = buildBook(pagesN);\n\n const { result, seenExact } = findExactMatches(book, pageStarts, patterns, patIdToOrigIdxs, excerpts.length);\n\n if (!seenExact.every((seen) => seen === 1)) {\n performFuzzyMatching(excerptsN, pagesN, seenExact, result, cfg);\n }\n\n return Array.from(result);\n}\n\n/**\n * Records exact matches for the findMatchesAll function.\n * Updates the hits tracking structure with exact match information.\n *\n * @param book - Concatenated text from all pages\n * @param pageStarts - Array of starting positions for each page\n * @param patterns - Array of deduplicated patterns to search for\n * @param patIdToOrigIdxs - Mapping from pattern IDs to original excerpt indices\n * @param hitsByExcerpt - Array of maps tracking hits per excerpt\n */\nfunction recordExactMatches(\n book: string,\n pageStarts: number[],\n patterns: string[],\n patIdToOrigIdxs: number[][],\n hitsByExcerpt: Array<Map<number, PageHit>>,\n): void {\n const ac = buildAhoCorasick(patterns);\n\n ac.find(book, (pid, endPos) => {\n const pat = patterns[pid];\n const startPos = endPos - pat.length;\n const startPage = posToPage(startPos, pageStarts);\n\n for (const origIdx of patIdToOrigIdxs[pid]) {\n const hits = hitsByExcerpt[origIdx];\n const prev = hits.get(startPage);\n if (!prev || !prev.exact) {\n hits.set(startPage, { score: 1, exact: true });\n }\n }\n });\n}\n\n/**\n * Processes a single fuzzy candidate and updates hits if a better match is found.\n * Used internally by the findMatchesAll function for comprehensive matching.\n *\n * @param candidate - Candidate position to evaluate\n * @param excerpt - Text excerpt being matched\n * @param pagesN - Array of normalized page texts\n * @param seams - Array of seam data\n * @param maxDist - Maximum edit distance threshold\n * @param hits - Map of page hits to update\n * @param keyset - Set to track processed candidates (for deduplication)\n */\nfunction processFuzzyCandidate(\n candidate: Candidate,\n excerpt: string,\n pagesN: string[],\n seams: SeamData[],\n maxDist: number,\n hits: Map<number, PageHit>,\n keyset: Set<string>,\n): void {\n const key = `${candidate.page}:${candidate.start}:${candidate.seam ? 1 : 0}`;\n if (keyset.has(key)) {\n return;\n }\n keyset.add(key);\n\n const dist = calculateFuzzyScore(excerpt, candidate, pagesN, seams, maxDist);\n if (dist === null) {\n return;\n }\n\n const score = 1 - dist / maxDist; // in (0, 1], higher is better\n const entry = hits.get(candidate.page);\n if (!entry || (!entry.exact && score > entry.score)) {\n hits.set(candidate.page, { score, exact: false });\n }\n}\n\n/**\n * Processes fuzzy matching for a single excerpt in the findMatchesAll function.\n * Generates candidates and evaluates them for potential matches.\n *\n * @param excerptIndex - Index of the excerpt being processed\n * @param excerpt - Text excerpt to find matches for\n * @param pagesN - Array of normalized page texts\n * @param seams - Array of seam data\n * @param qidx - Q-gram index for candidate generation\n * @param hitsByExcerpt - Array of maps tracking hits per excerpt\n * @param cfg - Match policy configuration\n */\nfunction processSingleExcerptFuzzy(\n excerptIndex: number,\n excerpt: string,\n pagesN: string[],\n seams: SeamData[],\n qidx: QGramIndex,\n hitsByExcerpt: Array<Map<number, PageHit>>,\n cfg: Required<MatchPolicy>,\n): void {\n // Skip if we already have exact hits\n const hasExactHits = Array.from(hitsByExcerpt[excerptIndex].values()).some((v) => v.exact);\n if (hasExactHits) {\n return;\n }\n\n if (!excerpt || excerpt.length < cfg.q) {\n return;\n }\n\n const candidates = generateCandidates(excerpt, qidx, cfg);\n if (candidates.length === 0) {\n return;\n }\n\n const maxDist = Math.max(cfg.maxEditAbs, Math.ceil(cfg.maxEditRel * excerpt.length));\n const keyset = new Set<string>();\n const hits = hitsByExcerpt[excerptIndex];\n\n for (const candidate of candidates) {\n processFuzzyCandidate(candidate, excerpt, pagesN, seams, maxDist, hits, keyset);\n }\n}\n\n/**\n * Records fuzzy matches for excerpts that don't have exact matches.\n * Used by findMatchesAll to provide comprehensive matching results.\n *\n * @param excerptsN - Array of normalized excerpts\n * @param pagesN - Array of normalized page texts\n * @param hitsByExcerpt - Array of maps tracking hits per excerpt\n * @param cfg - Match policy configuration\n */\nfunction recordFuzzyMatches(\n excerptsN: string[],\n pagesN: string[],\n hitsByExcerpt: Array<Map<number, PageHit>>,\n cfg: Required<MatchPolicy>,\n): void {\n const seams = createSeams(pagesN, cfg.seamLen);\n const qidx = buildQGramIndex(pagesN, seams, cfg.q);\n\n for (let i = 0; i < excerptsN.length; i++) {\n processSingleExcerptFuzzy(i, excerptsN[i], pagesN, seams, qidx, hitsByExcerpt, cfg);\n }\n}\n\n/**\n * Sorts matches by quality and page order for optimal ranking.\n * Exact matches are prioritized over fuzzy matches, with secondary sorting by page order.\n *\n * @param hits - Map of page hits with quality scores\n * @returns Array of page numbers sorted by match quality\n */\nfunction sortMatches(hits: Map<number, PageHit>): number[] {\n if (hits.size === 0) {\n return [];\n }\n\n const exact: [number, PageHit][] = [];\n const fuzzy: [number, PageHit][] = [];\n\n for (const entry of hits.entries()) {\n if (entry[1].exact) {\n exact.push(entry);\n } else {\n fuzzy.push(entry);\n }\n }\n\n exact.sort((a, b) => a[0] - b[0]); // reading order\n fuzzy.sort((a, b) => b[1].score - a[1].score || a[0] - b[0]); // score desc, then page asc\n\n return [...exact, ...fuzzy].map((entry) => entry[0]);\n}\n\n/**\n * Main function to find all matches per excerpt, ranked by quality.\n * Returns comprehensive results with both exact and fuzzy matches for each excerpt.\n *\n * @param pages - Array of page texts to search within\n * @param excerpts - Array of text excerpts to find matches for\n * @param policy - Optional matching policy configuration\n * @returns Array of page index arrays (one array per excerpt, sorted by match quality)\n *\n * @example\n * ```typescript\n * const pages = ['Hello world', 'Hello there', 'Goodbye world'];\n * const excerpts = ['Hello'];\n * const matches = findMatchesAll(pages, excerpts);\n * // Returns [[0, 1]] - both pages 0 and 1 contain \"Hello\", sorted by page order\n * ```\n */\nexport function findMatchesAll(pages: string[], excerpts: string[], policy: MatchPolicy = {}): number[][] {\n const cfg = { ...DEFAULT_POLICY, ...policy };\n\n const pagesN = pages.map((p) => sanitizeArabic(p, 'aggressive'));\n const excerptsN = excerpts.map((e) => sanitizeArabic(e, 'aggressive'));\n\n const { patIdToOrigIdxs, patterns } = deduplicateExcerpts(excerptsN);\n const { book, starts: pageStarts } = buildBook(pagesN);\n\n // Initialize hit tracking\n const hitsByExcerpt: Array<Map<number, PageHit>> = Array.from({ length: excerpts.length }, () => new Map());\n\n // Record exact matches\n recordExactMatches(book, pageStarts, patterns, patIdToOrigIdxs, hitsByExcerpt);\n\n // Record fuzzy matches if enabled\n if (cfg.enableFuzzy) {\n recordFuzzyMatches(excerptsN, pagesN, hitsByExcerpt, cfg);\n }\n\n // Sort and return results\n return hitsByExcerpt.map((hits) => sortMatches(hits));\n}\n","import { PATTERNS } from './utils/textUtils';\n\n/**\n * Character statistics for analyzing text content and patterns\n */\ntype CharacterStats = {\n /** Number of Arabic script characters in the text */\n arabicCount: number;\n /** Map of character frequencies for repetition analysis */\n charFreq: Map<string, number>;\n /** Number of digit characters (0-9) in the text */\n digitCount: number;\n /** Number of Latin alphabet characters (a-z, A-Z) in the text */\n latinCount: number;\n /** Number of punctuation characters in the text */\n punctuationCount: number;\n /** Number of whitespace characters in the text */\n spaceCount: number;\n /** Number of symbol characters (non-alphanumeric, non-punctuation) in the text */\n symbolCount: number;\n};\n\n/**\n * Determines if a given Arabic text string is likely to be noise or unwanted OCR artifacts.\n * This function performs comprehensive analysis to identify patterns commonly associated\n * with OCR errors, formatting artifacts, or meaningless content in Arabic text processing.\n *\n * @param text - The input string to analyze for noise patterns\n * @returns true if the text is likely noise or unwanted content, false if it appears to be valid Arabic content\n *\n * @example\n * ```typescript\n * import { isArabicTextNoise } from 'baburchi';\n *\n * console.log(isArabicTextNoise('---')); // true (formatting artifact)\n * console.log(isArabicTextNoise('السلام عليكم')); // false (valid Arabic)\n * console.log(isArabicTextNoise('ABC')); // true (uppercase pattern)\n * ```\n */\nexport const isArabicTextNoise = (text: string): boolean => {\n // Early return for empty or very short strings\n if (!text || text.trim().length === 0) {\n return true;\n }\n\n const trimmed = text.trim();\n const length = trimmed.length;\n\n // Very short strings are likely noise unless they're meaningful Arabic\n if (length < 2) {\n return true;\n }\n\n // Check for basic noise patterns first\n if (isBasicNoisePattern(trimmed)) {\n return true;\n }\n\n const charStats = analyzeCharacterStats(trimmed);\n\n // Check for excessive repetition\n if (hasExcessiveRepetition(charStats, length)) {\n return true;\n }\n\n // Check if text contains Arabic characters\n const hasArabic = PATTERNS.arabicCharacters.test(trimmed);\n\n // Handle non-Arabic text\n if (!hasArabic && /[a-zA-Z]/.test(trimmed)) {\n return true;\n }\n\n // Arabic-specific validation\n if (hasArabic) {\n return !isValidArabicContent(charStats, length);\n }\n\n // Non-Arabic content validation\n return isNonArabicNoise(charStats, length, trimmed);\n};\n\n/**\n * Analyzes character composition and frequency statistics for the input text.\n * Categorizes characters by type (Arabic, Latin, digits, spaces, punctuation, symbols)\n * and tracks character frequency for pattern analysis.\n *\n * @param text - The text string to analyze\n * @returns CharacterStats object containing detailed character analysis\n *\n * @example\n * ```typescript\n * import { analyzeCharacterStats } from 'baburchi';\n *\n * const stats = analyzeCharacterStats('مرحبا 123!');\n * console.log(stats.arabicCount); // 5\n * console.log(stats.digitCount); // 3\n * console.log(stats.symbolCount); // 1\n * ```\n */\nexport function analyzeCharacterStats(text: string): CharacterStats {\n const stats: CharacterStats = {\n arabicCount: 0,\n charFreq: new Map<string, number>(),\n digitCount: 0,\n latinCount: 0,\n punctuationCount: 0,\n spaceCount: 0,\n symbolCount: 0,\n };\n\n const chars = Array.from(text);\n\n for (const char of chars) {\n // Count character frequency for repetition detection\n stats.charFreq.set(char, (stats.charFreq.get(char) || 0) + 1);\n\n if (PATTERNS.arabicCharacters.test(char)) {\n stats.arabicCount++;\n } else if (/\\d/.test(char)) {\n stats.digitCount++;\n } else if (/[a-zA-Z]/.test(char)) {\n stats.latinCount++;\n } else if (/\\s/.test(char)) {\n stats.spaceCount++;\n } else if (/[.,;:()[\\]{}\"\"\"''`]/.test(char)) {\n stats.punctuationCount++;\n } else {\n stats.symbolCount++;\n }\n }\n\n return stats;\n}\n\n/**\n * Detects excessive repetition of specific characters that commonly indicate noise.\n * Focuses on repetitive characters like exclamation marks, dots, dashes, equals signs,\n * and underscores that often appear in OCR artifacts or formatting elements.\n *\n * @param charStats - Character statistics from analyzeCharacterStats\n * @param textLength - Total length of the original text\n * @returns true if excessive repetition is detected, false otherwise\n *\n * @example\n * ```typescript\n * import { hasExcessiveRepetition, analyzeCharacterStats } from 'baburchi';\n *\n * const stats = analyzeCharacterStats('!!!!!');\n * console.log(hasExcessiveRepetition(stats, 5)); // true\n *\n * const normalStats = analyzeCharacterStats('hello world');\n * console.log(hasExcessiveRepetition(normalStats, 11)); // false\n * ```\n */\nexport function hasExcessiveRepetition(charStats: CharacterStats, textLength: number): boolean {\n let repeatCount = 0;\n const repetitiveChars = ['!', '.', '-', '=', '_'];\n\n for (const [char, count] of charStats.charFreq) {\n if (count >= 5 && repetitiveChars.includes(char)) {\n repeatCount += count;\n }\n }\n\n // High repetition ratio indicates noise\n return repeatCount / textLength > 0.4;\n}\n\n/**\n * Identifies text that matches common noise patterns using regular expressions.\n * Detects patterns like repeated dashes, dot sequences, uppercase-only text,\n * digit-dash combinations, and other formatting artifacts commonly found in OCR output.\n *\n * @param text - The text string to check against noise patterns\n * @returns true if the text matches a basic noise pattern, false otherwise\n *\n * @example\n * ```typescript\n * import { isBasicNoisePattern } from 'baburchi';\n *\n * console.log(isBasicNoisePattern('---')); // true\n * console.log(isBasicNoisePattern('...')); // true\n * console.log(isBasicNoisePattern('ABC')); // true\n * console.log(isBasicNoisePattern('- 77')); // true\n * console.log(isBasicNoisePattern('hello world')); // false\n * ```\n */\nexport function isBasicNoisePattern(text: string): boolean {\n const noisePatterns = [\n /^[-=_━≺≻\\s]*$/, // Only dashes, equals, underscores, special chars, or spaces\n /^[.\\s]*$/, // Only dots and spaces\n /^[!\\s]*$/, // Only exclamation marks and spaces\n /^[A-Z\\s]*$/, // Only uppercase letters and spaces (like \"Ap Ap Ap\")\n /^[-\\d\\s]*$/, // Only dashes, digits and spaces (like \"- 77\", \"- 4\")\n /^\\d+\\s*$/, // Only digits and spaces (like \"1\", \" 1 \")\n /^[A-Z]\\s*$/, // Single uppercase letter with optional spaces\n /^[—\\s]*$/, // Only em-dashes and spaces\n /^[्र\\s-]*$/, // Devanagari characters (likely OCR errors)\n ];\n\n return noisePatterns.some((pattern) => pattern.test(text));\n}\n\n/**\n * Determines if non-Arabic content should be classified as noise based on various heuristics.\n * Analyzes symbol-to-content ratios, text length, spacing patterns, and content composition\n * to identify unwanted OCR artifacts or meaningless content.\n *\n * @param charStats - Character statistics from analyzeCharacterStats\n * @param textLength - Total length of the original text\n * @param text - The original text string for additional pattern matching\n * @returns true if the content is likely noise, false if it appears to be valid content\n *\n * @example\n * ```typescript\n * import { isNonArabicNoise, analyzeCharacterStats } from 'baburchi';\n *\n * const stats = analyzeCharacterStats('!!!');\n * console.log(isNonArabicNoise(stats, 3, '!!!')); // true\n *\n * const validStats = analyzeCharacterStats('2023');\n * console.log(isNonArabicNoise(validStats, 4, '2023')); // false\n * ```\n */\nexport function isNonArabicNoise(charStats: CharacterStats, textLength: number, text: string): boolean {\n const contentChars = charStats.arabicCount + charStats.latinCount + charStats.digitCount;\n\n // Text that's mostly symbols or punctuation is likely noise\n if (contentChars === 0) {\n return true;\n }\n\n // Check for specific spacing patterns that indicate noise\n if (isSpacingNoise(charStats, contentChars, textLength)) {\n return true;\n }\n\n // Special handling for Arabic numerals in parentheses (like \"(٦٠١٠).\")\n const hasArabicNumerals = /[٠-٩]/.test(text);\n if (hasArabicNumerals && charStats.digitCount >= 3) {\n return false;\n }\n\n // High symbol-to-content ratio indicates noise, but be more lenient with punctuation\n // Allow more punctuation for valid content like references, citations, etc.\n const adjustedNonContentChars = charStats.symbolCount + Math.max(0, charStats.punctuationCount - 5);\n if (adjustedNonContentChars / Math.max(contentChars, 1) > 2) {\n return true;\n }\n\n // Very short strings with no Arabic are likely noise (except substantial numbers)\n if (textLength <= 5 && charStats.arabicCount === 0 && !(/^\\d+$/.test(text) && charStats.digitCount >= 3)) {\n return true;\n }\n\n // Allow pure numbers if they're substantial (like years)\n if (/^\\d{3,4}$/.test(text)) {\n return false;\n }\n\n // Default to not noise for longer content\n return textLength <= 10;\n}\n\n/**\n * Detects problematic spacing patterns that indicate noise or OCR artifacts.\n * Identifies cases where spacing is excessive relative to content, or where\n * single characters are surrounded by spaces in a way that suggests OCR errors.\n *\n * @param charStats - Character statistics from analyzeCharacterStats\n * @param contentChars - Number of meaningful content characters (Arabic + Latin + digits)\n * @param textLength - Total length of the original text\n * @returns true if spacing patterns indicate noise, false otherwise\n *\n * @example\n * ```typescript\n * import { isSpacingNoise, analyzeCharacterStats } from 'baburchi';\n *\n * const stats = analyzeCharacterStats(' a ');\n * const contentChars = stats.arabicCount + stats.latinCount + stats.digitCount;\n * console.log(isSpacingNoise(stats, contentChars, 3)); // true\n *\n * const normalStats = analyzeCharacterStats('hello world');\n * const normalContent = normalStats.arabicCount + normalStats.latinCount + normalStats.digitCount;\n * console.log(isSpacingNoise(normalStats, normalContent, 11)); // false\n * ```\n */\nexport function isSpacingNoise(charStats: CharacterStats, contentChars: number, textLength: number): boolean {\n const { arabicCount, spaceCount } = charStats;\n\n // Too many spaces relative to content\n if (spaceCount > 0 && contentChars === spaceCount + 1 && contentChars <= 5) {\n return true;\n }\n\n // Short text with multiple spaces and no Arabic\n if (textLength <= 10 && spaceCount >= 2 && arabicCount === 0) {\n return true;\n }\n\n // Excessive spacing ratio\n if (spaceCount / textLength > 0.6) {\n return true;\n }\n\n return false;\n}\n\n/**\n * Validates whether Arabic content is substantial enough to be considered meaningful.\n * Uses character counts and text length to determine if Arabic text contains\n * sufficient content or if it's likely to be a fragment or OCR artifact.\n *\n * @param charStats - Character statistics from analyzeCharacterStats\n * @param textLength - Total length of the original text\n * @returns true if the Arabic content appears valid, false if it's likely noise\n *\n * @example\n * ```typescript\n * import { isValidArabicContent, analyzeCharacterStats } from 'baburchi';\n *\n * const validStats = analyzeCharacterStats('السلام عليكم');\n * console.log(isValidArabicContent(validStats, 12)); // true\n *\n * const shortStats = analyzeCharacterStats('ص');\n * console.log(isValidArabicContent(shortStats, 1)); // false\n *\n * const withDigitsStats = analyzeCharacterStats('ص 5');\n * console.log(isValidArabicContent(withDigitsStats, 3)); // true\n * ```\n */\nexport function isValidArabicContent(charStats: CharacterStats, textLength: number): boolean {\n // Arabic text with reasonable content length is likely valid\n if (charStats.arabicCount >= 3) {\n return true;\n }\n\n // Short Arabic snippets with numbers might be valid (like dates, references)\n if (charStats.arabicCount >= 1 && charStats.digitCount > 0 && textLength <= 20) {\n return true;\n }\n\n // Allow short Arabic words with punctuation (like \"له.\" - \"for him/it.\")\n if (charStats.arabicCount >= 2 && charStats.punctuationCount <= 2 && textLength <= 10) {\n return true;\n }\n\n // Allow single meaningful Arabic words that are common standalone terms\n // This handles cases like pronouns, prepositions, common short words\n if (charStats.arabicCount >= 1 && textLength <= 5 && charStats.punctuationCount <= 1) {\n return true;\n }\n\n return false;\n}\n","import type { FixTypoOptions } from './types';\nimport { sanitizeArabic } from './utils/sanitize';\nimport { alignTokenSequences, areSimilarAfterNormalization, calculateSimilarity } from './utils/similarity';\nimport {\n handleFootnoteFusion,\n handleFootnoteSelection,\n handleStandaloneFootnotes,\n tokenizeText,\n} from './utils/textUtils';\n\n/**\n * Selects the best token(s) from an aligned pair during typo correction.\n * Uses various heuristics including normalization, footnote handling, typo symbols,\n * and similarity scores to determine which token(s) to keep.\n *\n * @param originalToken - Token from the original OCR text (may be null)\n * @param altToken - Token from the alternative OCR text (may be null)\n * @param options - Configuration options including typo symbols and similarity threshold\n * @returns Array of selected tokens (usually contains one token, but may contain multiple)\n */\nconst selectBestTokens = (\n originalToken: null | string,\n altToken: null | string,\n { similarityThreshold, typoSymbols }: FixTypoOptions,\n): string[] => {\n // Handle missing tokens\n if (originalToken === null) {\n return [altToken!];\n }\n if (altToken === null) {\n return [originalToken];\n }\n\n // Preserve original if same after normalization (keeps diacritics)\n if (sanitizeArabic(originalToken) === sanitizeArabic(altToken)) {\n return [originalToken];\n }\n\n // Handle embedded footnotes\n const result = handleFootnoteSelection(originalToken, altToken);\n if (result) {\n return result;\n }\n\n // Handle standalone footnotes\n const footnoteResult = handleStandaloneFootnotes(originalToken, altToken);\n if (footnoteResult) {\n return footnoteResult;\n }\n\n // Handle typo symbols - prefer the symbol itself\n if (typoSymbols.includes(originalToken) || typoSymbols.includes(altToken)) {\n const typoSymbol = typoSymbols.find((symbol) => symbol === originalToken || symbol === altToken);\n return typoSymbol ? [typoSymbol] : [originalToken];\n }\n\n // Choose based on similarity\n const normalizedOriginal = sanitizeArabic(originalToken);\n const normalizedAlt = sanitizeArabic(altToken);\n const similarity = calculateSimilarity(normalizedOriginal, normalizedAlt);\n\n return [similarity > similarityThreshold ? originalToken : altToken];\n};\n\n/**\n * Removes duplicate tokens and handles footnote fusion in post-processing.\n * Identifies and removes tokens that are highly similar while preserving\n * important variations. Also handles special cases like footnote merging.\n *\n * @param tokens - Array of tokens to process\n * @param highSimilarityThreshold - Threshold for detecting duplicates (0.0 to 1.0)\n * @returns Array of tokens with duplicates removed and footnotes fused\n */\nconst removeDuplicateTokens = (tokens: string[], highSimilarityThreshold: number): string[] => {\n if (tokens.length === 0) {\n return tokens;\n }\n\n const result: string[] = [];\n\n for (const currentToken of tokens) {\n if (result.length === 0) {\n result.push(currentToken);\n continue;\n }\n\n const previousToken = result.at(-1)!;\n\n // Handle ordinary echoes (similar tokens)\n if (areSimilarAfterNormalization(previousToken, currentToken, highSimilarityThreshold)) {\n // Keep the shorter version\n if (currentToken.length < previousToken.length) {\n result[result.length - 1] = currentToken;\n }\n continue;\n }\n\n // Handle footnote fusion cases\n if (handleFootnoteFusion(result, previousToken, currentToken)) {\n continue;\n }\n\n result.push(currentToken);\n }\n\n return result;\n};\n\n/**\n * Processes text alignment between original and alternate OCR results to fix typos.\n * Uses the Needleman-Wunsch sequence alignment algorithm to align tokens,\n * then selects the best tokens and performs post-processing.\n *\n * @param originalText - Original OCR text that may contain typos\n * @param altText - Reference text from alternate OCR for comparison\n * @param options - Configuration options for alignment and selection\n * @returns Corrected text with typos fixed\n */\nexport const processTextAlignment = (originalText: string, altText: string, options: FixTypoOptions): string => {\n const originalTokens = tokenizeText(originalText, options.typoSymbols);\n const altTokens = tokenizeText(altText, options.typoSymbols);\n\n // Align token sequences\n const alignedPairs = alignTokenSequences(\n originalTokens,\n altTokens,\n options.typoSymbols,\n options.similarityThreshold,\n );\n\n // Select best tokens from each aligned pair\n const mergedTokens = alignedPairs.flatMap(([original, alt]) => selectBestTokens(original, alt, options));\n\n // Remove duplicates and handle post-processing\n const finalTokens = removeDuplicateTokens(mergedTokens, options.highSimilarityThreshold);\n\n return finalTokens.join(' ');\n};\n\nexport const fixTypo = (\n original: string,\n correction: string,\n {\n highSimilarityThreshold = 0.8,\n similarityThreshold = 0.6,\n typoSymbols,\n }: Partial<FixTypoOptions> & Pick<FixTypoOptions, 'typoSymbols'>,\n) => {\n return processTextAlignment(original, correction, { highSimilarityThreshold, similarityThreshold, typoSymbols });\n};\n"],"mappings":"AAwFA,IAAMA,GAAY,OACZC,EAAa,UACbC,GAAgB,mDAChBC,GAAmB,UACnBC,GAAmB,UACnBC,GAAiB,UACjBC,GAAgB,mDAChBC,GAAuB,qDACvBC,GAAwB,kFACxBC,GAA0B,oFAK1BC,GAAgBC,GAA0BA,IAAS,GAKnDC,GAAoBD,GAA2BA,GAAQ,IAAMA,GAAQ,IAAQA,GAAQ,MAAUA,GAAQ,KAMvGE,GAAuBC,GACzBA,EAAE,QAAQb,EAAY,CAACc,EAAIC,EAAWC,IAAgB,CAClD,IAAIC,EAAIF,EAAI,EACZ,KAAOE,GAAK,GAAKR,GAAaO,EAAI,WAAWC,CAAC,CAAC,GAC3CA,IAEJ,GAAIA,GAAK,EAAG,CACR,IAAMC,EAAOF,EAAI,WAAWC,CAAC,EAC7B,GAAIN,GAAiBO,CAAI,GAAKA,IAAS,KACnC,MAAO,QAEf,CACA,MAAO,EACX,CAAC,EAKCC,GAAyBN,GAC3BA,EAAE,QAAQ,sFAAuF,IAAI,EAKnGO,GAAwB,CAACP,EAAWQ,IAA6BA,GAAUR,EAAE,UAAYA,EAAE,UAAU,KAAK,EAAIA,EAK9GS,GAA0B,CAACT,EAAWQ,EAAiBE,IACzDF,EAASR,EAAE,QAAQR,GAAekB,EAAU,IAAM,EAAE,EAAIV,EAKtDW,GAA6B,CAC/BX,EACAY,EACAC,KAEID,IACAZ,EAAIA,EAAE,QAAQZ,GAAe,EAAE,GAE/ByB,IAAgB,OACTd,GAAoBC,CAAC,EAE5Ba,IAAgB,MAC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AKY,CAAY,EACxB,QACJ,CAEA,IAAMC,EAAgBb,EAAO,GAAG,EAAE,EAGlC,GAAIc,EAA6BD,EAAeD,EAAcD,CAAuB,EAAG,CAEhFC,EAAa,OAASC,EAAc,SACpCb,EAAOA,EAAO,OAAS,CAAC,EAAIY,GAEhC,QACJ,CAGIG,EAAqBf,EAAQa,EAAeD,CAAY,GAI5DZ,EAAO,KAAKY,CAAY,CAC5B,CAEA,OAAOZ,CACX,EAYagB,GAAuB,CAACC,EAAsBC,EAAiBC,IAAoC,CAC5G,IAAMC,EAAiBC,EAAaJ,EAAcE,EAAQ,WAAW,EAC/DG,EAAYD,EAAaH,EAASC,EAAQ,WAAW,EAWrDI,EAReC,EACjBJ,EACAE,EACAH,EAAQ,YACRA,EAAQ,mBACZ,EAGkC,QAAQ,CAAC,CAACM,EAAUC,CAAG,IAAMhC,GAAiB+B,EAAUC,EAAKP,CAAO,CAAC,EAKvG,OAFoBV,GAAsBc,EAAcJ,EAAQ,uBAAuB,EAEpE,KAAK,GAAG,CAC/B,EAEaQ,GAAU,CACnBF,EACAG,EACA,CACI,wBAAAjB,EAA0B,GAC1B,oBAAAd,EAAsB,GACtB,YAAAC,CACJ,IAEOkB,GAAqBS,EAAUG,EAAY,CAAE,wBAAAjB,EAAyB,oBAAAd,EAAqB,YAAAC,CAAY,CAAC","names":["RX_SPACES","RX_TATWEEL","RX_DIACRITICS","RX_ALIF_VARIANTS","RX_ALIF_MAQSURAH","RX_TA_MARBUTAH","RX_ZERO_WIDTH","RX_LATIN_AND_SYMBOLS","RX_NON_ARABIC_LETTERS","RX_NOT_LETTERS_OR_SPACE","isAsciiSpace","code","isDigitCodePoint","removeTatweelSafely","s","_m","i","str","j","prev","removeHijriDateMarker","applyNfcNormalization","enable","removeZeroWidthControls","asSpace","removeDiacriticsAndTatweel","removeDiacritics","tatweelMode","applyCharacterMappings","normalizeAlif","maqsurahToYa","taMarbutahToHa","removeLatinAndSymbolNoise","applyLetterFilters","lettersAndSpacesOnly","lettersOnly","normalizeWhitespace","collapse","doTrim","resolveBoolean","presetValue","override","resolveTatweelMode","PRESETS","PRESET_NONE","sanitizeArabic","input","optionsOrPreset","preset","opts","base","nfc","stripZW","zwAsSpace","removeDia","normAlif","maqToYa","taToHa","stripNoise","lettersSpacesOnly","collapseWS","removeHijri","calculateLevenshteinDistance","textA","textB","lengthA","lengthB","shorter","longer","shortLen","longLen","previousRow","_","index","i","currentRow","j","substitutionCost","minCost","shouldEarlyExit","a","b","maxDist","initializeBoundedArrays","m","prev","curr","getRowBounds","processBoundedCell","cost","del","ins","sub","processBoundedRow","big","from","to","rowMin","val","boundedLevenshtein","earlyResult","ALIGNMENT_SCORES","calculateSimilarity","textA","textB","maxLength","distance","calculateLevenshteinDistance","areSimilarAfterNormalization","threshold","normalizedA","sanitizeArabic","normalizedB","calculateAlignmentScore","tokenA","tokenB","typoSymbols","similarityThreshold","isTypoSymbol","isHighlySimilar","backtrackAlignment","matrix","tokensA","tokensB","alignment","i","j","initializeScoringMatrix","lengthA","lengthB","getBestAlignment","diagonalScore","upScore","leftScore","maxScore","alignTokenSequences","typoSymbolsSet","t","aNorm","bNorm","alignmentScore","isTypo","highSim","direction","score","alignTextSegments","targetLines","segmentLines","alignedLines","segmentIndex","targetLine","result","segmentsConsumed","processAlignmentTarget","findBestSegmentMerge","partA","partB","mergedForward","mergedReversed","normalizedTarget","sanitizeArabic","scoreForward","calculateSimilarity","scoreReversed","currentSegment","areSimilarAfterNormalization","checkQuoteBalance","str","errors","quoteCount","lastQuoteIndex","i","isBalanced","BRACKETS","OPEN_BRACKETS","CLOSE_BRACKETS","checkBracketBalance","stack","char","lastOpen","index","checkBalance","quoteResult","bracketResult","a","b","getUnbalancedErrors","text","characterErrors","lines","absoluteIndex","line","lineIndex","balanceResult","error","areQuotesBalanced","areBracketsBalanced","INTAHA_ACTUAL","PATTERNS","extractDigits","text","match","tokenizeText","preserveSymbols","processedText","symbol","symbolRegex","handleFootnoteFusion","result","previousToken","currentToken","prevIsStandalone","currHasEmbedded","currIsStandalone","prevHasEmbedded","prevDigits","currDigits","handleFootnoteSelection","tokenA","tokenB","aHasEmbedded","bHasEmbedded","handleStandaloneFootnotes","aIsFootnote","bIsFootnote","standardizeHijriSymbol","standardizeIntahaSymbol","INVALID_FOOTNOTE","hasInvalidFootnotes","text","PATTERNS","arabicFormatter","numberToArabic","num","ocrToArabic","char","arabicToNumber","arabicStr","lookup","digits","numStr","parsed","extractReferences","lines","arabicReferencesInBody","b","ocrConfusedReferencesInBody","arabicReferencesInFootnotes","ocrConfusedReferencesInFootnotes","convertedOcrBodyRefs","ref","convertedOcrFootnoteRefs","needsCorrection","references","line","bodySet","footnoteSet","correctReferences","initialReferences","sanitizedLines","updatedText","ocrRegex","match","cleanReferences","bodyRefSet","footnoteRefSet","uniqueBodyRefs","uniqueFootnoteRefs","bodyRefsForFootnotes","footnoteRefsForBody","allRefs","referenceCounter","availableRef","newRef","ACNode","AhoCorasick","pattern","id","v","i","ch","to","q","qi","link","nxt","linkOut","text","onMatch","pid","buildAhoCorasick","patterns","ac","pat","DEFAULT_POLICY","buildBook","pagesN","parts","starts","lens","off","i","p","posToPage","pos","pageStarts","lo","hi","ans","mid","findExactMatches","book","patterns","patIdToOrigIdxs","excerptsCount","ac","buildAhoCorasick","result","seenExact","pid","endPos","pat","startPos","startPage","origIdx","deduplicateExcerpts","excerptsN","keyToPatId","k","selectExistingGrams","map","sortedItems","gramsPerExcerpt","result","item","selectFallbackGrams","i","QGramIndex","q","page","text","seam","gram","postings","excerpt","items","seen","freq","a","b","selected","createSeams","pagesN","seamLen","seams","p","left","right","text","buildQGramIndex","q","qidx","QGramIndex","generateCandidates","excerpt","cfg","seeds","candidates","excerptLen","gram","offset","posts","startPos","calculateFuzzyScore","candidate","maxDist","src","L","extra","start0","end0","window","dist","boundedLevenshtein","findBestFuzzyMatch","keyset","best","key","performFuzzyMatching","excerptsN","seenExact","result","findMatches","pages","excerpts","policy","DEFAULT_POLICY","sanitizeArabic","e","patIdToOrigIdxs","patterns","deduplicateExcerpts","book","pageStarts","buildBook","findExactMatches","seen","recordExactMatches","hitsByExcerpt","buildAhoCorasick","pid","endPos","pat","startPage","posToPage","origIdx","hits","prev","processFuzzyCandidate","score","entry","processSingleExcerptFuzzy","excerptIndex","v","recordFuzzyMatches","i","sortMatches","exact","fuzzy","a","b","findMatchesAll","isArabicTextNoise","text","trimmed","length","isBasicNoisePattern","charStats","analyzeCharacterStats","hasExcessiveRepetition","hasArabic","PATTERNS","isValidArabicContent","isNonArabicNoise","stats","chars","char","textLength","repeatCount","repetitiveChars","count","pattern","contentChars","isSpacingNoise","arabicCount","spaceCount","selectBestTokens","originalToken","altToken","similarityThreshold","typoSymbols","sanitizeArabic","result","handleFootnoteSelection","footnoteResult","handleStandaloneFootnotes","typoSymbol","symbol","normalizedOriginal","normalizedAlt","calculateSimilarity","removeDuplicateTokens","tokens","highSimilarityThreshold","currentToken","previousToken","areSimilarAfterNormalization","handleFootnoteFusion","processTextAlignment","originalText","altText","options","originalTokens","tokenizeText","altTokens","mergedTokens","alignTokenSequences","original","alt","fixTypo","correction"]}
|
package/package.json
CHANGED
|
@@ -1,47 +1,37 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "baburchi",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.0",
|
|
4
4
|
"author": "Ragaeeb Haq",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "https://github.com/ragaeeb/baburchi.git"
|
|
8
8
|
},
|
|
9
9
|
"main": "dist/index.js",
|
|
10
|
-
"module": "dist/index.
|
|
10
|
+
"module": "dist/index.js",
|
|
11
11
|
"devDependencies": {
|
|
12
|
-
"@
|
|
13
|
-
"@types/bun": "^1.2.
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
"eslint-plugin-prettier": "^5.5.4",
|
|
18
|
-
"globals": "^16.3.0",
|
|
19
|
-
"prettier": "^3.6.2",
|
|
20
|
-
"semantic-release": "^24.2.7",
|
|
21
|
-
"tsup": "^8.5.0",
|
|
22
|
-
"typescript-eslint": "^8.39.1"
|
|
12
|
+
"@biomejs/biome": "^2.2.4",
|
|
13
|
+
"@types/bun": "^1.2.21",
|
|
14
|
+
"@types/node": "^24.3.3",
|
|
15
|
+
"semantic-release": "^24.2.8",
|
|
16
|
+
"tsup": "^8.5.0"
|
|
23
17
|
},
|
|
24
18
|
"bugs": {
|
|
25
19
|
"url": "https://github.com/ragaeeb/baburchi/issues"
|
|
26
20
|
},
|
|
27
21
|
"description": "A lightweight TypeScript library designed to fix typos in OCR post-processing.",
|
|
28
22
|
"engines": {
|
|
29
|
-
"bun": ">=1.2.
|
|
23
|
+
"bun": ">=1.2.21",
|
|
30
24
|
"node": ">=22.0.0"
|
|
31
25
|
},
|
|
26
|
+
"sideEffects": false,
|
|
32
27
|
"exports": {
|
|
33
28
|
".": {
|
|
34
29
|
"import": "./dist/index.js",
|
|
35
30
|
"types": "./dist/index.d.ts"
|
|
36
31
|
}
|
|
37
32
|
},
|
|
38
|
-
"sideEffects": false,
|
|
39
33
|
"files": [
|
|
40
|
-
"dist
|
|
41
|
-
"dist/index.js.map",
|
|
42
|
-
"dist/*.d.ts",
|
|
43
|
-
"LICENSE.md",
|
|
44
|
-
"README.md"
|
|
34
|
+
"dist/**"
|
|
45
35
|
],
|
|
46
36
|
"homepage": "https://github.com/ragaeeb/baburchi",
|
|
47
37
|
"keywords": [
|
|
@@ -57,7 +47,7 @@
|
|
|
57
47
|
"license": "MIT",
|
|
58
48
|
"scripts": {
|
|
59
49
|
"build": "tsup",
|
|
60
|
-
"
|
|
50
|
+
"lint": "biome check ."
|
|
61
51
|
},
|
|
62
52
|
"source": "src/index.ts",
|
|
63
53
|
"type": "module",
|