package iskra import "git.smesh.lol/iskradb/lattice" // InflectFunc computes a surface form for a given dict form, class code, and morph state. // Each domain registers its own implementation. type InflectFunc func(dictForm string, classCode uint8, state uint8) string var inflectFuncs = map[uint8]InflectFunc{} // RegisterInflectFunc registers the inflection function for a domain. func RegisterInflectFunc(domain uint8, fn InflectFunc) { inflectFuncs[domain] = fn } // InflectFromTree looks up the stored class code for dictForm in Bcooccur, // then calls the registered InflectFunc for domain. // Returns "" if no class registered or the inflect function produces no form. func InflectFromTree(tree *lattice.Tree, domain uint8, dictForm string, state uint8) string { fn, ok := inflectFuncs[domain] if !ok || fn == nil { return "" } key := MakeKey(domain, CoordVerbClass, dictForm) ri := tree.LookupRecIdx(lattice.Bcooccur, key) if ri == lattice.NullRec { return "" } rec := tree.GetRecord(ri) if rec == nil || rec.Branch == 0 { return "" } return fn(dictForm, rec.Branch, state) } // RegisterClassRecord stores a class code in Bcooccur at MakeKey(domain, CoordVerbClass, dictForm). func RegisterClassRecord(tree *lattice.Tree, pool *[]byte, domain uint8, dictForm string, classCode uint8) { if classCode == 0 || dictForm == "" { return } key := MakeKey(domain, CoordVerbClass, dictForm) ri := tree.LookupRecIdx(lattice.Bcooccur, key) if ri != lattice.NullRec { if r := tree.GetRecord(ri); r != nil { r.Branch = classCode } return } var rec lattice.Record rec.Branch = classCode tree.InsertRec(lattice.Bcooccur, key, rec) } // GetClassCode retrieves the stored class code for a dict form. // Returns (0, false) if not registered. func GetClassCode(tree *lattice.Tree, domain uint8, dictForm string) (uint8, bool) { key := MakeKey(domain, CoordVerbClass, dictForm) ri := tree.LookupRecIdx(lattice.Bcooccur, key) if ri == lattice.NullRec { return 0, false } rec := tree.GetRecord(ri) if rec == nil || rec.Branch == 0 { return 0, false } return rec.Branch, true }