rt_cross.go raw

   1  package iskra
   2  
   3  import "strings"
   4  
   5  // TranslateSet turns every atom of a set into dstLang through the atom index,
   6  // keeping the structural fields (Role, Morph, OblRole, Head, ModKind) and
   7  // replacing the language-bound ones (Atom, Mark, Class).
   8  //
   9  // cmd/iskra/roundtrip_cross.mx translateSetCtx: the governing context of an
  10  // entry is the atom at its Head, or the clause's verb atom for a top-level
  11  // entry that is not the verb itself. An atom with no link becomes
  12  // UntranslatedMarker, which the renderers emit verbatim and do not inflect.
  13  func TranslateSet(set []SetEntry, srcLang, dstLang uint8, idx *AtomIdx, qArch, qDisc uint8, stats *IngestStats) []SetEntry {
  14  	verbAtom := ""
  15  	for _, e := range set {
  16  		if e.Role == HistVerb {
  17  			verbAtom = e.Atom
  18  			break
  19  		}
  20  	}
  21  	out := make([]SetEntry, 0, len(set))
  22  	for _, e := range set {
  23  		ctx := ""
  24  		if e.Head >= 0 && int(e.Head) < len(set) {
  25  			ctx = set[e.Head].Atom
  26  		} else if e.Role != HistVerb {
  27  			ctx = verbAtom
  28  		}
  29  		atom := UntranslatedMarker
  30  		if idx != nil {
  31  			r := LookupAtomLink(idx, srcLang, dstLang, e.Atom, ctx, e.Role,
  32  				qArch, qDisc, DefaultSigmaArchaic, DefaultSigmaDiscourse, stats)
  33  			if r.DstAtom != "" {
  34  				atom = r.DstAtom
  35  			}
  36  		}
  37  		out = append(out, SetEntry{
  38  			Role: e.Role, Atom: atom, Morph: e.Morph, Class: 0,
  39  			OblRole: e.OblRole, Head: e.Head, ModKind: e.ModKind,
  40  		})
  41  	}
  42  	return out
  43  }
  44  
  45  // TranslateDiscourse is TranslateSet over every clause.
  46  func TranslateDiscourse(d []Clause, srcLang, dstLang uint8, idx *AtomIdx, qArch, qDisc uint8, stats *IngestStats) []Clause {
  47  	out := make([]Clause, 0, len(d))
  48  	for _, c := range d {
  49  		out = append(out, Clause{
  50  			Set:      TranslateSet(c.Set, srcLang, dstLang, idx, qArch, qDisc, stats),
  51  			Relation: c.Relation, Parent: c.Parent, HostIdx: c.HostIdx,
  52  		})
  53  	}
  54  	return out
  55  }
  56  
  57  // CrossResult is one pair's round trip.
  58  type CrossResult struct {
  59  	JA                 string
  60  	EN                 string
  61  	SetA, SetB, SetC   []SetEntry
  62  	JA1, EN1, JA2, EN2 string
  63  	JAEq, ENEQ         bool
  64  	SemAeqB, SemBeqC   bool
  65  }
  66  
  67  // RoundTripCross runs the rt-cross cycle on one pair:
  68  //
  69  //	en -> JA1 -> en1 -> JA2 -> en2
  70  //
  71  // and reports surface idempotence (JA1 == JA2, EN1 == EN2) and semantic
  72  // preservation (the set of en equals the set of en1 and of en2).
  73  func RoundTripCross(ja, en string, idx *AtomIdx, qArch, qDisc uint8, stats *IngestStats) CrossResult {
  74  	r := CrossResult{JA: ja, EN: en}
  75  	discA := ExtractEN(TokenizeEN(en)).Discourse
  76  	r.SetA = FlatSetFromDiscourse(discA)
  77  	discJA1 := TranslateDiscourse(discA, LangEN, LangJA, idx, qArch, qDisc, stats)
  78  	r.JA1 = RenderJADiscourse(discJA1)
  79  	discJA1back := ExtractJA(TokenizeJA(r.JA1)).Discourse
  80  	discEN1 := TranslateDiscourse(discJA1back, LangJA, LangEN, idx, qArch, qDisc, stats)
  81  	r.EN1 = RenderENDiscourse(discEN1)
  82  	discB := ExtractEN(TokenizeEN(r.EN1)).Discourse
  83  	r.SetB = FlatSetFromDiscourse(discB)
  84  
  85  	discJA2 := TranslateDiscourse(discB, LangEN, LangJA, idx, qArch, qDisc, stats)
  86  	r.JA2 = RenderJADiscourse(discJA2)
  87  	discJA2back := ExtractJA(TokenizeJA(r.JA2)).Discourse
  88  	discEN2 := TranslateDiscourse(discJA2back, LangJA, LangEN, idx, qArch, qDisc, stats)
  89  	r.EN2 = RenderENDiscourse(discEN2)
  90  	discC := ExtractEN(TokenizeEN(r.EN2)).Discourse
  91  	r.SetC = FlatSetFromDiscourse(discC)
  92  
  93  	r.JAEq = r.JA1 == r.JA2
  94  	r.ENEQ = r.EN1 == r.EN2
  95  	r.SemAeqB = DiscoursesEqual(discA, discB)
  96  	r.SemBeqC = DiscoursesEqual(discB, discC)
  97  	return r
  98  }
  99  
 100  // Score summarises a list of cross results.
 101  type CrossScore struct {
 102  	Total, JAStable, ENStable, Both, Semantic int
 103  }
 104  
 105  // Summarise counts a round-trip run.
 106  func Summarise(rs []CrossResult) CrossScore {
 107  	s := CrossScore{Total: len(rs)}
 108  	for _, r := range rs {
 109  		if r.JAEq {
 110  			s.JAStable++
 111  		}
 112  		if r.ENEQ {
 113  			s.ENStable++
 114  		}
 115  		if r.JAEq && r.ENEQ {
 116  			s.Both++
 117  		}
 118  		if r.SemAeqB && r.SemBeqC {
 119  			s.Semantic++
 120  		}
 121  	}
 122  	return s
 123  }
 124  
 125  // Percent is a whole-number percentage, 0 for an empty run.
 126  func Percent(a, b int) int {
 127  	if b == 0 {
 128  		return 0
 129  	}
 130  	return a * 100 / b
 131  }
 132  
 133  // FormatCrossSet renders a set the way rt-cross prints it.
 134  func FormatCrossSet(set []SetEntry) string {
 135  	var b strings.Builder
 136  	for i, e := range set {
 137  		if i > 0 {
 138  			b.WriteString(" | ")
 139  		}
 140  		b.WriteString("[")
 141  		b.WriteString(itoa(i))
 142  		b.WriteString(":r")
 143  		b.WriteString(itoa(int(e.Role)))
 144  		b.WriteString(" \"")
 145  		b.WriteString(e.Atom)
 146  		b.WriteString("\" m")
 147  		b.WriteString(hex(e.Morph, 2))
 148  		b.WriteString(" obl")
 149  		b.WriteString(itoa(int(e.OblRole)))
 150  		b.WriteString(" h")
 151  		b.WriteString(itoa(int(e.Head)))
 152  		b.WriteString(" mk")
 153  		b.WriteString(itoa(int(e.ModKind)))
 154  		b.WriteString("]")
 155  	}
 156  	return b.String()
 157  }
 158  
 159  func itoa(v int) string {
 160  	if v == 0 {
 161  		return "0"
 162  	}
 163  	neg := v < 0
 164  	if neg {
 165  		v = -v
 166  	}
 167  	var buf [12]byte
 168  	i := len(buf)
 169  	for v > 0 {
 170  		i--
 171  		buf[i] = byte('0' + v%10)
 172  		v /= 10
 173  	}
 174  	if neg {
 175  		i--
 176  		buf[i] = '-'
 177  	}
 178  	return string(buf[i:])
 179  }
 180  
 181  func hex(v uint16, width int) string {
 182  	const digits = "0123456789abcdef"
 183  	var buf [4]byte
 184  	i := len(buf)
 185  	for v > 0 || i > len(buf)-width {
 186  		i--
 187  		buf[i] = digits[v&0xF]
 188  		v >>= 4
 189  		if v == 0 && i <= len(buf)-width {
 190  			break
 191  		}
 192  	}
 193  	return string(buf[i:])
 194  }
 195