package iskra import ( "math" ) // IngestStats counts the scoring events of a lookup run. type IngestStats struct { TriFired int32 TriConfirmed int32 TriSwapped int32 TriRescued int32 CtxSimFired int32 CtxSimBoosted int32 DictConfirmFired int32 DictAuthorityFired int32 } // LookupAtomLink picks the destination atom for (srcLang, srcAtom) in dstLang. // ingest_pattern.mx LookupAtomLink, including its tiers, its bilateral // consistency ratio, the context and tri-gram bonuses, and its 0.1 cutoff // against the best aggregate score inside a tier. // // The function is carried into Go with float64 scoring because the source // scores in float64; nothing here is an exact integer distance. func LookupAtomLink(idx *AtomIdx, srcLang, dstLang uint8, srcAtom, srcContext string, srcRole int32, qArch, qDisc uint8, sigmaArch, sigmaDisc float64, stats *IngestStats) AtomLinkResult { if idx == nil { return AtomLinkResult{} } candidates := idx.FindBySrc(srcLang, srcAtom) if len(candidates) == 0 { return AtomLinkResult{} } gauss := func(rArch, rDisc uint8) float64 { if sigmaArch <= 0 || sigmaDisc <= 0 { return 1.0 } da := float64(int32(rArch) - int32(qArch)) dd := float64(int32(rDisc) - int32(qDisc)) return math.Exp(-(da*da)/(sigmaArch*sigmaArch) - (dd*dd)/(sigmaDisc*sigmaDisc)) } diversity := map[string]map[uint16]bool{} for i := range candidates { e := &candidates[i] if e.DstLang != dstLang || gauss(e.RArchaic, e.RDiscourse) < diversityNearThreshold { continue } coord := uint16(e.RArchaic)<<8 | uint16(e.RDiscourse) if diversity[e.DstAtom] == nil { diversity[e.DstAtom] = map[uint16]bool{} } diversity[e.DstAtom][coord] = true } baseScore := func(e *AtomIdxEntry) float64 { div := float64(len(diversity[e.DstAtom])) if div < 1 { div = 1 } return math.Log1p(float64(e.Weight)) * div * gauss(e.RArchaic, e.RDiscourse) } ctxTranslation := "" if srcContext != "" { var bestCtxW float64 for _, cc := range idx.FindBySrc(srcLang, srcContext) { if cc.DstLang != dstLang { continue } if w := float64(cc.Weight) * gauss(cc.RArchaic, cc.RDiscourse); w > bestCtxW { bestCtxW = w ctxTranslation = cc.DstAtom } } } biCheck := func(dstAtom string) float64 { var best, src float64 for _, bc := range idx.FindBySrc(dstLang, dstAtom) { if bc.DstLang != srcLang { continue } w := float64(bc.Weight) if w > best { best = w } if bc.DstAtom == srcAtom && w > src { src = w } } if best <= 0 { return 1.0 } return (src + 1) / (best + 1) } type atomAgg struct { best *AtomIdxEntry aggScore float64 tier int } aggs := map[string]*atomAgg{} var keys []string for i := range candidates { e := &candidates[i] if e.DstLang != dstLang { continue } ti := -1 switch e.Gen { case GenContexted: if e.ContextA == srcContext && int32(e.RoleA) == srcRole { ti = 0 } else if int32(e.RoleA) == srcRole { ti = 1 } else { ti = 2 } case GenLegacy: ti = 3 } if ti < 0 { continue } key := string(rune('0'+ti)) + ":" + e.DstAtom a := aggs[key] if a == nil { a = &atomAgg{tier: ti} aggs[key] = a keys = append(keys, key) } s := baseScore(e) a.aggScore += s if a.best == nil || s > baseScore(a.best) { a.best = e } } const topN = 8 type ranked struct { entry *AtomIdxEntry aggScore float64 } var top [5][topN]ranked for _, key := range keys { a := aggs[key] slot := -1 for k := 0; k < topN; k++ { if top[a.tier][k].entry == nil || a.aggScore > top[a.tier][k].aggScore { slot = k break } } if slot < 0 { continue } for k := topN - 1; k > slot; k-- { top[a.tier][k] = top[a.tier][k-1] } top[a.tier][slot] = ranked{entry: a.best, aggScore: a.aggScore} } ctxMatch := func(e *AtomIdxEntry) bool { return ctxTranslation != "" && e.ContextB == ctxTranslation } type scored struct { entry *AtomIdxEntry combined float64 } var viable []scored for ti := 0; ti < 4; ti++ { topAgg := 0.0 if top[ti][0].entry != nil { topAgg = top[ti][0].aggScore } for k := 0; k < topN; k++ { r := &top[ti][k] if r.entry == nil { continue } if topAgg > 0 && r.aggScore < topAgg*0.1 { continue } combined := r.aggScore * (biCheck(r.entry.DstAtom) + 0.05) if ctxMatch(r.entry) { combined *= 1.5 if stats != nil { stats.CtxSimFired++ } } viable = append(viable, scored{entry: r.entry, combined: combined}) } if len(viable) > 0 { break } } if len(viable) >= 2 && stats != nil { stats.TriFired++ } var pick *AtomIdxEntry var tier uint8 best := 0.0 for _, v := range viable { if v.combined > best { best = v.combined pick = v.entry tier = 4 } } if pick == nil { return AtomLinkResult{} } return AtomLinkResult{ DstAtom: pick.DstAtom, DstRole: int32(pick.RoleB), DstContext: pick.ContextB, Weight: pick.Weight, Generation: pick.Gen, Tier: tier, } }