baburchi 1.1.1 → 1.2.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 CHANGED
@@ -273,6 +273,140 @@ const corrected = correctReferences(textLines);
273
273
  // OCR characters (O) and (1) become proper Arabic numerals
274
274
  ```
275
275
 
276
+ ## Text Balance Validation
277
+
278
+ Baburchi includes robust text balance validation utilities for checking proper pairing of quotes and brackets in text. These functions help identify syntax errors, unclosed brackets, mismatched pairs, and other balance issues commonly found in OCR-processed text.
279
+
280
+ ### Balance Checking Functions
281
+
282
+ #### `checkBalance(text)`
283
+
284
+ Comprehensive balance checking for both quotes and brackets in a single function.
285
+
286
+ ```typescript
287
+ import { checkBalance } from 'baburchi';
288
+
289
+ const result = checkBalance('Hello "world" and (test)');
290
+ console.log(result.isBalanced); // true
291
+ console.log(result.errors); // []
292
+
293
+ const problematic = checkBalance('Hello "world and (test');
294
+ console.log(problematic.isBalanced); // false
295
+ console.log(problematic.errors);
296
+ // [
297
+ // { char: '"', index: 6, reason: 'unmatched', type: 'quote' },
298
+ // { char: '(', index: 17, reason: 'unclosed', type: 'bracket' }
299
+ // ]
300
+ ```
301
+
302
+ #### `getUnbalancedErrors(text)`
303
+
304
+ Advanced error detection for multi-line text with absolute character positioning.
305
+
306
+ ```typescript
307
+ import { getUnbalancedErrors } from 'baburchi';
308
+
309
+ const multiLineText = `First line with "unmatched quote
310
+ Second line with (unclosed bracket
311
+ Third line is balanced "properly"`;
312
+
313
+ const errors = getUnbalancedErrors(multiLineText);
314
+ console.log(errors);
315
+ // [
316
+ // { absoluteIndex: 16, char: '"', reason: 'unmatched', type: 'quote' },
317
+ // { absoluteIndex: 51, char: '(', reason: 'unclosed', type: 'bracket' }
318
+ // ]
319
+ ```
320
+
321
+ ### Supported Bracket Types
322
+
323
+ Baburchi supports the following bracket pairs:
324
+
325
+ - **Parentheses**: `()`
326
+ - **Square brackets**: `[]`
327
+ - **Curly brackets**: `{}`
328
+ - **Angle brackets**: `«»`
329
+
330
+ ### Error Types
331
+
332
+ The balance checker identifies three types of errors:
333
+
334
+ - **`unmatched`**: Opening or closing character without a corresponding pair
335
+ - **`unclosed`**: Opening character that was never closed
336
+ - **`mismatched`**: Wrong closing character for an opening character (e.g., `(]`)
337
+
338
+ ### Balance Checking Configuration
339
+
340
+ ```typescript
341
+ import { BRACKETS, OPEN_BRACKETS, CLOSE_BRACKETS } from 'baburchi';
342
+
343
+ // Access bracket mappings
344
+ console.log(BRACKETS); // { '«': '»', '(': ')', '[': ']', '{': '}' }
345
+
346
+ // Check if character is an opening bracket
347
+ console.log(OPEN_BRACKETS.has('(')); // true
348
+
349
+ // Check if character is a closing bracket
350
+ console.log(CLOSE_BRACKETS.has(')')); // true
351
+ ```
352
+
353
+ ### Use Cases
354
+
355
+ #### Text Editor Integration
356
+
357
+ Perfect for syntax highlighting and error detection in text editors:
358
+
359
+ ```typescript
360
+ import { getUnbalancedErrors } from 'baburchi';
361
+
362
+ const editorContent = getUserInput();
363
+ const errors = getUnbalancedErrors(editorContent);
364
+
365
+ // Highlight errors in the editor using absolute positions
366
+ errors.forEach((error) => {
367
+ highlightError(error.absoluteIndex, error.char, error.reason);
368
+ });
369
+ ```
370
+
371
+ #### OCR Post-Processing
372
+
373
+ Identify and flag potential OCR errors in processed text:
374
+
375
+ ```typescript
376
+ import { checkBalance } from 'baburchi';
377
+
378
+ const ocrText = processOCRDocument();
379
+ const { isBalanced, errors } = checkBalance(ocrText);
380
+
381
+ if (!isBalanced) {
382
+ console.log(`Found ${errors.length} balance errors requiring review`);
383
+ errors.forEach((error) => {
384
+ console.log(`${error.type} error: "${error.char}" at position ${error.index} (${error.reason})`);
385
+ });
386
+ }
387
+ ```
388
+
389
+ #### Document Validation
390
+
391
+ Validate document structure before processing:
392
+
393
+ ```typescript
394
+ import { getUnbalancedErrors } from 'baburchi';
395
+
396
+ const document = loadDocument();
397
+ const lines = document.split('\n');
398
+
399
+ // Only check lines longer than 10 characters (as per library behavior)
400
+ const longLines = lines.filter((line) => line.length > 10);
401
+ const errors = getUnbalancedErrors(document);
402
+
403
+ if (errors.length === 0) {
404
+ console.log('Document structure is valid');
405
+ } else {
406
+ console.log(`Document has ${errors.length} structural issues`);
407
+ }
408
+ ```
409
+
276
410
  ## Contributing
277
411
 
278
412
  Contributions are welcome. Please ensure your contributions adhere to the coding standards and include relevant tests.
package/dist/index.d.ts CHANGED
@@ -27,6 +27,143 @@ type FixTypoOptions = {
27
27
  readonly typoSymbols: string[];
28
28
  };
29
29
 
30
+ /**
31
+ * Represents an error found when checking balance of quotes or brackets in text.
32
+ */
33
+ type BalanceError = {
34
+ /** The character that caused the error */
35
+ char: string;
36
+ /** The position of the character in the string */
37
+ index: number;
38
+ /** The reason for the error */
39
+ reason: 'mismatched' | 'unclosed' | 'unmatched';
40
+ /** The type of character that caused the error */
41
+ type: 'bracket' | 'quote';
42
+ };
43
+ /**
44
+ * Result of a balance check operation.
45
+ */
46
+ type BalanceResult = {
47
+ /** Array of errors found during balance checking */
48
+ errors: BalanceError[];
49
+ /** Whether the text is properly balanced */
50
+ isBalanced: boolean;
51
+ };
52
+ /** Mapping of opening brackets to their corresponding closing brackets */
53
+ declare const BRACKETS: {
54
+ '\u00AB': string;
55
+ '(': string;
56
+ '[': string;
57
+ '{': string;
58
+ };
59
+ /** Set of all opening bracket characters */
60
+ declare const OPEN_BRACKETS: Set<string>;
61
+ /** Set of all closing bracket characters */
62
+ declare const CLOSE_BRACKETS: Set<string>;
63
+ /**
64
+ * Checks if both quotes and brackets are balanced in a string and returns detailed error information.
65
+ *
66
+ * This function combines the results of both quote and bracket balance checking,
67
+ * providing a comprehensive analysis of all balance issues in the text.
68
+ * The errors are sorted by their position in the string for easier debugging.
69
+ *
70
+ * @param str - The string to check for overall balance
71
+ * @returns An object containing combined balance status and all errors found, sorted by position
72
+ *
73
+ * @example
74
+ * ```typescript
75
+ * checkBalance('Hello "world" and (test)') // { errors: [], isBalanced: true }
76
+ * checkBalance('Hello "world and (test') // { errors: [...], isBalanced: false }
77
+ * ```
78
+ */
79
+ declare const checkBalance: (str: string) => BalanceResult;
80
+ /**
81
+ * Enhanced error detection that returns absolute character positions for use with HighlightableTextarea.
82
+ *
83
+ * This interface extends the basic BalanceError to include absolute positioning
84
+ * across multiple lines of text, making it suitable for text editors and
85
+ * syntax highlighters that need precise character positioning.
86
+ */
87
+ interface CharacterError {
88
+ /** Absolute character position from the start of the entire text */
89
+ absoluteIndex: number;
90
+ /** The character that caused the error */
91
+ char: string;
92
+ /** The reason for the error */
93
+ reason: 'mismatched' | 'unclosed' | 'unmatched';
94
+ /** The type of character that caused the error */
95
+ type: 'bracket' | 'quote';
96
+ }
97
+ /**
98
+ * Gets detailed character-level errors for unbalanced quotes and brackets in multi-line text.
99
+ *
100
+ * This function processes text line by line, but only checks lines longer than 10 characters
101
+ * for balance issues. It returns absolute positions that can be used with text editors
102
+ * or highlighting components that need precise character positioning across the entire text.
103
+ *
104
+ * The absolute index accounts for newline characters between lines, providing accurate
105
+ * positioning for the original text string.
106
+ *
107
+ * @param text - The multi-line text to analyze for balance errors
108
+ * @returns Array of character errors with absolute positioning information
109
+ *
110
+ * @example
111
+ * ```typescript
112
+ * const text = 'Line 1 with "quote\nLine 2 with (bracket';
113
+ * const errors = getUnbalancedErrors(text);
114
+ * // Returns errors with absoluteIndex pointing to exact character positions
115
+ * ```
116
+ */
117
+ declare const getUnbalancedErrors: (text: string) => CharacterError[];
118
+ /**
119
+ * Checks if all double quotes in a string are balanced.
120
+ *
121
+ * This is a convenience function that returns only the boolean result
122
+ * without detailed error information.
123
+ *
124
+ * @param str - The string to check for quote balance
125
+ * @returns True if quotes are balanced, false otherwise
126
+ *
127
+ * @example
128
+ * ```typescript
129
+ * areQuotesBalanced('Hello "world"') // true
130
+ * areQuotesBalanced('Hello "world') // false
131
+ * ```
132
+ */
133
+ declare const areQuotesBalanced: (str: string) => boolean;
134
+ /**
135
+ * Checks if all brackets in a string are properly balanced.
136
+ *
137
+ * This is a convenience function that returns only the boolean result
138
+ * without detailed error information.
139
+ *
140
+ * @param str - The string to check for bracket balance
141
+ * @returns True if brackets are balanced, false otherwise
142
+ *
143
+ * @example
144
+ * ```typescript
145
+ * areBracketsBalanced('(hello [world])') // true
146
+ * areBracketsBalanced('(hello [world') // false
147
+ * ```
148
+ */
149
+ declare const areBracketsBalanced: (str: string) => boolean;
150
+ /**
151
+ * Checks if both quotes and brackets are balanced in a string.
152
+ *
153
+ * This is a convenience function that returns only the boolean result
154
+ * without detailed error information.
155
+ *
156
+ * @param str - The string to check for overall balance
157
+ * @returns True if both quotes and brackets are balanced, false otherwise
158
+ *
159
+ * @example
160
+ * ```typescript
161
+ * isBalanced('Hello "world" and (test)') // true
162
+ * isBalanced('Hello "world and (test') // false
163
+ * ```
164
+ */
165
+ declare const isBalanced: (str: string) => boolean;
166
+
30
167
  /**
31
168
  * Checks if the given text contains invalid footnote references.
32
169
  * Invalid footnotes include empty parentheses "()" or OCR-confused characters
@@ -270,4 +407,4 @@ declare const handleStandaloneFootnotes: (tokenA: string, tokenB: string) => nul
270
407
  declare const processTextAlignment: (originalText: string, altText: string, options: FixTypoOptions) => string;
271
408
  declare const fixTypo: (original: string, correction: string, { highSimilarityThreshold, similarityThreshold, typoSymbols, }: Partial<FixTypoOptions> & Pick<FixTypoOptions, "typoSymbols">) => string;
272
409
 
273
- export { PATTERNS, alignTokenSequences, areSimilarAfterNormalization, backtrackAlignment, calculateAlignmentScore, calculateLevenshteinDistance, calculateSimilarity, correctReferences, extractDigits, fixTypo, handleFootnoteFusion, handleFootnoteSelection, handleStandaloneFootnotes, hasInvalidFootnotes, normalizeArabicText, processTextAlignment, tokenizeText };
410
+ export { BRACKETS, CLOSE_BRACKETS, type CharacterError, OPEN_BRACKETS, PATTERNS, alignTokenSequences, areBracketsBalanced, areQuotesBalanced, areSimilarAfterNormalization, backtrackAlignment, calculateAlignmentScore, calculateLevenshteinDistance, calculateSimilarity, checkBalance, correctReferences, extractDigits, fixTypo, getUnbalancedErrors, handleFootnoteFusion, handleFootnoteSelection, handleStandaloneFootnotes, hasInvalidFootnotes, isBalanced, normalizeArabicText, processTextAlignment, tokenizeText };
package/dist/index.js CHANGED
@@ -1,2 +1,3 @@
1
- var u={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,diacritics:/[\u0610-\u061A\u064B-\u065F\u0670\u06D6-\u06ED]/g,footnoteEmbedded:/\([0-9\u0660-\u0669]+\)/,footnoteStandalone:/^\(?[0-9\u0660-\u0669]+\)?[،.]?$/,invalidReferenceRegex:/\(\)|\([.1OV9]+\)/g,ocrConfusedFootnoteReferenceRegex:/^\([.1OV9]+\)/g,ocrConfusedReferenceRegex:/\([.1OV9]+\)/g,tatweel:/\u0640/g,whitespace:/\s+/},p=e=>e.replace(u.tatweel,"").replace(u.diacritics,"").trim(),F=e=>{let t=e.match(u.arabicDigits);return t?t[0]:""},T=(e,t=[])=>{let n=e;for(let r of t){let s=new RegExp(r,"g");n=n.replace(s,` ${r} `)}return n.trim().split(u.whitespace).filter(Boolean)},A=(e,t,n)=>{let r=u.footnoteStandalone.test(t),s=u.footnoteEmbedded.test(n),i=u.footnoteStandalone.test(n),c=u.footnoteEmbedded.test(t),o=F(t),a=F(n);return r&&s&&o===a?(e[e.length-1]=n,!0):!!(c&&i&&o===a)},E=(e,t)=>{let n=u.footnoteEmbedded.test(e),r=u.footnoteEmbedded.test(t);return n&&!r?[e]:r&&!n?[t]:n&&r?[e.length<=t.length?e:t]:null},C=(e,t)=>{let n=u.footnoteStandalone.test(e),r=u.footnoteStandalone.test(t);return n&&!r?[e,t]:r&&!n?[t,e]:n&&r?[e.length<=t.length?e:t]:null};var x={GAP_PENALTY:-1,MISMATCH_PENALTY:-2,PERFECT_MATCH:2,SOFT_MATCH:1},w=(e,t)=>{let n=e.length,r=t.length;if(n===0)return r;if(r===0)return n;let[s,i]=n<=r?[e,t]:[t,e],c=s.length,o=i.length,a=Array.from({length:c+1},(f,g)=>g);for(let f=1;f<=o;f++){let g=[f];for(let m=1;m<=c;m++){let b=i[f-1]===s[m-1]?0:1,l=Math.min(a[m]+1,g[m-1]+1,a[m-1]+b);g.push(l)}a=g}return a[c]},R=(e,t)=>{let n=Math.max(e.length,t.length)||1,r=w(e,t);return(n-r)/n},P=(e,t,n=.6)=>{let r=p(e),s=p(t);return R(r,s)>=n},I=(e,t,n,r)=>{let s=p(e),i=p(t);if(s===i)return x.PERFECT_MATCH;let c=n.includes(e)||n.includes(t),o=R(s,i)>=r;return c||o?x.SOFT_MATCH:x.MISMATCH_PENALTY},L=(e,t,n)=>{let r=[],s=t.length,i=n.length;for(;s>0||i>0;)switch(e[s][i].direction){case"diagonal":r.push([t[--s],n[--i]]);break;case"left":r.push([null,n[--i]]);break;case"up":r.push([t[--s],null]);break;default:throw new Error("Invalid alignment direction")}return r.reverse()},O=(e,t,n,r)=>{let s=e.length,i=t.length,c=Array.from({length:s+1},()=>Array.from({length:i+1},()=>({direction:null,score:0})));for(let o=1;o<=s;o++)c[o][0]={direction:"up",score:o*x.GAP_PENALTY};for(let o=1;o<=i;o++)c[0][o]={direction:"left",score:o*x.GAP_PENALTY};for(let o=1;o<=s;o++)for(let a=1;a<=i;a++){let f=I(e[o-1],t[a-1],n,r),g=c[o-1][a-1].score+f,m=c[o-1][a].score+x.GAP_PENALTY,b=c[o][a-1].score+x.GAP_PENALTY,l=Math.max(g,m,b),d="left";l===g?d="diagonal":l===m&&(d="up"),c[o][a]={direction:d,score:l}}return L(c,e,t)};var z="()",v=e=>u.invalidReferenceRegex.test(e),_=new Intl.NumberFormat("ar-SA"),D=e=>_.format(e),y=e=>({1:"\u0661",9:"\u0669",".":"\u0660",O:"\u0665",o:"\u0665",V:"\u0667",v:"\u0667"})[e]||e,H=e=>{let t={"\u0660":"0","\u0661":"1","\u0662":"2","\u0663":"3","\u0664":"4","\u0665":"5","\u0666":"6","\u0667":"7","\u0668":"8","\u0669":"9"},n=e.replace(/[()]/g,""),r="";for(let i of n)r+=t[i];let s=parseInt(r,10);return isNaN(s)?0:s},M=e=>{let t=e.filter(o=>!o.isFootnote).flatMap(o=>o.text.match(u.arabicReferenceRegex)||[]),n=e.filter(o=>!o.isFootnote).flatMap(o=>o.text.match(u.ocrConfusedReferenceRegex)||[]),r=e.filter(o=>o.isFootnote).flatMap(o=>o.text.match(u.arabicFootnoteReferenceRegex)||[]),s=e.filter(o=>o.isFootnote).flatMap(o=>o.text.match(u.ocrConfusedFootnoteReferenceRegex)||[]),i=n.map(o=>o.replace(/[.1OV9]/g,a=>y(a))),c=s.map(o=>o.replace(/[.1OV9]/g,a=>y(a)));return{bodyReferences:[...t,...i],footnoteReferences:[...r,...c],ocrConfusedInBody:n,ocrConfusedInFootnotes:s}},V=(e,t)=>{if(e.some(i=>v(i.text)))return!0;let r=new Set(t.bodyReferences),s=new Set(t.footnoteReferences);if(r.size!==s.size)return!0;for(let i of r)if(!s.has(i))return!0;return!1},K=e=>{let t=M(e);if(!V(e,t))return e;let n=e.map(l=>{let d=l.text,S=/\([.1OV9]+\)/g;return d=d.replace(S,h=>h.replace(/[.1OV9]/g,N=>y(N))),{...l,text:d}}),r=M(n),s=new Set(r.bodyReferences),i=new Set(r.footnoteReferences),c=[...new Set(r.bodyReferences)],o=[...new Set(r.footnoteReferences)],a=c.filter(l=>!i.has(l)),f=o.filter(l=>!s.has(l)),g=[...s,...i],b={count:(g.length>0?Math.max(0,...g.map(l=>H(l))):0)+1};return n.map(l=>{if(!l.text.includes(z))return l;let d=l.text;return d=d.replace(/\(\)/g,()=>{if(l.isFootnote){let h=a.shift();if(h)return h}else{let h=f.shift();if(h)return h}let S=`(${D(b.count)})`;return b.count++,S}),{...l,text:d}})};var Y=(e,t,{similarityThreshold:n,typoSymbols:r})=>{if(e===null)return[t];if(t===null)return[e];if(p(e)===p(t))return[e];let s=E(e,t);if(s)return s;let i=C(e,t);if(i)return i;if(r.includes(e)||r.includes(t)){let f=r.find(g=>g===e||g===t);return f?[f]:[e]}let c=p(e),o=p(t);return[R(c,o)>n?e:t]},G=(e,t)=>{if(e.length===0)return e;let n=[];for(let r of e){if(n.length===0){n.push(r);continue}let s=n.at(-1);if(P(s,r,t)){r.length<s.length&&(n[n.length-1]=r);continue}A(n,s,r)||n.push(r)}return n},j=(e,t,n)=>{let r=T(e,n.typoSymbols),s=T(t,n.typoSymbols),c=O(r,s,n.typoSymbols,n.similarityThreshold).flatMap(([a,f])=>Y(a,f,n));return G(c,n.highSimilarityThreshold).join(" ")},Z=(e,t,{highSimilarityThreshold:n=.8,similarityThreshold:r=.6,typoSymbols:s})=>j(e,t,{highSimilarityThreshold:n,similarityThreshold:r,typoSymbols:s});export{u as PATTERNS,O as alignTokenSequences,P as areSimilarAfterNormalization,L as backtrackAlignment,I as calculateAlignmentScore,w as calculateLevenshteinDistance,R as calculateSimilarity,K as correctReferences,F as extractDigits,Z as fixTypo,A as handleFootnoteFusion,E as handleFootnoteSelection,C as handleStandaloneFootnotes,v as hasInvalidFootnotes,p as normalizeArabicText,j as processTextAlignment,T as tokenizeText};
1
+ var u={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,diacritics:/[\u0610-\u061A\u064B-\u065F\u0670\u06D6-\u06ED]/g,footnoteEmbedded:/\([0-9\u0660-\u0669]+\)/,footnoteStandalone:/^\(?[0-9\u0660-\u0669]+\)?[،.]?$/,invalidReferenceRegex:/\(\)|\([.1OV9]+\)/g,ocrConfusedFootnoteReferenceRegex:/^\([.1OV9]+\)/g,ocrConfusedReferenceRegex:/\([.1OV9]+\)/g,tatweel:/\u0640/g,whitespace:/\s+/},m=e=>e.replace(u.tatweel,"").replace(u.diacritics,"").trim(),E=e=>{let t=e.match(u.arabicDigits);return t?t[0]:""},S=(e,t=[])=>{let n=e;for(let r of t){let o=new RegExp(r,"g");n=n.replace(o,` ${r} `)}return n.trim().split(u.whitespace).filter(Boolean)},A=(e,t,n)=>{let r=u.footnoteStandalone.test(t),o=u.footnoteEmbedded.test(n),c=u.footnoteStandalone.test(n),a=u.footnoteEmbedded.test(t),s=E(t),i=E(n);return r&&o&&s===i?(e[e.length-1]=n,!0):!!(a&&c&&s===i)},F=(e,t)=>{let n=u.footnoteEmbedded.test(e),r=u.footnoteEmbedded.test(t);return n&&!r?[e]:r&&!n?[t]:n&&r?[e.length<=t.length?e:t]:null},C=(e,t)=>{let n=u.footnoteStandalone.test(e),r=u.footnoteStandalone.test(t);return n&&!r?[e,t]:r&&!n?[t,e]:n&&r?[e.length<=t.length?e:t]:null};var x={GAP_PENALTY:-1,MISMATCH_PENALTY:-2,PERFECT_MATCH:2,SOFT_MATCH:1},L=(e,t)=>{let n=e.length,r=t.length;if(n===0)return r;if(r===0)return n;let[o,c]=n<=r?[e,t]:[t,e],a=o.length,s=c.length,i=Array.from({length:a+1},(f,d)=>d);for(let f=1;f<=s;f++){let d=[f];for(let p=1;p<=a;p++){let b=c[f-1]===o[p-1]?0:1,l=Math.min(i[p]+1,d[p-1]+1,i[p-1]+b);d.push(l)}i=d}return i[a]},R=(e,t)=>{let n=Math.max(e.length,t.length)||1,r=L(e,t);return(n-r)/n},B=(e,t,n=.6)=>{let r=m(e),o=m(t);return R(r,o)>=n},z=(e,t,n,r)=>{let o=m(e),c=m(t);if(o===c)return x.PERFECT_MATCH;let a=n.includes(e)||n.includes(t),s=R(o,c)>=r;return a||s?x.SOFT_MATCH:x.MISMATCH_PENALTY},_=(e,t,n)=>{let r=[],o=t.length,c=n.length;for(;o>0||c>0;)switch(e[o][c].direction){case"diagonal":r.push([t[--o],n[--c]]);break;case"left":r.push([null,n[--c]]);break;case"up":r.push([t[--o],null]);break;default:throw new Error("Invalid alignment direction")}return r.reverse()},P=(e,t,n,r)=>{let o=e.length,c=t.length,a=Array.from({length:o+1},()=>Array.from({length:c+1},()=>({direction:null,score:0})));for(let s=1;s<=o;s++)a[s][0]={direction:"up",score:s*x.GAP_PENALTY};for(let s=1;s<=c;s++)a[0][s]={direction:"left",score:s*x.GAP_PENALTY};for(let s=1;s<=o;s++)for(let i=1;i<=c;i++){let f=z(e[s-1],t[i-1],n,r),d=a[s-1][i-1].score+f,p=a[s-1][i].score+x.GAP_PENALTY,b=a[s][i-1].score+x.GAP_PENALTY,l=Math.max(d,p,b),g="left";l===d?g="diagonal":l===p&&(g="up"),a[s][i]={direction:g,score:l}}return _(a,e,t)};var O=e=>{let t=[],n=0,r=-1;for(let c=0;c<e.length;c++)e[c]==='"'&&(n++,r=c);let o=n%2===0;return!o&&r!==-1&&t.push({char:'"',index:r,reason:"unmatched",type:"quote"}),{errors:t,isBalanced:o}},v={"\xAB":"\xBB","(":")","[":"]","{":"}"},D=new Set(["\xAB","(","[","{"]),H=new Set(["\xBB",")","]","}"]),I=e=>{let t=[],n=[];for(let r=0;r<e.length;r++){let o=e[r];if(D.has(o))n.push({char:o,index:r});else if(H.has(o)){let c=n.pop();c?v[c.char]!==o&&(t.push({char:c.char,index:c.index,reason:"mismatched",type:"bracket"}),t.push({char:o,index:r,reason:"mismatched",type:"bracket"})):t.push({char:o,index:r,reason:"unmatched",type:"bracket"})}}return n.forEach(({char:r,index:o})=>{t.push({char:r,index:o,reason:"unclosed",type:"bracket"})}),{errors:t,isBalanced:t.length===0}},N=e=>{let t=O(e),n=I(e);return{errors:[...t.errors,...n.errors].sort((r,o)=>r.index-o.index),isBalanced:t.isBalanced&&n.isBalanced}},Z=e=>{let t=[],n=e.split(`
2
+ `),r=0;return n.forEach((o,c)=>{if(o.length>10){let a=N(o);a.isBalanced||a.errors.forEach(s=>{t.push({absoluteIndex:r+s.index,char:s.char,reason:s.reason,type:s.type})})}r+=o.length+(c<n.length-1?1:0)}),t},k=e=>O(e).isBalanced,ee=e=>I(e).isBalanced,te=e=>N(e).isBalanced;var q="()",V=e=>u.invalidReferenceRegex.test(e),Y=new Intl.NumberFormat("ar-SA"),G=e=>Y.format(e),T=e=>({1:"\u0661",9:"\u0669",".":"\u0660",O:"\u0665",o:"\u0665",V:"\u0667",v:"\u0667"})[e]||e,j=e=>{let t={"\u0660":"0","\u0661":"1","\u0662":"2","\u0663":"3","\u0664":"4","\u0665":"5","\u0666":"6","\u0667":"7","\u0668":"8","\u0669":"9"},n=e.replace(/[()]/g,""),r="";for(let c of n)r+=t[c];let o=parseInt(r,10);return isNaN(o)?0:o},w=e=>{let t=e.filter(s=>!s.isFootnote).flatMap(s=>s.text.match(u.arabicReferenceRegex)||[]),n=e.filter(s=>!s.isFootnote).flatMap(s=>s.text.match(u.ocrConfusedReferenceRegex)||[]),r=e.filter(s=>s.isFootnote).flatMap(s=>s.text.match(u.arabicFootnoteReferenceRegex)||[]),o=e.filter(s=>s.isFootnote).flatMap(s=>s.text.match(u.ocrConfusedFootnoteReferenceRegex)||[]),c=n.map(s=>s.replace(/[.1OV9]/g,i=>T(i))),a=o.map(s=>s.replace(/[.1OV9]/g,i=>T(i)));return{bodyReferences:[...t,...c],footnoteReferences:[...r,...a],ocrConfusedInBody:n,ocrConfusedInFootnotes:o}},K=(e,t)=>{if(e.some(c=>V(c.text)))return!0;let r=new Set(t.bodyReferences),o=new Set(t.footnoteReferences);if(r.size!==o.size)return!0;for(let c of r)if(!o.has(c))return!0;return!1},oe=e=>{let t=w(e);if(!K(e,t))return e;let n=e.map(l=>{let g=l.text,y=/\([.1OV9]+\)/g;return g=g.replace(y,h=>h.replace(/[.1OV9]/g,M=>T(M))),{...l,text:g}}),r=w(n),o=new Set(r.bodyReferences),c=new Set(r.footnoteReferences),a=[...new Set(r.bodyReferences)],s=[...new Set(r.footnoteReferences)],i=a.filter(l=>!c.has(l)),f=s.filter(l=>!o.has(l)),d=[...o,...c],b={count:(d.length>0?Math.max(0,...d.map(l=>j(l))):0)+1};return n.map(l=>{if(!l.text.includes(q))return l;let g=l.text;return g=g.replace(/\(\)/g,()=>{if(l.isFootnote){let h=i.shift();if(h)return h}else{let h=f.shift();if(h)return h}let y=`(${G(b.count)})`;return b.count++,y}),{...l,text:g}})};var Q=(e,t,{similarityThreshold:n,typoSymbols:r})=>{if(e===null)return[t];if(t===null)return[e];if(m(e)===m(t))return[e];let o=F(e,t);if(o)return o;let c=C(e,t);if(c)return c;if(r.includes(e)||r.includes(t)){let f=r.find(d=>d===e||d===t);return f?[f]:[e]}let a=m(e),s=m(t);return[R(a,s)>n?e:t]},$=(e,t)=>{if(e.length===0)return e;let n=[];for(let r of e){if(n.length===0){n.push(r);continue}let o=n.at(-1);if(B(o,r,t)){r.length<o.length&&(n[n.length-1]=r);continue}A(n,o,r)||n.push(r)}return n},U=(e,t,n)=>{let r=S(e,n.typoSymbols),o=S(t,n.typoSymbols),a=P(r,o,n.typoSymbols,n.similarityThreshold).flatMap(([i,f])=>Q(i,f,n));return $(a,n.highSimilarityThreshold).join(" ")},ie=(e,t,{highSimilarityThreshold:n=.8,similarityThreshold:r=.6,typoSymbols:o})=>U(e,t,{highSimilarityThreshold:n,similarityThreshold:r,typoSymbols:o});export{v as BRACKETS,H as CLOSE_BRACKETS,D as OPEN_BRACKETS,u as PATTERNS,P as alignTokenSequences,ee as areBracketsBalanced,k as areQuotesBalanced,B as areSimilarAfterNormalization,_ as backtrackAlignment,z as calculateAlignmentScore,L as calculateLevenshteinDistance,R as calculateSimilarity,N as checkBalance,oe as correctReferences,E as extractDigits,ie as fixTypo,Z as getUnbalancedErrors,A as handleFootnoteFusion,F as handleFootnoteSelection,C as handleStandaloneFootnotes,V as hasInvalidFootnotes,te as isBalanced,m as normalizeArabicText,U as processTextAlignment,S as tokenizeText};
2
3
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/textUtils.ts","../src/similarity.ts","../src/footnotes.ts","../src/index.ts"],"sourcesContent":["/**\n * Collection of regex patterns used throughout the library for text processing\n */\nexport const PATTERNS = {\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","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 { 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 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 './footnotes';\nexport * from './similarity';\nexport * from './textUtils';\n"],"mappings":"AAGO,IAAMA,EAAW,CAEpB,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,QAAQF,EAAS,QAAS,EAAE,EAAE,QAAQA,EAAS,WAAY,EAAE,EAAE,KAAK,EAavEG,EAAiBD,GAAyB,CACnD,IAAME,EAAQF,EAAK,MAAMF,EAAS,YAAY,EAC9C,OAAOI,EAAQA,EAAM,CAAC,EAAI,EAC9B,EAaaC,EAAe,CAACH,EAAcI,EAA4B,CAAC,IAAgB,CACpF,IAAIC,EAAgBL,EAGpB,QAAWM,KAAUF,EAAiB,CAClC,IAAMG,EAA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G,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,EAAuB,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,EAAiB+B,EAAUC,EAAKP,CAAO,CAAC,EAKvG,OAFoBV,EAAsBc,EAAcJ,EAAQ,uBAAuB,EAEpE,KAAK,GAAG,CAC/B,EAEaQ,EAAU,CACnBF,EACAG,EACA,CACI,wBAAAjB,EAA0B,GAC1B,oBAAAd,EAAsB,GACtB,YAAAC,CACJ,IAEOkB,EAAqBS,EAAUG,EAAY,CAAE,wBAAAjB,EAAyB,oBAAAd,EAAqB,YAAAC,CAAY,CAAC","names":["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","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","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","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/textUtils.ts","../src/similarity.ts","../src/balance.ts","../src/footnotes.ts","../src/index.ts"],"sourcesContent":["/**\n * Collection of regex patterns used throughout the library for text processing\n */\nexport const PATTERNS = {\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","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","/**\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 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 './balance';\nexport * from './footnotes';\nexport * from './similarity';\nexport * from './textUtils';\n"],"mappings":"AAGO,IAAMA,EAAW,CAEpB,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,QAAQF,EAAS,QAAS,EAAE,EA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AAW,EAAG,CACrBA,EAAO,KAAKY,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,EAAuB,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,EAAiB+B,EAAUC,EAAKP,CAAO,CAAC,EAKvG,OAFoBV,EAAsBc,EAAcJ,EAAQ,uBAAuB,EAEpE,KAAK,GAAG,CAC/B,EAEaQ,GAAU,CACnBF,EACAG,EACA,CACI,wBAAAjB,EAA0B,GAC1B,oBAAAd,EAAsB,GACtB,YAAAC,CACJ,IAEOkB,EAAqBS,EAAUG,EAAY,CAAE,wBAAAjB,EAAyB,oBAAAd,EAAqB,YAAAC,CAAY,CAAC","names":["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","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","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","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"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "baburchi",
3
- "version": "1.1.1",
3
+ "version": "1.2.0",
4
4
  "author": "Ragaeeb Haq",
5
5
  "repository": {
6
6
  "type": "git",
@@ -9,13 +9,13 @@
9
9
  "main": "dist/index.js",
10
10
  "module": "dist/index.ts",
11
11
  "devDependencies": {
12
- "@eslint/js": "^9.30.0",
12
+ "@eslint/js": "^9.30.1",
13
13
  "@types/bun": "^1.2.17",
14
- "eslint": "^9.30.0",
14
+ "eslint": "^9.30.1",
15
15
  "eslint-config-prettier": "^10.1.5",
16
16
  "eslint-plugin-perfectionist": "^4.15.0",
17
17
  "eslint-plugin-prettier": "^5.5.1",
18
- "globals": "^16.2.0",
18
+ "globals": "^16.3.0",
19
19
  "prettier": "^3.6.2",
20
20
  "semantic-release": "^24.2.6",
21
21
  "tsup": "^8.5.0",
@@ -26,7 +26,7 @@
26
26
  },
27
27
  "description": "A lightweight TypeScript library designed to fix typos in OCR post-processing.",
28
28
  "engines": {
29
- "bun": ">=1.2.16",
29
+ "bun": ">=1.2.17",
30
30
  "node": ">=22.0.0"
31
31
  },
32
32
  "exports": {