fix(reader): prevent false phrasal verb detection for noun collocations and improve literary translation accuracy
This commit is contained in:
@@ -1955,6 +1955,79 @@ def test_robust_sentence_split_and_ai_translation(monkeypatch):
|
||||
assert tr_data["target_word"] == "briefcase"
|
||||
|
||||
|
||||
def test_phrasal_verb_detection_no_false_positive_for_noun_phrase():
|
||||
from app.phrasal import detect_phrasal_verb
|
||||
|
||||
# Seed temporary test dictionary with tawny owl
|
||||
with get_db() as conn:
|
||||
conn.execute(
|
||||
"INSERT OR IGNORE INTO offline_dictionary (term, translation, phonetic, pos) VALUES (?, ?, '', ?)",
|
||||
("tawny owl", "неясыть, неясыть серая", "Noun"),
|
||||
)
|
||||
conn.execute(
|
||||
"INSERT OR IGNORE INTO offline_dictionary (term, translation, phonetic, pos) VALUES (?, ?, '', ?)",
|
||||
("owl", "сыч, сова, филин", "Noun"),
|
||||
)
|
||||
conn.execute(
|
||||
"INSERT OR IGNORE INTO offline_dictionary (term, translation, phonetic, pos) VALUES (?, ?, '', ?)",
|
||||
("tawny", "рыжевато-коричневый", "Adjective"),
|
||||
)
|
||||
|
||||
sentence = "None of them noticed a large, tawny owl flutter past the window."
|
||||
# tawny owl is a noun phrase (an animal species), NOT a phrasal verb!
|
||||
assert detect_phrasal_verb(sentence, "owl") is None
|
||||
assert detect_phrasal_verb(sentence, "tawny") is None
|
||||
assert detect_phrasal_verb(sentence, "large") is None
|
||||
|
||||
# Genuine phrasal verbs must still be detected
|
||||
s2 = "None of them noticed him take off his coat."
|
||||
pv2 = detect_phrasal_verb(s2, "take")
|
||||
assert pv2 is not None
|
||||
assert pv2["term"] == "take off"
|
||||
|
||||
s3 = "He turned it off quietly."
|
||||
pv3 = detect_phrasal_verb(s3, "turned")
|
||||
assert pv3 is not None
|
||||
assert pv3["term"] == "turn off"
|
||||
|
||||
|
||||
def test_sentence_dictionary_hints_and_fauna_precision():
|
||||
from app.ai import get_sentence_dictionary_hints, build_translation_prompt
|
||||
|
||||
with get_db() as conn:
|
||||
conn.execute(
|
||||
"INSERT OR IGNORE INTO offline_dictionary (term, translation, phonetic, pos) VALUES (?, ?, '', ?)",
|
||||
("tawny owl", "неясыть, неясыть серая", "Noun"),
|
||||
)
|
||||
conn.execute(
|
||||
"INSERT OR IGNORE INTO offline_dictionary (term, translation, phonetic, pos) VALUES (?, ?, '', ?)",
|
||||
("owl", "сыч, сова, филин", "Noun"),
|
||||
)
|
||||
conn.execute(
|
||||
"INSERT OR IGNORE INTO offline_dictionary (term, translation, phonetic, pos) VALUES (?, ?, '', ?)",
|
||||
("large", "большой, крупный", "Adjective"),
|
||||
)
|
||||
|
||||
sentence = "None of them noticed a large, tawny owl flutter past the window."
|
||||
hints = get_sentence_dictionary_hints(sentence, "tawny")
|
||||
assert "owl" in hints
|
||||
assert "НЕ орёл" in hints
|
||||
assert "large" in hints
|
||||
|
||||
prompt = build_translation_prompt(
|
||||
sentence=sentence,
|
||||
target_word="tawny",
|
||||
context="",
|
||||
dict_hint="рыжевато-коричневый",
|
||||
sentence_hints=hints,
|
||||
)
|
||||
assert "Словарные подсказки по ключевым словам предложения" in prompt
|
||||
assert "НЕ орёл" in prompt
|
||||
assert "ПОЛНОТА И СВЯЗНОСТЬ" in prompt
|
||||
assert "large" in prompt
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user