glost 0.4.0 → 0.5.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.
Files changed (67) hide show
  1. package/README.md +253 -114
  2. package/dist/index.d.ts +29 -8
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +38 -24
  5. package/dist/index.js.map +1 -1
  6. package/dist/presets.d.ts +7 -0
  7. package/dist/presets.d.ts.map +1 -0
  8. package/dist/presets.js +7 -0
  9. package/dist/presets.js.map +1 -0
  10. package/dist/processor.d.ts +8 -0
  11. package/dist/processor.d.ts.map +1 -0
  12. package/dist/processor.js +7 -0
  13. package/dist/processor.js.map +1 -0
  14. package/dist/registry.d.ts +8 -0
  15. package/dist/registry.d.ts.map +1 -0
  16. package/dist/registry.js +7 -0
  17. package/dist/registry.js.map +1 -0
  18. package/package.json +27 -40
  19. package/src/index.ts +126 -69
  20. package/src/presets.ts +18 -0
  21. package/src/processor.ts +24 -0
  22. package/src/registry.ts +23 -0
  23. package/tsconfig.json +6 -3
  24. package/CHANGELOG.md +0 -296
  25. package/dist/cli/migrate.d.ts +0 -8
  26. package/dist/cli/migrate.d.ts.map +0 -1
  27. package/dist/cli/migrate.js +0 -229
  28. package/dist/cli/migrate.js.map +0 -1
  29. package/dist/errors.d.ts +0 -168
  30. package/dist/errors.d.ts.map +0 -1
  31. package/dist/errors.js +0 -300
  32. package/dist/errors.js.map +0 -1
  33. package/dist/example.d.ts +0 -10
  34. package/dist/example.d.ts.map +0 -1
  35. package/dist/example.js +0 -138
  36. package/dist/example.js.map +0 -1
  37. package/dist/guards.d.ts +0 -103
  38. package/dist/guards.d.ts.map +0 -1
  39. package/dist/guards.js +0 -264
  40. package/dist/guards.js.map +0 -1
  41. package/dist/nodes.d.ts +0 -163
  42. package/dist/nodes.d.ts.map +0 -1
  43. package/dist/nodes.js +0 -185
  44. package/dist/nodes.js.map +0 -1
  45. package/dist/types.d.ts +0 -395
  46. package/dist/types.d.ts.map +0 -1
  47. package/dist/types.js +0 -6
  48. package/dist/types.js.map +0 -1
  49. package/dist/utils.d.ts +0 -203
  50. package/dist/utils.d.ts.map +0 -1
  51. package/dist/utils.js +0 -497
  52. package/dist/utils.js.map +0 -1
  53. package/dist/validators.d.ts +0 -1876
  54. package/dist/validators.d.ts.map +0 -1
  55. package/dist/validators.js +0 -302
  56. package/dist/validators.js.map +0 -1
  57. package/src/__tests__/README.md +0 -20
  58. package/src/__tests__/example.test.ts +0 -43
  59. package/src/__tests__/example.ts +0 -186
  60. package/src/__tests__/mock-data.ts +0 -624
  61. package/src/cli/migrate.ts +0 -294
  62. package/src/errors.ts +0 -394
  63. package/src/guards.ts +0 -341
  64. package/src/nodes.ts +0 -326
  65. package/src/types.ts +0 -581
  66. package/src/utils.ts +0 -652
  67. package/src/validators.ts +0 -336
package/src/utils.ts DELETED
@@ -1,652 +0,0 @@
1
- import { is as isNode } from "unist-util-is";
2
- import { visit } from "unist-util-visit";
3
-
4
- import type {
5
- LanguageCode,
6
- GLOSTCharacter,
7
- GLOSTClause,
8
- GLOSTNode,
9
- GLOSTParagraph,
10
- GLOSTPhrase,
11
- GLOSTRoot,
12
- GLOSTSentence,
13
- GLOSTSyllable,
14
- GLOSTWord,
15
- TranscriptionSystem,
16
- } from "./types.js";
17
-
18
- // ============================================================================
19
- // BCP-47 Language Tag Utilities
20
- // ============================================================================
21
-
22
- /**
23
- * Parse a BCP-47 language tag into its components
24
- * Format: language[-script][-region][-variant]
25
- */
26
- export function parseLanguageTag(tag: string): {
27
- language: string;
28
- script?: string;
29
- region?: string;
30
- variant?: string;
31
- fullTag: string;
32
- } {
33
- const parts = tag.split("-");
34
- const result = {
35
- language: parts[0] || "",
36
- script: undefined as string | undefined,
37
- region: undefined as string | undefined,
38
- variant: undefined as string | undefined,
39
- fullTag: tag,
40
- };
41
-
42
- if (parts.length >= 2) {
43
- // Check if second part is a script (4 letters) or region (2-3 letters)
44
- if (parts[1] && parts[1].length === 4 && /^[A-Za-z]{4}$/.test(parts[1])) {
45
- result.script = parts[1];
46
- if (parts.length >= 3 && parts[2]) {
47
- result.region = parts[2];
48
- if (parts.length >= 4) {
49
- result.variant = parts.slice(3).join("-");
50
- }
51
- }
52
- } else {
53
- // Second part is likely a region
54
- if (parts[1]) {
55
- result.region = parts[1];
56
- if (parts.length >= 3) {
57
- result.variant = parts.slice(2).join("-");
58
- }
59
- }
60
- }
61
- }
62
-
63
- return result;
64
- }
65
-
66
- /**
67
- * Get the base language from a BCP-47 tag
68
- * Examples: "en-US" -> "en", "zh-CN" -> "zh"
69
- */
70
- export function getBaseLanguage(tag: string): string {
71
- const parts = tag.split("-");
72
- return parts[0] || tag;
73
- }
74
-
75
- /**
76
- * Check if two language tags are compatible (same base language)
77
- * Examples: "en-US" and "en-GB" are compatible
78
- */
79
- export function areLanguagesCompatible(tag1: string, tag2: string): boolean {
80
- return getBaseLanguage(tag1) === getBaseLanguage(tag2);
81
- }
82
-
83
- /**
84
- * Find the best matching language tag from available options
85
- * Prioritizes exact matches, then region matches, then base language matches
86
- */
87
- export function findBestLanguageMatch(
88
- target: string,
89
- available: string[],
90
- ): string | null {
91
- if (available.includes(target)) {
92
- return target;
93
- }
94
-
95
- const targetParts = parseLanguageTag(target);
96
-
97
- // Try to find region-specific matches
98
- for (const option of available) {
99
- const optionParts = parseLanguageTag(option);
100
- if (
101
- optionParts.language === targetParts.language &&
102
- optionParts.region === targetParts.region
103
- ) {
104
- return option;
105
- }
106
- }
107
-
108
- // Try to find base language matches
109
- for (const option of available) {
110
- const optionParts = parseLanguageTag(option);
111
- if (optionParts.language === targetParts.language) {
112
- return option;
113
- }
114
- }
115
-
116
- return null;
117
- }
118
-
119
- /**
120
- * Get a fallback language tag when the exact one isn't available
121
- * Examples: "en-US" -> "en", "zh-CN" -> "zh"
122
- */
123
- export function getLanguageFallback(tag: string): string {
124
- const parts = parseLanguageTag(tag);
125
- return parts.language;
126
- }
127
-
128
- /**
129
- * Normalize a language tag to standard format
130
- * Converts to lowercase and ensures proper formatting
131
- */
132
- export function normalizeLanguageTag(tag: string): string {
133
- const parts = tag.split("-");
134
- const language = parts[0]?.toLowerCase() || "";
135
-
136
- if (parts.length === 1) {
137
- return language;
138
- }
139
-
140
- // Handle script (4 letters, title case)
141
- if (parts[1] && parts[1].length === 4) {
142
- const script = parts[1]
143
- .toLowerCase()
144
- .replace(/\b\w/g, (l) => l.toUpperCase());
145
-
146
- if (parts.length === 2) {
147
- return `${language}-${script}`;
148
- }
149
-
150
- // Handle region (2-3 letters, uppercase)
151
- const region = parts[2]?.toUpperCase() || "";
152
-
153
- if (parts.length === 3) {
154
- return `${language}-${script}-${region}`;
155
- }
156
-
157
- // Handle variants (lowercase)
158
- const variants = parts.slice(3).join("-").toLowerCase();
159
- return `${language}-${script}-${region}-${variants}`;
160
- } else {
161
- // No script, just language-region
162
- const region = parts[1]?.toUpperCase() || "";
163
-
164
- if (parts.length === 2) {
165
- return `${language}-${region}`;
166
- }
167
-
168
- // Handle variants
169
- const variants = parts.slice(2).join("-").toLowerCase();
170
- return `${language}-${region}-${variants}`;
171
- }
172
- }
173
-
174
- /**
175
- * Check if a language tag is valid BCP-47 format
176
- */
177
- export function isValidLanguageTag(tag: string): boolean {
178
- // Basic BCP-47 validation
179
- const bcp47Pattern =
180
- /^[a-z]{2,3}(-[A-Za-z]{4})?(-[A-Za-z]{2,3})?(-[A-Za-z0-9]+)*$/;
181
- return bcp47Pattern.test(tag);
182
- }
183
-
184
- // ============================================================================
185
- // Enhanced Tree Traversal Utilities (using unist-util-visit)
186
- // ============================================================================
187
-
188
- /**
189
- * Get all word nodes from an GLOST tree
190
- */
191
- export function getAllWords(node: GLOSTNode): GLOSTWord[] {
192
- const words: GLOSTWord[] = [];
193
-
194
- visit(node, "WordNode", (wordNode) => {
195
- if (isGLOSTWord(wordNode)) {
196
- words.push(wordNode);
197
- }
198
- });
199
-
200
- return words;
201
- }
202
-
203
- /**
204
- * Get all sentence nodes from an GLOST tree
205
- */
206
- export function getAllSentences(node: GLOSTNode): GLOSTSentence[] {
207
- const sentences: GLOSTSentence[] = [];
208
-
209
- visit(node, "SentenceNode", (sentenceNode) => {
210
- if (isGLOSTSentence(sentenceNode)) {
211
- sentences.push(sentenceNode);
212
- }
213
- });
214
-
215
- return sentences;
216
- }
217
-
218
- /**
219
- * Get all paragraph nodes from an GLOST tree
220
- */
221
- export function getAllParagraphs(node: GLOSTNode): GLOSTParagraph[] {
222
- const paragraphs: GLOSTParagraph[] = [];
223
-
224
- visit(node, "ParagraphNode", (paragraphNode) => {
225
- if (isGLOSTParagraph(paragraphNode)) {
226
- paragraphs.push(paragraphNode);
227
- }
228
- });
229
-
230
- return paragraphs;
231
- }
232
-
233
- /**
234
- * Get all clause nodes from an GLOST tree
235
- */
236
- export function getAllClauses(node: GLOSTNode): GLOSTClause[] {
237
- const clauses: GLOSTClause[] = [];
238
-
239
- visit(node, "ClauseNode", (clauseNode) => {
240
- if (isGLOSTClause(clauseNode)) {
241
- clauses.push(clauseNode);
242
- }
243
- });
244
-
245
- return clauses;
246
- }
247
-
248
- /**
249
- * Get all phrase nodes from an GLOST tree
250
- */
251
- export function getAllPhrases(node: GLOSTNode): GLOSTPhrase[] {
252
- const phrases: GLOSTPhrase[] = [];
253
-
254
- visit(node, "PhraseNode", (phraseNode) => {
255
- if (isGLOSTPhrase(phraseNode)) {
256
- phrases.push(phraseNode);
257
- }
258
- });
259
-
260
- return phrases;
261
- }
262
-
263
- /**
264
- * Get all syllable nodes from an GLOST tree
265
- */
266
- export function getAllSyllables(node: GLOSTNode): GLOSTSyllable[] {
267
- const syllables: GLOSTSyllable[] = [];
268
-
269
- visit(node, "SyllableNode", (syllableNode) => {
270
- if (isGLOSTSyllable(syllableNode)) {
271
- syllables.push(syllableNode);
272
- }
273
- });
274
-
275
- return syllables;
276
- }
277
-
278
- /**
279
- * Get all character nodes from an GLOST tree
280
- */
281
- export function getAllCharacters(node: GLOSTNode): GLOSTCharacter[] {
282
- const characters: GLOSTCharacter[] = [];
283
-
284
- visit(node, "CharacterNode", (characterNode) => {
285
- if (isGLOSTCharacter(characterNode)) {
286
- characters.push(characterNode);
287
- }
288
- });
289
-
290
- return characters;
291
- }
292
-
293
- /**
294
- * Find nodes by type with better typing
295
- */
296
- export function findNodesByType<T extends GLOSTNode>(
297
- node: GLOSTNode,
298
- type: string,
299
- ): T[] {
300
- const results: T[] = [];
301
-
302
- visit(node, type, (foundNode) => {
303
- results.push(foundNode as T);
304
- });
305
-
306
- return results;
307
- }
308
-
309
- // ============================================================================
310
- // New Utilities for Transcription Components
311
- // ============================================================================
312
-
313
- /**
314
- * Get all words from a document with proper typing
315
- */
316
- export function getWordsFromDocument(doc: GLOSTRoot): GLOSTWord[] {
317
- return getAllWords(doc);
318
- }
319
-
320
- /**
321
- * Get the first sentence from a document
322
- */
323
- export function getFirstSentence(doc: GLOSTRoot): GLOSTSentence | null {
324
- const paragraphs = getAllParagraphs(doc);
325
- if (paragraphs.length === 0) return null;
326
-
327
- const firstParagraph = paragraphs[0];
328
- if (!firstParagraph) return null;
329
-
330
- const sentences = getAllSentences(firstParagraph);
331
- if (sentences.length === 0) return null;
332
-
333
- const firstSentence = sentences[0];
334
- return firstSentence || null;
335
- }
336
-
337
- /**
338
- * Get words from a specific sentence
339
- */
340
- export function getWordsFromSentence(sentence: GLOSTSentence): GLOSTWord[] {
341
- return getAllWords(sentence);
342
- }
343
-
344
- /**
345
- * Get words from a specific paragraph
346
- */
347
- export function getWordsFromParagraph(paragraph: GLOSTParagraph): GLOSTWord[] {
348
- const words: GLOSTWord[] = [];
349
-
350
- visit(paragraph, "WordNode", (wordNode) => {
351
- if (isGLOSTWord(wordNode)) {
352
- words.push(wordNode);
353
- }
354
- });
355
-
356
- return words;
357
- }
358
-
359
- /**
360
- * Find word nodes with specific language
361
- */
362
- export function findWordsByLanguage(
363
- node: GLOSTNode,
364
- lang: LanguageCode,
365
- ): GLOSTWord[] {
366
- const words = getAllWords(node);
367
- return words.filter((word) => word.lang === lang);
368
- }
369
-
370
- /**
371
- * Find word nodes with specific transcription system
372
- */
373
- export function findWordsByTranscriptionSystem(
374
- node: GLOSTNode,
375
- system: TranscriptionSystem,
376
- ): GLOSTWord[] {
377
- const words = getAllWords(node);
378
- return words.filter(
379
- (word) => word.transcription && word.transcription[system],
380
- );
381
- }
382
-
383
- // ============================================================================
384
- // Enhanced Type Guards (using unist-util-is)
385
- // ============================================================================
386
-
387
- /**
388
- * Enhanced type guards for the new GLOST types
389
- */
390
- export function isGLOSTWord(node: any): node is GLOSTWord {
391
- return (
392
- isNode(node, "WordNode") && "transcription" in node && "metadata" in node
393
- );
394
- }
395
-
396
- export function isGLOSTSentence(node: any): node is GLOSTSentence {
397
- return (
398
- isNode(node, "SentenceNode") &&
399
- "lang" in node &&
400
- "script" in node &&
401
- "originalText" in node
402
- );
403
- }
404
-
405
- export function isGLOSTParagraph(node: any): node is GLOSTParagraph {
406
- return isNode(node, "ParagraphNode");
407
- }
408
-
409
- export function isGLOSTRoot(node: any): node is GLOSTRoot {
410
- return isNode(node, "Root") && "lang" in node && "script" in node;
411
- }
412
-
413
- /**
414
- * Type guard for GLOSTClause nodes
415
- */
416
- export function isGLOSTClause(node: any): node is GLOSTClause {
417
- return isNode(node, "ClauseNode") && "clauseType" in node;
418
- }
419
-
420
- /**
421
- * Type guard for GLOSTPhrase nodes
422
- */
423
- export function isGLOSTPhrase(node: any): node is GLOSTPhrase {
424
- return isNode(node, "PhraseNode") && "phraseType" in node;
425
- }
426
-
427
- /**
428
- * Type guard for GLOSTSyllable nodes
429
- */
430
- export function isGLOSTSyllable(node: any): node is GLOSTSyllable {
431
- return isNode(node, "SyllableNode") && "structure" in node;
432
- }
433
-
434
- /**
435
- * Type guard for GLOSTCharacter nodes
436
- */
437
- export function isGLOSTCharacter(node: any): node is GLOSTCharacter {
438
- return isNode(node, "CharacterNode") && "value" in node;
439
- }
440
-
441
- // ============================================================================
442
- // Utility Functions for Transcription Components
443
- // ============================================================================
444
-
445
- /**
446
- * Extract text value from a word node
447
- */
448
- export function getWordText(word: GLOSTWord): string {
449
- const textNode = word.children.find((child) => child.type === "TextNode");
450
- return textNode?.value ?? "";
451
- }
452
-
453
- /**
454
- * Get transcription for a specific system
455
- */
456
- export function getWordTranscription(
457
- word: GLOSTWord,
458
- system: TranscriptionSystem,
459
- ): string | null {
460
- return word.transcription[system]?.text ?? null;
461
- }
462
-
463
- /**
464
- * Check if a word has transcription for a specific system
465
- */
466
- export function hasWordTranscription(
467
- word: GLOSTWord,
468
- system: TranscriptionSystem,
469
- ): boolean {
470
- return system in word.transcription && !!word.transcription[system]?.text;
471
- }
472
-
473
- /**
474
- * Get word translation for a specific language
475
- * @param word - The word node
476
- * @param language - Target language code (default: "en-US")
477
- * @returns Translation string or empty string if not found
478
- */
479
- export function getWordTranslation(
480
- word: GLOSTWord,
481
- language = "en-US",
482
- ): string {
483
- // Check extras.translations first (preferred format)
484
- if (word.extras?.translations?.[language]) {
485
- return word.extras.translations[language];
486
- }
487
- // Also check short language code (e.g., "en" for "en-US")
488
- const shortLang = language.split("-")[0];
489
- if (shortLang && word.extras?.translations?.[shortLang]) {
490
- return word.extras.translations[shortLang];
491
- }
492
- return "";
493
- }
494
-
495
- /**
496
- * Get word meaning/definition
497
- * @deprecated Use getWordTranslation for multi-language support.
498
- * This function is kept for backward compatibility.
499
- */
500
- export function getWordMeaning(word: GLOSTWord): string {
501
- // Priority: extras.translations (preferred) > metadata.meaning (deprecated) > shortDefinition (deprecated)
502
- const translation = getWordTranslation(word, "en-US");
503
- if (translation) return translation;
504
-
505
- return (
506
- word.metadata?.meaning ?? word.shortDefinition ?? word.fullDefinition ?? ""
507
- );
508
- }
509
-
510
- /**
511
- * Get word part of speech
512
- */
513
- export function getWordPartOfSpeech(word: GLOSTWord): string {
514
- return word.metadata?.partOfSpeech ?? "";
515
- }
516
-
517
- /**
518
- * Get word difficulty
519
- */
520
- export function getWordDifficulty(word: GLOSTWord): string | number {
521
- return word.difficulty ?? word.extras?.metadata?.difficulty ?? "";
522
- }
523
-
524
- /**
525
- * Get sentence translation
526
- */
527
- export function getSentenceTranslation(
528
- sentence: GLOSTSentence,
529
- language = "en",
530
- ): string | null {
531
- if (sentence.extras?.translations?.[language]) {
532
- return sentence.extras.translations[language];
533
- }
534
-
535
- // Fallback: build from word meanings
536
- const words = getWordsFromSentence(sentence);
537
- const wordMeanings = words
538
- .map((word) => getWordMeaning(word))
539
- .filter(Boolean)
540
- .join(" ");
541
-
542
- return wordMeanings || null;
543
- }
544
-
545
- // ============================================================================
546
- // Content Statistics Utilities
547
- // ============================================================================
548
-
549
- /**
550
- * Generic paragraph structure for word count calculation
551
- * This interface allows converting external paragraph structures to GLOST format
552
- */
553
- export type ParagraphLike = {
554
- sentences: Array<{
555
- sentence: string;
556
- translation?: string;
557
- }>;
558
- };
559
-
560
- /**
561
- * Convert a paragraph-like structure to GLOST format for word count calculation
562
- * This is a minimal adapter that only converts what's needed for word counting
563
- *
564
- * @param paragraph - Paragraph structure with sentences containing text and optional translations
565
- * @returns GLOST paragraph node
566
- *
567
- * @example
568
- * ```ts
569
- * const paragraph = {
570
- * sentences: [
571
- * { sentence: "Hello", translation: "สวัสดี" },
572
- * { sentence: "World", translation: "โลก" }
573
- * ]
574
- * };
575
- * const mtstParagraph = adaptParagraphLikeToGLOST(paragraph);
576
- * const wordCount = getGLOSTWordCount(mtstParagraph);
577
- * ```
578
- */
579
- export function adaptParagraphLikeToGLOST(
580
- paragraph: ParagraphLike,
581
- ): GLOSTParagraph {
582
- return {
583
- type: "ParagraphNode",
584
- children: paragraph.sentences.map((sentence) => ({
585
- type: "SentenceNode",
586
- lang: "unknown",
587
- script: "unknown",
588
- originalText: sentence.sentence,
589
- children: [],
590
- extras: {
591
- translations: sentence.translation
592
- ? { en: sentence.translation }
593
- : undefined,
594
- },
595
- })),
596
- };
597
- }
598
-
599
- /**
600
- * Calculate word count from GLOST content
601
- * Counts words from sentence translations or original text
602
- *
603
- * @param content - GLOST paragraph, sentence, or root node
604
- * @param language - Optional language code for translation preference (default: 'en')
605
- * @returns Word count as a number, or undefined if content is empty
606
- *
607
- * @example
608
- * ```ts
609
- * const wordCount = getGLOSTWordCount(paragraph, 'en');
610
- * // Returns: 245
611
- * ```
612
- */
613
- export function getGLOSTWordCount(
614
- content: GLOSTParagraph | GLOSTSentence | GLOSTRoot,
615
- language = "en",
616
- ): number | undefined {
617
- if (isGLOSTParagraph(content)) {
618
- const sentences = getAllSentences(content);
619
- if (sentences.length === 0) {
620
- return undefined;
621
- }
622
-
623
- return sentences.reduce((count, sentence) => {
624
- const translation = getSentenceTranslation(sentence, language);
625
- const text = translation || sentence.originalText || "";
626
- return count + text.split(/\s+/).filter((word) => word.length > 0).length;
627
- }, 0);
628
- }
629
-
630
- if (isGLOSTSentence(content)) {
631
- const translation = getSentenceTranslation(content, language);
632
- const text = translation || content.originalText || "";
633
- if (!text) {
634
- return undefined;
635
- }
636
- return text.split(/\s+/).filter((word) => word.length > 0).length;
637
- }
638
-
639
- if (isGLOSTRoot(content)) {
640
- const paragraphs = getAllParagraphs(content);
641
- if (paragraphs.length === 0) {
642
- return undefined;
643
- }
644
-
645
- return paragraphs.reduce((count, paragraph) => {
646
- const paragraphCount = getGLOSTWordCount(paragraph, language);
647
- return count + (paragraphCount ?? 0);
648
- }, 0);
649
- }
650
-
651
- return undefined;
652
- }