Spaces:
Running
Running
Commit
·
9063a8e
1
Parent(s):
434c55b
rounds combination
Browse files- app.py +2 -1
- els_cache.db +2 -2
- quran.py +115 -72
app.py
CHANGED
@@ -372,7 +372,8 @@ with gr.Blocks() as app:
|
|
372 |
logger.debug(f"Adjusted Gematria Sum (using formula): {initial_gematria_sum}")
|
373 |
|
374 |
# Use ELS search on the Quran text, not Genesis
|
375 |
-
|
|
|
376 |
if not els_result:
|
377 |
logger.debug("No ELS result. Returning error message.")
|
378 |
return "No ELS result found in the Quran text."
|
|
|
372 |
logger.debug(f"Adjusted Gematria Sum (using formula): {initial_gematria_sum}")
|
373 |
|
374 |
# Use ELS search on the Quran text, not Genesis
|
375 |
+
# Use combined +1,-1 rounds as requested
|
376 |
+
els_result = quran_module.get_first_els_result_quran(initial_gematria_sum, tlang='en', rounds_combination="1,-1")
|
377 |
if not els_result:
|
378 |
logger.debug("No ELS result. Returning error message.")
|
379 |
return "No ELS result found in the Quran text."
|
els_cache.db
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
-
oid sha256:
|
3 |
-
size
|
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:ebdf43f0d50fc9a1ea4174cc233f2017f4c4dbe56b5390ecfdf06bf3b91d23f6
|
3 |
+
size 24576
|
quran.py
CHANGED
@@ -157,25 +157,32 @@ def get_sura_count() -> int:
|
|
157 |
return 114 # Default number of suras in the Quran
|
158 |
|
159 |
|
160 |
-
def get_first_els_result_quran(gematria_sum: int, tlang: str = "en") -> Dict[str, Any]:
|
161 |
"""
|
162 |
-
Gets the first ELS result from the Quran using the gematria sum as the step
|
|
|
|
|
|
|
|
|
|
|
163 |
|
164 |
Args:
|
165 |
gematria_sum: The gematria value to use as the ELS step
|
166 |
tlang: Target language for results
|
|
|
167 |
|
168 |
Returns:
|
169 |
The first ELS result found or None
|
170 |
"""
|
171 |
import hashlib
|
172 |
import json
|
173 |
-
|
|
|
174 |
|
175 |
-
logger.debug(f"Entering get_first_els_result_quran with gematria_sum: {gematria_sum}, tlang: {tlang}")
|
176 |
|
177 |
-
# Create a cache key
|
178 |
-
cache_key = f"quran_els_{gematria_sum}_{tlang}"
|
179 |
cache_file = "els_cache.db"
|
180 |
|
181 |
# Check cache first
|
@@ -195,89 +202,125 @@ def get_first_els_result_quran(gematria_sum: int, tlang: str = "en") -> Dict[str
|
|
195 |
# Cache miss, perform ELS search
|
196 |
logger.info(f"Cache miss for Quran ELS query: {cache_key}, performing search")
|
197 |
|
198 |
-
# Load
|
199 |
-
|
200 |
-
quran_data = process_quran_files(1, sura_count)
|
201 |
|
202 |
-
# Concatenate
|
203 |
all_text = ""
|
|
|
|
|
204 |
for sura_id, sura_info in sorted(quran_data.items()):
|
|
|
|
|
|
|
205 |
# Add a space between suras to prevent cross-sura word formation
|
206 |
if all_text:
|
207 |
all_text += " "
|
208 |
|
209 |
-
# Add all verses from this sura
|
210 |
-
|
211 |
all_text += " ".join(verses)
|
|
|
|
|
|
|
|
|
|
|
|
|
212 |
|
213 |
-
# Clean up the text: strip diacritics, remove
|
214 |
clean_text = strip_diacritics(all_text)
|
215 |
clean_text = ''.join(c for c in clean_text if c.isalpha() or c.isspace())
|
216 |
|
217 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
218 |
result = None
|
219 |
-
|
220 |
-
|
221 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
222 |
|
223 |
-
#
|
224 |
-
|
|
|
225 |
|
226 |
-
#
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
|
|
238 |
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
positions = []
|
244 |
-
pos = start_pos
|
245 |
|
246 |
-
#
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
positions.append(pos)
|
251 |
-
pos += gematria_sum
|
252 |
-
else:
|
253 |
-
break
|
254 |
|
255 |
-
if
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
|
|
|
|
|
|
|
|
281 |
|
282 |
# Cache the result
|
283 |
if result:
|
@@ -299,7 +342,7 @@ def get_first_els_result_quran(gematria_sum: int, tlang: str = "en") -> Dict[str
|
|
299 |
cursor.execute(
|
300 |
"INSERT OR REPLACE INTO els_cache (query_hash, function_name, args, kwargs, results) VALUES (?, ?, ?, ?, ?)",
|
301 |
(hashlib.sha256(cache_key.encode()).hexdigest(), "get_first_els_result_quran",
|
302 |
-
json.dumps([gematria_sum]), json.dumps({"tlang": tlang}), json.dumps(result)))
|
303 |
conn.commit()
|
304 |
logger.debug("Cached Quran ELS results in database.")
|
305 |
except sqlite3.Error as e:
|
|
|
157 |
return 114 # Default number of suras in the Quran
|
158 |
|
159 |
|
160 |
+
def get_first_els_result_quran(gematria_sum: int, tlang: str = "en", rounds_combination: str = "1,-1") -> Dict[str, Any]:
|
161 |
"""
|
162 |
+
Gets the first ELS result from the Quran using the gematria sum as the step,
|
163 |
+
following the same method as Torah ELS: combined +1/-1 rounds.
|
164 |
+
|
165 |
+
For Quran, the implementation specifically:
|
166 |
+
1. Takes +1 ELS round from the start of book 1 to the end of book 2
|
167 |
+
2. Takes -1 ELS round from the end of book 2 to the start of book 1
|
168 |
|
169 |
Args:
|
170 |
gematria_sum: The gematria value to use as the ELS step
|
171 |
tlang: Target language for results
|
172 |
+
rounds_combination: Comma-separated string of round directions, defaults to "1,-1"
|
173 |
|
174 |
Returns:
|
175 |
The first ELS result found or None
|
176 |
"""
|
177 |
import hashlib
|
178 |
import json
|
179 |
+
import math
|
180 |
+
from gematria import strip_diacritics, calculate_gematria
|
181 |
|
182 |
+
logger.debug(f"Entering get_first_els_result_quran with gematria_sum: {gematria_sum}, tlang: {tlang}, rounds_combination: {rounds_combination}")
|
183 |
|
184 |
+
# Create a cache key including the rounds_combination
|
185 |
+
cache_key = f"quran_els_{gematria_sum}_{tlang}_{rounds_combination}"
|
186 |
cache_file = "els_cache.db"
|
187 |
|
188 |
# Check cache first
|
|
|
202 |
# Cache miss, perform ELS search
|
203 |
logger.info(f"Cache miss for Quran ELS query: {cache_key}, performing search")
|
204 |
|
205 |
+
# Load Quran text for books 1 and 2 only (based on the requirement)
|
206 |
+
quran_data = process_quran_files(1, 2) # Only books 1 and 2 as specified
|
|
|
207 |
|
208 |
+
# Concatenate verses into a single text
|
209 |
all_text = ""
|
210 |
+
sura_verse_map = [] # Track (sura_id, sura_name, verse_idx) for each character
|
211 |
+
|
212 |
for sura_id, sura_info in sorted(quran_data.items()):
|
213 |
+
sura_name = sura_info['name']
|
214 |
+
verses = sura_info['text']
|
215 |
+
|
216 |
# Add a space between suras to prevent cross-sura word formation
|
217 |
if all_text:
|
218 |
all_text += " "
|
219 |
|
220 |
+
# Add all verses from this sura and track the mapping
|
221 |
+
sura_start_pos = len(all_text)
|
222 |
all_text += " ".join(verses)
|
223 |
+
|
224 |
+
# Track character positions to their original sura/verse for later lookup
|
225 |
+
current_verse_start = sura_start_pos
|
226 |
+
for verse_idx, verse in enumerate(verses, 1):
|
227 |
+
for _ in range(len(verse) + (1 if verse_idx < len(verses) else 0)): # Add 1 for space between verses
|
228 |
+
sura_verse_map.append((sura_id, sura_name, verse_idx))
|
229 |
|
230 |
+
# Clean up the text: strip diacritics, remove special characters
|
231 |
clean_text = strip_diacritics(all_text)
|
232 |
clean_text = ''.join(c for c in clean_text if c.isalpha() or c.isspace())
|
233 |
|
234 |
+
# Remove spaces for ELS search
|
235 |
+
text_no_spaces = clean_text.replace(" ", "")
|
236 |
+
text_length = len(text_no_spaces)
|
237 |
+
|
238 |
+
if text_length == 0:
|
239 |
+
logger.warning("No text available after cleaning")
|
240 |
+
return None
|
241 |
+
|
242 |
+
# Build a more accurate character map without spaces
|
243 |
+
char_map = []
|
244 |
+
char_idx = 0
|
245 |
+
for i, c in enumerate(clean_text):
|
246 |
+
if c.isalpha():
|
247 |
+
if char_idx < len(sura_verse_map):
|
248 |
+
char_map.append(sura_verse_map[i])
|
249 |
+
char_idx += 1
|
250 |
+
|
251 |
+
# Parse rounds combination - default is "1,-1"
|
252 |
+
rounds_list = list(map(float, rounds_combination.split(',')))
|
253 |
+
|
254 |
result = None
|
255 |
+
complete_result = ""
|
256 |
+
complete_positions = []
|
257 |
+
first_position = None
|
258 |
+
last_position = None
|
259 |
+
|
260 |
+
# Process each round direction (similar to Torah ELS)
|
261 |
+
for round_dir in rounds_list:
|
262 |
+
# Determine if this is a forward or backward search
|
263 |
+
is_forward = round_dir > 0
|
264 |
+
start_index = 0 if is_forward else (text_length - 1)
|
265 |
|
266 |
+
# Set step and direction
|
267 |
+
step = gematria_sum
|
268 |
+
direction = 1 if is_forward else -1
|
269 |
|
270 |
+
# Extract ELS characters
|
271 |
+
round_text = ""
|
272 |
+
positions = []
|
273 |
+
pos = start_index
|
274 |
+
|
275 |
+
# Extract up to 10 characters, but we'll use at least 3 for a valid result
|
276 |
+
for _ in range(10):
|
277 |
+
if 0 <= pos < text_length:
|
278 |
+
round_text += text_no_spaces[pos]
|
279 |
+
positions.append(pos)
|
280 |
+
pos += direction * step
|
281 |
+
else:
|
282 |
+
break
|
283 |
|
284 |
+
if len(round_text) >= 3:
|
285 |
+
# Save this round's results
|
286 |
+
complete_result += round_text
|
287 |
+
complete_positions.extend(positions)
|
|
|
|
|
288 |
|
289 |
+
# Track first and last positions for the overall result
|
290 |
+
if first_position is None or (is_forward and positions[0] < first_position):
|
291 |
+
first_position = positions[0]
|
292 |
+
first_loc = char_map[first_position] if first_position < len(char_map) else None
|
|
|
|
|
|
|
|
|
293 |
|
294 |
+
if last_position is None or (not is_forward and positions[-1] > last_position):
|
295 |
+
last_position = positions[-1]
|
296 |
+
last_loc = char_map[last_position] if last_position < len(char_map) else None
|
297 |
+
|
298 |
+
# Create result if we found something
|
299 |
+
if complete_result and len(complete_result) >= 3 and first_position is not None and last_position is not None:
|
300 |
+
if first_position < len(char_map) and last_position < len(char_map):
|
301 |
+
first_loc = char_map[first_position]
|
302 |
+
last_loc = char_map[last_position]
|
303 |
+
|
304 |
+
result = {
|
305 |
+
"result_text": complete_result,
|
306 |
+
"source": "Quran",
|
307 |
+
"step": gematria_sum,
|
308 |
+
"start_sura": first_loc[0],
|
309 |
+
"start_sura_name": first_loc[1],
|
310 |
+
"start_verse": first_loc[2],
|
311 |
+
"end_sura": last_loc[0],
|
312 |
+
"end_sura_name": last_loc[1],
|
313 |
+
"end_verse": last_loc[2],
|
314 |
+
"positions": complete_positions,
|
315 |
+
"rounds_combination": rounds_combination
|
316 |
+
}
|
317 |
+
|
318 |
+
# Calculate gematria of the result text
|
319 |
+
result["result_sum"] = calculate_gematria(complete_result)
|
320 |
+
|
321 |
+
logger.debug(f"Found ELS result: {complete_result} with gematria {result['result_sum']}")
|
322 |
+
else:
|
323 |
+
logger.warning(f"Character position mapping inconsistency: {first_position}, {last_position} vs {len(char_map)}")
|
324 |
|
325 |
# Cache the result
|
326 |
if result:
|
|
|
342 |
cursor.execute(
|
343 |
"INSERT OR REPLACE INTO els_cache (query_hash, function_name, args, kwargs, results) VALUES (?, ?, ?, ?, ?)",
|
344 |
(hashlib.sha256(cache_key.encode()).hexdigest(), "get_first_els_result_quran",
|
345 |
+
json.dumps([gematria_sum]), json.dumps({"tlang": tlang, "rounds_combination": rounds_combination}), json.dumps(result)))
|
346 |
conn.commit()
|
347 |
logger.debug("Cached Quran ELS results in database.")
|
348 |
except sqlite3.Error as e:
|