inflect.mx raw

   1  package iskra
   2  
   3  import "git.smesh.lol/iskradb/lattice"
   4  
   5  // InflectFunc computes a surface form for a given dict form, class code, and morph state.
   6  // Each domain registers its own implementation.
   7  type InflectFunc func(dictForm string, classCode uint8, state uint8) string
   8  
   9  var inflectFuncs = map[uint8]InflectFunc{}
  10  
  11  // RegisterInflectFunc registers the inflection function for a domain.
  12  func RegisterInflectFunc(domain uint8, fn InflectFunc) {
  13  	inflectFuncs[domain] = fn
  14  }
  15  
  16  // InflectFromTree looks up the stored class code for dictForm in Bcooccur,
  17  // then calls the registered InflectFunc for domain.
  18  // Returns "" if no class registered or the inflect function produces no form.
  19  func InflectFromTree(tree *lattice.Tree, domain uint8, dictForm string, state uint8) string {
  20  	fn, ok := inflectFuncs[domain]
  21  	if !ok || fn == nil {
  22  		return ""
  23  	}
  24  	key := MakeKey(domain, CoordVerbClass, dictForm)
  25  	ri := tree.LookupRecIdx(lattice.Bcooccur, key)
  26  	if ri == lattice.NullRec {
  27  		return ""
  28  	}
  29  	rec := tree.GetRecord(ri)
  30  	if rec == nil || rec.Branch == 0 {
  31  		return ""
  32  	}
  33  	return fn(dictForm, rec.Branch, state)
  34  }
  35  
  36  // RegisterClassRecord stores a class code in Bcooccur at MakeKey(domain, CoordVerbClass, dictForm).
  37  func RegisterClassRecord(tree *lattice.Tree, pool *[]byte, domain uint8, dictForm string, classCode uint8) {
  38  	if classCode == 0 || dictForm == "" {
  39  		return
  40  	}
  41  	key := MakeKey(domain, CoordVerbClass, dictForm)
  42  	ri := tree.LookupRecIdx(lattice.Bcooccur, key)
  43  	if ri != lattice.NullRec {
  44  		if r := tree.GetRecord(ri); r != nil {
  45  			r.Branch = classCode
  46  		}
  47  		return
  48  	}
  49  	var rec lattice.Record
  50  	rec.Branch = classCode
  51  	tree.InsertRec(lattice.Bcooccur, key, rec)
  52  }
  53  
  54  // GetClassCode retrieves the stored class code for a dict form.
  55  // Returns (0, false) if not registered.
  56  func GetClassCode(tree *lattice.Tree, domain uint8, dictForm string) (uint8, bool) {
  57  	key := MakeKey(domain, CoordVerbClass, dictForm)
  58  	ri := tree.LookupRecIdx(lattice.Bcooccur, key)
  59  	if ri == lattice.NullRec {
  60  		return 0, false
  61  	}
  62  	rec := tree.GetRecord(ri)
  63  	if rec == nil || rec.Branch == 0 {
  64  		return 0, false
  65  	}
  66  	return rec.Branch, true
  67  }
  68