Compare commits

..

No commits in common. "13babde93fbe95122c6bee01ca37c928a0c9af94" and "9f9f46264001ae121d1a4fa753b3e1c04512c453" have entirely different histories.

7 changed files with 41 additions and 118 deletions

View File

@ -326,8 +326,7 @@ Authorization: Bearer <your_jwt_token>
"chi_exp": "日语;日本的语言",
"example": "日本語を勉強しています。"
}
],
"hiragana": ["假名注音"]
]
}
```
@ -544,8 +543,8 @@ Authorization: Bearer <your_jwt_token>
- **请求参数说明**:
- `query`: 待翻译的文本
- `from_lang`: 源语言,支持值: `auto`(自动检测), `fra`(法语), `jp`(日语), `zh`(中文),默认为 `auto`
- `to_lang`: 目标语言,支持值: `fra`(法语), `jp`(日语), `zh`(中文),默认为 `zh`,不能为 `auto`
- `from_lang`: 源语言,支持值: `auto`(自动检测), `fr`(法语), `jp`(日语), `zh`(中文),默认为 `auto`
- `to_lang`: 目标语言,支持值: `fr`(法语), `jp`(日语), `zh`(中文),默认为 `zh`,不能为 `auto`
- **响应**:
@ -1234,11 +1233,12 @@ curl -X GET "http://127.0.0.1:8000/api/test/pron/start?count=5&lang=fr-FR" \
### 15. 部署说明
1. 安装依赖:`uv pip install -r requirements.txt`(或使用 `pip install -r requirements.txt`
2. 配置环境:在项目根目录准备 `.env`写入数据库、Redis、邮件、翻译等密钥字段参考 `settings.Settings`
3. 确保 MySQL 与 Redis 服务已启动并与 `.env` 中的连接信息匹配
4. 初始化数据库(首次部署):`aerich upgrade`
5. 启动服务:`uvicorn main:app --host 0.0.0.0 --port 8000`(开发环境可加 `--reload`
1. 安装依赖: `pip install -r requirements.txt`
2. 配置数据库连接 (settings.py)
3. 配置百度翻译API密钥 (BAIDU_APPID, BAIDU_APPKEY)
4. 启动Redis服务
5. 运行数据库迁移
6. 启动服务: `python main.py`
---

View File

@ -142,7 +142,6 @@ async def search(request: Request, body: SearchRequest, user=Depends(get_current
query=query,
pos=pos_contents,
contents=contents,
hiragana=query_kana,
)
@ -231,63 +230,32 @@ async def search_proverb_list(query_word: ProverbSearchRequest, user=Depends(get
@dict_search.post("/search/proverb")
async def search_proverb(proverb_id: int = Form(...), user=Depends(get_current_user)):
result = await service.accurate_idiom_proverb(search_id=proverb_id, model=ProverbFr,
only_fields=["id", "text", "chi_exp"])
only_fields=["text", "chi_exp"])
return {"result": result}
@dict_search.post("/search/list/idiom")
async def search_idiom_list(
query_idiom: ProverbSearchRequest,
user=Depends(get_current_user)
):
"""日语成语检索接口(带语言检测与分类逻辑)"""
async def search_idiom_list(query_idiom: ProverbSearchRequest, user=Depends(get_current_user)):
if query_idiom.dict_language == "fr":
raise HTTPException(status_code=400, detail="Dict language Error")
# 语言检测
mapping_query, lang, is_kangji = await service.detect_language(text=query_idiom.query)
query = query_idiom.query
# 初始化任务列表(后续依任务顺序返回)
tasks = []
# --- 1⃣ 日语输入 ---
if lang == "jp":
if is_kangji:
tasks.append(
# ✅ 并发任务列表
tasks = [
service.suggest_proverb(
query=query,
lang="jp",
model=IdiomJp,
search_field="text",
)
)
tasks.append(
service.suggest_proverb(
query=all_in_kana(query),
lang="jp",
lang=lang,
model=IdiomJp,
search_field="search_text",
target_field="text",
)
)
else:
tasks.append(
service.suggest_proverb(
query=query,
lang="jp",
model=IdiomJp,
search_field="search_text",
target_field="text",
)
)
]
# --- 2⃣ 中文输入(调整优先级) ---
elif lang == "zh":
# ✅ (1) 若存在映射mapping_query 优先匹配日语原型text
if is_kangji and mapping_query:
if lang == "zh" and is_kangji:
# jp_query = all_in_kana(text=query_idiom.query)
tasks.append(
service.suggest_proverb(
query=mapping_query,
@ -297,54 +265,15 @@ async def search_idiom_list(
)
)
# ✅ (2) 然后匹配中文释义chi_exp 或 search_text
tasks.append(
service.suggest_proverb(
query=query,
lang="zh",
model=IdiomJp,
target_field="text",
)
)
# ✅ (3) 最后用假名匹配映射(辅助补全)
if is_kangji and mapping_query:
tasks.append(
service.suggest_proverb(
query=all_in_kana(mapping_query),
lang="jp",
model=IdiomJp,
search_field="search_text",
)
)
# --- 3⃣ 其他语言(默认回退) ---
else:
tasks.append(
service.suggest_proverb(
query=query,
lang="jp",
model=IdiomJp,
search_field="search_text",
target_field="text",
)
)
# ✅ 并发执行任务(结果顺序与任务定义顺序一致)
# ✅ 并发执行(返回结果顺序与任务顺序一致)
results = await asyncio.gather(*tasks)
# ✅ 顺序合并 + 稳定去重
seen = set()
ordered_unique = []
for res in results:
for item in res:
key = item.get("proverb") or item.get("text")
if key and key not in seen:
seen.add(key)
ordered_unique.append(item)
return {"list": ordered_unique}
# ✅ 合并结果
result = results[0]
if len(results) > 1:
result[:0] = results[1] # 将中文映射查询结果插到最前面
return {"list": result}
@dict_search.post("/search/idiom")

View File

@ -32,7 +32,6 @@ class WordSearchResponse(BaseModel):
query: str
pos: list
contents: Union[List[SearchItemFr], List[SearchItemJp]]
hiragana : str
class ProverbSearchResponse(BaseModel):

View File

@ -92,8 +92,9 @@ async def suggest_proverb(
# ✅ 搜索条件:中文时双字段联合匹配
if lang == "zh":
start_condition = Q(**{f"{chi_exp_field}__istartswith": keyword})
contain_condition = Q(**{f"{chi_exp_field}__icontains": keyword})
start_condition = Q(**{f"{chi_exp_field}__istartswith": keyword}) | Q(
**{f"{search_field}__istartswith": keyword})
contain_condition = Q(**{f"{chi_exp_field}__icontains": keyword}) | Q(**{f"{search_field}__icontains": keyword})
else:
start_condition = Q(**{f"{search_field}__istartswith": keyword})
contain_condition = Q(**{f"{search_field}__icontains": keyword})

View File

@ -1,3 +1,4 @@
import json
import random
from typing import Tuple, Dict
@ -14,6 +15,8 @@ from settings import settings
translator_router = APIRouter()
# For list of language codes, please refer to `https://api.fanyi.baidu.com/doc/21`
from_lang = 'en'
to_lang = 'zh'
# endpoint = 'https://api.fanyi.baidu.com'
@ -78,7 +81,7 @@ async def baidu_translation(query: str, from_lang: str, to_lang: str):
raise HTTPException(status_code=500, detail=response.json())
data = response.json()
# print(json.dumps(data, indent=2, ensure_ascii=False))
print(json.dumps(data, indent=2, ensure_ascii=False))
if "trans_result" not in data:
raise HTTPException(status_code=500, detail={"error_code": data.get("error_code"), "error_msg": data.get("error_msg")})

View File

@ -14,16 +14,7 @@ async def get_search_time(request: Request):
if not count:
await redis.set(key, value=0)
count = 0
print(count, type(count))
return {
"count": int(count),
}
@ulit_router.get("/search/reset", tags=["search times reset"])
async def reset_search_time(request: Request):
redis = request.app.state.redis
key = f"search_time"
count = await redis.set(key, 0)
return {
"message": "search times reset successfully",
}

View File

@ -5,13 +5,13 @@ from pydantic import BaseModel, field_validator, model_validator
class TransRequest(BaseModel):
query: str
from_lang: Literal['auto', 'fra', 'jp', 'zh', 'en'] = 'auto'
to_lang: Literal['fra', 'jp', 'zh', 'en'] = 'zh'
from_lang: Literal['auto', 'fr', 'jp', 'zh', 'en'] = 'auto'
to_lang: Literal['fr', 'jp', 'zh', 'en'] = 'zh'
@field_validator('from_lang', 'to_lang')
@classmethod
def validate_lang(cls, v):
allowed_langs = {'auto', 'fra', 'jp', 'zh', 'en'}
allowed_langs = {'auto', 'fr', 'jp', 'zh', 'en'}
if v not in allowed_langs:
raise ValueError(f'Unsupported language: {v}')
return v