bartman081523 commited on
Commit
f83d5b7
·
1 Parent(s): b7151e3

fix max word count in results

Browse files
Files changed (1) hide show
  1. app.py +16 -13
app.py CHANGED
@@ -167,7 +167,7 @@ def translate_and_store(phrase):
167
  logging.debug(f"Translated phrase: {translation}")
168
  return translation
169
  except (exceptions.TranslationNotFound, exceptions.NotValidPayload,
170
- exceptions.ServerException, exceptions.RequestError, requests.exceptions.ConnectionError) as e:
171
  retries += 1
172
  logging.warning(f"Error translating phrase '{phrase}': {e}. Retrying... ({retries}/{max_retries})")
173
 
@@ -176,20 +176,23 @@ def translate_and_store(phrase):
176
 
177
  def search_gematria_in_db(gematria_sum, max_words):
178
  """Searches the database for phrases with a given Gematria value and word count.
179
- Returns phrases with word count <= max_words."""
180
  global conn
181
  cursor = conn.cursor()
 
182
  cursor.execute('''
183
  SELECT words, book, chapter, verse FROM results WHERE gematria_sum = ?
184
- ''', (gematria_sum,)) # Retrieve all matching phrases first
185
  results = cursor.fetchall()
186
  filtered_results = []
 
187
  for words, book, chapter, verse in results:
188
  # Filter by word count (including phrases with fewer words)
189
- word_count = words.count(' ') + 1 # Count spaces to get word count
190
- if word_count <= word_count: # Include phrases with word count <= max_words
 
191
  filtered_results.append((words, book, chapter, verse))
192
- logging.debug(f"Found {len(filtered_results)} matching phrases for Gematria: {gematria_sum} after filtering.")
193
  return filtered_results
194
 
195
  def gematria_search_interface(phrase, max_words, show_translation):
@@ -208,16 +211,16 @@ def gematria_search_interface(phrase, max_words, show_translation):
208
  logging.debug(f"Phrase Gematria: {phrase_gematria}")
209
  logging.debug(f"Max Words: {max_words}")
210
 
211
- # Check if Gematria is in cache
212
- if phrase_gematria in gematria_cache:
213
- matching_phrases = gematria_cache[phrase_gematria]
214
- logging.debug(f"Retrieved matching phrases from cache.")
215
  else:
216
  # Search in the database
217
  matching_phrases = search_gematria_in_db(phrase_gematria, max_words)
218
- # Cache the results for future searches
219
- gematria_cache[phrase_gematria] = matching_phrases
220
- logging.debug(f"Retrieved matching phrases from database.")
221
 
222
  if not matching_phrases:
223
  return "No matching phrases found."
 
167
  logging.debug(f"Translated phrase: {translation}")
168
  return translation
169
  except (exceptions.TranslationNotFound, exceptions.NotValidPayload,
170
+ exceptions.ServerException, exceptions.RequestError, requests.exceptions.ConnectionError) as e:
171
  retries += 1
172
  logging.warning(f"Error translating phrase '{phrase}': {e}. Retrying... ({retries}/{max_retries})")
173
 
 
176
 
177
  def search_gematria_in_db(gematria_sum, max_words):
178
  """Searches the database for phrases with a given Gematria value and word count.
179
+ Returns phrases with word count <= max_words."""
180
  global conn
181
  cursor = conn.cursor()
182
+ logging.debug(f"Searching for phrases with Gematria: {gematria_sum} and max words: {max_words}")
183
  cursor.execute('''
184
  SELECT words, book, chapter, verse FROM results WHERE gematria_sum = ?
185
+ ''', (gematria_sum,)) # Retrieve all matching phrases first
186
  results = cursor.fetchall()
187
  filtered_results = []
188
+ logging.debug(f"Found {len(results)} matching phrases before filtering.")
189
  for words, book, chapter, verse in results:
190
  # Filter by word count (including phrases with fewer words)
191
+ word_count = len(words.split()) # Correctly split and count words
192
+ logging.debug(f"Word count for '{words}': {word_count}")
193
+ if word_count <= max_words: # Include phrases with word count <= max_words
194
  filtered_results.append((words, book, chapter, verse))
195
+ logging.debug(f"Found {len(filtered_results)} matching phrases after filtering.")
196
  return filtered_results
197
 
198
  def gematria_search_interface(phrase, max_words, show_translation):
 
211
  logging.debug(f"Phrase Gematria: {phrase_gematria}")
212
  logging.debug(f"Max Words: {max_words}")
213
 
214
+ # Check if Gematria is in cache for the specific max_words value
215
+ if (phrase_gematria, max_words) in gematria_cache:
216
+ matching_phrases = gematria_cache[(phrase_gematria, max_words)]
217
+ logging.debug(f"Retrieved matching phrases from cache for max_words: {max_words}.")
218
  else:
219
  # Search in the database
220
  matching_phrases = search_gematria_in_db(phrase_gematria, max_words)
221
+ # Cache the results with the max_words value
222
+ gematria_cache[(phrase_gematria, max_words)] = matching_phrases
223
+ logging.debug(f"Retrieved matching phrases from database for max_words: {max_words}.")
224
 
225
  if not matching_phrases:
226
  return "No matching phrases found."