coord.go raw
1 package iskra
2
3 import "git.smesh.lol/go-iskra/lattice"
4
5 // 64-bit coordinate layout, coord.mx:
6 //
7 // bits 63-48 semantic (16 bits): 8 subject|object category pairs, 2 bits each
8 // bits 47-32 reserved
9 // bits 31-29 grammatical (3 bits)
10 // bits 28-25 cooccur (4 bits): prev_type(2) + next_type(2)
11 // bits 24-20 morphstate (5 bits)
12 // bits 19-18 pragmatic (2 bits)
13 // bits 17-16 valency (2 bits)
14 // bits 15-2 reserved
15 // bits 1-0 register (2 bits)
16 //
17 // coord 0 is the base key.
18 const (
19 CoordSemanticShift = 48
20 CoordGrammaticalShift = 29
21 CoordCooccurShift = 25
22 CoordMorphShift = 20
23 CoordPragmaticShift = 18
24 CoordValencyShift = 16
25 CoordRegisterShift = 0
26 )
27
28 // Semantic bitfield: 2 bits per ontological category (subject | object flag).
29 const (
30 SemanticHumanSubj uint64 = 1 << 0
31 SemanticHumanObj uint64 = 1 << 1
32 SemanticAnimSubj uint64 = 1 << 2
33 SemanticAnimObj uint64 = 1 << 3
34 SemanticAbstSubj uint64 = 1 << 4
35 SemanticAbstObj uint64 = 1 << 5
36 SemanticPlaceSubj uint64 = 1 << 6
37 SemanticPlaceObj uint64 = 1 << 7
38 SemanticArtiSubj uint64 = 1 << 8
39 SemanticArtiObj uint64 = 1 << 9
40 SemanticNatSubj uint64 = 1 << 10
41 SemanticNatObj uint64 = 1 << 11
42 SemanticEventSubj uint64 = 1 << 12
43 SemanticEventObj uint64 = 1 << 13
44 SemanticCollSubj uint64 = 1 << 14
45 SemanticCollObj uint64 = 1 << 15
46 )
47
48 // Co-occurrence word types.
49 const (
50 CooccurNone uint8 = 0
51 CooccurNominal uint8 = 1
52 CooccurVerbal uint8 = 2
53 CooccurFunction uint8 = 3
54 )
55
56 // PackCoord assembles a coordinate from its axes.
57 func PackCoord(semantic, grammatical, cooccur, morph, pragmatic, valency, register uint64) uint64 {
58 return ((semantic & 0xFFFF) << CoordSemanticShift) |
59 ((grammatical & 0x7) << CoordGrammaticalShift) |
60 ((cooccur & 0xF) << CoordCooccurShift) |
61 ((morph & 0x1F) << CoordMorphShift) |
62 ((pragmatic & 0x3) << CoordPragmaticShift) |
63 ((valency & 0x3) << CoordValencyShift) |
64 (register & 0x3)
65 }
66
67 // CoordSemantic extracts the 16-bit semantic bitfield.
68 func CoordSemantic(coord uint64) uint64 { return (coord >> CoordSemanticShift) & 0xFFFF }
69
70 // CoordGrammatical extracts the 3-bit grammatical role.
71 func CoordGrammatical(coord uint64) uint64 {
72 return (coord >> CoordGrammaticalShift) & 0x7
73 }
74
75 // CoordMorph extracts the 5-bit morphological state.
76 func CoordMorph(coord uint64) uint8 { return uint8((coord >> CoordMorphShift) & 0x1F) }
77
78 // CoordPragmatic extracts the 2-bit pragmatic axis.
79 func CoordPragmatic(coord uint64) uint8 { return uint8((coord >> CoordPragmaticShift) & 0x3) }
80
81 // CoordValency extracts the 2-bit valency axis.
82 func CoordValency(coord uint64) uint8 { return uint8((coord >> CoordValencyShift) & 0x3) }
83
84 // CoordRegister extracts the 2-bit register axis.
85 func CoordRegister(coord uint64) uint8 { return uint8(coord & 0x3) }
86
87 // CoordCooccur packs (prevType, nextType) into the 4-bit cooccurrence field.
88 func CoordCooccur(prevType, nextType uint8) uint64 {
89 return uint64(prevType&3) | (uint64(nextType&3) << 2)
90 }
91
92 // CoordPrevType extracts the previous word type.
93 func CoordPrevType(coord uint64) uint8 { return uint8((coord >> CoordCooccurShift) & 3) }
94
95 // CoordNextType extracts the next word type.
96 func CoordNextType(coord uint64) uint8 { return uint8((coord >> (CoordCooccurShift + 2)) & 3) }
97
98 // RelaxCoord returns the coordinates to try in fallback order, specific to
99 // general: pragmatic, register, valency, semantic bits high to low,
100 // grammatical, cooccurrent, morphstate.
101 func RelaxCoord(coord uint64) []uint64 {
102 if coord == 0 {
103 return []uint64{0}
104 }
105 seq := []uint64{coord}
106 add := func(c uint64) {
107 if c != seq[len(seq)-1] {
108 seq = append(seq, c)
109 }
110 }
111 c := coord
112 c &^= uint64(0x3) << CoordPragmaticShift
113 add(c)
114 c &^= 0x3
115 add(c)
116 c &^= uint64(0x3) << CoordValencyShift
117 add(c)
118 sem := (c >> CoordSemanticShift) & 0xFFFF
119 for bit := 15; bit >= 0; bit-- {
120 if (sem>>uint(bit))&1 == 1 {
121 sem &^= 1 << uint(bit)
122 c = (c &^ (uint64(0xFFFF) << CoordSemanticShift)) | (sem << CoordSemanticShift)
123 add(c)
124 }
125 }
126 c &^= uint64(0x7) << CoordGrammaticalShift
127 add(c)
128 c &^= uint64(0xF) << CoordCooccurShift
129 add(c)
130 c &^= uint64(0x1F) << CoordMorphShift
131 add(c)
132 return seq
133 }
134
135 // WordKey is the key for (domain, coord, word): SipHash-2-4-128 of
136 // domain(1) | coord LE(8) | word, low 64 bits, under moxie's default key. This
137 // is coord.mx MakeKey, which shadows lattice's two-argument MakeKey.
138 func WordKey(domain uint8, coord uint64, word string) lattice.Key {
139 buf := make([]byte, 9+len(word))
140 buf[0] = domain
141 for i := 0; i < 8; i++ {
142 buf[1+i] = byte(coord >> (8 * uint(i)))
143 }
144 copy(buf[9:], word)
145 return lattice.HashKey(buf)
146 }
147