Spaces:
Running
Running
Commit
·
5d23d59
1
Parent(s):
9063a8e
simple calculation for Sura
Browse files- app.py +1 -2
- els_cache.db +1 -1
- quran.py +72 -115
app.py
CHANGED
@@ -372,8 +372,7 @@ 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 |
-
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."
|
|
|
372 |
logger.debug(f"Adjusted Gematria Sum (using formula): {initial_gematria_sum}")
|
373 |
|
374 |
# Use ELS search on the Quran text, not Genesis
|
375 |
+
els_result = quran_module.get_first_els_result_quran(initial_gematria_sum, tlang='en')
|
|
|
376 |
if not els_result:
|
377 |
logger.debug("No ELS result. Returning error message.")
|
378 |
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 24576
|
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:92f45b840047aa969a71057a9c7635071c96c4a6ed32c01c156f1d26abe92f0e
|
3 |
size 24576
|
quran.py
CHANGED
@@ -157,32 +157,25 @@ 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"
|
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
|
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}
|
183 |
|
184 |
-
# Create a cache key
|
185 |
-
cache_key = f"quran_els_{gematria_sum}_{tlang}
|
186 |
cache_file = "els_cache.db"
|
187 |
|
188 |
# Check cache first
|
@@ -202,125 +195,89 @@ def get_first_els_result_quran(gematria_sum: int, tlang: str = "en", rounds_comb
|
|
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
|
206 |
-
|
|
|
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
|
221 |
-
|
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 |
-
#
|
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 |
-
|
256 |
-
|
257 |
-
|
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 |
-
#
|
276 |
-
|
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 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
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 |
-
|
305 |
-
"
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
|
318 |
-
#
|
319 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
320 |
|
321 |
-
|
322 |
-
|
323 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
324 |
|
325 |
# Cache the result
|
326 |
if result:
|
@@ -342,7 +299,7 @@ def get_first_els_result_quran(gematria_sum: int, tlang: str = "en", rounds_comb
|
|
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
|
346 |
conn.commit()
|
347 |
logger.debug("Cached Quran ELS results in database.")
|
348 |
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") -> 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 |
+
from gematria import strip_diacritics
|
|
|
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 |
# Cache miss, perform ELS search
|
196 |
logger.info(f"Cache miss for Quran ELS query: {cache_key}, performing search")
|
197 |
|
198 |
+
# Load all Quran text
|
199 |
+
sura_count = get_sura_count()
|
200 |
+
quran_data = process_quran_files(1, sura_count)
|
201 |
|
202 |
+
# Concatenate all verses from all suras into a single text
|
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 |
+
verses = sura_info['text']
|
211 |
all_text += " ".join(verses)
|
|
|
|
|
|
|
|
|
|
|
|
|
212 |
|
213 |
+
# Clean up the text: strip diacritics, remove any special characters, etc.
|
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 |
+
# Perform ELS search with the gematria_sum as the step
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
218 |
result = None
|
219 |
+
if clean_text:
|
220 |
+
# Remove spaces for ELS search
|
221 |
+
text_no_spaces = clean_text.replace(" ", "")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
222 |
|
223 |
+
# Track character positions to their original sura/verse
|
224 |
+
char_map = [] # List of (sura_id, verse_id) for each character
|
|
|
|
|
|
|
|
|
|
|
|
|
225 |
|
226 |
+
# Build character position mapping
|
227 |
+
current_pos = 0
|
228 |
+
for sura_id, sura_info in sorted(quran_data.items()):
|
229 |
+
sura_name = sura_info['name']
|
230 |
+
verses = sura_info['text']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
231 |
|
232 |
+
for verse_idx, verse in enumerate(verses, 1):
|
233 |
+
cleaned_verse = strip_diacritics(verse).replace(" ", "")
|
234 |
+
for _ in cleaned_verse:
|
235 |
+
if current_pos < len(text_no_spaces):
|
236 |
+
char_map.append((sura_id, sura_name, verse_idx))
|
237 |
+
current_pos += 1
|
238 |
+
|
239 |
+
# Start positions to try (we'll try the first 100 positions for better coverage)
|
240 |
+
for start_pos in range(min(100, len(text_no_spaces))):
|
241 |
+
# Extract characters at positions: start_pos, start_pos+step, start_pos+2*step, etc.
|
242 |
+
extracted = ""
|
243 |
+
positions = []
|
244 |
+
pos = start_pos
|
245 |
|
246 |
+
# Extract up to 7 characters (typical ELS result length)
|
247 |
+
for _ in range(7):
|
248 |
+
if pos < len(text_no_spaces):
|
249 |
+
extracted += text_no_spaces[pos]
|
250 |
+
positions.append(pos)
|
251 |
+
pos += gematria_sum
|
252 |
+
else:
|
253 |
+
break
|
254 |
|
255 |
+
if len(extracted) >= 3: # At least 3 characters
|
256 |
+
# Look up the sura/verse for the first and last character
|
257 |
+
first_pos = positions[0]
|
258 |
+
last_pos = positions[-1]
|
259 |
+
|
260 |
+
if first_pos < len(char_map) and last_pos < len(char_map):
|
261 |
+
first_loc = char_map[first_pos]
|
262 |
+
last_loc = char_map[last_pos]
|
263 |
+
|
264 |
+
result = {
|
265 |
+
"result_text": extracted,
|
266 |
+
"source": "Quran",
|
267 |
+
"start_position": start_pos,
|
268 |
+
"step": gematria_sum,
|
269 |
+
"start_sura": first_loc[0],
|
270 |
+
"start_sura_name": first_loc[1],
|
271 |
+
"start_verse": first_loc[2],
|
272 |
+
"end_sura": last_loc[0],
|
273 |
+
"end_sura_name": last_loc[1],
|
274 |
+
"end_verse": last_loc[2],
|
275 |
+
"positions": positions
|
276 |
+
}
|
277 |
+
break # Found a result, stop searching
|
278 |
+
else:
|
279 |
+
logger.warning(f"Character position mapping inconsistency: {first_pos}, {last_pos} vs {len(char_map)}")
|
280 |
+
continue
|
281 |
|
282 |
# Cache the result
|
283 |
if result:
|
|
|
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:
|