kaabalah 4.6.0 → 4.7.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.
@@ -286,6 +286,32 @@ function getAvailableLetters(tree, includeDigraphs) {
286
286
  const digraphs = includeDigraphs ? Array.from(DIGRAPHS) : [];
287
287
  return [...singleLetters, ...digraphs];
288
288
  }
289
+ function parseSuggestionToLetterUnits(text) {
290
+ const upper = text.toUpperCase();
291
+ const units = [];
292
+ let i = 0;
293
+ while (i < upper.length) {
294
+ const char = upper[i];
295
+ if (i + 1 < upper.length) {
296
+ const twoChar = char + upper[i + 1].toLowerCase();
297
+ if (DIGRAPHS.has(twoChar)) {
298
+ units.push(twoChar);
299
+ i += 2;
300
+ continue;
301
+ }
302
+ }
303
+ units.push(char);
304
+ i += 1;
305
+ }
306
+ return units;
307
+ }
308
+ function countLetterUnits(units) {
309
+ const counts = /* @__PURE__ */ new Map();
310
+ for (const unit of units) {
311
+ counts.set(unit, (counts.get(unit) || 0) + 1);
312
+ }
313
+ return counts;
314
+ }
289
315
  var reverseGematria = (options, tree) => {
290
316
  const {
291
317
  targetVowels,
@@ -294,7 +320,10 @@ var reverseGematria = (options, tree) => {
294
320
  minLength = 1,
295
321
  maxLength = 8,
296
322
  maxResults = 100,
297
- includeDigraphs = true
323
+ includeDigraphs = true,
324
+ maxLetterRepeat,
325
+ suggestionText,
326
+ suggestionMode = "subsequence"
298
327
  } = options;
299
328
  if (targetVowels === void 0 && targetConsonants === void 0 && targetSynthesis === void 0) {
300
329
  return { results: [], hasMore: false, totalFound: 0 };
@@ -302,6 +331,13 @@ var reverseGematria = (options, tree) => {
302
331
  if (!tree) {
303
332
  tree = createTree({ system: SYSTEM, parts: [] });
304
333
  }
334
+ if (suggestionText !== void 0) {
335
+ if (suggestionMode === "subsequence") {
336
+ return reverseGematriaFromSubsequence(options, tree);
337
+ } else {
338
+ return reverseGematriaAnagram(options, tree);
339
+ }
340
+ }
305
341
  const results = [];
306
342
  let totalFound = 0;
307
343
  let hasMore = false;
@@ -314,7 +350,7 @@ var reverseGematria = (options, tree) => {
314
350
  }
315
351
  return letterInfoCache.get(cacheKey);
316
352
  };
317
- const backtrack = (currentLetters, currentInfos, runningVowelsSum, runningConsonantsSum) => {
353
+ const backtrack = (currentLetters, currentInfos, runningVowelsSum, runningConsonantsSum, letterCounts) => {
318
354
  if (results.length >= maxResults) {
319
355
  hasMore = true;
320
356
  return;
@@ -380,6 +416,12 @@ var reverseGematria = (options, tree) => {
380
416
  const isStarting = currentLength === 0;
381
417
  const letterInfo = getLetterInfoCached(letter, isStarting);
382
418
  if (!letterInfo) continue;
419
+ if (maxLetterRepeat !== void 0) {
420
+ const currentCount = letterCounts.get(letter) || 0;
421
+ if (currentCount >= maxLetterRepeat) {
422
+ continue;
423
+ }
424
+ }
383
425
  const value = letterInfo.normalValue;
384
426
  const newVowelsSum = runningVowelsSum + (letterInfo.isVowel ? value : 0);
385
427
  const newConsonantsSum = runningConsonantsSum + (letterInfo.isVowel ? 0 : value);
@@ -394,22 +436,373 @@ var reverseGematria = (options, tree) => {
394
436
  }
395
437
  currentLetters.push(letter);
396
438
  currentInfos.push(letterInfo);
397
- backtrack(currentLetters, currentInfos, newVowelsSum, newConsonantsSum);
439
+ letterCounts.set(letter, (letterCounts.get(letter) || 0) + 1);
440
+ backtrack(currentLetters, currentInfos, newVowelsSum, newConsonantsSum, letterCounts);
398
441
  currentLetters.pop();
399
442
  currentInfos.pop();
443
+ letterCounts.set(letter, letterCounts.get(letter) - 1);
400
444
  if (results.length >= maxResults) {
401
445
  hasMore = true;
402
446
  return;
403
447
  }
404
448
  }
405
449
  };
406
- backtrack([], [], 0, 0);
450
+ backtrack([], [], 0, 0, /* @__PURE__ */ new Map());
407
451
  return {
408
452
  results,
409
453
  hasMore,
410
454
  totalFound
411
455
  };
412
456
  };
457
+ function reverseGematriaAnagram(options, tree) {
458
+ const {
459
+ targetVowels,
460
+ targetConsonants,
461
+ targetSynthesis,
462
+ minLength = 1,
463
+ maxLength = 8,
464
+ maxResults = 100,
465
+ maxLetterRepeat,
466
+ suggestionText = ""
467
+ } = options;
468
+ const results = [];
469
+ let totalFound = 0;
470
+ let hasMore = false;
471
+ const suggestionUnits = parseSuggestionToLetterUnits(suggestionText);
472
+ const letterUnits = suggestionUnits.filter((u) => u !== " ");
473
+ const maxSpaces = suggestionUnits.filter((u) => u === " ").length;
474
+ const availablePool = countLetterUnits(letterUnits);
475
+ const availableLetters = Array.from(availablePool.keys());
476
+ const letterInfoCache = /* @__PURE__ */ new Map();
477
+ const getLetterInfoCached = (letter, isStarting) => {
478
+ const cacheKey = `${letter}-${isStarting}`;
479
+ if (!letterInfoCache.has(cacheKey)) {
480
+ letterInfoCache.set(cacheKey, buildLetterInfo(tree, letter, isStarting));
481
+ }
482
+ return letterInfoCache.get(cacheKey);
483
+ };
484
+ const seenResults = /* @__PURE__ */ new Set();
485
+ const backtrack = (currentLetters, currentInfos, runningVowelsSum, runningConsonantsSum, letterCounts, usedFromPool, spacesUsed) => {
486
+ if (results.length >= maxResults) {
487
+ hasMore = true;
488
+ return;
489
+ }
490
+ const letterOnlyLength = currentLetters.filter((l) => l !== " ").length;
491
+ if (letterOnlyLength >= minLength && letterOnlyLength <= maxLength) {
492
+ let actualVowelsSum = runningVowelsSum;
493
+ let actualConsonantsSum = runningConsonantsSum;
494
+ if (currentInfos.length > 1) {
495
+ const lastInfo = currentInfos[currentInfos.length - 1];
496
+ if (lastInfo.endingValue !== void 0) {
497
+ const diff = lastInfo.endingValue - lastInfo.normalValue;
498
+ if (lastInfo.isVowel) {
499
+ actualVowelsSum += diff;
500
+ } else {
501
+ actualConsonantsSum += diff;
502
+ }
503
+ }
504
+ }
505
+ const actualSynthesisSum = actualVowelsSum + actualConsonantsSum;
506
+ const matchesVowels = targetVowels === void 0 || actualVowelsSum === targetVowels;
507
+ const matchesConsonants = targetConsonants === void 0 || actualConsonantsSum === targetConsonants;
508
+ const matchesSynthesis = targetSynthesis === void 0 || actualSynthesisSum === targetSynthesis;
509
+ if (matchesVowels && matchesConsonants && matchesSynthesis) {
510
+ const lettersStr = currentLetters.join("").toUpperCase();
511
+ if (!seenResults.has(lettersStr)) {
512
+ seenResults.add(lettersStr);
513
+ totalFound++;
514
+ if (results.length < maxResults) {
515
+ const letterDetails = currentInfos.map((info, idx) => {
516
+ const isEnding = idx === currentInfos.length - 1 && currentInfos.length > 1;
517
+ const useEndingValue = isEnding && info.endingValue !== void 0;
518
+ return {
519
+ latinLetterId: id("latinLetter" /* LATIN_LETTER */, info.letter),
520
+ value: useEndingValue ? info.endingValue : info.normalValue,
521
+ hebrewLetterId: info.hebrewLetterId,
522
+ hebrewCharacter: useEndingValue && info.hebrewCharacterWhenEnding ? info.hebrewCharacterWhenEnding : info.hebrewCharacter,
523
+ isVowel: info.isVowel
524
+ };
525
+ });
526
+ results.push({
527
+ letters: lettersStr,
528
+ letterDetails,
529
+ vowelsSum: actualVowelsSum,
530
+ consonantsSum: actualConsonantsSum,
531
+ synthesisSum: actualSynthesisSum
532
+ });
533
+ } else {
534
+ hasMore = true;
535
+ }
536
+ }
537
+ }
538
+ }
539
+ if (letterOnlyLength >= maxLength) {
540
+ return;
541
+ }
542
+ if (targetVowels !== void 0 && runningVowelsSum > targetVowels) {
543
+ return;
544
+ }
545
+ if (targetConsonants !== void 0 && runningConsonantsSum > targetConsonants) {
546
+ return;
547
+ }
548
+ if (targetSynthesis !== void 0 && runningVowelsSum + runningConsonantsSum > targetSynthesis) {
549
+ return;
550
+ }
551
+ if (spacesUsed < maxSpaces && currentLetters.length > 0 && currentLetters[currentLetters.length - 1] !== " ") {
552
+ currentLetters.push(" ");
553
+ backtrack(
554
+ currentLetters,
555
+ currentInfos,
556
+ runningVowelsSum,
557
+ runningConsonantsSum,
558
+ letterCounts,
559
+ usedFromPool,
560
+ spacesUsed + 1
561
+ );
562
+ currentLetters.pop();
563
+ if (results.length >= maxResults) {
564
+ hasMore = true;
565
+ return;
566
+ }
567
+ }
568
+ for (const letter of availableLetters) {
569
+ const poolLimit = availablePool.get(letter) || 0;
570
+ const usedCount = usedFromPool.get(letter) || 0;
571
+ if (usedCount >= poolLimit) {
572
+ continue;
573
+ }
574
+ if (maxLetterRepeat !== void 0) {
575
+ const currentCount = letterCounts.get(letter) || 0;
576
+ if (currentCount >= maxLetterRepeat) {
577
+ continue;
578
+ }
579
+ }
580
+ const isStarting = currentInfos.length === 0;
581
+ const letterInfo = getLetterInfoCached(letter, isStarting);
582
+ if (!letterInfo) continue;
583
+ const value = letterInfo.normalValue;
584
+ const newVowelsSum = runningVowelsSum + (letterInfo.isVowel ? value : 0);
585
+ const newConsonantsSum = runningConsonantsSum + (letterInfo.isVowel ? 0 : value);
586
+ if (targetVowels !== void 0 && newVowelsSum > targetVowels) {
587
+ continue;
588
+ }
589
+ if (targetConsonants !== void 0 && newConsonantsSum > targetConsonants) {
590
+ continue;
591
+ }
592
+ if (targetSynthesis !== void 0 && newVowelsSum + newConsonantsSum > targetSynthesis) {
593
+ continue;
594
+ }
595
+ currentLetters.push(letter);
596
+ currentInfos.push(letterInfo);
597
+ letterCounts.set(letter, (letterCounts.get(letter) || 0) + 1);
598
+ usedFromPool.set(letter, usedCount + 1);
599
+ backtrack(
600
+ currentLetters,
601
+ currentInfos,
602
+ newVowelsSum,
603
+ newConsonantsSum,
604
+ letterCounts,
605
+ usedFromPool,
606
+ spacesUsed
607
+ );
608
+ currentLetters.pop();
609
+ currentInfos.pop();
610
+ letterCounts.set(letter, letterCounts.get(letter) - 1);
611
+ usedFromPool.set(letter, usedCount);
612
+ if (results.length >= maxResults) {
613
+ hasMore = true;
614
+ return;
615
+ }
616
+ }
617
+ };
618
+ backtrack([], [], 0, 0, /* @__PURE__ */ new Map(), /* @__PURE__ */ new Map(), 0);
619
+ return {
620
+ results,
621
+ hasMore,
622
+ totalFound
623
+ };
624
+ }
625
+ function reverseGematriaFromSubsequence(options, tree) {
626
+ const {
627
+ targetVowels,
628
+ targetConsonants,
629
+ targetSynthesis,
630
+ minLength = 1,
631
+ maxLength = 8,
632
+ maxResults = 100,
633
+ maxLetterRepeat,
634
+ suggestionText = ""
635
+ } = options;
636
+ const results = [];
637
+ let totalFound = 0;
638
+ let hasMore = false;
639
+ const suggestionUnits = parseSuggestionToLetterUnits(suggestionText);
640
+ const letterUnitsWithPositions = [];
641
+ for (let i = 0; i < suggestionUnits.length; i++) {
642
+ letterUnitsWithPositions.push({
643
+ unit: suggestionUnits[i],
644
+ isSpace: suggestionUnits[i] === " ",
645
+ originalIndex: i
646
+ });
647
+ }
648
+ const letterOnlyUnits = letterUnitsWithPositions.filter((u) => !u.isSpace);
649
+ const maxSpaces = letterUnitsWithPositions.filter((u) => u.isSpace).length;
650
+ const letterInfoCache = /* @__PURE__ */ new Map();
651
+ const getLetterInfoCached = (letter, isStarting) => {
652
+ const cacheKey = `${letter}-${isStarting}`;
653
+ if (!letterInfoCache.has(cacheKey)) {
654
+ letterInfoCache.set(cacheKey, buildLetterInfo(tree, letter, isStarting));
655
+ }
656
+ return letterInfoCache.get(cacheKey);
657
+ };
658
+ const seenResults = /* @__PURE__ */ new Set();
659
+ const generateSubsequences = (index, currentLetters, currentInfos, runningVowelsSum, runningConsonantsSum, letterCounts, spacesUsed) => {
660
+ if (results.length >= maxResults) {
661
+ hasMore = true;
662
+ return;
663
+ }
664
+ const letterOnlyLength = currentLetters.filter((l) => l !== " ").length;
665
+ if (letterOnlyLength >= minLength && letterOnlyLength <= maxLength) {
666
+ let actualVowelsSum = runningVowelsSum;
667
+ let actualConsonantsSum = runningConsonantsSum;
668
+ if (currentInfos.length > 1) {
669
+ const lastInfo = currentInfos[currentInfos.length - 1];
670
+ if (lastInfo.endingValue !== void 0) {
671
+ const diff = lastInfo.endingValue - lastInfo.normalValue;
672
+ if (lastInfo.isVowel) {
673
+ actualVowelsSum += diff;
674
+ } else {
675
+ actualConsonantsSum += diff;
676
+ }
677
+ }
678
+ }
679
+ const actualSynthesisSum = actualVowelsSum + actualConsonantsSum;
680
+ const matchesVowels = targetVowels === void 0 || actualVowelsSum === targetVowels;
681
+ const matchesConsonants = targetConsonants === void 0 || actualConsonantsSum === targetConsonants;
682
+ const matchesSynthesis = targetSynthesis === void 0 || actualSynthesisSum === targetSynthesis;
683
+ if (matchesVowels && matchesConsonants && matchesSynthesis) {
684
+ const lettersStr = currentLetters.join("").toUpperCase();
685
+ if (!seenResults.has(lettersStr)) {
686
+ seenResults.add(lettersStr);
687
+ totalFound++;
688
+ if (results.length < maxResults) {
689
+ const letterDetails = currentInfos.map((info, idx) => {
690
+ const isEnding = idx === currentInfos.length - 1 && currentInfos.length > 1;
691
+ const useEndingValue = isEnding && info.endingValue !== void 0;
692
+ return {
693
+ latinLetterId: id("latinLetter" /* LATIN_LETTER */, info.letter),
694
+ value: useEndingValue ? info.endingValue : info.normalValue,
695
+ hebrewLetterId: info.hebrewLetterId,
696
+ hebrewCharacter: useEndingValue && info.hebrewCharacterWhenEnding ? info.hebrewCharacterWhenEnding : info.hebrewCharacter,
697
+ isVowel: info.isVowel
698
+ };
699
+ });
700
+ results.push({
701
+ letters: lettersStr,
702
+ letterDetails,
703
+ vowelsSum: actualVowelsSum,
704
+ consonantsSum: actualConsonantsSum,
705
+ synthesisSum: actualSynthesisSum
706
+ });
707
+ } else {
708
+ hasMore = true;
709
+ }
710
+ }
711
+ }
712
+ }
713
+ if (index >= letterOnlyUnits.length || letterOnlyLength >= maxLength) {
714
+ return;
715
+ }
716
+ if (targetVowels !== void 0 && runningVowelsSum > targetVowels) {
717
+ return;
718
+ }
719
+ if (targetConsonants !== void 0 && runningConsonantsSum > targetConsonants) {
720
+ return;
721
+ }
722
+ if (targetSynthesis !== void 0 && runningVowelsSum + runningConsonantsSum > targetSynthesis) {
723
+ return;
724
+ }
725
+ generateSubsequences(
726
+ index + 1,
727
+ currentLetters,
728
+ currentInfos,
729
+ runningVowelsSum,
730
+ runningConsonantsSum,
731
+ letterCounts,
732
+ spacesUsed
733
+ );
734
+ if (results.length >= maxResults) {
735
+ hasMore = true;
736
+ return;
737
+ }
738
+ const letter = letterOnlyUnits[index].unit;
739
+ if (maxLetterRepeat !== void 0) {
740
+ const currentCount = letterCounts.get(letter) || 0;
741
+ if (currentCount >= maxLetterRepeat) {
742
+ return;
743
+ }
744
+ }
745
+ const isStarting = currentInfos.length === 0;
746
+ const letterInfo = getLetterInfoCached(letter, isStarting);
747
+ if (!letterInfo) return;
748
+ const value = letterInfo.normalValue;
749
+ const newVowelsSum = runningVowelsSum + (letterInfo.isVowel ? value : 0);
750
+ const newConsonantsSum = runningConsonantsSum + (letterInfo.isVowel ? 0 : value);
751
+ if (targetVowels !== void 0 && newVowelsSum > targetVowels) {
752
+ return;
753
+ }
754
+ if (targetConsonants !== void 0 && newConsonantsSum > targetConsonants) {
755
+ return;
756
+ }
757
+ if (targetSynthesis !== void 0 && newVowelsSum + newConsonantsSum > targetSynthesis) {
758
+ return;
759
+ }
760
+ currentLetters.push(letter);
761
+ currentInfos.push(letterInfo);
762
+ letterCounts.set(letter, (letterCounts.get(letter) || 0) + 1);
763
+ generateSubsequences(
764
+ index + 1,
765
+ currentLetters,
766
+ currentInfos,
767
+ newVowelsSum,
768
+ newConsonantsSum,
769
+ letterCounts,
770
+ spacesUsed
771
+ );
772
+ currentLetters.pop();
773
+ currentInfos.pop();
774
+ letterCounts.set(letter, letterCounts.get(letter) - 1);
775
+ if (results.length >= maxResults) {
776
+ hasMore = true;
777
+ return;
778
+ }
779
+ if (spacesUsed < maxSpaces && currentLetters.length > 0 && currentLetters[currentLetters.length - 1] !== " ") {
780
+ currentLetters.push(" ");
781
+ currentLetters.push(letter);
782
+ currentInfos.push(letterInfo);
783
+ letterCounts.set(letter, (letterCounts.get(letter) || 0) + 1);
784
+ generateSubsequences(
785
+ index + 1,
786
+ currentLetters,
787
+ currentInfos,
788
+ newVowelsSum,
789
+ newConsonantsSum,
790
+ letterCounts,
791
+ spacesUsed + 1
792
+ );
793
+ currentLetters.pop();
794
+ currentLetters.pop();
795
+ currentInfos.pop();
796
+ letterCounts.set(letter, letterCounts.get(letter) - 1);
797
+ }
798
+ };
799
+ generateSubsequences(0, [], [], 0, 0, /* @__PURE__ */ new Map(), 0);
800
+ return {
801
+ results,
802
+ hasMore,
803
+ totalFound
804
+ };
805
+ }
413
806
  var calculateGematria = (phrase, options = {
414
807
  missing: false,
415
808
  percentages: false
@@ -477,5 +870,5 @@ var calculateGematria = (phrase, options = {
477
870
  };
478
871
 
479
872
  export { calculateGematria, data_exports, reverseGematria };
480
- //# sourceMappingURL=chunk-N3WZFB6U.mjs.map
481
- //# sourceMappingURL=chunk-N3WZFB6U.mjs.map
873
+ //# sourceMappingURL=chunk-VBO5EFEQ.mjs.map
874
+ //# sourceMappingURL=chunk-VBO5EFEQ.mjs.map