neuralworm commited on
Commit
ddbe479
·
1 Parent(s): d300238

add search interface

Browse files
Files changed (1) hide show
  1. app.py +244 -0
app.py CHANGED
@@ -359,6 +359,82 @@ def update_rounds_dropdown():
359
  new_choices = extract_rounds_combinations()
360
  return new_choices
361
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
362
  # --- Main Gradio App ---
363
  with gr.Blocks() as app:
364
  with gr.Tab("ELS Search"):
@@ -586,6 +662,174 @@ with gr.Blocks() as app:
586
  else:
587
  return {"visible": False, "__type__": "update"}, {"visible": True, "__type__": "update"}
588
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
589
  # --- Event Handlers ---
590
 
591
  search_type.change(
 
359
  new_choices = extract_rounds_combinations()
360
  return new_choices
361
 
362
+ def perform_gematria_calculation_for_date_range(start_date, end_date):
363
+ logger.debug(f"Calculating date gematria for range: {start_date} - {end_date}")
364
+ results = {}
365
+ delta = timedelta(days=1)
366
+ current_date = start_date
367
+
368
+ while current_date <= end_date:
369
+ date_string = current_date.strftime("%Y-%m-%d")
370
+ date_words = date_to_words(date_string)
371
+ date_gematria = calculate_gematria_sum(date_words, "") # Angepasst, um der Funktion calculate_gematria_sum zu entsprechen
372
+
373
+ results[date_string] = {
374
+ "date_words": date_words,
375
+ "date_gematria": date_gematria,
376
+ }
377
+ current_date += delta
378
+ logger.debug(f"Finished calculating date gematria.")
379
+ return results
380
+
381
+ def find_matching_dates(date_gematrias, names, search_journal_sum):
382
+ logger.debug(f"Searching for matches with journal sum: {search_journal_sum}")
383
+ matching_dates = {}
384
+
385
+ for name in names:
386
+ name_gematria = calculate_gematria_sum(name, "") # Angepasst, um der Funktion calculate_gematria_sum zu entsprechen
387
+ target_date_gematria = search_journal_sum - name_gematria if name_gematria is not None else None
388
+ logger.debug(f"Name: {name}, Gematria: {name_gematria}, Target Date Gematria: {target_date_gematria}")
389
+
390
+ if target_date_gematria is not None:
391
+ for date_str, date_data in date_gematrias.items():
392
+ if date_data["date_gematria"] == target_date_gematria:
393
+ if name not in matching_dates:
394
+ matching_dates[name] = []
395
+ matching_dates[name].append(date_str)
396
+ logger.debug(f"Matches for {name}: {matching_dates.get(name, [])}")
397
+ return matching_dates
398
+
399
+ def find_shared_journal_sums(date_gematrias, names):
400
+ """Finds shared journal sums and formats output with names and dates together."""
401
+ logger.debug("Calculating shared journal sums...")
402
+ shared_sums = {}
403
+ name_gematrias = {name: calculate_gematria_sum(name, "") for name in names}
404
+
405
+ for date_str, date_data in date_gematrias.items():
406
+ date_gematria = date_data["date_gematria"]
407
+ for name, name_gematria in name_gematrias.items():
408
+ journal_sum = date_gematria + name_gematria
409
+ journal_sum_str = str(journal_sum) # Konvertiere den Schlüssel (journal_sum) in einen String
410
+ if journal_sum_str not in shared_sums:
411
+ shared_sums[journal_sum_str] = {}
412
+ if name not in shared_sums[journal_sum_str]:
413
+ shared_sums[journal_sum_str][name] = []
414
+ shared_sums[journal_sum_str][name].append(date_str)
415
+
416
+ # Filter out sums not shared by at least two names and format output
417
+ result = {}
418
+ for journal_sum_str, data in shared_sums.items():
419
+ if len(data) >= 2:
420
+ result[journal_sum_str] = {}
421
+ for name, dates in data.items():
422
+ result[journal_sum_str][name] = dates
423
+
424
+ logger.debug(f"Shared Journal Sums: {result}")
425
+ return result
426
+
427
+ def calculate_and_find_dates(start_date, end_date, names_input, search_journal_sum, find_shared=False):
428
+ names = [n.strip() for n in names_input.split("\n") if n.strip()]
429
+ date_gematrias = perform_gematria_calculation_for_date_range(start_date, end_date)
430
+ if find_shared:
431
+ shared_sums = find_shared_journal_sums(date_gematrias, names)
432
+ return None, shared_sums
433
+ else:
434
+ matching_dates = find_matching_dates(date_gematrias, names, int(search_journal_sum))
435
+ return matching_dates, None
436
+
437
+
438
  # --- Main Gradio App ---
439
  with gr.Blocks() as app:
440
  with gr.Tab("ELS Search"):
 
662
  else:
663
  return {"visible": False, "__type__": "update"}, {"visible": True, "__type__": "update"}
664
 
665
+ with gr.Tab("Date Range Journal Sum Search"):
666
+ with gr.Row():
667
+ start_date_jr = Calendar(type="datetime", label="Start Date")
668
+ end_date_jr = Calendar(type="datetime", label="End Date")
669
+ with gr.Row():
670
+ names_input_jr = gr.Textbox(label="Names (one per line)", lines=5)
671
+ search_sum_jr = gr.Number(label="Search Journal Sum", precision=0)
672
+
673
+ with gr.Row():
674
+ calculate_btn_jr = gr.Button("Search Journal Sum")
675
+ shared_sums_btn_jr = gr.Button("Find Shared Journal Sums")
676
+
677
+ matching_dates_output_jr = gr.JSON(label="Matching Dates")
678
+ shared_sums_output_jr = gr.JSON(label="Shared Journal Sums")
679
+
680
+ calculate_btn_jr.click(
681
+ lambda start_date, end_date, names_input, search_sum: calculate_and_find_dates(
682
+ start_date, end_date, names_input, search_sum, find_shared=False),
683
+ inputs=[start_date_jr, end_date_jr, names_input_jr, search_sum_jr],
684
+ outputs=[matching_dates_output_jr, shared_sums_output_jr]
685
+ )
686
+
687
+ shared_sums_btn_jr.click(
688
+ lambda start_date, end_date, names_input: calculate_and_find_dates(
689
+ start_date, end_date, names_input, 0, find_shared=True),
690
+ inputs=[start_date_jr, end_date_jr, names_input_jr],
691
+ outputs=[matching_dates_output_jr, shared_sums_output_jr]
692
+ )
693
+
694
+
695
+ with gr.Tab("Date Range ELS Search"):
696
+ with gr.Row():
697
+ start_date_els = Calendar(type="datetime", label="Start Date")
698
+ end_date_els = Calendar(type="datetime", label="End Date")
699
+ with gr.Row():
700
+ names_input_els = gr.Textbox(label="Names (one per line)", lines=5)
701
+ with gr.Row():
702
+ search_type_els = gr.Radio(
703
+ label="Search by",
704
+ choices=["Text in result_text", "Gematria Sum in results"],
705
+ value="Text in result_text"
706
+ )
707
+ with gr.Row():
708
+ search_mode_els = gr.Radio(
709
+ label="Search Mode",
710
+ choices=["Exact Search", "Contains Word"],
711
+ value="Contains Word"
712
+ )
713
+ with gr.Row():
714
+ search_term_els = gr.Textbox(label="Search Term", visible=True)
715
+ gematria_sum_search_els = gr.Number(label="Gematria Sum", visible=False)
716
+ with gr.Row():
717
+ include_torah_chk_els = gr.Checkbox(label="Include Torah", value=True)
718
+ include_bible_chk_els = gr.Checkbox(label="Include Bible", value=True)
719
+ include_quran_chk_els = gr.Checkbox(label="Include Quran", value=True)
720
+ include_hindu_chk_els = gr.Checkbox(label="Include Rigveda", value=False)
721
+ include_tripitaka_chk_els = gr.Checkbox(label="Include Tripitaka", value=False)
722
+
723
+ with gr.Row():
724
+ perform_search_btn_els = gr.Button("Perform Search")
725
+
726
+ filtered_results_output_els = gr.JSON(label="Filtered Results")
727
+
728
+ # Funktionen zur Aktualisierung der Sichtbarkeit der Sucheingabefelder
729
+ def update_search_components_els(search_type):
730
+ if search_type == "Text in result_text":
731
+ return gr.Textbox(visible=True), gr.Number(visible=False)
732
+ else:
733
+ return gr.Textbox(visible=False), gr.Number(visible=True)
734
+
735
+ search_type_els.change(
736
+ fn=update_search_components_els,
737
+ inputs=[search_type_els],
738
+ outputs=[search_term_els, gematria_sum_search_els]
739
+ )
740
+
741
+ # Hauptfunktion für den vierten Tab
742
+ def perform_date_range_els_search(start_date, end_date, names_input, search_type, search_term, gematria_sum_search, search_mode, include_torah, include_bible, include_quran, include_hindu, include_tripitaka):
743
+ names = [n.strip() for n in names_input.split("\n") if n.strip()]
744
+ date_gematrias = perform_gematria_calculation_for_date_range(start_date, end_date)
745
+
746
+ # Zwischenergebnisse mit Datum, Namen und Gematria-Summe speichern
747
+ intermediate_results = []
748
+ for date_str, date_data in date_gematrias.items():
749
+ for name in names:
750
+ name_gematria = calculate_gematria_sum(name, "")
751
+ combined_gematria_sum = date_data["date_gematria"] + name_gematria
752
+ intermediate_results.append(
753
+ {"date": date_str, "name": name, "gematria_sum": combined_gematria_sum}
754
+ )
755
+
756
+ # Ergebnisse nach Datum sortieren
757
+ intermediate_results.sort(key=lambda x: x["date"])
758
+
759
+ all_results = []
760
+ for intermediate_result in intermediate_results:
761
+ date_str = intermediate_result["date"]
762
+ name = intermediate_result["name"]
763
+ gematria_sum = intermediate_result["gematria_sum"]
764
+
765
+ # Hier die ELS-Suche für jede Gematria-Summe durchführen
766
+ els_results = perform_els_search(
767
+ step=gematria_sum,
768
+ rounds_combination="1,-1",
769
+ tlang="english",
770
+ strip_spaces=True,
771
+ strip_in_braces=True,
772
+ strip_diacritics_chk=True,
773
+ include_torah=include_torah,
774
+ include_bible=include_bible,
775
+ include_quran=include_quran,
776
+ include_hindu=include_hindu,
777
+ include_tripitaka=include_tripitaka
778
+ )
779
+
780
+ # Die Ergebnisse für jedes Buch und jede Gematria-Summe verarbeiten
781
+ for book_name, book_results in els_results.items():
782
+ if book_results:
783
+ for result in book_results:
784
+ try:
785
+ result_text = result['result_text']
786
+ result_sum = result['result_sum']
787
+
788
+ # Filtern der Ergebnisse
789
+ if search_type == "Text in result_text":
790
+ if search_mode == "Exact Search" and search_term == result_text:
791
+ all_results.append(
792
+ {
793
+ "date": date_str,
794
+ "name": name,
795
+ "gematria_sum": gematria_sum,
796
+ "book": book_name,
797
+ "result": result
798
+ }
799
+ )
800
+ elif search_mode == "Contains Word" and search_term in result_text:
801
+ all_results.append(
802
+ {
803
+ "date": date_str,
804
+ "name": name,
805
+ "gematria_sum": gematria_sum,
806
+ "book": book_name,
807
+ "result": result
808
+ }
809
+ )
810
+ elif search_type == "Gematria Sum in results":
811
+ if result_sum == gematria_sum_search:
812
+ all_results.append(
813
+ {
814
+ "date": date_str,
815
+ "name": name,
816
+ "gematria_sum": gematria_sum,
817
+ "book": book_name,
818
+ "result": result
819
+ }
820
+ )
821
+ except KeyError as e:
822
+ logger.error(f"KeyError - Key '{e.args[0]}' not found in result. Skipping this result.")
823
+ continue
824
+
825
+ return all_results
826
+
827
+ perform_search_btn_els.click(
828
+ perform_date_range_els_search,
829
+ inputs=[start_date_els, end_date_els, names_input_els, search_type_els, search_term_els, gematria_sum_search_els, search_mode_els, include_torah_chk_els, include_bible_chk_els, include_quran_chk_els, include_hindu_chk_els, include_tripitaka_chk_els],
830
+ outputs=[filtered_results_output_els]
831
+ )
832
+
833
  # --- Event Handlers ---
834
 
835
  search_type.change(