-
Notifications
You must be signed in to change notification settings - Fork 6
/
text.ts
289 lines (271 loc) · 8.81 KB
/
text.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import { Word } from "./word";
import { convertsQametsQatan } from "./utils/qametsQatan";
import { sequence } from "./utils/sequence";
import { holemWaw } from "./utils/holemWaw";
import { Syllable } from "./syllable";
import { Cluster } from "./cluster";
import { Char } from "./char";
import { splitGroup } from "./utils/regularExpressions";
/**
* options for determining syllabification that may differ according to reading traditions
*/
export interface SylOpts {
/**
* determines whether to regard the shewa under the letters שׁשׂסצנמלוי when preceded by a waw-consecutive with a missing dagesh chazaq as a _shewa na'_. If a metheg is present, the shewa is always a _shewa na'_.
*
* @defaultValue true
* @example
* ```ts
* const default = new Text("וַיְצַחֵק֙");
* default.syllables.map(syl => syl.text);
* // ["וַ", "יְ", "צַ", "חֵק֙"]
*
* const optional = new Text("וַיְצַחֵק֙", { sqnmlvy: false });
* optional.syllables.map(syl => syl.text);
* // ["וַיְ", "צַ", "חֵק֙"]
* ```
*/
sqnmlvy?: boolean;
/**
* determines whether to regard the shewa under the letters ילמ when preceded by the article and with a missing dagesh chazaq as as a _shewa na'_. If a metheg is present, the shewa is always a _shewa na'_.
*
* @defaultValue true
* @example
* ```ts
* const default = new Text("הַיְאֹ֗ר");
* default.syllables.map(syl => syl.text);
* // ["הַ", "יְ", "אֹ֗ר"]
*
* const optional = new Text("הַיְאֹ֗ר", { article: false });
* optional.syllables.map(syl => syl.text);
* // ["הַיְ", "אֹ֗ר"]
* ```
*
* @remarks
*
* results in example displayed in reverse order to mimic Hebrew writing; the rightmost value is the 0 item
*/
article?: boolean;
/**
* determines whether to regard a shewa after a long vowel (excluding waw-shureq, see {@link wawShureq}) as a _shewa na'_. If a metheg is present, the shewa is always a _shewa na'_.
*
* @defaultValue true
* @example
* ```ts
* const default = new Text("יָדְךָ");
* default.syllables.map(syl => syl.text);
* // ["יָ", "דְ", "ךָ"]
*
* const optional = new Text("יָדְךָ", { longVowels: false });
* optional.syllables.map(syl => syl.text);
* // ["יָדְ", "ךָ"]
* ```
*
* @remarks
*
* results in example displayed in reverse order to mimic Hebrew writing; the rightmost value is the 0 item
*/
longVowels?: boolean;
/**
* determines whether to regard a shewa after a vav-shureq as vocal. If a metheg is present, the shewa is always a _shewa na'_.
*
* @defaultValue true
* @example
* ```ts
* const default = new Text("וּלְמַזֵּר");
* default.syllables.map(syl => syl.text);
* // "וּ", "לְ", "מַ", "זֵּר"]
*
* const optional = new Text("וּלְמַזֵּר", { wawShureq: false });
* optional.syllables.map(syl => syl.text);
* // ["וּלְ", "מַ", "זֵּר"]
* ```
*
* @remarks
*
* results in example displayed in reverse order to mimic Hebrew writing; the rightmost value is the 0 item
*/
wawShureq?: boolean;
/**
* converts regular qamets characters to qamets qatan characters where appropriate. The former is a "long-vowel" whereas the latter is a "short-vowel."
*
* @defaultValue true
* @example
* ```ts
* const qQRegx = /\u{05C7}/u;
* const default = new Text("חָפְנִי֙");
* qQRegx.test(default.text);
* // true
*
* const optional = new Text("חָפְנִי֙", { qametsQatan: false });
* qQRegx.test(optional.text);
* // false
* ```
*/
qametsQatan?: boolean;
/**
* preset syllabification options; for more, see [syllabification](../pages/Linguistic/syllabification.html#schemas)
*
*/
schema?: Schema;
}
type Schema = "tiberian" | "traditional";
/**
* `Text` is the main exported class.
*
*/
export class Text {
#original: string;
private options: SylOpts;
/**
* `Text` requires an input string,
* and has optional arguments for syllabification,
* which can be read about in the {@page Syllabification} page
*/
constructor(text: string, options: SylOpts = {}) {
this.#original = this.validateInput(text);
this.options = this.setOptions(options);
}
private validateInput(text: string): string {
const niqqud = /[\u{05B0}-\u{05BC}\u{05C7}]/u;
if (!niqqud.test(text)) {
throw new Error("Text must contain niqqud");
}
return text;
}
private validateOptions(options: SylOpts): SylOpts {
const validOpts = ["sqnmlvy", "longVowels", "wawShureq", "qametsQatan", "article"];
for (const [k, v] of Object.entries(options)) {
if (!validOpts.includes(k)) {
throw new Error(`${k} is not a valid option`);
}
if (typeof v !== "boolean") {
throw new Error(`The value ${String(v)} is not a valid option for ${k}`);
}
}
return options;
}
private setOptions(options: SylOpts): SylOpts {
const schema = options.schema;
return schema ? this.setSchemaOptions(schema) : this.setDefaultOptions(options);
}
private setSchemaOptions(schema: Schema): SylOpts {
const schemaText = schema.toLowerCase();
if (schemaText !== "traditional" && schemaText !== "tiberian") {
throw new Error(`${schemaText} is not a valid schema`);
}
const traditionalOpts = { qametsQatan: true, sqnmlvy: true, longVowels: true, vavShureq: true };
const tiberianOpts = { qametsQatan: false, sqnmlvy: true, longVowels: false, vavShureq: false };
return schemaText === "traditional" ? traditionalOpts : tiberianOpts;
}
private setDefaultOptions(options: SylOpts): SylOpts {
options = this.validateOptions(options);
return {
sqnmlvy: options.sqnmlvy ?? true,
article: options.article ?? true,
longVowels: options.longVowels ?? true,
wawShureq: options.wawShureq ?? true,
qametsQatan: options.qametsQatan ?? true
};
}
private get normalized(): string {
return this.original.normalize("NFKD");
}
private get sanitized(): string {
const text = this.normalized.trim();
const sequencedChar = sequence(text).flat();
const sequencedText = sequencedChar.reduce((a, c) => a + c.text, "");
// split text at spaces and maqqef, spaces are added to the array as separate entries
const textArr = sequencedText.split(splitGroup).filter((group) => group);
const mapQQatan = this.options.qametsQatan ? textArr.map((word) => convertsQametsQatan(word)) : textArr;
const mapHolemWaw = mapQQatan.map((word) => holemWaw(word));
return mapHolemWaw.join("");
}
/**
* @returns the original string passed
*
* ```typescript
* const text: Text = new Text("הֲבָרֹות");
* text.original;
* // "הֲבָרֹות"
* ```
*/
get original(): string {
return this.#original;
}
/**
* @returns a string that has been decomposed, sequenced, qamets qatan patterns converted to the appropriate unicode character (U+05C7), and holem-waw sequences corrected
*
* ```typescript
* import { Text } from "havarotjs";
* const text: Text = new Text("וַתָּשָׁב");
* text.text;
* // וַתָּשׇׁב
* ```
*/
get text(): string {
return this.words.reduce((a, c) => `${a}${c.text}${c.whiteSpaceAfter ?? ""}`, "");
}
/**
* @returns a one dimensional array of Words
*
* ```typescript
* const text: Text = new Text("הֲבָרֹות");
* text.words;
* // [Word { original: "הֲבָרֹות" }]
* ```
*/
get words(): Word[] {
const split = this.sanitized.split(splitGroup);
const groups = split.filter((group) => group);
const words = groups.map((word) => new Word(word, this.options));
return words;
}
/**
* @returns a one dimensional array of Syllables
*
* ```typescript
* const text: Text = new Text("הֲבָרֹות");
* text.syllables;
* // [
* // Syllable { original: "הֲ" },
* // Syllable { original: "בָ" },
* // Syllable { original: "רֹות" }
* // ]
* ```
*/
get syllables(): Syllable[] {
return this.words.map((word) => word.syllables).flat();
}
/**
* @returns a one dimensional array of Clusters
*
* ```typescript
* const text: Text = new Text("יָד");
* text.clusters;
* // [
* // Cluster { original: "יָ" },
* // Cluster { original: "ד" }
* // ]
* ```
*/
get clusters(): Cluster[] {
return this.syllables.map((syllable) => syllable.clusters).flat();
}
/**
* @returns a one dimensional array of Chars
*
* ```typescript
* const text: Text = new Text("יָד");
* text.chars;
* // [
* // Char { original: "י" },
* // Char { original: "ָ" },
* // Char { original: "ד" }
* // ]
* ```
*/
get chars(): Char[] {
return this.clusters.map((cluster) => cluster.chars).flat();
}
}