package iskra import "sort" // ENFixpoint is the outcome of the EN renderer fixpoint test. // // Two comparisons: positional (Set1 equals Set2 entry for entry, which is what // SetDiffKind measures) and bag (match Set1 entries to Set2 by role and atom, // since the renderer reorders to SVO while the extractor emits token order). type ENFixpoint struct { Total int ENStable int PosMatch int BagMatch int TotalAtoms int AtomOK int AtomRole int AtomMorph int AtomObl int AtomMark int AtomLost int AtomGained int RoleDrift [8][8]int MorphDrift map[string]int } // SetDiffKind reports the first field that differs between two sets, or "". func SetDiffKind(a, b []SetEntry) string { if len(a) != len(b) { return "LEN" } for i := range a { if a[i].Role != b[i].Role { return "ROLE" } if a[i].Atom != b[i].Atom { return "ATOM" } if a[i].Morph != b[i].Morph { return "MORPH" } if a[i].OblRole != b[i].OblRole { return "OBL" } if a[i].Head != b[i].Head { return "HEAD" } if a[i].ModKind != b[i].ModKind { return "MODKIND" } } return "" } // RoundTripEN runs en -> Set -> RenderEN -> ExtractEN -> Set' on one sentence // and accumulates the fixpoint statistics. func (f *ENFixpoint) RoundTripEN(en string) { f.Total++ ext1 := ExtractEN(TokenizeEN(en)) set1 := FlatSetFromDiscourse(ext1.Discourse) en1 := RenderENDiscourse(ext1.Discourse) ext2 := ExtractEN(TokenizeEN(en1)) set2 := FlatSetFromDiscourse(ext2.Discourse) en2 := RenderENDiscourse(ext2.Discourse) if en1 == en2 { f.ENStable++ } if SetDiffKind(set1, set2) == "" { f.PosMatch++ } used2 := make([]bool, len(set2)) sentOK := true for _, e1 := range set1 { f.TotalAtoms++ matched := false for j, e2 := range set2 { if used2[j] || e1.Atom != e2.Atom || e1.Role != e2.Role { continue } used2[j] = true matched = true switch { case e1.Morph != e2.Morph: f.AtomMorph++ sentOK = false key := itoa(int(e1.Morph)) + "->" + itoa(int(e2.Morph)) if f.MorphDrift == nil { f.MorphDrift = map[string]int{} } f.MorphDrift[key]++ case e1.OblRole != e2.OblRole: f.AtomObl++ sentOK = false case e1.Mark != e2.Mark: f.AtomMark++ sentOK = false default: f.AtomOK++ } break } if matched { continue } roleMatched := false for j, e2 := range set2 { if used2[j] || e1.Atom != e2.Atom { continue } used2[j] = true roleMatched = true f.AtomRole++ sentOK = false if e1.Role >= 0 && e1.Role < 8 && e2.Role >= 0 && e2.Role < 8 { f.RoleDrift[e1.Role][e2.Role]++ } break } if !roleMatched { f.AtomLost++ sentOK = false } } for j := range set2 { if !used2[j] { f.AtomGained++ sentOK = false } } if sentOK { f.BagMatch++ } } // MorphDriftTop returns the morph drift labels by descending count, at most n. func (f *ENFixpoint) MorphDriftTop(n int) [][2]any { type kv struct { k string v int } var list []kv for k, v := range f.MorphDrift { list = append(list, kv{k, v}) } sort.Slice(list, func(i, j int) bool { if list[i].v != list[j].v { return list[i].v > list[j].v } return list[i].k < list[j].k }) if len(list) > n { list = list[:n] } out := make([][2]any, 0, len(list)) for _, e := range list { out = append(out, [2]any{e.k, e.v}) } return out }