package iskra import "strings" // TranslateSet turns every atom of a set into dstLang through the atom index, // keeping the structural fields (Role, Morph, OblRole, Head, ModKind) and // replacing the language-bound ones (Atom, Mark, Class). // // cmd/iskra/roundtrip_cross.mx translateSetCtx: the governing context of an // entry is the atom at its Head, or the clause's verb atom for a top-level // entry that is not the verb itself. An atom with no link becomes // UntranslatedMarker, which the renderers emit verbatim and do not inflect. func TranslateSet(set []SetEntry, srcLang, dstLang uint8, idx *AtomIdx, qArch, qDisc uint8, stats *IngestStats) []SetEntry { verbAtom := "" for _, e := range set { if e.Role == HistVerb { verbAtom = e.Atom break } } out := make([]SetEntry, 0, len(set)) for _, e := range set { ctx := "" if e.Head >= 0 && int(e.Head) < len(set) { ctx = set[e.Head].Atom } else if e.Role != HistVerb { ctx = verbAtom } atom := UntranslatedMarker if idx != nil { r := LookupAtomLink(idx, srcLang, dstLang, e.Atom, ctx, e.Role, qArch, qDisc, DefaultSigmaArchaic, DefaultSigmaDiscourse, stats) if r.DstAtom != "" { atom = r.DstAtom } } out = append(out, SetEntry{ Role: e.Role, Atom: atom, Morph: e.Morph, Class: 0, OblRole: e.OblRole, Head: e.Head, ModKind: e.ModKind, }) } return out } // TranslateDiscourse is TranslateSet over every clause. func TranslateDiscourse(d []Clause, srcLang, dstLang uint8, idx *AtomIdx, qArch, qDisc uint8, stats *IngestStats) []Clause { out := make([]Clause, 0, len(d)) for _, c := range d { out = append(out, Clause{ Set: TranslateSet(c.Set, srcLang, dstLang, idx, qArch, qDisc, stats), Relation: c.Relation, Parent: c.Parent, HostIdx: c.HostIdx, }) } return out } // CrossResult is one pair's round trip. type CrossResult struct { JA string EN string SetA, SetB, SetC []SetEntry JA1, EN1, JA2, EN2 string JAEq, ENEQ bool SemAeqB, SemBeqC bool } // RoundTripCross runs the rt-cross cycle on one pair: // // en -> JA1 -> en1 -> JA2 -> en2 // // and reports surface idempotence (JA1 == JA2, EN1 == EN2) and semantic // preservation (the set of en equals the set of en1 and of en2). func RoundTripCross(ja, en string, idx *AtomIdx, qArch, qDisc uint8, stats *IngestStats) CrossResult { r := CrossResult{JA: ja, EN: en} discA := ExtractEN(TokenizeEN(en)).Discourse r.SetA = FlatSetFromDiscourse(discA) discJA1 := TranslateDiscourse(discA, LangEN, LangJA, idx, qArch, qDisc, stats) r.JA1 = RenderJADiscourse(discJA1) discJA1back := ExtractJA(TokenizeJA(r.JA1)).Discourse discEN1 := TranslateDiscourse(discJA1back, LangJA, LangEN, idx, qArch, qDisc, stats) r.EN1 = RenderENDiscourse(discEN1) discB := ExtractEN(TokenizeEN(r.EN1)).Discourse r.SetB = FlatSetFromDiscourse(discB) discJA2 := TranslateDiscourse(discB, LangEN, LangJA, idx, qArch, qDisc, stats) r.JA2 = RenderJADiscourse(discJA2) discJA2back := ExtractJA(TokenizeJA(r.JA2)).Discourse discEN2 := TranslateDiscourse(discJA2back, LangJA, LangEN, idx, qArch, qDisc, stats) r.EN2 = RenderENDiscourse(discEN2) discC := ExtractEN(TokenizeEN(r.EN2)).Discourse r.SetC = FlatSetFromDiscourse(discC) r.JAEq = r.JA1 == r.JA2 r.ENEQ = r.EN1 == r.EN2 r.SemAeqB = DiscoursesEqual(discA, discB) r.SemBeqC = DiscoursesEqual(discB, discC) return r } // Score summarises a list of cross results. type CrossScore struct { Total, JAStable, ENStable, Both, Semantic int } // Summarise counts a round-trip run. func Summarise(rs []CrossResult) CrossScore { s := CrossScore{Total: len(rs)} for _, r := range rs { if r.JAEq { s.JAStable++ } if r.ENEQ { s.ENStable++ } if r.JAEq && r.ENEQ { s.Both++ } if r.SemAeqB && r.SemBeqC { s.Semantic++ } } return s } // Percent is a whole-number percentage, 0 for an empty run. func Percent(a, b int) int { if b == 0 { return 0 } return a * 100 / b } // FormatCrossSet renders a set the way rt-cross prints it. func FormatCrossSet(set []SetEntry) string { var b strings.Builder for i, e := range set { if i > 0 { b.WriteString(" | ") } b.WriteString("[") b.WriteString(itoa(i)) b.WriteString(":r") b.WriteString(itoa(int(e.Role))) b.WriteString(" \"") b.WriteString(e.Atom) b.WriteString("\" m") b.WriteString(hex(e.Morph, 2)) b.WriteString(" obl") b.WriteString(itoa(int(e.OblRole))) b.WriteString(" h") b.WriteString(itoa(int(e.Head))) b.WriteString(" mk") b.WriteString(itoa(int(e.ModKind))) b.WriteString("]") } return b.String() } func itoa(v int) string { if v == 0 { return "0" } neg := v < 0 if neg { v = -v } var buf [12]byte i := len(buf) for v > 0 { i-- buf[i] = byte('0' + v%10) v /= 10 } if neg { i-- buf[i] = '-' } return string(buf[i:]) } func hex(v uint16, width int) string { const digits = "0123456789abcdef" var buf [4]byte i := len(buf) for v > 0 || i > len(buf)-width { i-- buf[i] = digits[v&0xF] v >>= 4 if v == 0 && i <= len(buf)-width { break } } return string(buf[i:]) }