Spaces:
Running
Running
fallback if no results
Browse files
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
|
| 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
|
| 2786 |
-
|
| 2787 |
-
|
| 2788 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2789 |
result = _analyze_word_with_hanta(word, top_n)
|
| 2790 |
-
if
|
| 2791 |
-
|
| 2792 |
-
|
| 2793 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2794 |
result = _analyze_word_with_iwnlp(word, top_n)
|
| 2795 |
-
if
|
| 2796 |
-
|
| 2797 |
-
|
| 2798 |
-
|
| 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"
|
| 2807 |
"traceback": traceback.format_exc()
|
| 2808 |
}
|
| 2809 |
|
| 2810 |
-
#
|
| 2811 |
-
|
| 2812 |
-
|
| 2813 |
-
|
| 2814 |
-
|
| 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 |
# ============================================================================
|