rt_en.go raw

   1  package iskra
   2  
   3  import "sort"
   4  
   5  // ENFixpoint is the outcome of the EN renderer fixpoint test.
   6  //
   7  // Two comparisons: positional (Set1 equals Set2 entry for entry, which is what
   8  // SetDiffKind measures) and bag (match Set1 entries to Set2 by role and atom,
   9  // since the renderer reorders to SVO while the extractor emits token order).
  10  type ENFixpoint struct {
  11  	Total      int
  12  	ENStable   int
  13  	PosMatch   int
  14  	BagMatch   int
  15  	TotalAtoms int
  16  	AtomOK     int
  17  	AtomRole   int
  18  	AtomMorph  int
  19  	AtomObl    int
  20  	AtomMark   int
  21  	AtomLost   int
  22  	AtomGained int
  23  	RoleDrift  [8][8]int
  24  	MorphDrift map[string]int
  25  }
  26  
  27  // SetDiffKind reports the first field that differs between two sets, or "".
  28  func SetDiffKind(a, b []SetEntry) string {
  29  	if len(a) != len(b) {
  30  		return "LEN"
  31  	}
  32  	for i := range a {
  33  		if a[i].Role != b[i].Role {
  34  			return "ROLE"
  35  		}
  36  		if a[i].Atom != b[i].Atom {
  37  			return "ATOM"
  38  		}
  39  		if a[i].Morph != b[i].Morph {
  40  			return "MORPH"
  41  		}
  42  		if a[i].OblRole != b[i].OblRole {
  43  			return "OBL"
  44  		}
  45  		if a[i].Head != b[i].Head {
  46  			return "HEAD"
  47  		}
  48  		if a[i].ModKind != b[i].ModKind {
  49  			return "MODKIND"
  50  		}
  51  	}
  52  	return ""
  53  }
  54  
  55  // RoundTripEN runs en -> Set -> RenderEN -> ExtractEN -> Set' on one sentence
  56  // and accumulates the fixpoint statistics.
  57  func (f *ENFixpoint) RoundTripEN(en string) {
  58  	f.Total++
  59  	ext1 := ExtractEN(TokenizeEN(en))
  60  	set1 := FlatSetFromDiscourse(ext1.Discourse)
  61  	en1 := RenderENDiscourse(ext1.Discourse)
  62  	ext2 := ExtractEN(TokenizeEN(en1))
  63  	set2 := FlatSetFromDiscourse(ext2.Discourse)
  64  	en2 := RenderENDiscourse(ext2.Discourse)
  65  	if en1 == en2 {
  66  		f.ENStable++
  67  	}
  68  	if SetDiffKind(set1, set2) == "" {
  69  		f.PosMatch++
  70  	}
  71  	used2 := make([]bool, len(set2))
  72  	sentOK := true
  73  	for _, e1 := range set1 {
  74  		f.TotalAtoms++
  75  		matched := false
  76  		for j, e2 := range set2 {
  77  			if used2[j] || e1.Atom != e2.Atom || e1.Role != e2.Role {
  78  				continue
  79  			}
  80  			used2[j] = true
  81  			matched = true
  82  			switch {
  83  			case e1.Morph != e2.Morph:
  84  				f.AtomMorph++
  85  				sentOK = false
  86  				key := itoa(int(e1.Morph)) + "->" + itoa(int(e2.Morph))
  87  				if f.MorphDrift == nil {
  88  					f.MorphDrift = map[string]int{}
  89  				}
  90  				f.MorphDrift[key]++
  91  			case e1.OblRole != e2.OblRole:
  92  				f.AtomObl++
  93  				sentOK = false
  94  			case e1.Mark != e2.Mark:
  95  				f.AtomMark++
  96  				sentOK = false
  97  			default:
  98  				f.AtomOK++
  99  			}
 100  			break
 101  		}
 102  		if matched {
 103  			continue
 104  		}
 105  		roleMatched := false
 106  		for j, e2 := range set2 {
 107  			if used2[j] || e1.Atom != e2.Atom {
 108  				continue
 109  			}
 110  			used2[j] = true
 111  			roleMatched = true
 112  			f.AtomRole++
 113  			sentOK = false
 114  			if e1.Role >= 0 && e1.Role < 8 && e2.Role >= 0 && e2.Role < 8 {
 115  				f.RoleDrift[e1.Role][e2.Role]++
 116  			}
 117  			break
 118  		}
 119  		if !roleMatched {
 120  			f.AtomLost++
 121  			sentOK = false
 122  		}
 123  	}
 124  	for j := range set2 {
 125  		if !used2[j] {
 126  			f.AtomGained++
 127  			sentOK = false
 128  		}
 129  	}
 130  	if sentOK {
 131  		f.BagMatch++
 132  	}
 133  }
 134  
 135  // MorphDriftTop returns the morph drift labels by descending count, at most n.
 136  func (f *ENFixpoint) MorphDriftTop(n int) [][2]any {
 137  	type kv struct {
 138  		k string
 139  		v int
 140  	}
 141  	var list []kv
 142  	for k, v := range f.MorphDrift {
 143  		list = append(list, kv{k, v})
 144  	}
 145  	sort.Slice(list, func(i, j int) bool {
 146  		if list[i].v != list[j].v {
 147  			return list[i].v > list[j].v
 148  		}
 149  		return list[i].k < list[j].k
 150  	})
 151  	if len(list) > n {
 152  		list = list[:n]
 153  	}
 154  	out := make([][2]any, 0, len(list))
 155  	for _, e := range list {
 156  		out = append(out, [2]any{e.k, e.v})
 157  	}
 158  	return out
 159  }
 160