package iskra import "git.smesh.lol/go-iskra/lattice" // 64-bit coordinate layout, coord.mx: // // bits 63-48 semantic (16 bits): 8 subject|object category pairs, 2 bits each // bits 47-32 reserved // bits 31-29 grammatical (3 bits) // bits 28-25 cooccur (4 bits): prev_type(2) + next_type(2) // bits 24-20 morphstate (5 bits) // bits 19-18 pragmatic (2 bits) // bits 17-16 valency (2 bits) // bits 15-2 reserved // bits 1-0 register (2 bits) // // coord 0 is the base key. const ( CoordSemanticShift = 48 CoordGrammaticalShift = 29 CoordCooccurShift = 25 CoordMorphShift = 20 CoordPragmaticShift = 18 CoordValencyShift = 16 CoordRegisterShift = 0 ) // Semantic bitfield: 2 bits per ontological category (subject | object flag). const ( SemanticHumanSubj uint64 = 1 << 0 SemanticHumanObj uint64 = 1 << 1 SemanticAnimSubj uint64 = 1 << 2 SemanticAnimObj uint64 = 1 << 3 SemanticAbstSubj uint64 = 1 << 4 SemanticAbstObj uint64 = 1 << 5 SemanticPlaceSubj uint64 = 1 << 6 SemanticPlaceObj uint64 = 1 << 7 SemanticArtiSubj uint64 = 1 << 8 SemanticArtiObj uint64 = 1 << 9 SemanticNatSubj uint64 = 1 << 10 SemanticNatObj uint64 = 1 << 11 SemanticEventSubj uint64 = 1 << 12 SemanticEventObj uint64 = 1 << 13 SemanticCollSubj uint64 = 1 << 14 SemanticCollObj uint64 = 1 << 15 ) // Co-occurrence word types. const ( CooccurNone uint8 = 0 CooccurNominal uint8 = 1 CooccurVerbal uint8 = 2 CooccurFunction uint8 = 3 ) // PackCoord assembles a coordinate from its axes. func PackCoord(semantic, grammatical, cooccur, morph, pragmatic, valency, register uint64) uint64 { return ((semantic & 0xFFFF) << CoordSemanticShift) | ((grammatical & 0x7) << CoordGrammaticalShift) | ((cooccur & 0xF) << CoordCooccurShift) | ((morph & 0x1F) << CoordMorphShift) | ((pragmatic & 0x3) << CoordPragmaticShift) | ((valency & 0x3) << CoordValencyShift) | (register & 0x3) } // CoordSemantic extracts the 16-bit semantic bitfield. func CoordSemantic(coord uint64) uint64 { return (coord >> CoordSemanticShift) & 0xFFFF } // CoordGrammatical extracts the 3-bit grammatical role. func CoordGrammatical(coord uint64) uint64 { return (coord >> CoordGrammaticalShift) & 0x7 } // CoordMorph extracts the 5-bit morphological state. func CoordMorph(coord uint64) uint8 { return uint8((coord >> CoordMorphShift) & 0x1F) } // CoordPragmatic extracts the 2-bit pragmatic axis. func CoordPragmatic(coord uint64) uint8 { return uint8((coord >> CoordPragmaticShift) & 0x3) } // CoordValency extracts the 2-bit valency axis. func CoordValency(coord uint64) uint8 { return uint8((coord >> CoordValencyShift) & 0x3) } // CoordRegister extracts the 2-bit register axis. func CoordRegister(coord uint64) uint8 { return uint8(coord & 0x3) } // CoordCooccur packs (prevType, nextType) into the 4-bit cooccurrence field. func CoordCooccur(prevType, nextType uint8) uint64 { return uint64(prevType&3) | (uint64(nextType&3) << 2) } // CoordPrevType extracts the previous word type. func CoordPrevType(coord uint64) uint8 { return uint8((coord >> CoordCooccurShift) & 3) } // CoordNextType extracts the next word type. func CoordNextType(coord uint64) uint8 { return uint8((coord >> (CoordCooccurShift + 2)) & 3) } // RelaxCoord returns the coordinates to try in fallback order, specific to // general: pragmatic, register, valency, semantic bits high to low, // grammatical, cooccurrent, morphstate. func RelaxCoord(coord uint64) []uint64 { if coord == 0 { return []uint64{0} } seq := []uint64{coord} add := func(c uint64) { if c != seq[len(seq)-1] { seq = append(seq, c) } } c := coord c &^= uint64(0x3) << CoordPragmaticShift add(c) c &^= 0x3 add(c) c &^= uint64(0x3) << CoordValencyShift add(c) sem := (c >> CoordSemanticShift) & 0xFFFF for bit := 15; bit >= 0; bit-- { if (sem>>uint(bit))&1 == 1 { sem &^= 1 << uint(bit) c = (c &^ (uint64(0xFFFF) << CoordSemanticShift)) | (sem << CoordSemanticShift) add(c) } } c &^= uint64(0x7) << CoordGrammaticalShift add(c) c &^= uint64(0xF) << CoordCooccurShift add(c) c &^= uint64(0x1F) << CoordMorphShift add(c) return seq } // WordKey is the key for (domain, coord, word): SipHash-2-4-128 of // domain(1) | coord LE(8) | word, low 64 bits, under moxie's default key. This // is coord.mx MakeKey, which shadows lattice's two-argument MakeKey. func WordKey(domain uint8, coord uint64, word string) lattice.Key { buf := make([]byte, 9+len(word)) buf[0] = domain for i := 0; i < 8; i++ { buf[1+i] = byte(coord >> (8 * uint(i))) } copy(buf[9:], word) return lattice.HashKey(buf) }