ingest_pattern.go raw
1 package iskra
2
3 import "git.smesh.lol/go-iskra/lattice"
4
5 // PatternKey is the key of a sentence pattern at a domain, pattern.mx
6 // PatternKey: domain | 'P' | pattern bytes.
7 func PatternKey(domain uint8, pat []byte) lattice.Key {
8 buf := make([]byte, 0, 2+len(pat))
9 buf = append(buf, domain, 'P')
10 buf = append(buf, pat...)
11 return lattice.HashKey(buf)
12 }
13
14 // AtomKey is the key of one content atom at a domain: domain | 'W' | atom.
15 func AtomKey(domain uint8, atom string) lattice.Key {
16 buf := make([]byte, 0, 2+len(atom))
17 buf = append(buf, domain, 'W')
18 buf = append(buf, atom...)
19 return lattice.HashKey(buf)
20 }
21
22 // DeepPatternKey is the key of a canonical role sequence: 'D' | roles.
23 func DeepPatternKey(deepPat []byte) lattice.Key {
24 buf := make([]byte, 0, 1+len(deepPat))
25 buf = append(buf, 'D')
26 buf = append(buf, deepPat...)
27 return lattice.HashKey(buf)
28 }
29
30 // CrossPatternKey is the key of a pattern pair across two domains:
31 // src | dst | 'X' | srcPat | dstPat.
32 func CrossPatternKey(srcDomain, dstDomain uint8, srcPat, dstPat []byte) lattice.Key {
33 buf := make([]byte, 0, 3+len(srcPat)+len(dstPat))
34 buf = append(buf, srcDomain, dstDomain, 'X')
35 buf = append(buf, srcPat...)
36 buf = append(buf, dstPat...)
37 return lattice.HashKey(buf)
38 }
39
40 // RoleHist is an atom's role histogram: which roles it has been seen in.
41 // pattern.mx stores it in MetaEntry.Extra[0:16] as eight uint16 counters.
42 type RoleHist [8]uint16
43
44 // Inc bumps one histogram bucket.
45 func (h *RoleHist) Inc(role int32) {
46 if role >= 0 && role < 8 && h[role] < 0xFFFF {
47 h[role]++
48 }
49 }
50
51 // Encode writes the histogram into Extra.
52 func (h *RoleHist) Encode(extra *[16]byte) {
53 for i := 0; i < 8; i++ {
54 extra[i*2] = byte(h[i])
55 extra[i*2+1] = byte(h[i] >> 8)
56 }
57 }
58
59 // Decode reads the histogram from Extra.
60 func (h *RoleHist) Decode(extra [16]byte) {
61 for i := 0; i < 8; i++ {
62 h[i] = uint16(extra[i*2]) | uint16(extra[i*2+1])<<8
63 }
64 }
65
66 // DominantRole is the bucket with the highest count.
67 func (h *RoleHist) DominantRole() uint8 {
68 best, idx := uint16(0), 0
69 for i, v := range h {
70 if v > best {
71 best, idx = v, i
72 }
73 }
74 return uint8(idx)
75 }
76
77 // IngestPattern stores one extraction: the pattern in the grammatical branch
78 // and every content slot as an atom in the semantic branch, with each atom's
79 // role added to its histogram. Returns the pattern's record index.
80 func (t *Tree) IngestPattern(domain uint8, ext ExtractResult) (uint32, error) {
81 if len(ext.Pattern) == 0 {
82 return lattice.NullRec, nil
83 }
84 patKey := PatternKey(domain, ext.Pattern)
85 patRI, err := t.DB.LookupRecIdx(lattice.Bgrammatical, patKey)
86 if err != nil {
87 return 0, err
88 }
89 if patRI != lattice.NullRec {
90 t.MetaInc(patRI)
91 } else {
92 var rec lattice.Record
93 lattice.SetFormOnRecord(&rec, string(ext.Pattern), &t.StringPool)
94 rec.Branch = uint8(lattice.Bgrammatical)
95 patRI, err = t.DB.InsertRec(lattice.Bgrammatical, patKey, rec)
96 if err != nil {
97 return 0, err
98 }
99 t.SetMeta(patRI, lattice.MetaRecord{Count: 1, StageTag: domain})
100 }
101 for i, word := range ext.Slots {
102 if word == "" {
103 continue
104 }
105 atomForm := word
106 if i < len(ext.Set) && ext.Set[i].Atom != "" {
107 atomForm = ext.Set[i].Atom
108 }
109 atomKey := AtomKey(domain, atomForm)
110 atomRI, err := t.DB.LookupRecIdx(lattice.Bsemantic, atomKey)
111 if err != nil {
112 return 0, err
113 }
114 if atomRI != lattice.NullRec {
115 t.MetaInc(atomRI)
116 if i < len(ext.Roles) {
117 if m := t.Meta(atomRI); m != nil {
118 var h RoleHist
119 h.Decode(m.Extra)
120 h.Inc(ext.Roles[i])
121 h.Encode(&m.Extra)
122 }
123 }
124 continue
125 }
126 var rec lattice.Record
127 lattice.SetFormOnRecord(&rec, atomForm, &t.StringPool)
128 rec.Branch = uint8(lattice.Bsemantic)
129 atomRI, err = t.DB.InsertRec(lattice.Bsemantic, atomKey, rec)
130 if err != nil {
131 return 0, err
132 }
133 m := lattice.MetaRecord{Count: 1, StageTag: domain}
134 if i < len(ext.Roles) {
135 var h RoleHist
136 h.Inc(ext.Roles[i])
137 h.Encode(&m.Extra)
138 }
139 t.SetMeta(atomRI, m)
140 }
141 return patRI, nil
142 }
143
144 // IngestCrossDomain links a source pattern to its translation's pattern.
145 func (t *Tree) IngestCrossDomain(srcDomain, dstDomain uint8, srcPat, dstPat []byte) error {
146 if len(srcPat) == 0 || len(dstPat) == 0 {
147 return nil
148 }
149 key := CrossPatternKey(srcDomain, dstDomain, srcPat, dstPat)
150 ri, err := t.DB.LookupRecIdx(lattice.Bcooccur, key)
151 if err != nil {
152 return err
153 }
154 if ri != lattice.NullRec {
155 t.MetaInc(ri)
156 return nil
157 }
158 var rec lattice.Record
159 lattice.SetFormOnRecord(&rec, string(srcPat)+"="+string(dstPat), &t.StringPool)
160 rec.Branch = uint8(lattice.Bcooccur)
161 ri, err = t.DB.InsertRec(lattice.Bcooccur, key, rec)
162 if err != nil {
163 return err
164 }
165 t.SetMeta(ri, lattice.MetaRecord{Count: 1, StageTag: srcDomain})
166 return nil
167 }
168
169 // IngestDeepPattern stores a canonical role sequence with stage tag 0.
170 func (t *Tree) IngestDeepPattern(deepPat []byte) error {
171 if len(deepPat) == 0 {
172 return nil
173 }
174 key := DeepPatternKey(deepPat)
175 ri, err := t.DB.LookupRecIdx(lattice.Bgrammatical, key)
176 if err != nil {
177 return err
178 }
179 if ri != lattice.NullRec {
180 t.MetaInc(ri)
181 return nil
182 }
183 var rec lattice.Record
184 lattice.SetFormOnRecord(&rec, string(deepPat), &t.StringPool)
185 rec.Branch = uint8(lattice.Bgrammatical)
186 ri, err = t.DB.InsertRec(lattice.Bgrammatical, key, rec)
187 if err != nil {
188 return err
189 }
190 t.SetMeta(ri, lattice.MetaRecord{Count: 1, StageTag: 0})
191 return nil
192 }
193
194 // IngestAtomLink stores one legacy bilateral atom link: src=dst in the
195 // pragmatic branch, with the register coordinates in Extra[2] and Extra[3].
196 func (t *Tree) IngestAtomLink(srcDomain, dstDomain uint8, srcAtom, dstAtom string, rArch, rDisc uint8) error {
197 key := LookupKey(srcDomain, dstDomain, srcAtom, dstAtom, rArch, rDisc)
198 ri, err := t.DB.LookupRecIdx(lattice.Bpragmatic, key)
199 if err != nil {
200 return err
201 }
202 if ri != lattice.NullRec {
203 t.MetaInc(ri)
204 return nil
205 }
206 var rec lattice.Record
207 lattice.SetFormOnRecord(&rec, LinkForm(srcAtom, dstAtom), &t.StringPool)
208 rec.Branch = uint8(lattice.Bpragmatic)
209 ri, err = t.DB.InsertRec(lattice.Bpragmatic, key, rec)
210 if err != nil {
211 return err
212 }
213 m := lattice.MetaRecord{Count: 1, StageTag: srcDomain}
214
215 m.Extra[2] = rArch
216 m.Extra[3] = rDisc
217 t.SetMeta(ri, m)
218 return nil
219 }
220