cstr commited on
Commit
9de49fe
·
verified ·
1 Parent(s): d69777a

fallback if no results

Browse files
Files changed (1) hide show
  1. app.py +40 -25
app.py CHANGED
@@ -2763,12 +2763,12 @@ def _analyze_word_with_iwnlp(word: str, top_n_value: int) -> Dict[str, Any]:
2763
 
2764
  # --- 7b. NEW: Word Encyclopedia (Non-Contextual) Analyzer ---
2765
 
 
2766
  # --- THIS IS THE NEW PUBLIC DISPATCHER FUNCTION ---
2767
  def analyze_word_encyclopedia(word: str, top_n_value: Optional[float] = 0, engine_choice: str = "wiktionary") -> Dict[str, Any]:
2768
  """
2769
- (PUBLIC DISPATCHER V20) Analyzes a single word using the selected engine.
2770
-
2771
- The user can now choose which engine to run.
2772
  """
2773
  if not word or not word.strip():
2774
  return {"info": "Please enter a word."}
@@ -2776,45 +2776,60 @@ def analyze_word_encyclopedia(word: str, top_n_value: Optional[float] = 0, engin
2776
  word = word.strip()
2777
  top_n = int(top_n_value) if top_n_value is not None else 0
2778
  result = {}
 
2779
 
2780
  log(f"\n[Word Encyclopedia] User selected engine: '{engine_choice}' for word: '{word}'")
2781
 
2782
  try:
 
2783
  if engine_choice == "wiktionary":
 
2784
  result = _analyze_word_with_wiktionary(word, top_n)
2785
- if not result or not result.get("analysis"):
2786
- result["info"] = f"Wiktionary (Primary Engine) found no results for '{word}'. You can try a fallback engine."
2787
-
2788
- elif engine_choice == "hanta":
 
 
 
 
 
 
 
2789
  result = _analyze_word_with_hanta(word, top_n)
2790
- if not result or not result.get("analysis"):
2791
- result["info"] = f"HanTa (Fallback 1) found no results for '{word}'."
2792
-
2793
- elif engine_choice == "iwnlp":
 
 
 
 
 
 
 
 
2794
  result = _analyze_word_with_iwnlp(word, top_n)
2795
- if not result or not result.get("analysis"):
2796
- result["info"] = f"IWNLP (Fallback 2) found no results for '{word}'."
2797
-
2798
- else:
2799
- result = {"error": f"Unknown engine choice: {engine_choice}"}
2800
 
2801
  except Exception as e:
2802
  log(f"--- Dispatcher FAILED for engine {engine_choice}: {e} ---")
2803
  traceback.print_exc()
2804
  return {
2805
  "input_word": word,
2806
- "error": f"The '{engine_choice}' engine failed during analysis.",
2807
  "traceback": traceback.format_exc()
2808
  }
2809
 
2810
- # If the engine ran but found nothing, return a clear info message
2811
- if not result.get("analysis"):
2812
- return {
2813
- "input_word": word,
2814
- "info": result.get("info", f"The selected engine '{engine_choice}' found no valid analysis for this word.")
2815
- }
2816
-
2817
- return result
2818
 
2819
 
2820
  # ============================================================================
 
2763
 
2764
  # --- 7b. NEW: Word Encyclopedia (Non-Contextual) Analyzer ---
2765
 
2766
+ # --- THIS IS THE NEW PUBLIC DISPATCHER FUNCTION ---
2767
  # --- THIS IS THE NEW PUBLIC DISPATCHER FUNCTION ---
2768
  def analyze_word_encyclopedia(word: str, top_n_value: Optional[float] = 0, engine_choice: str = "wiktionary") -> Dict[str, Any]:
2769
  """
2770
+ (PUBLIC DISPATCHER V21) Analyzes a single word using the selected engine
2771
+ as a starting point, then automatically falls back if no results are found.
 
2772
  """
2773
  if not word or not word.strip():
2774
  return {"info": "Please enter a word."}
 
2776
  word = word.strip()
2777
  top_n = int(top_n_value) if top_n_value is not None else 0
2778
  result = {}
2779
+ info_log = [] # To track which engines failed
2780
 
2781
  log(f"\n[Word Encyclopedia] User selected engine: '{engine_choice}' for word: '{word}'")
2782
 
2783
  try:
2784
+ # --- 1. Try Wiktionary (if selected) ---
2785
  if engine_choice == "wiktionary":
2786
+ log(f"[DEBUG] V21 Dispatcher: Trying Wiktionary (Primary) for '{word}'...")
2787
  result = _analyze_word_with_wiktionary(word, top_n)
2788
+ if result and result.get("analysis"):
2789
+ return result # Success
2790
+ info_log.append("Wiktionary found no results.")
2791
+ log(f"[DEBUG] V21 Dispatcher: Wiktionary found no results for '{word}'. Falling back to HanTa...")
2792
+
2793
+ # --- 2. Try HanTa (if selected or as fallback) ---
2794
+ # This block runs if:
2795
+ # a) User selected "hanta" OR
2796
+ # b) User selected "wiktionary" and it found nothing
2797
+ if engine_choice == "hanta" or (engine_choice == "wiktionary" and not result.get("analysis")):
2798
+ log(f"[DEBUG] V21 Dispatcher: Trying HanTa (Fallback 1) for '{word}'...")
2799
  result = _analyze_word_with_hanta(word, top_n)
2800
+ if result and result.get("analysis"):
2801
+ result["info"] = f"Analysis from HanTa (Fallback 1). {(' '.join(info_log))}"
2802
+ return result # Success
2803
+ info_log.append("HanTa found no results.")
2804
+ log(f"[DEBUG] V21 Dispatcher: HanTa found no results for '{word}'. Falling back to IWNLP...")
2805
+
2806
+ # --- 3. Try IWNLP (if selected or as fallback) ---
2807
+ # This block runs if:
2808
+ # a) User selected "iwnlp" OR
2809
+ # b) The previous engines were tried and all failed (result['analysis'] is still empty)
2810
+ if engine_choice == "iwnlp" or (not result.get("analysis")):
2811
+ log(f"[DEBUG] V21 Dispatcher: Trying IWNLP (Fallback 2) for '{word}'...")
2812
  result = _analyze_word_with_iwnlp(word, top_n)
2813
+ if result and result.get("analysis"):
2814
+ result["info"] = f"Analysis from IWNLP (Fallback 2). {(' '.join(info_log))}"
2815
+ return result # Success
2816
+ info_log.append("IWNLP found no results.")
 
2817
 
2818
  except Exception as e:
2819
  log(f"--- Dispatcher FAILED for engine {engine_choice}: {e} ---")
2820
  traceback.print_exc()
2821
  return {
2822
  "input_word": word,
2823
+ "error": f"An engine failed during analysis.",
2824
  "traceback": traceback.format_exc()
2825
  }
2826
 
2827
+ # --- No engines found anything ---
2828
+ log(f"[DEBUG] V21 Dispatcher: All engines failed to find results for '{word}'.")
2829
+ return {
2830
+ "input_word": word,
2831
+ "info": f"No analysis found. All engines failed. ({' '.join(info_log)})"
2832
+ }
 
 
2833
 
2834
 
2835
  # ============================================================================