@kirnu/arabic-core 0.1.0 → 0.1.1
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 +2 -1
- package/dist/index.cjs +8 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -3
- package/dist/index.d.ts +4 -3
- package/dist/index.js +8 -6
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +1 -1
- package/src/numbers/lexicon.ts +159 -152
package/src/numbers/lexicon.ts
CHANGED
|
@@ -1,152 +1,159 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Arabic number grammar (Modern Standard Arabic).
|
|
3
|
-
*
|
|
4
|
-
* Every number–noun phrase is built by `countNoun`, which applies the counted-noun rules:
|
|
5
|
-
* 1 → the noun alone (its "one" form) ألف · ريال سعودي واحد
|
|
6
|
-
* 2 → dual ألفان · ريالان سعوديان
|
|
7
|
-
* 3–10 → number (opposite gender) + genitive plural ثلاثة آلاف · ثلاث هللات
|
|
8
|
-
* 11–99 → number + accusative singular (tamyiz) أحد عشر ألفاً · خمسون هللة
|
|
9
|
-
* ×100 (last two digits 00) → number + genitive singular مئة ألف · ثلاثة آلاف ريال
|
|
10
|
-
* For larger numbers the noun follows the LAST part
|
|
11
|
-
*
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
export type
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
plural
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
scale('
|
|
55
|
-
scale('
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
export const
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
if (n ===
|
|
80
|
-
|
|
81
|
-
if (n
|
|
82
|
-
if (n ===
|
|
83
|
-
if (n
|
|
84
|
-
if (n
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
const
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
*
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
Math.floor(n /
|
|
117
|
-
n % 1000,
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
if (
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
else tail =
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Arabic number grammar (Modern Standard Arabic).
|
|
3
|
+
*
|
|
4
|
+
* Every number–noun phrase is built by `countNoun`, which applies the counted-noun rules:
|
|
5
|
+
* 1 → the noun alone (its "one" form) ألف · ريال سعودي واحد
|
|
6
|
+
* 2 → dual ألفان · ريالان سعوديان
|
|
7
|
+
* 3–10 → number (opposite gender) + genitive plural ثلاثة آلاف · ثلاث هللات
|
|
8
|
+
* 11–99 → number + accusative singular (tamyiz) أحد عشر ألفاً · خمسون هللة
|
|
9
|
+
* ×100 (last two digits 00) → number + genitive singular مئة ألف · ثلاثة آلاف ريال
|
|
10
|
+
* For larger numbers the noun follows the LAST part, except that a scale with hundreds + 1 or 2 repeats the scale
|
|
11
|
+
* word so the amount can't be read as a sum: 101 thousand → مئة ألف وألف, 102 thousand → مئة ألف وألفان.
|
|
12
|
+
* A noun directly following the number puts the last scale in construct: مئتا ألف · ألفا ريال · أحد عشر ألف ريال.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
export type Gender = 'masculine' | 'feminine';
|
|
16
|
+
export type GrammaticalCase = 'nominative' | 'accusative' | 'genitive';
|
|
17
|
+
|
|
18
|
+
export interface CaseForms {
|
|
19
|
+
nominative: string;
|
|
20
|
+
accusative: string;
|
|
21
|
+
genitive: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface CountedNoun {
|
|
25
|
+
gender: Gender;
|
|
26
|
+
/** "One X" — for scales the bare noun (ألف), for currencies noun + واحد. */
|
|
27
|
+
one: CaseForms;
|
|
28
|
+
/** Singular; accusative is used as tamyiz after 11–99, genitive after hundreds. */
|
|
29
|
+
singular: CaseForms;
|
|
30
|
+
dual: CaseForms;
|
|
31
|
+
/** Dual in construct (number directly followed by a noun): ألفا / ألفي. */
|
|
32
|
+
dualConstruct?: CaseForms;
|
|
33
|
+
/** Genitive plural used after 3–10. */
|
|
34
|
+
plural: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export const cf = (nominative: string, accusative: string, genitive: string = nominative): CaseForms => ({
|
|
38
|
+
nominative,
|
|
39
|
+
accusative,
|
|
40
|
+
genitive,
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const scale = (sg: string, sgAcc: string, du: string, duObl: string, cons: string, consObl: string, pl: string): CountedNoun => ({
|
|
44
|
+
gender: 'masculine',
|
|
45
|
+
one: cf(sg, sgAcc),
|
|
46
|
+
singular: cf(sg, sgAcc),
|
|
47
|
+
dual: cf(du, duObl, duObl),
|
|
48
|
+
dualConstruct: cf(cons, consObl, consObl),
|
|
49
|
+
plural: pl,
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
/** Index 0 = thousand, 1 = million, 2 = billion. */
|
|
53
|
+
export const SCALES: readonly CountedNoun[] = [
|
|
54
|
+
scale('ألف', 'ألفاً', 'ألفان', 'ألفين', 'ألفا', 'ألفي', 'آلاف'),
|
|
55
|
+
scale('مليون', 'مليوناً', 'مليونان', 'مليونين', 'مليونا', 'مليوني', 'ملايين'),
|
|
56
|
+
scale('مليار', 'ملياراً', 'ملياران', 'مليارين', 'مليارا', 'ملياري', 'مليارات'),
|
|
57
|
+
];
|
|
58
|
+
|
|
59
|
+
export const ZERO = 'صفر';
|
|
60
|
+
export const AND = ' و';
|
|
61
|
+
|
|
62
|
+
/** 3–10 agreeing with a MASCULINE counted noun (the form ending in ة). Index = value. */
|
|
63
|
+
const UNITS_FOR_MASC = ['', '', '', 'ثلاثة', 'أربعة', 'خمسة', 'ستة', 'سبعة', 'ثمانية', 'تسعة', 'عشرة'];
|
|
64
|
+
/** 3–10 agreeing with a FEMININE counted noun. */
|
|
65
|
+
const UNITS_FOR_FEM = ['', '', '', 'ثلاث', 'أربع', 'خمس', 'ست', 'سبع', 'ثماني', 'تسع', 'عشر'];
|
|
66
|
+
/** Hundreds stems: ثلاثمئة … تسعمئة (written joined). */
|
|
67
|
+
const HUNDREDS_STEM = ['', '', '', 'ثلاث', 'أربع', 'خمس', 'ست', 'سبع', 'ثمان', 'تسع'];
|
|
68
|
+
const TENS = ['', '', 'عشر', 'ثلاث', 'أربع', 'خمس', 'ست', 'سبع', 'ثمان', 'تسع'];
|
|
69
|
+
|
|
70
|
+
const isNom = (c: GrammaticalCase) => c === 'nominative';
|
|
71
|
+
|
|
72
|
+
function tens(t: number, c: GrammaticalCase): string {
|
|
73
|
+
return TENS[t] + (isNom(c) ? 'ون' : 'ين');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** 1–99 agreeing with a counted noun of gender `g`. */
|
|
77
|
+
export function belowHundred(n: number, g: Gender, c: GrammaticalCase): string {
|
|
78
|
+
const m = g === 'masculine';
|
|
79
|
+
if (n === 0) return '';
|
|
80
|
+
// Accusative «واحداً» (standalone or as the unit of 21–91); the feminine «واحدة/إحدى» look the same unvocalised.
|
|
81
|
+
if (n === 1) return m ? (c === 'accusative' ? 'واحداً' : 'واحد') : 'واحدة';
|
|
82
|
+
if (n === 2) return m ? (isNom(c) ? 'اثنان' : 'اثنين') : isNom(c) ? 'اثنتان' : 'اثنتين';
|
|
83
|
+
if (n <= 10) return (m ? UNITS_FOR_MASC : UNITS_FOR_FEM)[n]!;
|
|
84
|
+
if (n === 11) return m ? 'أحد عشر' : 'إحدى عشرة';
|
|
85
|
+
if (n === 12) return m ? (isNom(c) ? 'اثنا عشر' : 'اثني عشر') : isNom(c) ? 'اثنتا عشرة' : 'اثنتي عشرة';
|
|
86
|
+
if (n < 20) return m ? `${UNITS_FOR_MASC[n - 10]} عشر` : `${UNITS_FOR_FEM[n - 10]} عشرة`;
|
|
87
|
+
|
|
88
|
+
const t = Math.floor(n / 10);
|
|
89
|
+
const u = n % 10;
|
|
90
|
+
if (u === 0) return tens(t, c);
|
|
91
|
+
const unit = u === 1 && !m ? 'إحدى' : belowHundred(u, g, c);
|
|
92
|
+
return unit + AND + tens(t, c);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** 1–999. `construct`: a following noun attaches directly (200 → مئتا / مئتي). */
|
|
96
|
+
export function belowThousand(n: number, g: Gender, c: GrammaticalCase, construct = false): string {
|
|
97
|
+
const h = Math.floor(n / 100);
|
|
98
|
+
const r = n % 100;
|
|
99
|
+
let hw = '';
|
|
100
|
+
if (h === 1) hw = 'مئة';
|
|
101
|
+
else if (h === 2) {
|
|
102
|
+
if (construct && r === 0) hw = isNom(c) ? 'مئتا' : 'مئتي';
|
|
103
|
+
else hw = isNom(c) ? 'مئتان' : 'مئتين';
|
|
104
|
+
} else if (h > 2) hw = `${HUNDREDS_STEM[h]}مئة`;
|
|
105
|
+
const rw = belowHundred(r, g, c);
|
|
106
|
+
return [hw, rw].filter(Boolean).join(AND);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Any positive integer up to 999,999,999,999 as words.
|
|
111
|
+
* `g` is the gender of the counted noun (affects only the last three digits);
|
|
112
|
+
* thousands/millions/billions are masculine nouns and always agree as such.
|
|
113
|
+
*/
|
|
114
|
+
export function integerWords(n: number, g: Gender, c: GrammaticalCase, construct = false): string {
|
|
115
|
+
const groups = [
|
|
116
|
+
Math.floor(n / 1e9) % 1000,
|
|
117
|
+
Math.floor(n / 1e6) % 1000,
|
|
118
|
+
Math.floor(n / 1e3) % 1000,
|
|
119
|
+
n % 1000,
|
|
120
|
+
];
|
|
121
|
+
let last = groups.length - 1;
|
|
122
|
+
while (last > 0 && groups[last] === 0) last--;
|
|
123
|
+
|
|
124
|
+
const parts: string[] = [];
|
|
125
|
+
groups.forEach((v, i) => {
|
|
126
|
+
if (v === 0) return;
|
|
127
|
+
const cons = construct && i === last;
|
|
128
|
+
if (i === 3) parts.push(belowThousand(v, g, c, cons));
|
|
129
|
+
else parts.push(countNoun(v, SCALES[2 - i]!, c, cons));
|
|
130
|
+
});
|
|
131
|
+
return parts.join(AND);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** `n` (≥ 1) of `noun`, applying the counted-noun rules documented at the top of this file. */
|
|
135
|
+
export function countNoun(n: number, noun: CountedNoun, c: GrammaticalCase, construct = false): string {
|
|
136
|
+
if (n <= 0) return '';
|
|
137
|
+
const r = n % 100;
|
|
138
|
+
const rest = n - r;
|
|
139
|
+
|
|
140
|
+
if (r === 0) return `${integerWords(rest, noun.gender, c, true)} ${noun.singular.genitive}`;
|
|
141
|
+
|
|
142
|
+
let tail: string;
|
|
143
|
+
if (r === 1) tail = noun.one[c];
|
|
144
|
+
else if (r === 2) tail = (construct && noun.dualConstruct ? noun.dualConstruct : noun.dual)[c];
|
|
145
|
+
else if (r <= 10) tail = `${belowHundred(r, noun.gender, c)} ${noun.plural}`;
|
|
146
|
+
// 11–99: tamyiz «ألفاً»; but followed by a noun the scale is in construct and loses tanween: أحد عشر ألفَ ريالٍ.
|
|
147
|
+
else tail = `${belowHundred(r, noun.gender, c)} ${construct ? noun.singular.nominative : noun.singular.accusative}`;
|
|
148
|
+
|
|
149
|
+
if (rest === 0) return tail;
|
|
150
|
+
// A scale with hundreds + 1 or 2 repeats the scale word, so the amount can't be read as a sum:
|
|
151
|
+
// 101,000 → مئة ألف وألف (not مئة وألف, which reads as 1,100); 102,000 → مئة ألف وألفان.
|
|
152
|
+
if (r <= 2 && SCALES.includes(noun)) return `${integerWords(rest, noun.gender, c, true)} ${noun.singular.genitive}${AND}${tail}`;
|
|
153
|
+
return integerWords(rest, noun.gender, c) + AND + tail;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** "مئة" (modern, default) or "مائة" (classic, common on Gulf cheques). */
|
|
157
|
+
export function applySpelling(text: string, spelling: 'modern' | 'classic'): string {
|
|
158
|
+
return spelling === 'classic' ? text.replaceAll('مئ', 'مائ') : text;
|
|
159
|
+
}
|