echogarden 1.4.4 → 1.6.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 (240) hide show
  1. package/data/schemas/options.json +310 -25
  2. package/dist/alignment/DTWMfccSequenceAlignment.d.ts +1 -1
  3. package/dist/alignment/DTWMfccSequenceAlignment.js +5 -5
  4. package/dist/alignment/DTWSequenceAlignmentWindowed.js +1 -3
  5. package/dist/alignment/DTWSequenceAlignmentWindowed.js.map +1 -1
  6. package/dist/alignment/{TextAlignment.d.ts → SemanticTextAlignment.d.ts} +4 -2
  7. package/dist/alignment/SemanticTextAlignment.js +336 -0
  8. package/dist/alignment/SemanticTextAlignment.js.map +1 -0
  9. package/dist/alignment/SpeechAlignment.d.ts +4 -3
  10. package/dist/alignment/SpeechAlignment.js +130 -39
  11. package/dist/alignment/SpeechAlignment.js.map +1 -1
  12. package/dist/api/API.d.ts +7 -3
  13. package/dist/api/API.js +7 -2
  14. package/dist/api/API.js.map +1 -1
  15. package/dist/api/APIOptions.d.ts +4 -1
  16. package/dist/api/Alignment.d.ts +1 -1
  17. package/dist/api/Alignment.js +13 -5
  18. package/dist/api/Alignment.js.map +1 -1
  19. package/dist/api/LanguageDetectionCommon.d.ts +6 -0
  20. package/dist/api/LanguageDetectionCommon.js +2 -0
  21. package/dist/api/LanguageDetectionCommon.js.map +1 -0
  22. package/dist/api/Recognition.js.map +1 -1
  23. package/dist/api/{LanguageDetection.d.ts → SpeechLanguageDetection.d.ts} +1 -25
  24. package/dist/api/{LanguageDetection.js → SpeechLanguageDetection.js} +1 -68
  25. package/dist/api/SpeechLanguageDetection.js.map +1 -0
  26. package/dist/api/{Translation.js → SpeechTranslation.js} +3 -3
  27. package/dist/api/SpeechTranslation.js.map +1 -0
  28. package/dist/api/Synthesis.d.ts +0 -1
  29. package/dist/api/Synthesis.js +4 -4
  30. package/dist/api/TextLanguageDetection.d.ts +21 -0
  31. package/dist/api/TextLanguageDetection.js +67 -0
  32. package/dist/api/TextLanguageDetection.js.map +1 -0
  33. package/dist/api/TextTranslation.d.ts +25 -0
  34. package/dist/api/TextTranslation.js +101 -0
  35. package/dist/api/TextTranslation.js.map +1 -0
  36. package/dist/api/TimelineTranslationAlignment.d.ts +23 -0
  37. package/dist/api/TimelineTranslationAlignment.js +92 -0
  38. package/dist/api/TimelineTranslationAlignment.js.map +1 -0
  39. package/dist/api/TranscriptAndTranslationAlignment.d.ts +35 -0
  40. package/dist/api/TranscriptAndTranslationAlignment.js +78 -0
  41. package/dist/api/TranscriptAndTranslationAlignment.js.map +1 -0
  42. package/dist/api/TranslationAlignment.d.ts +4 -3
  43. package/dist/api/TranslationAlignment.js +9 -8
  44. package/dist/api/TranslationAlignment.js.map +1 -1
  45. package/dist/api/VoiceActivityDetection.js +16 -1
  46. package/dist/api/VoiceActivityDetection.js.map +1 -1
  47. package/dist/audio/AudioBufferConversion.d.ts +0 -1
  48. package/dist/audio/AudioPlayer.d.ts +0 -1
  49. package/dist/audio/AudioPlayer.js +62 -41
  50. package/dist/audio/AudioPlayer.js.map +1 -1
  51. package/dist/audio/AudioUtilities.d.ts +0 -1
  52. package/dist/cli/CLI.d.ts +28 -7
  53. package/dist/cli/CLI.js +265 -37
  54. package/dist/cli/CLI.js.map +1 -1
  55. package/dist/codecs/FFMpegTranscoder.d.ts +0 -1
  56. package/dist/codecs/FFMpegTranscoder.js +7 -0
  57. package/dist/codecs/FFMpegTranscoder.js.map +1 -1
  58. package/dist/codecs/TIMITCodec.d.ts +0 -1
  59. package/dist/codecs/WaveCodec.d.ts +0 -1
  60. package/dist/dsp/FFT.d.ts +1 -1
  61. package/dist/dsp/FFT.js +6 -0
  62. package/dist/dsp/FFT.js.map +1 -1
  63. package/dist/dsp/KWeightingFilter.js +1 -1
  64. package/dist/dsp/KWeightingFilter.js.map +1 -1
  65. package/dist/dsp/MelSpectogram.d.ts +3 -2
  66. package/dist/dsp/MelSpectogram.js +14 -8
  67. package/dist/dsp/MelSpectogram.js.map +1 -1
  68. package/dist/math/VectorMath.d.ts +9 -9
  69. package/dist/math/VectorMath.js +10 -10
  70. package/dist/math/VectorMath.js.map +1 -1
  71. package/dist/nlp/ChineseSegmentation.js +4 -4
  72. package/dist/nlp/ChineseSegmentation.js.map +1 -1
  73. package/dist/nlp/Segmentation.d.ts +2 -2
  74. package/dist/nlp/Segmentation.js +20 -13
  75. package/dist/nlp/Segmentation.js.map +1 -1
  76. package/dist/recognition/OpenAICloudSTT.d.ts +2 -1
  77. package/dist/recognition/OpenAICloudSTT.js +30 -19
  78. package/dist/recognition/OpenAICloudSTT.js.map +1 -1
  79. package/dist/recognition/SileroSTT.d.ts +0 -1
  80. package/dist/recognition/WhisperCppSTT.d.ts +3 -3
  81. package/dist/recognition/WhisperCppSTT.js +21 -9
  82. package/dist/recognition/WhisperCppSTT.js.map +1 -1
  83. package/dist/recognition/WhisperSTT.d.ts +9 -6
  84. package/dist/recognition/WhisperSTT.js +227 -46
  85. package/dist/recognition/WhisperSTT.js.map +1 -1
  86. package/dist/server/Client.d.ts +3 -4
  87. package/dist/server/Client.js.map +1 -1
  88. package/dist/server/Worker.d.ts +3 -3
  89. package/dist/server/Worker.js +3 -2
  90. package/dist/server/Worker.js.map +1 -1
  91. package/dist/source-separation/MDXNetSourceSeparation.d.ts +0 -1
  92. package/dist/source-separation/MDXNetSourceSeparation.js +1 -1
  93. package/dist/source-separation/MDXNetSourceSeparation.js.map +1 -1
  94. package/dist/speech-embeddings/WavToVec2BertFeatureEmbeddings.d.ts +12 -0
  95. package/dist/speech-embeddings/WavToVec2BertFeatureEmbeddings.js +68 -0
  96. package/dist/speech-embeddings/WavToVec2BertFeatureEmbeddings.js.map +1 -0
  97. package/dist/speech-language-detection/SileroLanguageDetection.d.ts +1 -2
  98. package/dist/speech-language-detection/SileroLanguageDetection.js +1 -1
  99. package/dist/speech-language-detection/SileroLanguageDetection.js.map +1 -1
  100. package/dist/subtitles/Subtitles.js +2 -2
  101. package/dist/subtitles/Subtitles.js.map +1 -1
  102. package/dist/synthesis/GoogleCloudTTS.d.ts +0 -1
  103. package/dist/synthesis/GoogleTranslateTTS.d.ts +0 -1
  104. package/dist/synthesis/GoogleTranslateTTS.js +6 -21
  105. package/dist/synthesis/GoogleTranslateTTS.js.map +1 -1
  106. package/dist/synthesis/StreamlabsPollyTTS.d.ts +0 -1
  107. package/dist/synthesis/VitsTTS.d.ts +0 -1
  108. package/dist/synthesis/VitsTTS.js +30 -0
  109. package/dist/synthesis/VitsTTS.js.map +1 -1
  110. package/dist/tests/Test.js +0 -31
  111. package/dist/tests/Test.js.map +1 -1
  112. package/dist/text-language-detection/FastTextLanguageDetection.d.ts +1 -1
  113. package/dist/text-language-detection/FastTextLanguageDetection.js.map +1 -1
  114. package/dist/text-language-detection/TinyLDLanguageDetection.d.ts +1 -1
  115. package/dist/text-language-detection/TinyLDLanguageDetection.js.map +1 -1
  116. package/dist/text-translation/DeepLTextTranslation.d.ts +2 -0
  117. package/dist/text-translation/DeepLTextTranslation.js +67 -0
  118. package/dist/text-translation/DeepLTextTranslation.js.map +1 -0
  119. package/dist/text-translation/GoogleTranslateTextTranslation.d.ts +10 -0
  120. package/dist/text-translation/GoogleTranslateTextTranslation.js +554 -0
  121. package/dist/text-translation/GoogleTranslateTextTranslation.js.map +1 -0
  122. package/dist/text-translation/NLLBTextTranslation.d.ts +2 -1
  123. package/dist/text-translation/NLLBTextTranslation.js +249 -19
  124. package/dist/text-translation/NLLBTextTranslation.js.map +1 -1
  125. package/dist/utilities/BinaryArrayConversion.d.ts +0 -1
  126. package/dist/utilities/BrowserRequestHeaders.d.ts +6 -0
  127. package/dist/utilities/BrowserRequestHeaders.js +52 -0
  128. package/dist/utilities/BrowserRequestHeaders.js.map +1 -0
  129. package/dist/utilities/BufferFileReadStream.d.ts +20 -0
  130. package/dist/utilities/BufferFileReadStream.js +81 -0
  131. package/dist/utilities/BufferFileReadStream.js.map +1 -0
  132. package/dist/utilities/DynamicUint8Array.d.ts +9 -0
  133. package/dist/utilities/DynamicUint8Array.js +31 -0
  134. package/dist/utilities/DynamicUint8Array.js.map +1 -0
  135. package/dist/utilities/FileSystem.d.ts +0 -2
  136. package/dist/utilities/Hashing.d.ts +3 -10
  137. package/dist/utilities/Hashing.js +10 -127
  138. package/dist/utilities/Hashing.js.map +1 -1
  139. package/dist/utilities/LEB128.d.ts +15 -5
  140. package/dist/utilities/LEB128.js +199 -119
  141. package/dist/utilities/LEB128.js.map +1 -1
  142. package/dist/utilities/LPVarInt.d.ts +11 -0
  143. package/dist/utilities/LPVarInt.js +187 -0
  144. package/dist/utilities/LPVarInt.js.map +1 -0
  145. package/dist/utilities/Locale.d.ts +1 -1
  146. package/dist/utilities/Locale.js +1 -1
  147. package/dist/utilities/OnnxUtilities.d.ts +1 -2
  148. package/dist/utilities/PVarInt.d.ts +4 -0
  149. package/dist/utilities/PVarInt.js +166 -0
  150. package/dist/utilities/PVarInt.js.map +1 -0
  151. package/dist/utilities/PackageManager.js +48 -25
  152. package/dist/utilities/PackageManager.js.map +1 -1
  153. package/dist/utilities/RandomGenerator.d.ts +3 -17
  154. package/dist/utilities/RandomGenerator.js +12 -81
  155. package/dist/utilities/RandomGenerator.js.map +1 -1
  156. package/dist/utilities/Timeline.d.ts +2 -0
  157. package/dist/utilities/Timeline.js +129 -20
  158. package/dist/utilities/Timeline.js.map +1 -1
  159. package/dist/utilities/Utilities.d.ts +1 -3
  160. package/dist/utilities/Utilities.js +30 -3
  161. package/dist/utilities/Utilities.js.map +1 -1
  162. package/dist/utilities/VarInt.d.ts +4 -0
  163. package/dist/utilities/VarInt.js +166 -0
  164. package/dist/utilities/VarInt.js.map +1 -0
  165. package/dist/utilities/VirtualFileReadStream.d.ts +20 -0
  166. package/dist/utilities/VirtualFileReadStream.js +79 -0
  167. package/dist/utilities/VirtualFileReadStream.js.map +1 -0
  168. package/dist/utilities/WebReader.js +7 -23
  169. package/dist/utilities/WebReader.js.map +1 -1
  170. package/dist/voice-activity-detection/SileroVAD.d.ts +0 -1
  171. package/docs/API.md +105 -3
  172. package/docs/CLI.md +51 -1
  173. package/docs/Engines.md +32 -3
  174. package/docs/Options.md +53 -12
  175. package/docs/Tasklist.md +1 -13
  176. package/package.json +20 -24
  177. package/src/alignment/DTWMfccSequenceAlignment.ts +5 -5
  178. package/src/alignment/DTWSequenceAlignmentWindowed.ts +1 -3
  179. package/src/alignment/SemanticTextAlignment.ts +467 -0
  180. package/src/alignment/SpeechAlignment.ts +214 -56
  181. package/src/api/API.ts +18 -2
  182. package/src/api/APIOptions.ts +14 -1
  183. package/src/api/Alignment.ts +31 -9
  184. package/src/api/LanguageDetectionCommon.ts +7 -0
  185. package/src/api/Recognition.ts +2 -0
  186. package/src/api/{LanguageDetection.ts → SpeechLanguageDetection.ts} +1 -119
  187. package/src/api/{Translation.ts → SpeechTranslation.ts} +2 -2
  188. package/src/api/Synthesis.ts +4 -4
  189. package/src/api/TextLanguageDetection.ts +116 -0
  190. package/src/api/TextTranslation.ts +177 -0
  191. package/src/api/TimelineTranslationAlignment.ts +162 -0
  192. package/src/api/TranscriptAndTranslationAlignment.ts +164 -0
  193. package/src/api/TranslationAlignment.ts +12 -10
  194. package/src/api/VoiceActivityDetection.ts +24 -3
  195. package/src/audio/AudioPlayer.ts +2 -0
  196. package/src/cli/CLI.ts +376 -40
  197. package/src/codecs/FFMpegTranscoder.ts +6 -0
  198. package/src/dsp/FFT.ts +8 -2
  199. package/src/dsp/KWeightingFilter.ts +1 -1
  200. package/src/dsp/MelSpectogram.ts +17 -8
  201. package/src/math/VectorMath.ts +15 -15
  202. package/src/nlp/ChineseSegmentation.ts +6 -4
  203. package/src/nlp/Segmentation.ts +18 -13
  204. package/src/recognition/OpenAICloudSTT.ts +47 -29
  205. package/src/recognition/WhisperCppSTT.ts +26 -11
  206. package/src/recognition/WhisperSTT.ts +364 -49
  207. package/src/server/Client.ts +3 -2
  208. package/src/server/Worker.ts +3 -2
  209. package/src/source-separation/MDXNetSourceSeparation.ts +1 -1
  210. package/src/speech-embeddings/WavToVec2BertFeatureEmbeddings.ts +107 -0
  211. package/src/speech-language-detection/SileroLanguageDetection.ts +2 -1
  212. package/src/subtitles/Subtitles.ts +2 -2
  213. package/src/synthesis/GoogleTranslateTTS.ts +7 -21
  214. package/src/synthesis/VitsTTS.ts +31 -3
  215. package/src/tests/Test.ts +1 -38
  216. package/src/text-language-detection/FastTextLanguageDetection.ts +1 -1
  217. package/src/text-language-detection/TinyLDLanguageDetection.ts +1 -1
  218. package/src/text-translation/DeepLTextTranslation.ts +88 -0
  219. package/src/text-translation/GoogleTranslateTextTranslation.ts +667 -0
  220. package/src/text-translation/NLLBTextTranslation.ts +261 -21
  221. package/src/typings/Fillers.d.ts +25 -2
  222. package/src/utilities/BrowserRequestHeaders.ts +59 -0
  223. package/src/utilities/DynamicUint8Array.ts +39 -0
  224. package/src/utilities/Hashing.ts +14 -167
  225. package/src/utilities/LEB128.ts +273 -148
  226. package/src/utilities/LPVarInt.ts +292 -0
  227. package/src/utilities/Locale.ts +1 -1
  228. package/src/utilities/OnnxUtilities.ts +1 -1
  229. package/src/utilities/PackageManager.ts +51 -30
  230. package/src/utilities/RandomGenerator.ts +12 -113
  231. package/src/utilities/Timeline.ts +162 -23
  232. package/src/utilities/Utilities.ts +40 -3
  233. package/src/utilities/VirtualFileReadStream.ts +109 -0
  234. package/src/utilities/WebReader.ts +9 -23
  235. package/dist/alignment/TextAlignment.js +0 -156
  236. package/dist/alignment/TextAlignment.js.map +0 -1
  237. package/dist/api/LanguageDetection.js.map +0 -1
  238. package/dist/api/Translation.js.map +0 -1
  239. package/src/alignment/TextAlignment.ts +0 -234
  240. /package/dist/api/{Translation.d.ts → SpeechTranslation.d.ts} +0 -0
@@ -1,47 +1,80 @@
1
+ import chalk from 'chalk'
2
+ import { TranslationPair } from '../api/TextTranslation.js'
3
+ import { splitToSentences } from '../nlp/Segmentation.js'
1
4
  import { Logger } from '../utilities/Logger.js'
2
5
  import { loadPackage } from '../utilities/PackageManager.js'
3
6
 
4
- export async function translateText(sourceText: string, sourceLanguage: string, targetLanguage: string) {
7
+ export async function translateText(sourceText: string, sourceLanguage: string, targetLanguage: string): Promise<TranslationPair[]> {
5
8
  const logger = new Logger()
6
9
 
7
- const { AutoTokenizer, M2M100ForConditionalGeneration } = await import('@echogarden/transformers-nodejs-lite')
10
+ //const languageNames = Object.keys(languageNameToNLLBCode)
8
11
 
9
- const modelPath = await loadPackage(`xenova-nllb-200-distilled-600M-quantized`)
12
+ //logger.log(languageNames)
10
13
 
14
+ logger.start(`Load transformers.js module`)
15
+ const { AutoTokenizer, M2M100ForConditionalGeneration } = await import('@echogarden/transformers-nodejs-lite')
16
+
17
+ logger.start(`Load NLLB package`)
18
+ const modelPath = await loadPackage(`xenova-nllb-200-distilled-600M-q8`)
19
+
20
+ logger.start(`Load NLLB tokenizer`)
11
21
  const tokenizer = await AutoTokenizer.from_pretrained(modelPath)
22
+
23
+ logger.start(`Load NLLB model`)
12
24
  const model = await M2M100ForConditionalGeneration.from_pretrained(modelPath)
13
25
 
26
+ logger.start(`Split to sentences`)
27
+
14
28
  const config = {
15
- src_lang: 'eng_Latn',
16
- tgt_lang: 'fra_Latn'
29
+ src_lang: 'spa_Latn',
30
+ tgt_lang: 'eng_Latn'
17
31
  }
18
32
 
19
- const inputs = (tokenizer as any)._build_translation_inputs(sourceText, {
20
- padding: true,
21
- truncation: true,
22
- }, config)
33
+ const sentences = splitToSentences(sourceText, sourceLanguage)
34
+
35
+ logger.end()
23
36
 
24
- const result = await model.generate(inputs.input_ids, config)
37
+ const translationPairs: TranslationPair[] = []
25
38
 
26
- logger.log(tokenizer.model.convert_ids_to_tokens(result[0]))
39
+ for (let i = 0; i < sentences.length; i++) {
40
+ const sentence = sentences[i]
27
41
 
28
- const inputTokens = tokenizer.model.convert_ids_to_tokens(Array.from(inputs.input_ids.data))
29
- const embeddingResult = await model(inputs)
42
+ logger.logTitledMessage(`Translate sentence ${i + 1}/${sentences.length}`, `"${sentence.trim()}"`, chalk.magentaBright)
30
43
 
31
- const lastHiddenState = embeddingResult.last_hidden_state
44
+ logger.start(`Tokenize sentence`)
45
+ const inputs = (tokenizer as any)._build_translation_inputs(sentence, {
46
+ padding: true,
47
+ truncation: true,
48
+ }, config)
32
49
 
33
- const tokenCount = lastHiddenState.dims[1]
34
- const embeddingSize = lastHiddenState.dims[2]
50
+ logger.start(`Translate sentence with NLLB model`)
51
+ const translationTokenIds = await model.generate(inputs.input_ids, config)
35
52
 
36
- for (let i = 0; i < tokenCount; i++) {
37
- const tokenEmbedding = lastHiddenState.data.slice(i * embeddingSize, (i + 1) * embeddingSize)
53
+ logger.start(`Extract tokens`)
38
54
 
39
- const tokenId = inputTokens[i]
55
+ const translationTokens = tokenizer.model.convert_ids_to_tokens(translationTokenIds[0])
40
56
 
41
- logger.log(`Token ${i} (${tokenId}):`, tokenEmbedding);
57
+ const translatedText = translationTokens
58
+ .slice(2, translationTokens.length - 1)
59
+ .map(token => {
60
+ if (token.startsWith('▁')) {
61
+ return token.replaceAll('▁', ' ')
62
+ }
63
+
64
+ return token
65
+ })
66
+ .join('')
67
+ .trim()
68
+
69
+ translationPairs.push({
70
+ sourceText: sentence,
71
+ translatedText
72
+ })
73
+
74
+ logger.end()
42
75
  }
43
76
 
44
- logger.log(inputTokens)
77
+ return translationPairs
45
78
  }
46
79
 
47
80
  const languageNameToNLLBCode: Record<string, string> = {
@@ -250,3 +283,210 @@ const languageNameToNLLBCode: Record<string, string> = {
250
283
  'Yue Chinese': 'yue_Hant',
251
284
  'Zulu': 'zul_Latn',
252
285
  }
286
+
287
+ const languageNameToISO931: { [langName: string]: string } = {
288
+ 'Acehnese (Arabic script)': 'unknown', // No ISO 639-1 code for Acehnese
289
+ 'Acehnese (Latin script)': 'unknown', // No ISO 639-1 code for Acehnese
290
+ 'Afrikaans': 'af',
291
+ 'Akan': 'ak',
292
+ 'Amharic': 'am',
293
+ 'Armenian': 'hy',
294
+ 'Assamese': 'as',
295
+ 'Asturian': 'ast', // Approximate: ISO 639-3 code
296
+ 'Awadhi': 'awa', // Approximate: ISO 639-3 code
297
+ 'Ayacucho Quechua': 'qu', // Approximate: Quechua has many variants
298
+ 'Balinese': 'ban', // Approximate: ISO 639-3 code
299
+ 'Bambara': 'bm',
300
+ 'Banjar (Arabic script)': 'unknown', // No ISO 639-1 code for Banjar
301
+ 'Banjar (Latin script)': 'unknown', // No ISO 639-1 code for Banjar
302
+ 'Bashkir': 'ba',
303
+ 'Basque': 'eu',
304
+ 'Belarusian': 'be',
305
+ 'Bemba': 'bem', // Approximate: ISO 639-3 code
306
+ 'Bengali': 'bn',
307
+ 'Bhojpuri': 'bho',
308
+ 'Bosnian': 'bs',
309
+ 'Buginese': 'bug',
310
+ 'Bulgarian': 'bg',
311
+ 'Burmese': 'my',
312
+ 'Catalan': 'ca',
313
+ 'Cebuano': 'ceb', // Approximate: ISO 639-2 code
314
+ 'Central Atlas Tamazight': 'tzm',
315
+ 'Central Aymara': 'ay', // Approximate: Aymara has variants
316
+ 'Central Kanuri (Arabic script)': 'kr', // Approximate: Kanuri uses multiple scripts
317
+ 'Central Kanuri (Latin script)': 'kr', // Approximate: Kanuri uses multiple scripts
318
+ 'Central Kurdish': 'ckb', // Approximate: Kurdish has several variants
319
+ 'Chhattisgarhi': 'hne', // Approximate: ISO 639-3 code
320
+ 'Chinese (Simplified)': 'zh', // Approximate: zh covers both Simplified and Traditional
321
+ 'Chinese (Traditional)': 'zh', // Approximate: zh covers both Simplified and Traditional
322
+ 'Chokwe': 'cjk', // Approximate: ISO 639-3 code
323
+ 'Crimean Tatar': 'crh', // Approximate: ISO 639-3 code
324
+ 'Croatian': 'hr',
325
+ 'Czech': 'cs',
326
+ 'Danish': 'da',
327
+ 'Dari': 'prs', // Approximate: Dari is considered a dialect of Persian
328
+ 'Dutch': 'nl',
329
+ 'Dyula': 'dyu',
330
+ 'Dzongkha': 'dz',
331
+ 'Eastern Panjabi': 'pa', // Approximate: Panjabi has multiple writing systems
332
+ 'Eastern Yiddish': 'yid', // Approximate: Yiddish has multiple variants
333
+ 'Egyptian Arabic': 'arz',
334
+ 'English': 'en',
335
+ 'Esperanto': 'eo',
336
+ 'Estonian': 'et',
337
+ 'Ewe': 'ee',
338
+ 'Faroese': 'fo',
339
+ 'Fijian': 'fj',
340
+ 'Finnish': 'fi',
341
+ 'Fon': 'fon',
342
+ 'French': 'fr',
343
+ 'Friulian': 'fur', // Approximate: ISO 639-3 code
344
+ 'Galician': 'gl',
345
+ 'Ganda': 'lg',
346
+ 'Georgian': 'ka',
347
+ 'German': 'de',
348
+ 'Greek': 'el',
349
+ 'Guarani': 'gn',
350
+ 'Gujarati': 'gu',
351
+ 'Haitian Creole': 'ht',
352
+ 'Halh Mongolian': 'mn', // Approximate: Mongolian encompasses several dialects
353
+ 'Hausa': 'ha',
354
+ 'Hebrew': 'he',
355
+ 'Hindi': 'hi',
356
+ 'Hungarian': 'hu',
357
+ 'Icelandic': 'is',
358
+ 'Igbo': 'ig',
359
+ 'Ilocano': 'ilo',
360
+ 'Indonesian': 'id',
361
+ 'Irish': 'ga',
362
+ 'Italian': 'it',
363
+ 'Japanese': 'ja',
364
+ 'Javanese': 'jv',
365
+ 'Jingpho': 'kac', // Approximate: ISO 639-3 code
366
+ 'Kabiyè': 'kbp', // Approximate: ISO 639-3 code
367
+ 'Kabuverdianu': 'kea', // Approximate: ISO 639-3 code
368
+ 'Kabyle': 'kab',
369
+ 'Kamba': 'kam', // Approximate: ISO 639-3 code
370
+ 'Kannada': 'kn',
371
+ 'Kashmiri (Arabic script)': 'ks', // Approximate: Kashmiri uses multiple scripts
372
+ 'Kashmiri (Devanagari script)': 'ks', // Approximate: Kashmiri uses multiple scripts
373
+ 'Kazakh': 'kk',
374
+ 'Khmer': 'km',
375
+ 'Kikongo': 'kg',
376
+ 'Kikuyu': 'ki',
377
+ 'Kimbundu': 'kmb',
378
+ 'Kinyarwanda': 'rw',
379
+ 'Korean': 'ko',
380
+ 'Kyrgyz': 'ky',
381
+ 'Lao': 'lo',
382
+ 'Latgalian': 'ltg', // Approximate: ISO 639-3 code
383
+ 'Ligurian': 'lij', // Approximate: ISO 639-3 code
384
+ 'Limburgish': 'li', // Approximate: ISO 639-1 code for Limburgish-Ripuarian
385
+ 'Lingala': 'ln',
386
+ 'Lithuanian': 'lt',
387
+ 'Lombard': 'lmo', // Approximate: ISO 639-3 code
388
+ 'Luba-Kasai': 'lua',
389
+ 'Luo': 'luo',
390
+ 'Luxembourgish': 'lb',
391
+ 'Macedonian': 'mk',
392
+ 'Magahi': 'mag', // Approximate: ISO 639-3 code
393
+ 'Maithili': 'mai', // Approximate: ISO 639-3 code
394
+ 'Malayalam': 'ml',
395
+ 'Maltese': 'mt',
396
+ 'Maori': 'mi',
397
+ 'Marathi': 'mr',
398
+ 'Meitei (Bengali script)': 'mni', // Approximate: Meitei uses multiple scripts
399
+ 'Mesopotamian Arabic': 'acm', // Approximate: ISO 639-3 code
400
+ 'Minangkabau (Arabic script)': 'min', // Approximate: Minangkabau uses multiple scripts
401
+ 'Minangkabau (Latin script)': 'min', // Approximate: Minangkabau uses multiple scripts
402
+ 'Mizo': 'lus',
403
+ 'Modern Standard Arabic (Romanized)': 'ar', // Approximate: Modern Standard Arabic is a standardized form
404
+ 'Modern Standard Arabic': 'ar', // Approximate: Modern Standard Arabic is a standardized form
405
+ 'Moroccan Arabic': 'ary',
406
+ 'Mossi': 'mos',
407
+ 'Najdi Arabic': 'ars', // Approximate: ISO 639-3 code for Najdi Arabic
408
+ 'Nepali': 'ne',
409
+ 'Nigerian Fulfulde': 'fuv',
410
+ 'North Azerbaijani': 'az', // Approximate: Azerbaijani is a pluricentric language
411
+ 'North Levantine Arabic': 'apc', // Approximate: ISO 639-3 code for North Levantine Arabic
412
+ 'Northern Kurdish': 'kmr', // Approximate: Kurdish has several variants
413
+ 'Northern Sotho': 'nso',
414
+ 'Northern Uzbek': 'uz', // Approximate: Uzbek is a pluricentric language
415
+ 'Norwegian Bokmål': 'nb',
416
+ 'Norwegian Nynorsk': 'nn',
417
+ 'Nuer': 'nus', // Approximate: ISO 639-3 code
418
+ 'Nyanja': 'ny',
419
+ 'Occitan': 'oc',
420
+ 'Odia': 'or',
421
+ 'Pangasinan': 'pag',
422
+ 'Papiamento': 'pap',
423
+ 'Plateau Malagasy': 'plt', // Approximate: ISO 639-3 code for Plateau Malagasy
424
+ 'Polish': 'pl',
425
+ 'Portuguese': 'pt',
426
+ 'Romanian': 'ro',
427
+ 'Rundi': 'rn',
428
+ 'Russian': 'ru',
429
+ 'Samoan': 'sm',
430
+ 'Sango': 'sg',
431
+ 'Sanskrit': 'sa',
432
+ 'Santali': 'sat',
433
+ 'Sardinian': 'sc', // Approximate: Sardinian has several variants
434
+ 'Scottish Gaelic': 'gd',
435
+ 'Serbian': 'sr',
436
+ 'Shan': 'shn', // Approximate: ISO 639-3 code
437
+ 'Shona': 'sn',
438
+ 'Sicilian': 'scn', // Approximate: ISO 639-3 code
439
+ 'Silesian': 'szl', // Approximate: ISO 639-3 code
440
+ 'Sindhi': 'sd',
441
+ 'Sinhala': 'si',
442
+ 'Slovak': 'sk',
443
+ 'Slovenian': 'sl',
444
+ 'Somali': 'so',
445
+ 'South Azerbaijani': 'az', // Approximate: Azerbaijani is a pluricentric language
446
+ 'South Levantine Arabic': 'ajp', // Approximate: ISO 639-3 code for South Levantine Arabic
447
+ 'Southern Pashto': 'pst', // Approximate: Pashto has several variants
448
+ 'Southern Sotho': 'st',
449
+ 'Southwestern Dinka': 'dik', // Approximate: ISO 639-3 code for Southwestern Dinka
450
+ 'Spanish': 'es',
451
+ 'Standard Latvian': 'lv', // Approximate: Standard Latvian is the official form of Latvian
452
+ 'Standard Malay': 'ms', // Approximate: Standard Malay is the basis for both Malaysian and Indonesian
453
+ 'Standard Tibetan': 'bo', // Approximate: Tibetan has several variants
454
+ 'Sundanese': 'su',
455
+ 'Swahili': 'sw',
456
+ 'Swati': 'ss',
457
+ 'Swedish': 'sv',
458
+ 'Tagalog': 'tl',
459
+ 'Tajik': 'tg',
460
+ 'Tamasheq (Latin script)': 'tmh', // Approximate: Tamasheq uses multiple scripts
461
+ 'Tamasheq (Tifinagh script)': 'tmh', // Approximate: Tamasheq uses multiple scripts
462
+ 'Tamil': 'ta',
463
+ 'Tatar': 'tt',
464
+ 'Ta’izzi-Adeni Arabic': 'acq', // Approximate: ISO 639-3 code for Ta'izzi-Adeni Arabic
465
+ 'Telugu': 'te',
466
+ 'Thai': 'th',
467
+ 'Tigrinya': 'ti',
468
+ 'Tok Pisin': 'tpi',
469
+ 'Tosk Albanian': 'sq', // Approximate: Albanian encompasses both Tosk and Gheg dialects
470
+ 'Tsonga': 'ts',
471
+ 'Tswana': 'tn',
472
+ 'Tumbuka': 'tum',
473
+ 'Tunisian Arabic': 'aeb', // Approximate: ISO 639-3 code for Tunisian Arabic
474
+ 'Turkish': 'tr',
475
+ 'Turkmen': 'tk',
476
+ 'Twi': 'tw',
477
+ 'Ukrainian': 'uk',
478
+ 'Umbundu': 'umb',
479
+ 'Urdu': 'ur',
480
+ 'Uyghur': 'ug',
481
+ 'Venetian': 'vec', // Approximate: ISO 639-3 code
482
+ 'Vietnamese': 'vi',
483
+ 'Waray': 'war',
484
+ 'Welsh': 'cy',
485
+ 'West Central Oromo': 'om', // Approximate: Oromo has several variants
486
+ 'Western Persian': 'fa', // Approximate: Western Persian is the most common variety of Persian
487
+ 'Wolof': 'wo',
488
+ 'Xhosa': 'xh',
489
+ 'Yoruba': 'yo',
490
+ 'Yue Chinese': 'yue', // Approximate: ISO 639-3 code
491
+ 'Zulu': 'zu'
492
+ };
@@ -1,6 +1,25 @@
1
1
  declare module 'moving-median'
2
- declare module 'html-to-text'
3
- declare module 'cldr-segmentation'
2
+
3
+ declare module 'html-to-text' {
4
+ export function htmlToText(html: string, options: {
5
+ wordwrap: boolean | number
6
+ selectors?: any[]
7
+ })
8
+ }
9
+
10
+ declare module 'cldr-segmentation' {
11
+ export function sentenceSplit(text: string, suppressions: any)
12
+ export function wordSplit(text: string, suppressions: any)
13
+ export const suppressions: Record<string, Suppressions>
14
+
15
+ export class Suppressions {
16
+ constructor(forwardTrie: any, backwardTrie: any, list: string)
17
+
18
+ merge(other: Suppressions)
19
+
20
+ static create(list: string[]): Suppressions
21
+ }
22
+ }
4
23
 
5
24
  declare module 'html-escaper' {
6
25
  export function escape(str: string): string
@@ -46,10 +65,12 @@ declare module 'onnxruntime-node' {
46
65
 
47
66
  // From backend.d.ts
48
67
  import { Backend, InferenceSession, SessionHandler, OnnxValue } from 'onnxruntime-common'
68
+
49
69
  class OnnxruntimeBackend implements Backend {
50
70
  init(): Promise<void>
51
71
  createSessionHandler(pathOrBuffer: string | Uint8Array, options?: InferenceSession.SessionOptions): Promise<SessionHandler>
52
72
  }
73
+
53
74
  export const onnxruntimeBackend: OnnxruntimeBackend
54
75
 
55
76
  // From binding.d.ts
@@ -76,8 +97,10 @@ declare module 'onnxruntime-node' {
76
97
  interface InferenceSession {
77
98
  loadModel(modelPath: string, options: SessionOptions): void
78
99
  loadModel(buffer: ArrayBuffer, byteOffset: number, byteLength: number, options: SessionOptions): void
100
+
79
101
  readonly inputNames: string[]
80
102
  readonly outputNames: string[]
103
+
81
104
  run(feeds: FeedsType, fetches: FetchesType, options: RunOptions): ReturnType
82
105
  }
83
106
  interface InferenceSessionConstructor {
@@ -0,0 +1,59 @@
1
+ export function getChromeOnWindowsHeaders(options: BrowserRequestHeadersOptions) {
2
+ const headers: Record<string, string> = {
3
+ 'Sec-Ch-Ua': `"Google Chrome";v="125", "Chromium";v="125", "Not.A/Brand";v="24"`,
4
+ 'X-Same-Domain': '1',
5
+ 'Dnt': '1',
6
+ 'Sec-Ch-Ua-Mobile': '?0',
7
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36',
8
+ 'Sec-Ch-Ua-Arch': 'x86',
9
+ 'Sec-Ch-Ua-Full-Version': '125.0.6422.114',
10
+ 'Sec-Ch-Ua-Platform-Version': '"15.0.0"',
11
+ 'Sec-Ch-Ua-Full-Version-List': `"Google Chrome";v="125.0.6422.114", "Chromium";v="125.0.6422.114", "Not.A/Brand";v="24.0.0.0"`,
12
+ 'Sec-Ch-Ua-Bitness': '"64"',
13
+ 'Sec-Ch-Ua-Model': '""',
14
+ 'Sec-Ch-Ua-Platform': '"Windows"',
15
+ 'Accept': '*/*',
16
+ 'Origin': options.origin,
17
+ 'Sec-Fetch-Site': 'same-origin',
18
+ 'Sec-Fetch-Mode': 'cors',
19
+ 'Sec-Fetch-Dest': 'empty',
20
+ 'Referer': options.referrer,
21
+ 'Accept-Encoding': 'gzip, deflate, br',
22
+ 'Accept-Language': 'en-US,en;q=0.9',
23
+ }
24
+
25
+ return headers
26
+ }
27
+
28
+ export function getChromeOnAndroidHeaders(options: BrowserRequestHeadersOptions) {
29
+ const headers: Record<string, string> = {
30
+ 'Sec-Ch-Ua': `"Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126"`,
31
+ 'Sec-Ch-Ua-Mobile': '?1',
32
+ 'Sec-Ch-Ua-Full-Version': '"126.0.6478.122"',
33
+ 'Sec-Ch-Ua-Arch': `""`,
34
+ 'Sec-Ch-Ua-Platform': `"Android"`,
35
+ 'Sec-Ch-Ua-Platform-Version': `"13"`,
36
+ 'Sec-Ch-Ua-Model': `"Pixel 5"`,
37
+ 'Sec-Ch-Ua-Bitness': `"64"`,
38
+ 'Sec-Ch-Ua-Wow64': `?0`,
39
+ 'Dnt': '1',
40
+ 'Upgrade-Insecure-Requests': '1',
41
+ 'User-Agent': `Mozilla/5.0 (Linux; Android 13; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.6478.122 Mobile Safari/537.36`,
42
+ 'Accept': '*/*',
43
+ 'Origin': options.origin,
44
+ 'Sec-Fetch-Site': 'none',
45
+ 'Sec-Fetch-Mode': 'navigate',
46
+ 'Sec-Fetch-User': '?1',
47
+ 'Sec-Fetch-Dest': 'document',
48
+ 'Referer': options.referrer,
49
+ 'Accept-Encoding': 'gzip, deflate, br',
50
+ 'Accept-Language': 'en-US,en;q=0.9',
51
+ }
52
+
53
+ return headers
54
+ }
55
+
56
+ export interface BrowserRequestHeadersOptions {
57
+ origin: string
58
+ referrer: string
59
+ }
@@ -0,0 +1,39 @@
1
+ export class DynamicUint8Array {
2
+ data = new Uint8Array(4)
3
+ length = 0
4
+
5
+ add(element: number) {
6
+ this.ensureCapacity(this.length + 1)
7
+
8
+ this.data[this.length] = element
9
+ this.length += 1
10
+ }
11
+
12
+ addMany(...elements: number[]) {
13
+ this.ensureCapacity(this.length + elements.length)
14
+
15
+ for (const element of elements) {
16
+ this.data[this.length] = element
17
+ this.length += 1
18
+ }
19
+ }
20
+
21
+ ensureCapacity(requiredCapacity: number) {
22
+ if (requiredCapacity > this.data.length) {
23
+ const newCapacity = requiredCapacity * 2
24
+
25
+ const newData = new Uint8Array(newCapacity)
26
+ newData.set(this.toUint8Array())
27
+
28
+ this.data = newData
29
+ }
30
+ }
31
+
32
+ toUint8Array() {
33
+ return this.data.subarray(0, this.length)
34
+ }
35
+
36
+ clear() {
37
+ this.length = 0
38
+ }
39
+ }
@@ -1,170 +1,4 @@
1
- export function knuthMultiplicative(bytes: Buffer) {
2
- let hash = 0
3
-
4
- for (const byte of bytes) {
5
- hash += Math.imul(byte, 2654435761)
6
- }
7
-
8
- return hash
9
- }
10
-
11
- export function xorShift32Hash(bytes: Buffer) {
12
- let s = 0
13
-
14
- for (const byte of bytes) {
15
- s += byte
16
-
17
- s ^= s << 13
18
- s ^= s >> 17
19
- s ^= s << 5
20
- }
21
-
22
- return s
23
- }
24
-
25
- export function jenkinsOneAtATime(bytes: Buffer) {
26
- let hash = 0
27
-
28
- for (const byte of bytes) {
29
- hash += byte
30
- hash += hash << 10
31
- hash ^= hash >> 6
32
- }
33
-
34
- hash += hash << 3
35
- hash ^= hash >> 11
36
- hash += hash << 15
37
-
38
- return hash >>> 0
39
- }
40
-
41
- export function FNV1a(bytes: Buffer) {
42
- let hval = 2166136261 | 0
43
-
44
- for (const byte of bytes) {
45
- hval = Math.imul(hval ^ byte, 16777619)
46
- }
47
-
48
- return hval >>> 0
49
- }
50
-
51
- export function superFastHash(bytes: Buffer) {
52
- let hash = bytes.length, tmp, p = 0
53
- const len = bytes.length >>> 2
54
-
55
- for (let i = 0; i < len; i++) {
56
- hash += bytes[p] | bytes[p + 1] << 8
57
- tmp = ((bytes[p + 2] | bytes[p + 3] << 8) << 11) ^ hash
58
- hash = (hash << 16) ^ tmp
59
- hash += hash >>> 11
60
- p += 4
61
- }
62
-
63
- switch (bytes.length & 3) {
64
- case 3:
65
- hash += bytes[p] | bytes[p + 1] << 8
66
- hash ^= hash << 16
67
- hash ^= bytes[p + 2] << 18
68
- hash += hash >>> 11
69
- break
70
- case 2:
71
- hash += bytes[p] | bytes[p + 1] << 8
72
- hash ^= hash << 11
73
- hash += hash >>> 17
74
- break
75
- case 1:
76
- hash += bytes[p]
77
- hash ^= hash << 10
78
- hash += hash >>> 1
79
- break
80
- }
81
-
82
- hash ^= hash << 3
83
- hash += hash >>> 5
84
- hash ^= hash << 4
85
- hash += hash >>> 17
86
- hash ^= hash << 25
87
- hash += hash >>> 6
88
-
89
- return hash >>> 0
90
- }
91
-
92
- export function cyrb53Hash(bytes: Buffer, seed = 0) {
93
- // https://github.com/bryc/code/blob/master/jshash/experimental/cyrb53.js
94
-
95
- let h1 = 0xdeadbeef ^ seed
96
- let h2 = 0x41c6ce57 ^ seed
97
-
98
- for (const byte of bytes) {
99
- h1 = Math.imul(h1 ^ byte, 2654435761)
100
- h2 = Math.imul(h2 ^ byte, 1597334677)
101
- }
102
-
103
- h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909)
104
- h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909)
105
-
106
- return (4294967296 * (2097151 & h2)) + (h1 >>> 0)
107
- }
108
-
109
- export function djb2(bytes: Buffer) {
110
- let hash = 5381
111
-
112
- for (const byte of bytes) {
113
- //hash += (hash << 5) + byte
114
- hash += (hash * 33) + byte
115
- }
116
-
117
- return hash >>> 0
118
- }
119
-
120
-
121
- export function murmurHash1(bytes: Buffer, seed = 0) {
122
- // https://github.com/bryc/code/blob/master/jshash/hashes/murmurhash1.js
123
-
124
- const length = bytes.length
125
-
126
- const multiplier = 3332679571
127
- const intIterationMaxIndex = length & -4
128
-
129
- let hash = seed ^ Math.imul(length, multiplier)
130
- let index = 0
131
-
132
- for (; index < intIterationMaxIndex; index += 4) {
133
- hash += bytes[index + 3] << 24 |
134
- bytes[index + 2] << 16 |
135
- bytes[index + 1] << 8 |
136
- bytes[index]
137
-
138
- hash = Math.imul(hash, multiplier)
139
- hash ^= hash >>> 16
140
- }
141
-
142
- switch (length & 3) {
143
- case 3: {
144
- hash += bytes[index + 2] << 16
145
- }
146
-
147
- case 2: {
148
- hash += bytes[index + 1] << 8
149
- }
150
-
151
- case 1: {
152
- hash += bytes[index]
153
- hash = Math.imul(hash, multiplier)
154
- hash ^= hash >>> 16
155
- }
156
- }
157
-
158
- hash = Math.imul(hash, multiplier)
159
- hash ^= hash >>> 10
160
-
161
- hash = Math.imul(hash, multiplier)
162
- hash ^= hash >>> 17
163
-
164
- return hash >>> 0
165
- }
166
-
167
- export function MurmurHash3(bytes: Buffer, seed = 0) {
1
+ export function murmurHash3(bytes: Uint8Array, seed = 0) {
168
2
  // https://github.com/bryc/code/blob/master/jshash/hashes/murmurhash3.js
169
3
 
170
4
  const p1 = 3432918353
@@ -228,3 +62,16 @@ export function MurmurHash3(bytes: Buffer, seed = 0) {
228
62
 
229
63
  return hash >>> 0
230
64
  }
65
+
66
+ export function convertToSingleInt32Hash(hash: (bytes: Uint8Array) => number) {
67
+ const bytes = new Uint8Array(4)
68
+ const ints = new Int32Array(bytes.buffer)
69
+
70
+ return (int32Val: number): number => {
71
+ ints[0] = int32Val
72
+
73
+ return hash(bytes)
74
+ }
75
+ }
76
+
77
+ export const murmurHash3_int32Input = convertToSingleInt32Hash(murmurHash3)