add a retranslate chinese function
This commit is contained in:
@@ -4,7 +4,7 @@ import json
|
||||
import re
|
||||
import threading
|
||||
|
||||
from sentence_api.ai_tutor import AITutor, AITutorError, _parse_translation_payload
|
||||
from sentence_api.ai_tutor import AITutor, AITutorError, _apply_translation_terms, _parse_translation_payload
|
||||
|
||||
|
||||
class FakeResponse:
|
||||
@@ -173,6 +173,38 @@ def test_ai_tutor_splits_invalid_windows_and_retries(monkeypatch):
|
||||
assert calls["count"] >= 3
|
||||
|
||||
|
||||
def test_ai_tutor_includes_name_terms_and_rewrites_output(monkeypatch):
|
||||
requests_payloads = []
|
||||
|
||||
def fake_post(endpoint, headers, json, timeout):
|
||||
requests_payloads.append(json)
|
||||
return FakeResponse(
|
||||
body={"choices": [{"message": {"content": '{"0":"哈达马托击败了 Harimoto。"}'}}]}
|
||||
)
|
||||
|
||||
monkeypatch.setattr("sentence_api.ai_tutor.requests.post", fake_post)
|
||||
tutor = AITutor("https://ai.test/v1", "secret", "test-model", 1)
|
||||
|
||||
translated = tutor.translate_sentences(
|
||||
[(0, "Harimoto won.")],
|
||||
course_title="Table tennis",
|
||||
terms=[("Harimoto", "哈瑞么托"), ("哈达马托", "哈瑞么托")],
|
||||
)
|
||||
|
||||
assert translated == {0: "哈瑞么托击败了 哈瑞么托。"}
|
||||
user_prompt = requests_payloads[0]["messages"][1]["content"]
|
||||
assert "Harimoto => 哈瑞么托" in user_prompt
|
||||
assert "哈达马托 => 哈瑞么托" in user_prompt
|
||||
|
||||
|
||||
def test_apply_translation_terms_replaces_english_and_wrong_chinese():
|
||||
assert _apply_translation_terms(
|
||||
"Harimoto won.",
|
||||
"哈达马托击败了 Harimoto。",
|
||||
[("Harimoto", "哈瑞么托"), ("哈达马托", "哈瑞么托")],
|
||||
) == "哈瑞么托击败了 哈瑞么托。"
|
||||
|
||||
|
||||
def test_parse_translation_payload_accepts_fenced_json_and_numbered_lines():
|
||||
assert _parse_translation_payload('```json\n["一", "二"]\n```', 2) == {0: "一", 1: "二"}
|
||||
assert _parse_translation_payload("1. 一\n2. 二", 2) == {0: "一", 1: "二"}
|
||||
|
||||
Reference in New Issue
Block a user