set.go raw

   1  package iskra
   2  
   3  // SetEntry is one element of the sentence-as-set abstraction: Role + Atom +
   4  // Morph + Class + Mark + OblRole + Head + ModKind. extract.mx SetEntry.
   5  type SetEntry struct {
   6  	Role    int32
   7  	Atom    string
   8  	Morph   uint16
   9  	Class   uint8
  10  	Mark    uint8
  11  	OblRole uint8
  12  	Head    int16
  13  	ModKind uint8
  14  }
  15  
  16  // Clause is one predication in a Discourse.
  17  type Clause struct {
  18  	Set      []SetEntry
  19  	Relation uint8
  20  	Parent   int16
  21  	HostIdx  int16
  22  }
  23  
  24  // ExtractResult is one extraction: pattern, slots, roles and the clause set.
  25  type ExtractResult struct {
  26  	Pattern   []byte
  27  	Slots     []string
  28  	Roles     []int32
  29  	DeepPat   []uint8
  30  	Set       []SetEntry
  31  	Discourse []Clause
  32  }
  33  
  34  // UntranslatedMarker stands in for an atom with no cross-language link.
  35  const UntranslatedMarker = "[missing]"
  36  
  37  // SetsEqualCross compares two sets on the language-independent fields.
  38  func SetsEqualCross(a, b []SetEntry) bool {
  39  	if len(a) != len(b) {
  40  		return false
  41  	}
  42  	for i := range a {
  43  		if a[i].Role != b[i].Role || a[i].Atom != b[i].Atom || a[i].Morph != b[i].Morph ||
  44  			a[i].OblRole != b[i].OblRole || a[i].Head != b[i].Head || a[i].ModKind != b[i].ModKind {
  45  			return false
  46  		}
  47  	}
  48  	return true
  49  }
  50  
  51  // DiscoursesEqual compares two clause lists.
  52  func DiscoursesEqual(a, b []Clause) bool {
  53  	if len(a) != len(b) {
  54  		return false
  55  	}
  56  	for i := range a {
  57  		if a[i].Relation != b[i].Relation || a[i].Parent != b[i].Parent || a[i].HostIdx != b[i].HostIdx {
  58  			return false
  59  		}
  60  		if !SetsEqualCross(a[i].Set, b[i].Set) {
  61  			return false
  62  		}
  63  	}
  64  	return true
  65  }
  66  
  67  // FlatSetFromDiscourse concatenates every clause's entries.
  68  func FlatSetFromDiscourse(d []Clause) []SetEntry {
  69  	var out []SetEntry
  70  	for _, c := range d {
  71  		out = append(out, c.Set...)
  72  	}
  73  	return out
  74  }
  75