Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -37,48 +37,64 @@ def coref_chat(user_input):
|
|
| 37 |
return "請輸入內容", "等待輸入..."
|
| 38 |
|
| 39 |
try:
|
| 40 |
-
# 1.
|
| 41 |
has_chinese = any('\u4e00' <= char <= '\u9fff' for char in user_input)
|
| 42 |
-
|
| 43 |
if has_chinese:
|
| 44 |
working_text = GoogleTranslator(source='zh-CN', target='en').translate(user_input)
|
| 45 |
-
mode_notice = "【模式:中文 ➔
|
| 46 |
else:
|
| 47 |
working_text = user_input
|
| 48 |
-
mode_notice = "【模式:
|
| 49 |
|
| 50 |
# 2. 執行指代消解
|
| 51 |
preds = model.predict(texts=[working_text])
|
| 52 |
clusters = preds[0].get_clusters()
|
| 53 |
|
| 54 |
-
# 3.
|
| 55 |
result = f"✨ {mode_notice}\n"
|
| 56 |
result += f"📝 英文邏輯空間: {working_text}\n"
|
| 57 |
-
|
| 58 |
-
# --- 英文翻譯中文 ---
|
| 59 |
try:
|
| 60 |
translation_back = GoogleTranslator(source='en', target='zh-TW').translate(working_text)
|
| 61 |
-
result += f"📖 中
|
| 62 |
except:
|
| 63 |
-
|
| 64 |
|
| 65 |
result += "---------------------------------\n"
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
if not clusters:
|
| 68 |
-
result += "🔍 分析結果:指代關係明確,
|
| 69 |
else:
|
| 70 |
result += "🎯【偵測到之實體鏈 (Entity Chains)】:\n"
|
| 71 |
for i, cluster in enumerate(clusters):
|
| 72 |
cluster_str_en = ' ↔ '.join(cluster)
|
| 73 |
-
# 實體鏈個別翻譯
|
| 74 |
try:
|
| 75 |
translated_items = [GoogleTranslator(source='en', target='zh-TW').translate(item) for item in cluster]
|
| 76 |
cluster_str_zh = ' ↔ '.join(translated_items)
|
| 77 |
except:
|
| 78 |
-
cluster_str_zh =
|
| 79 |
-
|
| 80 |
result += f" 🔗 鏈結 {i+1} (繁中): {cluster_str_zh}\n"
|
| 81 |
-
result += f" └─ (原文): {cluster_str_en}\n"
|
| 82 |
|
| 83 |
return user_input, result
|
| 84 |
|
|
|
|
| 37 |
return "請輸入內容", "等待輸入..."
|
| 38 |
|
| 39 |
try:
|
| 40 |
+
# 1. 跨語言橋接
|
| 41 |
has_chinese = any('\u4e00' <= char <= '\u9fff' for char in user_input)
|
|
|
|
| 42 |
if has_chinese:
|
| 43 |
working_text = GoogleTranslator(source='zh-CN', target='en').translate(user_input)
|
| 44 |
+
mode_notice = "【模式:中文輸入 ➔ 多維度 AI 學習系統】"
|
| 45 |
else:
|
| 46 |
working_text = user_input
|
| 47 |
+
mode_notice = "【模式:英文原語 ➔ 多維度 AI 學習系統】"
|
| 48 |
|
| 49 |
# 2. 執行指代消解
|
| 50 |
preds = model.predict(texts=[working_text])
|
| 51 |
clusters = preds[0].get_clusters()
|
| 52 |
|
| 53 |
+
# 3. 建立基礎報告排版
|
| 54 |
result = f"✨ {mode_notice}\n"
|
| 55 |
result += f"📝 英文邏輯空間: {working_text}\n"
|
|
|
|
|
|
|
| 56 |
try:
|
| 57 |
translation_back = GoogleTranslator(source='en', target='zh-TW').translate(working_text)
|
| 58 |
+
result += f"📖 英翻中結果: {translation_back}\n"
|
| 59 |
except:
|
| 60 |
+
pass
|
| 61 |
|
| 62 |
result += "---------------------------------\n"
|
| 63 |
|
| 64 |
+
# 4. ✨ 新增:智慧學單字功能 (從實體鏈中自動提取核心單字) ✨
|
| 65 |
+
if clusters:
|
| 66 |
+
result += "📚【AI 智慧單字卡 / Vocabulary Cards】\n"
|
| 67 |
+
extracted_words = set()
|
| 68 |
+
# 抓取實體鏈裡的所有單字
|
| 69 |
+
for cluster in clusters:
|
| 70 |
+
for item in cluster:
|
| 71 |
+
# 清理冠詞 (the, a, her 等),只留下核心單字
|
| 72 |
+
clean_word = item.lower().replace("the ", "").replace("a ", "").replace("her ", "").replace("his ", "").strip()
|
| 73 |
+
if len(clean_word) > 2 and clean_word not in ['she', 'he', 'him', 'her', 'it', 'they']:
|
| 74 |
+
extracted_words.add(clean_word)
|
| 75 |
+
|
| 76 |
+
# 翻譯單字並製作卡片
|
| 77 |
+
for word in extracted_words:
|
| 78 |
+
try:
|
| 79 |
+
word_zh = GoogleTranslator(source='en', target='zh-TW').translate(word)
|
| 80 |
+
result += f" 💡 單字: {word:<12} ➔ 中文釋義: {word_zh}\n"
|
| 81 |
+
except:
|
| 82 |
+
pass
|
| 83 |
+
result += "---------------------------------\n"
|
| 84 |
+
|
| 85 |
+
# 5. 輸出實體鏈
|
| 86 |
if not clusters:
|
| 87 |
+
result += "🔍 分析結果:指代關係明確,無需額外消解。"
|
| 88 |
else:
|
| 89 |
result += "🎯【偵測到之實體鏈 (Entity Chains)】:\n"
|
| 90 |
for i, cluster in enumerate(clusters):
|
| 91 |
cluster_str_en = ' ↔ '.join(cluster)
|
|
|
|
| 92 |
try:
|
| 93 |
translated_items = [GoogleTranslator(source='en', target='zh-TW').translate(item) for item in cluster]
|
| 94 |
cluster_str_zh = ' ↔ '.join(translated_items)
|
| 95 |
except:
|
| 96 |
+
cluster_str_zh = cluster_str_en
|
|
|
|
| 97 |
result += f" 🔗 鏈結 {i+1} (繁中): {cluster_str_zh}\n"
|
|
|
|
| 98 |
|
| 99 |
return user_input, result
|
| 100 |
|