Update app/noi_tu.py
Browse files- app/noi_tu.py +19 -17
app/noi_tu.py
CHANGED
|
@@ -4,36 +4,42 @@ from typing import Optional
|
|
| 4 |
|
| 5 |
class NoiTuApp:
|
| 6 |
def __init__(self):
|
| 7 |
-
|
|
|
|
| 8 |
self.words = set()
|
| 9 |
|
| 10 |
async def start(self):
|
| 11 |
"""Tải dữ liệu từ vựng khi khởi động server"""
|
| 12 |
try:
|
| 13 |
-
async with httpx.AsyncClient(timeout=
|
| 14 |
response = await client.get(self.url)
|
| 15 |
if response.status_code == 200:
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
|
|
|
| 19 |
else:
|
| 20 |
-
print(f"Failed to load words: {response.status_code}")
|
| 21 |
except Exception as e:
|
| 22 |
-
print(f"Error loading words: {e}")
|
| 23 |
|
| 24 |
async def stop(self):
|
| 25 |
self.words.clear()
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
def get_next_word(self, current_word: str) -> Optional[str]:
|
| 28 |
-
"""Tìm
|
| 29 |
current_word = current_word.lower().strip()
|
| 30 |
parts = current_word.split()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
last_syllable = parts[-1]
|
| 34 |
-
|
| 35 |
-
# Tìm các từ trong danh sách bắt đầu bằng last_syllable
|
| 36 |
-
# Và phải là từ có ít nhất 2 tiếng (để game tiếp tục được)
|
| 37 |
candidates = [
|
| 38 |
w for w in self.words
|
| 39 |
if w.startswith(last_syllable + " ") and w != current_word
|
|
@@ -43,7 +49,3 @@ class NoiTuApp:
|
|
| 43 |
return None
|
| 44 |
|
| 45 |
return random.choice(candidates)
|
| 46 |
-
|
| 47 |
-
def is_valid_word(self, word: str) -> bool:
|
| 48 |
-
"""Kiểm tra từ có trong từ điển không"""
|
| 49 |
-
return word.lower().strip() in self.words
|
|
|
|
| 4 |
|
| 5 |
class NoiTuApp:
|
| 6 |
def __init__(self):
|
| 7 |
+
# Sử dụng raw file từ điển tiếng Việt (từ ghép 2 tiếng)
|
| 8 |
+
self.url = "https://raw.githubusercontent.com/duyet/vietnamese-wordlist/master/words.txt"
|
| 9 |
self.words = set()
|
| 10 |
|
| 11 |
async def start(self):
|
| 12 |
"""Tải dữ liệu từ vựng khi khởi động server"""
|
| 13 |
try:
|
| 14 |
+
async with httpx.AsyncClient(timeout=60.0) as client:
|
| 15 |
response = await client.get(self.url)
|
| 16 |
if response.status_code == 200:
|
| 17 |
+
# Lọc lấy các từ có đúng 2 tiếng để chơi game 2-word
|
| 18 |
+
raw_words = {line.strip().lower() for line in response.text.splitlines() if line.strip()}
|
| 19 |
+
self.words = {w for w in raw_words if len(w.split()) == 2}
|
| 20 |
+
print(f"✅ Loaded {len(self.words)} Vietnamese compound words.")
|
| 21 |
else:
|
| 22 |
+
print(f"❌ Failed to load words: {response.status_code}")
|
| 23 |
except Exception as e:
|
| 24 |
+
print(f"❌ Error loading words: {e}")
|
| 25 |
|
| 26 |
async def stop(self):
|
| 27 |
self.words.clear()
|
| 28 |
|
| 29 |
+
def is_valid_word(self, word: str) -> bool:
|
| 30 |
+
"""Kiểm tra từ người dùng nhập có trong từ điển không"""
|
| 31 |
+
return word.lower().strip() in self.words
|
| 32 |
+
|
| 33 |
def get_next_word(self, current_word: str) -> Optional[str]:
|
| 34 |
+
"""Tìm từ nối tiếp dựa trên tiếng cuối của từ hiện tại"""
|
| 35 |
current_word = current_word.lower().strip()
|
| 36 |
parts = current_word.split()
|
| 37 |
+
if len(parts) < 2:
|
| 38 |
+
return None
|
| 39 |
+
|
| 40 |
+
last_syllable = parts[-1] # Ví dụ: "sinh" trong "học sinh"
|
| 41 |
|
| 42 |
+
# Tìm các từ bắt đầu bằng 'sinh ...'
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
candidates = [
|
| 44 |
w for w in self.words
|
| 45 |
if w.startswith(last_syllable + " ") and w != current_word
|
|
|
|
| 49 |
return None
|
| 50 |
|
| 51 |
return random.choice(candidates)
|
|
|
|
|
|
|
|
|
|
|
|