@met4citizen/headtts 0.2.1 → 0.3.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
@@ -56,7 +56,7 @@ const headtts = new HeadTTS({
56
56
  ```
57
57
 
58
58
  <details>
59
- <summary>Click to see the options.</summary>
59
+ <summary>CLICK HERE to see all the options.</summary>
60
60
 
61
61
  Option | Description | Default value
62
62
  --- | --- | ---
@@ -122,7 +122,7 @@ headtts.onmessage = (message) => {
122
122
  ```
123
123
 
124
124
  <details>
125
- <summary>Click to see all the available class events.</summary>
125
+ <summary>CLICK HERE to see all the available class events.</summary>
126
126
 
127
127
  Event handler | Description
128
128
  --- | ---
@@ -169,7 +169,7 @@ try {
169
169
  ```
170
170
 
171
171
  <details>
172
- <summary>Click to see all the class method.</summary>
172
+ <summary>CLICK HERE to see all the class method.</summary>
173
173
 
174
174
  Method | Description
175
175
  --- | ---
@@ -181,7 +181,7 @@ Method | Description
181
181
  </details>
182
182
 
183
183
  <details>
184
- <summary>Click to see the available input types for synthesize request.</summary>
184
+ <summary>CLICK HERE to see the available input types for synthesize request.</summary>
185
185
 
186
186
  The `input` parameter can be a string or, alternatively, an array
187
187
  of strings or inputs items.
@@ -238,7 +238,7 @@ npm start
238
238
  ```
239
239
 
240
240
  <details>
241
- <summary>Click to see the command line options.</summary>
241
+ <summary>CLICK HERE to see the command line options.</summary>
242
242
 
243
243
  Option|Description|Default
244
244
  ---|---|---
@@ -254,7 +254,7 @@ node ./modules/headtts-node.mjs --trace 16
254
254
  </details>
255
255
 
256
256
  <details>
257
- <summary>Click to see the configuration file options</summary>
257
+ <summary>CLICK HERE to see the configuration file options</summary>
258
258
 
259
259
  Property|Description|Default
260
260
  ---|---|---
@@ -523,7 +523,7 @@ solutions use streaming, some not.
523
523
  audio. If RTF < 1, synthesis is faster than real-time (i.e., good).
524
524
 
525
525
  <details>
526
- <summary>Click here to see the test setup.</summary>
526
+ <summary>CLICK HERE here to see the test setup.</summary>
527
527
 
528
528
  **Test setup**: Macbook Air M2 laptop, 8 cores, 16GB memory,
529
529
  macOS Sequoia 15.3.2, Metal2 GPU 10 cores, 300/50 Mbit/s internet connection.
@@ -109,43 +109,47 @@ class HeadTTS {
109
109
  }
110
110
 
111
111
  /**
112
- * Divide the given text into parts
113
- *
114
- * @param {string} text Text
115
- * @param {number} [initialLen=0] Initial length
116
- * @return {string[]} Array of text parts.
117
- */
118
- divideToParts(text, initialLen=0) {
112
+ * Divide the given text into parts, handling emojis and complex Unicode correctly.
113
+ *
114
+ * @param {string} text Text
115
+ * @param {number} [initialLen=0] Initial length
116
+ * @return {string[]} Array of text parts.
117
+ */
118
+ divideToParts(text, initialLen = 0) {
119
+ const segmenter = new Intl.Segmenter('en', { granularity: 'grapheme' });
120
+ const letters = Array.from(segmenter.segment(text), s => s.segment);
121
+ const textLen = letters.length;
122
+
119
123
  const parts = [];
120
- const textLen = text.length;
121
- const letters = [...text];
122
124
  let lastSpace = 0;
123
125
  let part = "";
124
- for( let i=0; i<textLen; i++ ) {
126
+
127
+ for (let i = 0; i < textLen; i++) {
125
128
  const letter = letters[i];
126
- const isLast = i === (textLen-1);
127
- const letterTwo = isLast ? null : (letter + letters[i+1]);
128
- const isDivider = isLast ? false : this.dividers.hasOwnProperty(letterTwo);
129
- if ( letter === ' ' ) lastSpace = i;
130
- const isMax = i >= (this.settings.splitLength - initialLen);
129
+ const isLast = i === textLen - 1;
130
+ const nextLetter = isLast ? null : letters[i + 1];
131
+ const letterTwo = isLast ? null : letter + nextLetter;
132
+ const isDivider = isLast ? false : this.dividers?.hasOwnProperty(letterTwo);
131
133
 
134
+ if (letter === " ") lastSpace = i;
135
+
136
+ const isMax = i >= this.settings.splitLength - initialLen;
132
137
  part += letter;
133
138
 
134
- let s = null;
135
- if ( isMax ) {
136
- if ( lastSpace === 0 ) lastSpace = i;
137
- parts.push( part.slice(0,lastSpace) );
138
- part = part.slice(lastSpace+1);
139
+ if (isMax) {
140
+ if (lastSpace === 0) lastSpace = i;
141
+ parts.push(letters.slice(i - part.length + 1, lastSpace + 1).join(""));
142
+ part = letters.slice(lastSpace + 1, i + 1).join("");
139
143
  lastSpace = 0;
140
144
  initialLen = 0;
141
- } else if ( isLast || isDivider ) {
142
- parts.push( part );
145
+ } else if (isLast || isDivider) {
146
+ parts.push(part);
143
147
  part = "";
144
148
  lastSpace = 0;
145
149
  initialLen = 0;
146
150
  }
147
-
148
151
  }
152
+
149
153
  return parts;
150
154
  }
151
155
 
@@ -300,6 +300,19 @@ class Language extends LanguageBase {
300
300
  this.ones = ['','ONE','TWO','THREE','FOUR','FIVE','SIX','SEVEN','EIGHT','NINE'];
301
301
  this.tens = ['','','TWENTY','THIRTY','FORTY','FIFTY','SIXTY','SEVENTY','EIGHTY','NINETY'];
302
302
  this.teens = ['TEN','ELEVEN','TWELVE','THIRTEEN','FOURTEEN','FIFTEEN','SIXTEEN','SEVENTEEN','EIGHTEEN','NINETEEN'];
303
+ this.decades = {
304
+ 20: "TWENTIES", 30: "THIRTIES", 40: "FORTIES", 50: "FIFTIES",
305
+ 60: "SIXTIES", 70: "SEVENTIES", 80: "EIGHTIES", 90: "NINETIES"
306
+ };
307
+ this.ordinals = {
308
+ 1: "FIRST", 2: "SECOND", 3: "THIRD", 4: "FOURTH", 5: "FIFTH",
309
+ 6: "SIXTH", 7: "SEVENTH", 8: "EIGHTH", 9: "NINTH", 10: "TENTH",
310
+ 11: "ELEVENTH", 12: "TWELFTH", 13: "THIRTEENTH", 14: "FOURTEENTH",
311
+ 15: "FIFTEENTH", 16: "SIXTEENTH", 17: "SEVENTEETH", 18: "EIGHTEENTH",
312
+ 19: "NINETEENTH", 20: "TWENTIETH", 30: "THIRTIETH", 40: "FORTIETH",
313
+ 50: "FIFTIETH", 60: "SIXTIETH",70: "SEVENTIETH", 80: "EIGHTIETH",
314
+ 90: "NINETIETH"
315
+ };
303
316
 
304
317
  // Date & Time
305
318
  this.months = [
@@ -421,28 +434,104 @@ class Language extends LanguageBase {
421
434
  * Convert number to words. Try to decide how to read it.
422
435
  *
423
436
  * @param {number|string} num Number
437
+ * @param {boolean} [isNotSpecial=false] If true, this is not a special number (e.g. year, zip code)
424
438
  * @return {string} String
425
439
  */
426
- convertNumberToWords(num) {
440
+ convertNumberToWords(num, isNotSpecial=false) {
427
441
  const n = parseFloat(num);
428
442
  if (num == "0") {
429
443
  return "ZERO";
430
444
  } else if (num < 0 ) {
431
- return "MINUS " + this.convertNumberToWords( Math.abs(num).toString() ).trim();
445
+ return "MINUS " + this.convertNumberToWords( Math.abs(num).toString(), isNotSpecial ).trim();
432
446
  } else if ( n && !Number.isInteger(n) ) {
433
447
  const parts = n.toString().split('.');
434
- return this.convertNumberToWords(parts[0]).trim() + " POINT " + this.convertDigitByDigit(parts[1]).trim();
448
+ return this.convertNumberToWords(parts[0], isNotSpecial).trim() + " POINT " + this.convertDigitByDigit(parts[1]).trim();
435
449
  } else if(num.toString().startsWith('0')){
436
450
  return this.convertDigitByDigit(num).trim();
437
- } else if ((num<1000 && num>99 && (num % 100) !== 0) || (num>10000&&num<1000000)) { //read area and zip codes digit by digit
451
+ } else if (!isNotSpecial && ((num<1000 && num>99 && (num % 100) !== 0) || (num>10000&&num<1000000))) { //read area and zip codes digit by digit
438
452
  return this.convertDigitByDigit(num).trim();
439
- } else if ((num > 1000 && num < 2000)||(num>2009&&num<3000)) { //read years as two sets of two digits
453
+ } else if (!isNotSpecial && ((num > 1000 && num < 2000)||(num>2009&&num<3000))) { //read years as two sets of two digits
440
454
  return (num % 100 != 0 ? this.convertSetsOfTwo(num).trim() : this.convertTens(num.toString().substring(0, 2)).trim() + " HUNDRED");
441
455
  } else {
442
456
  return this.convertMillions(num).trim();
443
457
  }
444
458
  }
445
459
 
460
+ /**
461
+ * Expand decade to text.
462
+ *
463
+ * @param {string} decade Decade
464
+ * @return {string} Normalized text
465
+ */
466
+ convertDecade(decade) {
467
+ const num = parseInt(decade);
468
+ const isShort = !isNaN(num) && decade.length === 2;
469
+ const isLong = !isNaN(num) && decade.length > 2 && num > 0 && num <= 3000;
470
+ const thousands = (isLong && (num % 1000) === 0 ) ? Math.floor(num / 1000) : null;
471
+ const hundreds = (isLong && !thousands) ? Math.floor(num / 100) : null;
472
+ const tens = (isShort || isLong) ? Math.floor((num % 100) / 10) * 10 : null;
473
+
474
+ let s = [];
475
+ if ( thousands ) {
476
+ s.push( this.convertNumberToWords(thousands).trim(), "THOUSANDS" );
477
+ } else {
478
+ if ( hundreds ) {
479
+ s.push( this.convertNumberToWords(hundreds).trim() );
480
+ }
481
+ if ( tens ) {
482
+ s.push( this.decades[tens] || (this.convertNumberToWords(tens).trim() + 'S') );
483
+ } else if ( hundreds ) {
484
+ s.push( "HUNDREDS" );
485
+ } else {
486
+ s.push( decade );
487
+ }
488
+ }
489
+
490
+ return s.join(" ");
491
+ }
492
+
493
+ /**
494
+ * Convert ordinal number to text.
495
+ *
496
+ * @param {number} num Ordinal number
497
+ * @return {string} Normalized text
498
+ */
499
+ convertOrdinal(num) {
500
+
501
+ // Return immediately, if we have the number in our map
502
+ if ( this.ordinals.hasOwnProperty(num) ) {
503
+ return this.ordinals[num];
504
+ }
505
+
506
+ const hundreds = Math.floor(num / 100);
507
+ const tens = Math.floor( (num % 100) / 10) * 10;
508
+ const ones = num % 10;
509
+
510
+ let s = [];
511
+ if ( hundreds ) {
512
+ s.push( this.convertNumberToWords(hundreds).trim() );
513
+ if ( tens || ones ) {
514
+ s.push( "HUNDRED" );
515
+ } else {
516
+ s.push( "HUNDREDTH" );
517
+ }
518
+ }
519
+
520
+ if ( tens ) {
521
+ if ( ones ) {
522
+ s.push( this.convertNumberToWords(tens).trim() );
523
+ } else {
524
+ s.push( this.ordinals[tens] );
525
+ }
526
+ }
527
+
528
+ if ( ones ) {
529
+ s.push( this.ordinals[ones] );
530
+ }
531
+
532
+ return s.join(" ");
533
+ }
534
+
446
535
  /**
447
536
  * Set the `text` to be spoken by analysing the part content.
448
537
  *
@@ -459,14 +548,48 @@ class Language extends LanguageBase {
459
548
  switch( part.type ) {
460
549
 
461
550
  case "text":
462
- // Check if this is actually a number
463
- const s = part.text;
464
- if ( s ) {
465
- const num = s.replace(/,/g, '').trim();
466
- if ( !isNaN(num) && !isNaN(parseFloat(num)) ) {
467
- part.text = this.convertNumberToWords(num) + " ";
468
- }
551
+
552
+ // Process numbers, if any
553
+ if ( /\d/.test(part.text) ) {
554
+
555
+ // Decades: 70s, 1970s -> SEVENTIES, NINETEEN SEVENTIES
556
+ part.text = part.text.replace(/\b(\d{2,4})[''']?\s?[sS](?=\s|[.,!?;:]|$)/g, (match, decade) => {
557
+ const result = this.convertDecade(decade);
558
+ return result === decade ? match : result;
559
+ });
560
+
561
+ // Ordinals: 1st, 22nd -> FIRST, TWENTY SECOND
562
+ part.text = part.text.replace(/\b(\d+)\s*(st|nd|rd|th)(?=\s|[.,!?;:]|$)/gi, (match, number) => {
563
+ return this.convertOrdinal(Number(number));
564
+ });
565
+
566
+ // Handle mixed alphanumeric sequences
567
+ part.text = part.text.replace(/\b(\w*?)(\d+)([A-Za-z]+)\b/g, (match, prefix, numbers, letters) => {
568
+ const processedNumber = this.convertNumberToWords(numbers);
569
+ return `${prefix}${processedNumber} ${letters}`;
570
+ }).replace(/\b([A-Za-z]+)(\d+)(\w*?)\b/g, (match, letters, numbers, suffix) => {
571
+ const processedNumber = this.convertNumberToWords(numbers);
572
+ return `${letters} ${processedNumber}${suffix}`;
573
+ });
574
+
575
+ // Process the remaining numbers
576
+ // Note: If there are thousand separators or a decimal part, we know
577
+ // that this is not a special number e.g. phone number, zip code or year
578
+ part.text = part.text.replace(/-?(?:\d{1,3}(?:,\d{3})+|\d+)(\.\d+)?/g, (match, decimal) => {
579
+ let s = match;
580
+ let isNotSpecial = false;
581
+ if ( /,/.test(s) ) {
582
+ s = s.replace( /,/g, "" );
583
+ isNotSpecial = true;
584
+ }
585
+ if ( decimal ) {
586
+ isNotSpecial = true;
587
+ }
588
+ return this.convertNumberToWords(s, isNotSpecial);
589
+ });
590
+
469
591
  }
592
+
470
593
  break;
471
594
 
472
595
  case "characters":
@@ -485,7 +608,7 @@ class Language extends LanguageBase {
485
608
  break;
486
609
 
487
610
  case "number":
488
- part.text = this.convertNumberToWords(part.value) + " ";
611
+ part.text = this.convertRegexNumberToWords(part.value) + " ";
489
612
  break;
490
613
 
491
614
  case "date":
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@met4citizen/headtts",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "description": "HeadTTS: A free Javascript text-to-speech with timestamps and visemes.",
5
5
  "main": "modules/headtts.mjs",
6
6
  "type": "module",
@@ -12,7 +12,7 @@
12
12
  "dictionaries/"
13
13
  ],
14
14
  "dependencies": {
15
- "@huggingface/transformers": "^3.5.1",
15
+ "@huggingface/transformers": "^3.6.0",
16
16
  "ws": "^8.18.1"
17
17
  },
18
18
  "scripts": {