package iskra // QualityThreshold is the accept/reject cutoff on a pair's combined score. // cmd/iskra/quality.mx: the 2/3 line, calibrated so fidelity corpora pass at // 85-95% and loose or paraphrastic corpora at 30-37%. const QualityThreshold float32 = 0.666 // MinTokensForGate is the measurement floor: under this many tokens the ratio // components swing too far on a single word to be acted on, so the pair passes // unconditionally. const MinTokensForGate = 8 // PairQuality is the per-pair component breakdown. type PairQuality struct { LengthRatio float32 CharRatio float32 ClauseRatio float32 ContentRatio float32 RoleJaccard float32 PredShapeMatch float32 ModifierRatio float32 Score float32 } // ratioToScore maps a ratio to [0,1]: symmetric in multiplicative space around // center, linear to 0 at center*(1+bandwidth). func ratioToScore(r, center, bandwidth float32) float32 { if r <= 0 { return 0 } dev := r / center if dev < 1 { dev = 1 / dev } excess := dev - 1 if excess >= bandwidth { return 0 } return 1 - excess/bandwidth } // ScorePair scores one JA/EN pair. The combined score is the equal-weighted // mean of the five discriminative components; PredShapeMatch and ModifierRatio // are recorded for diagnosis and kept out of it, as in the source. func ScorePair(ja, en string, jaToks, enToks []string, jaExt, enExt ExtractResult) PairQuality { var q PairQuality if len(enToks) > 0 { q.LengthRatio = ratioToScore(float32(len(jaToks))/float32(len(enToks)), 1.3, 2.0) } if len(en) > 0 { q.CharRatio = ratioToScore(float32(len(ja))/float32(len(en)), 1.35, 1.5) } jaClauses, enClauses := len(jaExt.Discourse), len(enExt.Discourse) if jaClauses < 1 { jaClauses = 1 } if enClauses < 1 { enClauses = 1 } q.ClauseRatio = ratioToScore(float32(jaClauses)/float32(enClauses), 1.0, 1.5) jaContent, enContent := countContent(jaExt), countContent(enExt) if enContent > 0 { q.ContentRatio = ratioToScore(float32(jaContent)/float32(enContent), 1.3, 2.0) } q.RoleJaccard = roleJaccard(jaExt, enExt) q.PredShapeMatch = predicationShapeMatch(jaExt, enExt) jaMods, enMods := countModifiers(jaExt), countModifiers(enExt) switch { case jaMods+enMods == 0: q.ModifierRatio = 1.0 case enMods == 0: q.ModifierRatio = 0.5 default: q.ModifierRatio = ratioToScore(float32(jaMods)/float32(enMods), 1.0, 2.0) } q.Score = (q.LengthRatio + q.CharRatio + q.ClauseRatio + q.ContentRatio + q.RoleJaccard) / 5.0 return q } // PassesGate decides whether a pair is stored. Below the measurement floor a // pair passes unconditionally; above it the score must reach QualityThreshold. func PassesGate(q PairQuality, jaToks, enToks []string) bool { n := len(jaToks) if len(enToks) > n { n = len(enToks) } if n < MinTokensForGate { return true } return q.Score >= QualityThreshold } func countContent(ext ExtractResult) int { n := 0 for _, c := range ext.Discourse { n += len(c.Set) } return n } func countModifiers(ext ExtractResult) int { n := 0 for _, c := range ext.Discourse { for _, e := range c.Set { if e.ModKind != MKNone { n++ } } } return n } // normalizeRoleCognate maps roles onto their cross-language cognate class so a // JA topic counts as an EN subject and a JA scope as an EN modifier. func normalizeRoleCognate(r int32) int32 { switch r { case HistTopic: return HistSubject case HistScope: return HistModifier } return r } // roleJaccard is the set Jaccard over cognate-normalized roles present on each // side. Set rather than multiset: JA splits obliques into more slots than EN, so // a faithful pair has the same role set and different role counts. func roleJaccard(jaExt, enExt ExtractResult) float32 { const numRoles = 16 var jaSet, enSet [numRoles]bool for _, c := range jaExt.Discourse { for _, e := range c.Set { if r := normalizeRoleCognate(e.Role); r >= 0 && r < numRoles { jaSet[r] = true } } } for _, c := range enExt.Discourse { for _, e := range c.Set { if r := normalizeRoleCognate(e.Role); r >= 0 && r < numRoles { enSet[r] = true } } } inter, union := 0, 0 for i := 0; i < numRoles; i++ { if jaSet[i] && enSet[i] { inter++ union++ } else if jaSet[i] || enSet[i] { union++ } } if union == 0 { return 0 } return float32(inter) / float32(union) } // predicationShapeMatch is the fraction of clauses, over the shorter side, // whose predication shape agrees. func predicationShapeMatch(jaExt, enExt ExtractResult) float32 { n := len(jaExt.Discourse) if len(enExt.Discourse) < n { n = len(enExt.Discourse) } if n == 0 { return 0 } matches := 0 for i := 0; i < n; i++ { if ClauseShape(jaExt.Discourse[i]) == ClauseShape(enExt.Discourse[i]) { matches++ } } return float32(matches) / float32(n) } // ClauseShape is 2 for a copula, 3 for a predicative adjective, 1 for a verb, // 0 for a fragment. func ClauseShape(c Clause) int32 { hasVerb, hasCop, hasAdj := false, false, false for _, e := range c.Set { if e.Role == HistVerb { hasVerb = true } if e.ModKind == MKCop { hasCop = true } if e.ModKind == MKAdj { hasAdj = true } } switch { case hasCop: return 2 case hasAdj: return 3 case hasVerb: return 1 } return 0 } // QualitySummary accumulates the accept and reject counts of an ingest. type QualitySummary struct { Scored, Accepted, Rejected int Short, Long int AcceptedShort, AcceptedLong int ScoreSum float64 } // RecordQuality adds one pair's decision to the summary. func (q *QualitySummary) RecordQuality(pq PairQuality, jaToks, enToks []string, passed bool) { q.Scored++ q.ScoreSum += float64(pq.Score) n := len(jaToks) if len(enToks) > n { n = len(enToks) } if n < MinTokensForGate { q.Short++ if passed { q.AcceptedShort++ } } else { q.Long++ if passed { q.AcceptedLong++ } } if passed { q.Accepted++ } else { q.Rejected++ } } // Mean is the average pair score. func (q *QualitySummary) Mean() float64 { if q.Scored == 0 { return 0 } return q.ScoreSum / float64(q.Scored) }