quality.go raw

   1  package iskra
   2  
   3  // QualityThreshold is the accept/reject cutoff on a pair's combined score.
   4  // cmd/iskra/quality.mx: the 2/3 line, calibrated so fidelity corpora pass at
   5  // 85-95% and loose or paraphrastic corpora at 30-37%.
   6  const QualityThreshold float32 = 0.666
   7  
   8  // MinTokensForGate is the measurement floor: under this many tokens the ratio
   9  // components swing too far on a single word to be acted on, so the pair passes
  10  // unconditionally.
  11  const MinTokensForGate = 8
  12  
  13  // PairQuality is the per-pair component breakdown.
  14  type PairQuality struct {
  15  	LengthRatio    float32
  16  	CharRatio      float32
  17  	ClauseRatio    float32
  18  	ContentRatio   float32
  19  	RoleJaccard    float32
  20  	PredShapeMatch float32
  21  	ModifierRatio  float32
  22  	Score          float32
  23  }
  24  
  25  // ratioToScore maps a ratio to [0,1]: symmetric in multiplicative space around
  26  // center, linear to 0 at center*(1+bandwidth).
  27  func ratioToScore(r, center, bandwidth float32) float32 {
  28  	if r <= 0 {
  29  		return 0
  30  	}
  31  	dev := r / center
  32  	if dev < 1 {
  33  		dev = 1 / dev
  34  	}
  35  	excess := dev - 1
  36  	if excess >= bandwidth {
  37  		return 0
  38  	}
  39  	return 1 - excess/bandwidth
  40  }
  41  
  42  // ScorePair scores one JA/EN pair. The combined score is the equal-weighted
  43  // mean of the five discriminative components; PredShapeMatch and ModifierRatio
  44  // are recorded for diagnosis and kept out of it, as in the source.
  45  func ScorePair(ja, en string, jaToks, enToks []string, jaExt, enExt ExtractResult) PairQuality {
  46  	var q PairQuality
  47  	if len(enToks) > 0 {
  48  		q.LengthRatio = ratioToScore(float32(len(jaToks))/float32(len(enToks)), 1.3, 2.0)
  49  	}
  50  	if len(en) > 0 {
  51  		q.CharRatio = ratioToScore(float32(len(ja))/float32(len(en)), 1.35, 1.5)
  52  	}
  53  	jaClauses, enClauses := len(jaExt.Discourse), len(enExt.Discourse)
  54  	if jaClauses < 1 {
  55  		jaClauses = 1
  56  	}
  57  	if enClauses < 1 {
  58  		enClauses = 1
  59  	}
  60  	q.ClauseRatio = ratioToScore(float32(jaClauses)/float32(enClauses), 1.0, 1.5)
  61  	jaContent, enContent := countContent(jaExt), countContent(enExt)
  62  	if enContent > 0 {
  63  		q.ContentRatio = ratioToScore(float32(jaContent)/float32(enContent), 1.3, 2.0)
  64  	}
  65  	q.RoleJaccard = roleJaccard(jaExt, enExt)
  66  	q.PredShapeMatch = predicationShapeMatch(jaExt, enExt)
  67  	jaMods, enMods := countModifiers(jaExt), countModifiers(enExt)
  68  	switch {
  69  	case jaMods+enMods == 0:
  70  		q.ModifierRatio = 1.0
  71  	case enMods == 0:
  72  		q.ModifierRatio = 0.5
  73  	default:
  74  		q.ModifierRatio = ratioToScore(float32(jaMods)/float32(enMods), 1.0, 2.0)
  75  	}
  76  	q.Score = (q.LengthRatio + q.CharRatio + q.ClauseRatio + q.ContentRatio + q.RoleJaccard) / 5.0
  77  	return q
  78  }
  79  
  80  // PassesGate decides whether a pair is stored. Below the measurement floor a
  81  // pair passes unconditionally; above it the score must reach QualityThreshold.
  82  func PassesGate(q PairQuality, jaToks, enToks []string) bool {
  83  	n := len(jaToks)
  84  	if len(enToks) > n {
  85  		n = len(enToks)
  86  	}
  87  	if n < MinTokensForGate {
  88  		return true
  89  	}
  90  	return q.Score >= QualityThreshold
  91  }
  92  
  93  func countContent(ext ExtractResult) int {
  94  	n := 0
  95  	for _, c := range ext.Discourse {
  96  		n += len(c.Set)
  97  	}
  98  	return n
  99  }
 100  
 101  func countModifiers(ext ExtractResult) int {
 102  	n := 0
 103  	for _, c := range ext.Discourse {
 104  		for _, e := range c.Set {
 105  			if e.ModKind != MKNone {
 106  				n++
 107  			}
 108  		}
 109  	}
 110  	return n
 111  }
 112  
 113  // normalizeRoleCognate maps roles onto their cross-language cognate class so a
 114  // JA topic counts as an EN subject and a JA scope as an EN modifier.
 115  func normalizeRoleCognate(r int32) int32 {
 116  	switch r {
 117  	case HistTopic:
 118  		return HistSubject
 119  	case HistScope:
 120  		return HistModifier
 121  	}
 122  	return r
 123  }
 124  
 125  // roleJaccard is the set Jaccard over cognate-normalized roles present on each
 126  // side. Set rather than multiset: JA splits obliques into more slots than EN, so
 127  // a faithful pair has the same role set and different role counts.
 128  func roleJaccard(jaExt, enExt ExtractResult) float32 {
 129  	const numRoles = 16
 130  	var jaSet, enSet [numRoles]bool
 131  	for _, c := range jaExt.Discourse {
 132  		for _, e := range c.Set {
 133  			if r := normalizeRoleCognate(e.Role); r >= 0 && r < numRoles {
 134  				jaSet[r] = true
 135  			}
 136  		}
 137  	}
 138  	for _, c := range enExt.Discourse {
 139  		for _, e := range c.Set {
 140  			if r := normalizeRoleCognate(e.Role); r >= 0 && r < numRoles {
 141  				enSet[r] = true
 142  			}
 143  		}
 144  	}
 145  	inter, union := 0, 0
 146  	for i := 0; i < numRoles; i++ {
 147  		if jaSet[i] && enSet[i] {
 148  			inter++
 149  			union++
 150  		} else if jaSet[i] || enSet[i] {
 151  			union++
 152  		}
 153  	}
 154  	if union == 0 {
 155  		return 0
 156  	}
 157  	return float32(inter) / float32(union)
 158  }
 159  
 160  // predicationShapeMatch is the fraction of clauses, over the shorter side,
 161  // whose predication shape agrees.
 162  func predicationShapeMatch(jaExt, enExt ExtractResult) float32 {
 163  	n := len(jaExt.Discourse)
 164  	if len(enExt.Discourse) < n {
 165  		n = len(enExt.Discourse)
 166  	}
 167  	if n == 0 {
 168  		return 0
 169  	}
 170  	matches := 0
 171  	for i := 0; i < n; i++ {
 172  		if ClauseShape(jaExt.Discourse[i]) == ClauseShape(enExt.Discourse[i]) {
 173  			matches++
 174  		}
 175  	}
 176  	return float32(matches) / float32(n)
 177  }
 178  
 179  // ClauseShape is 2 for a copula, 3 for a predicative adjective, 1 for a verb,
 180  // 0 for a fragment.
 181  func ClauseShape(c Clause) int32 {
 182  	hasVerb, hasCop, hasAdj := false, false, false
 183  	for _, e := range c.Set {
 184  		if e.Role == HistVerb {
 185  			hasVerb = true
 186  		}
 187  		if e.ModKind == MKCop {
 188  			hasCop = true
 189  		}
 190  		if e.ModKind == MKAdj {
 191  			hasAdj = true
 192  		}
 193  	}
 194  	switch {
 195  	case hasCop:
 196  		return 2
 197  	case hasAdj:
 198  		return 3
 199  	case hasVerb:
 200  		return 1
 201  	}
 202  	return 0
 203  }
 204  
 205  // QualitySummary accumulates the accept and reject counts of an ingest.
 206  type QualitySummary struct {
 207  	Scored, Accepted, Rejected  int
 208  	Short, Long                 int
 209  	AcceptedShort, AcceptedLong int
 210  	ScoreSum                    float64
 211  }
 212  
 213  // RecordQuality adds one pair's decision to the summary.
 214  func (q *QualitySummary) RecordQuality(pq PairQuality, jaToks, enToks []string, passed bool) {
 215  	q.Scored++
 216  	q.ScoreSum += float64(pq.Score)
 217  	n := len(jaToks)
 218  	if len(enToks) > n {
 219  		n = len(enToks)
 220  	}
 221  	if n < MinTokensForGate {
 222  		q.Short++
 223  		if passed {
 224  			q.AcceptedShort++
 225  		}
 226  	} else {
 227  		q.Long++
 228  		if passed {
 229  			q.AcceptedLong++
 230  		}
 231  	}
 232  	if passed {
 233  		q.Accepted++
 234  	} else {
 235  		q.Rejected++
 236  	}
 237  }
 238  
 239  // Mean is the average pair score.
 240  func (q *QualitySummary) Mean() float64 {
 241  	if q.Scored == 0 {
 242  		return 0
 243  	}
 244  	return q.ScoreSum / float64(q.Scored)
 245  }
 246