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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import logging
|
2 |
+
logger = logging.getLogger(__name__)
|
3 |
+
logging.basicConfig(level=logging.INFO)
|
4 |
+
|
5 |
+
import gradio as gr
|
6 |
+
import torah
|
7 |
+
import bible
|
8 |
+
import quran
|
9 |
+
from utils import number_to_ordinal_word, custom_normalize, date_to_words, translate_date_to_words
|
10 |
+
from gematria import calculate_gematria, strip_diacritics
|
11 |
+
|
12 |
+
import pandas as pd
|
13 |
+
from deep_translator import GoogleTranslator
|
14 |
+
from gradio_calendar import Calendar
|
15 |
+
from datetime import datetime
|
16 |
+
import math
|
17 |
+
import json
|
18 |
+
import re
|
19 |
+
import sqlite3
|
20 |
+
from collections import defaultdict
|
21 |
+
|
22 |
+
# --- Constants ---
|
23 |
+
DATABASE_FILE = 'gematria.db'
|
24 |
+
MAX_PHRASE_LENGTH_LIMIT = 20
|
25 |
+
|
26 |
+
# --- Database Initialization ---
|
27 |
+
def initialize_database():
|
28 |
+
global conn
|
29 |
+
conn = sqlite3.connect(DATABASE_FILE)
|
30 |
+
cursor = conn.cursor()
|
31 |
+
cursor.execute('''
|
32 |
+
CREATE TABLE IF NOT EXISTS results (
|
33 |
+
gematria_sum INTEGER,
|
34 |
+
words TEXT,
|
35 |
+
translation TEXT,
|
36 |
+
book TEXT,
|
37 |
+
chapter INTEGER,
|
38 |
+
verse INTEGER,
|
39 |
+
phrase_length INTEGER,
|
40 |
+
word_position TEXT,
|
41 |
+
PRIMARY KEY (gematria_sum, words, book, chapter, verse, word_position)
|
42 |
+
)
|
43 |
+
''')
|
44 |
+
cursor.execute('''
|
45 |
+
CREATE INDEX IF NOT EXISTS idx_results_gematria
|
46 |
+
ON results (gematria_sum)
|
47 |
+
''')
|
48 |
+
cursor.execute('''
|
49 |
+
CREATE TABLE IF NOT EXISTS processed_books (
|
50 |
+
book TEXT PRIMARY KEY,
|
51 |
+
max_phrase_length INTEGER
|
52 |
+
)
|
53 |
+
''')
|
54 |
+
conn.commit()
|
55 |
+
|
56 |
+
# --- Initialize Database ---
|
57 |
+
initialize_database()
|
58 |
+
|
59 |
+
# --- Helper Functions (from Network app.py) ---
|
60 |
+
def flatten_text(text: List) -> str:
|
61 |
+
if isinstance(text, list):
|
62 |
+
return " ".join(flatten_text(item) if isinstance(item, list) else item for item in text)
|
63 |
+
return text
|
64 |
+
|
65 |
+
def search_gematria_in_db(gematria_sum: int, max_words: int) -> List[Tuple[str, str, int, int, int, str]]:
|
66 |
+
global conn
|
67 |
+
with sqlite3.connect(DATABASE_FILE) as conn:
|
68 |
+
cursor = conn.cursor()
|
69 |
+
cursor.execute('''
|
70 |
+
SELECT words, book, chapter, verse, phrase_length, word_position
|
71 |
+
FROM results
|
72 |
+
WHERE gematria_sum = ? AND phrase_length <= ?
|
73 |
+
''', (gematria_sum, max_words))
|
74 |
+
results = cursor.fetchall()
|
75 |
+
return results
|
76 |
+
|
77 |
+
def get_most_frequent_phrase(results):
|
78 |
+
phrase_counts = defaultdict(int)
|
79 |
+
for words, book, chapter, verse, phrase_length, word_position in results:
|
80 |
+
phrase_counts[words] += 1
|
81 |
+
most_frequent_phrase = max(phrase_counts, key=phrase_counts.get)
|
82 |
+
return most_frequent_phrase
|
83 |
+
|
84 |
+
# --- Functions from BOS app.py ---
|
85 |
+
def create_language_dropdown(label, default_value='en', show_label=True):
|
86 |
+
languages = GoogleTranslator(source='en', target='en').get_supported_languages(as_dict=True)
|
87 |
+
return gr.Dropdown(
|
88 |
+
choices=list(languages.keys()),
|
89 |
+
label=label,
|
90 |
+
value=default_value,
|
91 |
+
show_label=show_label
|
92 |
+
)
|
93 |
+
|
94 |
+
def calculate_gematria_sum(text, date_words):
|
95 |
+
if text or date_words:
|
96 |
+
combined_input = f"{text} {date_words}"
|
97 |
+
numbers = re.findall(r'\d+', combined_input)
|
98 |
+
text_without_numbers = re.sub(r'\d+', '', combined_input)
|
99 |
+
number_sum = sum(int(number) for number in numbers)
|
100 |
+
text_gematria = calculate_gematria(strip_diacritics(text_without_numbers))
|
101 |
+
total_sum = text_gematria + number_sum
|
102 |
+
return total_sum
|
103 |
+
else:
|
104 |
+
return None
|
105 |
+
|
106 |
+
def perform_els_search(step, rounds_combination, tlang, strip_spaces, strip_in_braces, strip_diacritics_chk, merge_results, include_torah, include_bible, include_quran):
|
107 |
+
if step == 0 or rounds_combination == "0,0":
|
108 |
+
return None
|
109 |
+
|
110 |
+
torah_results = []
|
111 |
+
bible_results = []
|
112 |
+
quran_results = []
|
113 |
+
|
114 |
+
if include_torah:
|
115 |
+
torah_results.extend(torah.process_json_files(1, 39, step, rounds_combination, 0, tlang, strip_spaces, strip_in_braces, strip_diacritics))
|
116 |
+
|
117 |
+
if include_bible:
|
118 |
+
bible_results.extend(bible.process_json_files(40, 66, step, rounds_combination, 0, tlang, strip_spaces, strip_in_braces, strip_diacritics))
|
119 |
+
|
120 |
+
if include_quran:
|
121 |
+
quran_results.extend(quran.process_json_files(1, 114, step, rounds_combination, 0, tlang, strip_spaces, strip_in_braces, strip_diacritics))
|
122 |
+
|
123 |
+
if merge_results:
|
124 |
+
results = []
|
125 |
+
max_length = max(len(torah_results), len(bible_results), len(quran_results))
|
126 |
+
for i in range(max_length):
|
127 |
+
if i < len(torah_results):
|
128 |
+
results.append(torah_results[i])
|
129 |
+
if i < len(bible_results):
|
130 |
+
results.append(bible_results[i])
|
131 |
+
if i < len(quran_results):
|
132 |
+
results.append(quran_results[i])
|
133 |
+
else:
|
134 |
+
results = torah_results + bible_results + quran_results
|
135 |
+
|
136 |
+
return results
|
137 |
+
|
138 |
+
# --- Main Gradio App ---
|
139 |
+
with gr.Blocks() as app:
|
140 |
+
with gr.Row():
|
141 |
+
tlang = create_language_dropdown("Target Language for Translation", default_value='english')
|
142 |
+
selected_date = Calendar(type="datetime", label="Date to investigate (optional)", info="Pick a date from the calendar")
|
143 |
+
date_language_input = create_language_dropdown("Language of the person/topic (optional) (Date Word Language)", default_value='english')
|
144 |
+
date_words_output = gr.Textbox(label="Date in Words Translated (optional)")
|
145 |
+
|
146 |
+
with gr.Row():
|
147 |
+
gematria_text = gr.Textbox(label="Name and/or Topic (required)", value="Hans Albert Einstein")
|
148 |
+
gematria_result = gr.Number(label="Journal Sum")
|
149 |
+
|
150 |
+
with gr.Row():
|
151 |
+
step = gr.Number(label="Jump Width (Steps) for ELS")
|
152 |
+
float_step = gr.Number(visible=False, value=1)
|
153 |
+
half_step_btn = gr.Button("Steps / 2")
|
154 |
+
double_step_btn = gr.Button("Steps * 2")
|
155 |
+
|
156 |
+
with gr.Column():
|
157 |
+
round_x = gr.Number(label="Round (1)", value=1)
|
158 |
+
round_y = gr.Number(label="Round (2)", value=-1)
|
159 |
+
|
160 |
+
rounds_combination = gr.Textbox(label="Combined Rounds", value="1,-1")
|
161 |
+
|
162 |
+
with gr.Row():
|
163 |
+
include_torah_chk = gr.Checkbox(label="Include Torah", value=True)
|
164 |
+
include_bible_chk = gr.Checkbox(label="Include Bible", value=True)
|
165 |
+
include_quran_chk = gr.Checkbox(label="Include Quran", value=True)
|
166 |
+
merge_results_chk = gr.Checkbox(label="Merge Results (Torah-Bible-Quran)", value=True)
|
167 |
+
|
168 |
+
strip_spaces = gr.Checkbox(label="Strip Spaces from Books", value=True)
|
169 |
+
strip_in_braces = gr.Checkbox(label="Strip Text in Braces from Books", value=True)
|
170 |
+
strip_diacritics_chk = gr.Checkbox(label="Strip Diacritics from Books", value=True)
|
171 |
+
|
172 |
+
translate_btn = gr.Button("Search with ELS")
|
173 |
+
|
174 |
+
# --- Output Components ---
|
175 |
+
markdown_output = gr.Dataframe(label="ELS Results")
|
176 |
+
most_frequent_phrase_output = gr.Textbox(label="Most Frequent Phrase in Network Search")
|
177 |
+
|
178 |
+
# --- Event Handlers ---
|
179 |
+
def update_date_words(selected_date, date_language_input):
|
180 |
+
return translate_date_to_words(selected_date, date_language_input)
|
181 |
+
|
182 |
+
def update_journal_sum(gematria_text, date_words_output):
|
183 |
+
sum_value = calculate_gematria_sum(gematria_text, date_words_output)
|
184 |
+
return sum_value, sum_value, sum_value
|
185 |
+
|
186 |
+
def update_rounds_combination(round_x, round_y):
|
187 |
+
return f"{int(round_x)},{int(round_y)}"
|
188 |
+
|
189 |
+
def update_step_half(float_step):
|
190 |
+
new_step = math.ceil(float_step / 2)
|
191 |
+
return new_step, float_step / 2
|
192 |
+
|
193 |
+
def update_step_double(float_step):
|
194 |
+
new_step = math.ceil(float_step * 2)
|
195 |
+
return new_step, float_step * 2
|
196 |
+
|
197 |
+
def perform_search(step, rounds_combination, tlang, strip_spaces, strip_in_braces, strip_diacritics_chk, merge_results, include_torah, include_bible, include_quran, gematria_text, date_words_output):
|
198 |
+
els_results = perform_els_search(step, rounds_combination, tlang, strip_spaces, strip_in_braces, strip_diacritics_chk, merge_results, include_torah, include_bible, include_quran)
|
199 |
+
|
200 |
+
# --- Network Search Integration ---
|
201 |
+
network_search_results = []
|
202 |
+
for result in els_results:
|
203 |
+
gematria_sum = calculate_gematria(result['match'])
|
204 |
+
max_words = len(result['match'].split())
|
205 |
+
matching_phrases = search_gematria_in_db(gematria_sum, max_words)
|
206 |
+
network_search_results.extend(matching_phrases)
|
207 |
+
|
208 |
+
most_frequent_phrase = get_most_frequent_phrase(network_search_results)
|
209 |
+
|
210 |
+
# --- Prepare Dataframe ---
|
211 |
+
df = pd.DataFrame(els_results)
|
212 |
+
df.index = range(1, len(df) + 1)
|
213 |
+
df.reset_index(inplace=True)
|
214 |
+
df.rename(columns={'index': 'Result Number'}, inplace=True)
|
215 |
+
df['Most Frequent Phrase'] = most_frequent_phrase # Add new column
|
216 |
+
|
217 |
+
return df, most_frequent_phrase
|
218 |
+
|
219 |
+
# --- Event Triggers ---
|
220 |
+
round_x.change(update_rounds_combination, inputs=[round_x, round_y], outputs=rounds_combination)
|
221 |
+
round_y.change(update_rounds_combination, inputs=[round_x, round_y], outputs=rounds_combination)
|
222 |
+
|
223 |
+
selected_date.change(update_date_words, inputs=[selected_date, date_language_input], outputs=[date_words_output])
|
224 |
+
date_language_input.change(update_date_words, inputs=[selected_date, date_language_input], outputs=[date_words_output])
|
225 |
+
|
226 |
+
gematria_text.change(update_journal_sum, inputs=[gematria_text, date_words_output], outputs=[gematria_result, step, float_step])
|
227 |
+
date_words_output.change(update_journal_sum, inputs=[gematria_text, date_words_output], outputs=[gematria_result, step, float_step])
|
228 |
+
|
229 |
+
half_step_btn.click(update_step_half, inputs=[float_step], outputs=[step, float_step])
|
230 |
+
double_step_btn.click(update_step_double, inputs=[float_step], outputs=[step, float_step])
|
231 |
+
|
232 |
+
translate_btn.click(
|
233 |
+
perform_search,
|
234 |
+
inputs=[step, rounds_combination, tlang, strip_spaces, strip_in_braces, strip_diacritics_chk, merge_results_chk, include_torah_chk, include_bible_chk, include_quran_chk, gematria_text, date_words_output],
|
235 |
+
outputs=[markdown_output, most_frequent_phrase_output]
|
236 |
+
)
|
237 |
+
|
238 |
+
app.load(
|
239 |
+
update_date_words,
|
240 |
+
inputs=[selected_date, date_language_input],
|
241 |
+
outputs=[date_words_output]
|
242 |
+
)
|
243 |
+
app.load(
|
244 |
+
update_journal_sum,
|
245 |
+
inputs=[gematria_text, date_words_output],
|
246 |
+
outputs=[gematria_result, step, float_step]
|
247 |
+
)
|
248 |
+
|
249 |
+
if __name__ == "__main__":
|
250 |
+
app.launch(share=False)
|