package iskra import "git.smesh.lol/go-iskra/lattice" // PatternKey is the key of a sentence pattern at a domain, pattern.mx // PatternKey: domain | 'P' | pattern bytes. func PatternKey(domain uint8, pat []byte) lattice.Key { buf := make([]byte, 0, 2+len(pat)) buf = append(buf, domain, 'P') buf = append(buf, pat...) return lattice.HashKey(buf) } // AtomKey is the key of one content atom at a domain: domain | 'W' | atom. func AtomKey(domain uint8, atom string) lattice.Key { buf := make([]byte, 0, 2+len(atom)) buf = append(buf, domain, 'W') buf = append(buf, atom...) return lattice.HashKey(buf) } // DeepPatternKey is the key of a canonical role sequence: 'D' | roles. func DeepPatternKey(deepPat []byte) lattice.Key { buf := make([]byte, 0, 1+len(deepPat)) buf = append(buf, 'D') buf = append(buf, deepPat...) return lattice.HashKey(buf) } // CrossPatternKey is the key of a pattern pair across two domains: // src | dst | 'X' | srcPat | dstPat. func CrossPatternKey(srcDomain, dstDomain uint8, srcPat, dstPat []byte) lattice.Key { buf := make([]byte, 0, 3+len(srcPat)+len(dstPat)) buf = append(buf, srcDomain, dstDomain, 'X') buf = append(buf, srcPat...) buf = append(buf, dstPat...) return lattice.HashKey(buf) } // RoleHist is an atom's role histogram: which roles it has been seen in. // pattern.mx stores it in MetaEntry.Extra[0:16] as eight uint16 counters. type RoleHist [8]uint16 // Inc bumps one histogram bucket. func (h *RoleHist) Inc(role int32) { if role >= 0 && role < 8 && h[role] < 0xFFFF { h[role]++ } } // Encode writes the histogram into Extra. func (h *RoleHist) Encode(extra *[16]byte) { for i := 0; i < 8; i++ { extra[i*2] = byte(h[i]) extra[i*2+1] = byte(h[i] >> 8) } } // Decode reads the histogram from Extra. func (h *RoleHist) Decode(extra [16]byte) { for i := 0; i < 8; i++ { h[i] = uint16(extra[i*2]) | uint16(extra[i*2+1])<<8 } } // DominantRole is the bucket with the highest count. func (h *RoleHist) DominantRole() uint8 { best, idx := uint16(0), 0 for i, v := range h { if v > best { best, idx = v, i } } return uint8(idx) } // IngestPattern stores one extraction: the pattern in the grammatical branch // and every content slot as an atom in the semantic branch, with each atom's // role added to its histogram. Returns the pattern's record index. func (t *Tree) IngestPattern(domain uint8, ext ExtractResult) (uint32, error) { if len(ext.Pattern) == 0 { return lattice.NullRec, nil } patKey := PatternKey(domain, ext.Pattern) patRI, err := t.DB.LookupRecIdx(lattice.Bgrammatical, patKey) if err != nil { return 0, err } if patRI != lattice.NullRec { t.MetaInc(patRI) } else { var rec lattice.Record lattice.SetFormOnRecord(&rec, string(ext.Pattern), &t.StringPool) rec.Branch = uint8(lattice.Bgrammatical) patRI, err = t.DB.InsertRec(lattice.Bgrammatical, patKey, rec) if err != nil { return 0, err } t.SetMeta(patRI, lattice.MetaRecord{Count: 1, StageTag: domain}) } for i, word := range ext.Slots { if word == "" { continue } atomForm := word if i < len(ext.Set) && ext.Set[i].Atom != "" { atomForm = ext.Set[i].Atom } atomKey := AtomKey(domain, atomForm) atomRI, err := t.DB.LookupRecIdx(lattice.Bsemantic, atomKey) if err != nil { return 0, err } if atomRI != lattice.NullRec { t.MetaInc(atomRI) if i < len(ext.Roles) { if m := t.Meta(atomRI); m != nil { var h RoleHist h.Decode(m.Extra) h.Inc(ext.Roles[i]) h.Encode(&m.Extra) } } continue } var rec lattice.Record lattice.SetFormOnRecord(&rec, atomForm, &t.StringPool) rec.Branch = uint8(lattice.Bsemantic) atomRI, err = t.DB.InsertRec(lattice.Bsemantic, atomKey, rec) if err != nil { return 0, err } m := lattice.MetaRecord{Count: 1, StageTag: domain} if i < len(ext.Roles) { var h RoleHist h.Inc(ext.Roles[i]) h.Encode(&m.Extra) } t.SetMeta(atomRI, m) } return patRI, nil } // IngestCrossDomain links a source pattern to its translation's pattern. func (t *Tree) IngestCrossDomain(srcDomain, dstDomain uint8, srcPat, dstPat []byte) error { if len(srcPat) == 0 || len(dstPat) == 0 { return nil } key := CrossPatternKey(srcDomain, dstDomain, srcPat, dstPat) ri, err := t.DB.LookupRecIdx(lattice.Bcooccur, key) if err != nil { return err } if ri != lattice.NullRec { t.MetaInc(ri) return nil } var rec lattice.Record lattice.SetFormOnRecord(&rec, string(srcPat)+"="+string(dstPat), &t.StringPool) rec.Branch = uint8(lattice.Bcooccur) ri, err = t.DB.InsertRec(lattice.Bcooccur, key, rec) if err != nil { return err } t.SetMeta(ri, lattice.MetaRecord{Count: 1, StageTag: srcDomain}) return nil } // IngestDeepPattern stores a canonical role sequence with stage tag 0. func (t *Tree) IngestDeepPattern(deepPat []byte) error { if len(deepPat) == 0 { return nil } key := DeepPatternKey(deepPat) ri, err := t.DB.LookupRecIdx(lattice.Bgrammatical, key) if err != nil { return err } if ri != lattice.NullRec { t.MetaInc(ri) return nil } var rec lattice.Record lattice.SetFormOnRecord(&rec, string(deepPat), &t.StringPool) rec.Branch = uint8(lattice.Bgrammatical) ri, err = t.DB.InsertRec(lattice.Bgrammatical, key, rec) if err != nil { return err } t.SetMeta(ri, lattice.MetaRecord{Count: 1, StageTag: 0}) return nil } // IngestAtomLink stores one legacy bilateral atom link: src=dst in the // pragmatic branch, with the register coordinates in Extra[2] and Extra[3]. func (t *Tree) IngestAtomLink(srcDomain, dstDomain uint8, srcAtom, dstAtom string, rArch, rDisc uint8) error { key := LookupKey(srcDomain, dstDomain, srcAtom, dstAtom, rArch, rDisc) ri, err := t.DB.LookupRecIdx(lattice.Bpragmatic, key) if err != nil { return err } if ri != lattice.NullRec { t.MetaInc(ri) return nil } var rec lattice.Record lattice.SetFormOnRecord(&rec, LinkForm(srcAtom, dstAtom), &t.StringPool) rec.Branch = uint8(lattice.Bpragmatic) ri, err = t.DB.InsertRec(lattice.Bpragmatic, key, rec) if err != nil { return err } m := lattice.MetaRecord{Count: 1, StageTag: srcDomain} m.Extra[2] = rArch m.Extra[3] = rDisc t.SetMeta(ri, m) return nil }