fix(翻译bug):修复翻译超时失败!
deploy-hxtesting / build-and-deploy (push) Successful in 1m15s

This commit is contained in:
runny committed 2026-11-30 23:22:23 +08:00
1 parent 74d21cb204
commit 7d3b6804af
5 files changed
+220 -15

No files matched your search

+7
View File
@@ -141,6 +141,13 @@ func trimSpaceASCII(s string) string {
return strings.Trim(s, " \t\n\r")
}
// collapseWhitespace trims and collapses every whitespace run (including
// full-width spaces) to a single ASCII space. Used as the dedupe key so
// layout-padded variants of one term share a single translation unit.
func collapseWhitespace(s string) string {
return strings.Join(strings.Fields(s), " ")
}
// itoa is a tiny helper so this file needs no strconv import noise.
func itoa(i int) string {
if i == 0 {
+25 -8
View File
@@ -238,15 +238,24 @@ func Translate(ctx context.Context, data []byte, opt *Options, tr Translator) ([
}
}
// Deduplicate across entries, preserving first-seen order.
// Deduplicate across entries, preserving first-seen order. Near
// duplicates that differ only in whitespace collapse to one unit:
// sending both in one batch makes the model answer with a single
// translation for "the same string twice", which desynchronises the
// positional array protocol.
unitOrder := []string{}
unitSeen := map[string]struct{}{}
unitSeen := map[string]string{}
unitCanonical := map[string]string{} // raw text -> canonical raw text
for _, job := range jobs {
for _, u := range job.units {
if _, ok := unitSeen[u]; !ok {
unitSeen[u] = struct{}{}
unitOrder = append(unitOrder, u)
key := collapseWhitespace(u)
if canonical, ok := unitSeen[key]; ok {
unitCanonical[u] = canonical
continue
}
unitSeen[key] = u
unitCanonical[u] = u
unitOrder = append(unitOrder, u)
}
}
// Oversized units are skipped rather than risking a broken LLM call.
@@ -366,6 +375,12 @@ func Translate(ctx context.Context, data []byte, opt *Options, tr Translator) ([
report.AppliedTranslations = len(translations)
// ---- Pass 3: rewrite affected entries, repack in original order ----
alias := func(raw string) string {
if canonical, ok := unitCanonical[raw]; ok {
return canonical
}
return raw
}
type outEntry struct {
idx int
data []byte
@@ -374,7 +389,7 @@ func Translate(ctx context.Context, data []byte, opt *Options, tr Translator) ([
for i := range reader.File {
data := entryData[i]
if job, ok := jobs[i]; ok {
data = []byte(applyJob(string(data), job, translations))
data = []byte(applyJob(string(data), job, translations, alias))
}
out = append(out, outEntry{idx: i, data: data})
}
@@ -518,7 +533,9 @@ func scanDrawing(src string) (*scanJob, int) {
// applyJob rewrites one entry with the resolved translations. Bytes outside
// the recorded edits — styles, geometry, image references — are untouched.
func applyJob(src string, job *scanJob, translations map[string]string) string {
// alias resolves a raw unit to its canonical unit (whitespace-variant
// near duplicates share one translation).
func applyJob(src string, job *scanJob, translations map[string]string, alias func(string) string) string {
type repl struct {
start, end int
text string
@@ -527,7 +544,7 @@ func applyJob(src string, job *scanJob, translations map[string]string) string {
for _, e := range job.edits {
switch e.kind {
case editCarrier:
t, ok := translations[job.units[e.unitIdx]]
t, ok := translations[alias(job.units[e.unitIdx])]
if !ok {
continue // oversized/skipped unit: keep the original bytes
}
+31
View File
@@ -440,3 +440,34 @@ func min(a, b int) int {
}
return b
}
// TestTranslateCollapsesWhitespaceVariants covers the positional-protocol
// desync: whitespace-padded variants of one term must collapse into a single
// translation unit, or a batch containing both loses an answer (19 of 20).
func TestTranslateCollapsesWhitespaceVariants(t *testing.T) {
var buf bytes.Buffer
zw := zip.NewWriter(&buf)
w, _ := zw.Create("xl/sharedStrings.xml")
w.Write([]byte(`<sst xmlns="x">` +
`<si><t>客 戶</t></si>` +
`<si><t>客 戶</t></si>` +
`<si><t>客户资料清单</t></si>` +
`</sst>`))
zw.Close()
tr := &fakeTranslator{}
out, report, err := Translate(context.Background(), buf.Bytes(), nil, tr)
if err != nil {
t.Fatalf("Translate: %v", err)
}
if report.TranslatedUnits != 2 {
t.Fatalf("expected 2 canonical units after whitespace collapse, got %d", report.TranslatedUnits)
}
ss := string(zipEntry(t, out, "xl/sharedStrings.xml"))
if hasCJK(ss) {
t.Fatalf("variants left untranslated:\n%s", ss)
}
if strings.Count(ss, "Customer") != 2 {
t.Fatalf("both whitespace variants should carry the glossary translation:\n%s", ss)
}
}