neuralworm commited on
Commit
94105ed
·
1 Parent(s): 8ea2147

fix average calculation

Browse files
Files changed (1) hide show
  1. app.py +37 -20
app.py CHANGED
@@ -216,6 +216,8 @@ def gematria_search_interface(phrases: str, max_words: int, show_translation: bo
216
  results = []
217
  all_results = [] # Store results for each phrase
218
  middle_words_results = [] # Store middle word results for all books
 
 
219
 
220
  phrases = phrases.strip().splitlines()
221
  if not phrases:
@@ -265,13 +267,25 @@ def gematria_search_interface(phrases: str, max_words: int, show_translation: bo
265
  <a href='{link}' target='_blank' class='bible-link'>[See on Bible Gateway]</a>
266
  </div>
267
  """)
268
- results.append("</div>")
269
 
 
 
 
 
 
 
 
 
270
  all_results.append(results_by_book) # Store results by book without the phrase
271
 
272
- # Calculate middle words for all input lines
 
 
 
 
 
273
  if len(all_results) >= 2:
274
- results.append("<h2>Middle Words:</h2>")
275
  results.append("<div class='results-container'>")
276
 
277
  common_books = set.intersection(*[set(results.keys()) for results in all_results])
@@ -356,27 +370,30 @@ def gematria_search_interface(phrases: str, max_words: int, show_translation: bo
356
  """
357
  return style + "\n".join(results)
358
 
 
 
 
 
 
 
 
 
 
359
  def find_nearest_positions(results_lists: List[List]) -> List[int]:
360
  """Finds the nearest word positions among multiple lists of results."""
361
  nearest_positions = []
362
  for i in range(len(results_lists)):
363
- positions_i = [(int(pos.split('-')[0]), int(pos.split('-')[1])) for _, _, _, _, pos in results_lists[i]] # Get start and end positions
364
- min_distance = float('inf')
365
- nearest_pos_start = None
366
- nearest_pos_end = None
367
- for j in range(len(results_lists)):
368
- if i == j:
369
- continue
370
- positions_j = [(int(pos.split('-')[0]), int(pos.split('-')[1])) for _, _, _, _, pos in results_lists[j]] # Get start and end positions
371
- for pos_i_start, pos_i_end in positions_i:
372
- for pos_j_start, pos_j_end in positions_j:
373
- distance = abs((pos_i_start + pos_i_end) // 2 - (pos_j_start + pos_j_end) // 2)
374
- if distance < min_distance:
375
- min_distance = distance
376
- nearest_pos_start = pos_i_start # Take the start position from the first list
377
- nearest_pos_end = pos_i_end # Take the end position from the first list
378
- if nearest_pos_start is not None and nearest_pos_end is not None:
379
- nearest_positions.extend([nearest_pos_start, nearest_pos_end])
380
  return nearest_positions
381
 
382
 
 
216
  results = []
217
  all_results = [] # Store results for each phrase
218
  middle_words_results = [] # Store middle word results for all books
219
+ all_names_average_position = 0 # Initialize variable for average position across all names and books
220
+ total_name_count = 0 # Initialize counter for the total number of names processed
221
 
222
  phrases = phrases.strip().splitlines()
223
  if not phrases:
 
267
  <a href='{link}' target='_blank' class='bible-link'>[See on Bible Gateway]</a>
268
  </div>
269
  """)
 
270
 
271
+ # Calculate average position for the current name across all books
272
+ name_average_position = calculate_average_position_for_name(results_by_book)
273
+ if name_average_position is not None:
274
+ results.append(f"<p><b>Average Word Position for '{phrase}' across all books:</b> {name_average_position:.2f}</p>")
275
+ all_names_average_position += name_average_position
276
+ total_name_count += 1
277
+
278
+ results.append("</div>")
279
  all_results.append(results_by_book) # Store results by book without the phrase
280
 
281
+ # Calculate the average word position across all names and all their books
282
+ if total_name_count > 0:
283
+ all_names_average_position /= total_name_count
284
+ results.append(f"<h2>Average Word Position Across All Names and Books: {all_names_average_position:.2f}</h2>")
285
+
286
+ # Calculate middle words for all input lines (common books)
287
  if len(all_results) >= 2:
288
+ results.append("<h2>Middle Words (Common Books):</h2>")
289
  results.append("<div class='results-container'>")
290
 
291
  common_books = set.intersection(*[set(results.keys()) for results in all_results])
 
370
  """
371
  return style + "\n".join(results)
372
 
373
+ def calculate_average_position_for_name(results_by_book: Dict[str, List[Tuple]]) -> float:
374
+ """Calculates the average word position for a single name across all books."""
375
+ positions = []
376
+ for book, phrases in results_by_book.items():
377
+ for _, _, _, _, word_position in phrases:
378
+ start, end = map(int, word_position.split('-'))
379
+ positions.append((start + end) / 2)
380
+ return sum(positions) / len(positions) if positions else None
381
+
382
  def find_nearest_positions(results_lists: List[List]) -> List[int]:
383
  """Finds the nearest word positions among multiple lists of results."""
384
  nearest_positions = []
385
  for i in range(len(results_lists)):
386
+ positions_i = [(int(pos.split('-')[0]) + int(pos.split('-')[1])) / 2
387
+ for _, _, _, _, pos in results_lists[i]] # Get average of start and end positions
388
+ logging.debug(f"Positions for phrase {i+1}: {positions_i}")
389
+
390
+ # Calculate the average position for the current phrase
391
+ average_position = sum(positions_i) / len(positions_i) if positions_i else None
392
+ logging.debug(f"Average position for phrase {i+1}: {average_position}")
393
+
394
+ if average_position is not None:
395
+ nearest_positions.append(average_position)
396
+
 
 
 
 
 
 
397
  return nearest_positions
398
 
399