lattice.go raw

   1  // Package lattice is the storage engine behind .iskr files: a C=8 B-tree per
   2  // branch, 136-byte nodes, 48-byte records, format version 5 (64-bit keys).
   3  //
   4  // Everything in this package is a port of iskradb/lattice (*.mx). Where the
   5  // moxie source never wrote the data a structure needs, the structure is left
   6  // out with a comment naming the file that is missing, rather than stubbed.
   7  package lattice
   8  
   9  // C is the coordination number: 8 keys and 9 children per node.
  10  const C = 8
  11  
  12  // Branch selects one of the eight per-branch B-trees held by a Tree.
  13  type Branch uint8
  14  
  15  const (
  16  	Bsemantic    Branch = 0 // ontological category (animacy, abstractness)
  17  	Bgrammatical Branch = 1 // syntactic role (nominal/verbal/function)
  18  	Bcooccur     Branch = 2 // PVN co-occurrence context
  19  	Bmorphology  Branch = 3 // morphological state (tense/polarity/aspect)
  20  	Bpragmatic   Branch = 4 // domain/register context
  21  	Bphonology   Branch = 5 // phonological (dormant: all coordinates 0)
  22  	Bvalency     Branch = 6 // argument count
  23  	Bregister    Branch = 7 // social register
  24  )
  25  
  26  // Key is a 64-bit key. iskradb calls it SipHash-2-4, but the word keys the
  27  // iskra ingest wrote were made by iskra/hash.mx HashBytes, which is FNV-1a
  28  // truncated to 56 bits; see HashWord.
  29  type Key uint64
  30  
  31  // KeyZero is the zero key, used as the null sentinel in node arrays.
  32  const KeyZero = Key(0)
  33  
  34  // NullIndex and NullRec are the 0xFFFFFFFF sentinels for empty child pointers
  35  // and empty record pointers.
  36  const (
  37  	NullIndex uint32 = 0xFFFFFFFF
  38  	NullRec   uint32 = 0xFFFFFFFF
  39  )
  40  
  41  // Branch aliases kept from iskradb's C=3 era, which iskra/record.mx still uses.
  42  const (
  43  	Bnoun     = Bgrammatical
  44  	Bverb     = Bmorphology
  45  	Bmodifier = Bpragmatic
  46  )
  47  
  48  // MakeKey is the go-ref two-argument key builder: language byte in the top
  49  // eight bits, 56-bit hash below. It differs from MakeCodeKey (stage byte, same
  50  // layout) and from iskra/stage.mx MakeCodeKey (hash, stage argument order).
  51  func MakeKey(lang uint8, hash uint64) Key {
  52  	return Key(uint64(lang)<<56 | hash&0x00FFFFFFFFFFFFFF)
  53  }
  54  
  55  // KeyLang extracts the language byte from a key.
  56  func KeyLang(key Key) uint8 { return uint8(key >> 56) }
  57  
  58  // Language tags for the go-ref load path. iskra does not use them: its keys put
  59  // the stage byte in the same high eight bits, see MakeCodeKey.
  60  const (
  61  	LangBG uint8 = 0x01
  62  	LangEN uint8 = 0x02
  63  	LangHR uint8 = 0x03
  64  	LangSR uint8 = 0x04
  65  	LangMK uint8 = 0x05
  66  	LangRU uint8 = 0x06
  67  )
  68  
  69  // Corpus source tags, go-ref's provenance field on MetaEntry. Extra[6] in the
  70  // go-ref layout. iskra never writes one: ja-test.meta has a zero in every
  71  // entry's Extra[6].
  72  const (
  73  	SourceBase    uint8 = 0
  74  	SourceBible   uint8 = 1
  75  	SourceWikdict uint8 = 2
  76  	SourceModern  uint8 = 3
  77  	SourceFormal  uint8 = 4
  78  )
  79  
  80  // SetSourceTag writes the corpus provenance. go-ref layout only.
  81  func (m *MetaEntry) SetSourceTag(src uint8) { m.Extra[6] = src }
  82  
  83  // GetSourceTag reads the corpus provenance. go-ref layout only.
  84  func (m *MetaEntry) GetSourceTag() uint8 { return m.Extra[6] }
  85  
  86  // SetPredictedBranch stores the predicted next branch and confidence in
  87  // Extra[5], the go-ref layout. In iskra's layout Extra[5] is the low byte of
  88  // the signature hash, so a tree carrying both in one MetaEntry is corrupt.
  89  func (m *MetaEntry) SetPredictedBranch(branch, confidence uint8) {
  90  	if confidence > 3 {
  91  		confidence = 3
  92  	}
  93  	m.Extra[5] = (branch & 0x3) | ((confidence & 0x3) << 2)
  94  }
  95  
  96  // GetPredictedBranch reads the prediction stored by SetPredictedBranch.
  97  func (m *MetaEntry) GetPredictedBranch() (branch, confidence uint8) {
  98  	return m.Extra[5] & 0x3, (m.Extra[5] >> 2) & 0x3
  99  }
 100  
 101  // POSToTritPath maps a POS bitfield to a trit path, go-ref's rule: the highest
 102  // priority branch wins, noun over verb over modifier.
 103  func POSToTritPath(pos POS) TritPath {
 104  	switch {
 105  	case pos.Has(POSProperNoun):
 106  		return MakeTritPath(BranchType, 2)
 107  	case pos.Has(POSPronoun):
 108  		return MakeTritPath(BranchType, 3)
 109  	case pos.Has(POSNumeral):
 110  		return MakeTritPath(BranchType, SubField, 2)
 111  	case pos.Has(POSNoun):
 112  		return MakeTritPath(BranchType, SubField)
 113  	case pos.Has(POSAuxiliary):
 114  		return MakeTritPath(BranchFunc, 2)
 115  	case pos.Has(POSVerb):
 116  		return MakeTritPath(BranchFunc, SubField)
 117  	case pos.Has(POSAdjective):
 118  		return MakeTritPath(BranchData, SubField)
 119  	case pos.Has(POSAdverb):
 120  		return MakeTritPath(BranchData, 2)
 121  	case pos.Has(POSPreposition):
 122  		return MakeTritPath(BranchData, 3, SubField)
 123  	case pos.Has(POSConjunction):
 124  		return MakeTritPath(BranchData, 3, 2)
 125  	case pos.Has(POSParticle):
 126  		return MakeTritPath(BranchData, 3, 3)
 127  	case pos.Has(POSInterjection):
 128  		return MakeTritPath(BranchData, 2, 2)
 129  	case pos.Has(POSPrefix):
 130  		return MakeTritPath(BranchData, SubField, 3)
 131  	case pos.Has(POSSuffix):
 132  		return MakeTritPath(BranchData, 2, 3)
 133  	default:
 134  		return MakeTritPath()
 135  	}
 136  }
 137