key.mx raw
1 package transdb
2
3 import (
4 "git.smesh.lol/iskra"
5 "git.smesh.lol/iskradb/lattice"
6 )
7
8 // Language domain codes.
9 const (
10 LangEN uint8 = 0x01
11 LangJA uint8 = 0x02
12 LangUnknown uint8 = 0x00
13 )
14
15 // Coord layout constants — re-exported from iskra for package-internal use.
16 const (
17 CoordSemanticShift = iskra.CoordSemanticShift
18 CoordGrammaticalShift = iskra.CoordGrammaticalShift
19 CoordCooccurShift = iskra.CoordCooccurShift
20 CoordMorphShift = iskra.CoordMorphShift
21 CoordPragmaticShift = iskra.CoordPragmaticShift
22 CoordValencyShift = iskra.CoordValencyShift
23 CoordRegisterShift = iskra.CoordRegisterShift
24 )
25
26 // Semantic bitfield constants — re-exported from iskra.
27 const (
28 SemanticHumanSubj = iskra.SemanticHumanSubj
29 SemanticHumanObj = iskra.SemanticHumanObj
30 SemanticAnimSubj = iskra.SemanticAnimSubj
31 SemanticAnimObj = iskra.SemanticAnimObj
32 SemanticAbstSubj = iskra.SemanticAbstSubj
33 SemanticAbstObj = iskra.SemanticAbstObj
34 SemanticPlaceSubj = iskra.SemanticPlaceSubj
35 SemanticPlaceObj = iskra.SemanticPlaceObj
36 SemanticArtiSubj = iskra.SemanticArtiSubj
37 SemanticArtiObj = iskra.SemanticArtiObj
38 SemanticNatSubj = iskra.SemanticNatSubj
39 SemanticNatObj = iskra.SemanticNatObj
40 SemanticEventSubj = iskra.SemanticEventSubj
41 SemanticEventObj = iskra.SemanticEventObj
42 SemanticCollSubj = iskra.SemanticCollSubj
43 SemanticCollObj = iskra.SemanticCollObj
44 )
45
46 // Co-occurrence type constants — re-exported from iskra.
47 const (
48 CooccurNone = iskra.CooccurNone
49 CooccurNominal = iskra.CooccurNominal
50 CooccurVerbal = iskra.CooccurVerbal
51 CooccurFunction = iskra.CooccurFunction
52 )
53
54 // Coord functions delegated to iskra.
55 func PackCoord(semantic, grammatical, cooccur, morph, pragmatic, valency, register uint64) uint64 {
56 return iskra.PackCoord(semantic, grammatical, cooccur, morph, pragmatic, valency, register)
57 }
58
59 func RelaxCoord(coord uint64) []uint64 { return iskra.RelaxCoord(coord) }
60
61 func CoordSemantic(coord uint64) uint64 { return iskra.CoordSemantic(coord) }
62
63 func CoordMorph(coord uint64) uint8 { return iskra.CoordMorph(coord) }
64
65 func CoordCooccur(prevType, nextType uint8) uint64 { return iskra.CoordCooccur(prevType, nextType) }
66
67 func CoordPrevType(coord uint64) uint8 { return iskra.CoordPrevType(coord) }
68
69 func CoordNextType(coord uint64) uint8 { return iskra.CoordNextType(coord) }
70
71 // MakeKey returns the 128-bit SipHash key for (lang, coord, word).
72 // Delegates to iskra.MakeKey with lang as the domain byte.
73 func MakeKey(lang uint8, coord uint64, word string) lattice.Key {
74 return iskra.MakeKey(lang, coord, word)
75 }
76
77 // ActiveBranches are the 3 branches used for JA/EN lattice data.
78 var ActiveBranches [3]lattice.Branch
79
80 var _keyInitDone bool
81
82 // KeyInitOnce initializes ActiveBranches. Safe to call multiple times.
83 func KeyInitOnce() {
84 if _keyInitDone {
85 return
86 }
87 _keyInitDone = true
88 ActiveBranches = [3]lattice.Branch{lattice.Bnoun, lattice.Bverb, lattice.Bmodifier}
89 }
90
91 // POSForWord returns the POS type of a word at coord=0 (JMdict baseline).
92 // Returns 1=nominal, 2=verbal, 3=modifier, 0=unknown.
93 func POSForWord(tree *lattice.Tree, lang uint8, word string) uint8 {
94 KeyInitOnce()
95 key := MakeKey(lang, 0, word)
96 for i, b := range ActiveBranches {
97 if tree.LookupRecIdx(b, key) != lattice.NullRec {
98 return uint8(i + 1)
99 }
100 }
101 return 0
102 }
103
104 // POSTypeFor maps POSForWord result to CooccurNominal/Verbal/Function.
105 func POSTypeFor(posResult uint8) uint8 {
106 switch posResult {
107 case 1:
108 return CooccurNominal
109 case 2:
110 return CooccurVerbal
111 case 3:
112 return CooccurFunction
113 }
114 return CooccurNone
115 }
116
117 // branchOrderJA returns branch search order from coord's cooccurrence axis.
118 func branchOrderJA(coord uint64) [3]uint8 {
119 bMod := uint8(lattice.Bmodifier)
120 bNou := uint8(lattice.Bnoun)
121 bVer := uint8(lattice.Bverb)
122 prevType := CoordPrevType(coord)
123 nextType := CoordNextType(coord)
124 switch {
125 case prevType == CooccurNominal:
126 return [3]uint8{bMod, bNou, bVer}
127 case prevType == CooccurVerbal:
128 return [3]uint8{bMod, bVer, bNou}
129 case nextType == CooccurVerbal:
130 return [3]uint8{bNou, bMod, bVer}
131 default:
132 return [3]uint8{bNou, bVer, bMod}
133 }
134 }
135
136 // PackCtx — legacy wrapper kept for callers during migration.
137 func PackCtx(prev, cur, next uint8) uint8 {
138 return (prev << 4) | (cur << 2) | next
139 }
140
141 // CtxToCoord converts old 6-bit ctx to new coord cooccurrence field.
142 func CtxToCoord(ctx uint8) uint64 {
143 prevPOS := (ctx >> 4) & 3
144 nextPOS := ctx & 3
145 return CoordCooccur(prevPOS, nextPOS) << CoordCooccurShift
146 }
147
148 func KeyLang(key lattice.Key) uint8 { return LangUnknown }
149
150 func LangName(lang uint8) string {
151 switch lang {
152 case LangEN:
153 return "EN"
154 case LangJA:
155 return "JA"
156 }
157 return "?"
158 }
159