jingest.go raw

   1  package iskra
   2  
   3  import (
   4  	"strings"
   5  )
   6  
   7  // langJA and langEN are the domains the JA-EN pair ingest and the round trip
   8  // use. cmd/iskra/ja.mx: LangEN 0x01, LangJA 0x02.
   9  const (
  10  	LangEN uint8 = 0x01
  11  	LangJA uint8 = 0x02
  12  	LangKO uint8 = 0x03
  13  	LangZH uint8 = 0x04
  14  )
  15  
  16  // PairStats counts what an ingest run put in the lattice.
  17  type PairStats struct {
  18  	Pairs          int
  19  	Atoms          int
  20  	Patterns       int
  21  	Links          int
  22  	DeepStaffs     int
  23  	Rejected       int
  24  	Quality        QualitySummary
  25  	RejectedByGate []string // the pairs the gate dropped, JA side
  26  }
  27  
  28  // IngestPair stores one JA/EN pair: both extractions' patterns and atoms, the
  29  // cross-domain pattern link, the deep pattern, and a role-matched atom link for
  30  // every pair of slots whose roles are equivalent.
  31  //
  32  // cmd/iskra/ja.mx ingestPair with its quality gate. The versioned contexted
  33  // atom link (GenContexted, the AtomLinkKey form with per-slot contexts) is not
  34  // stored: the legacy bilateral link is what makes roundtrip_cross's lookups
  35  // resolve, and the contexted generation is a scoring refinement over it.
  36  func (t *Tree) IngestPair(ja, en string, rArch, rDisc uint8, stats *PairStats) error {
  37  	stats.Pairs++
  38  	jaToks := TokenizeJA(ja)
  39  	enToks := TokenizeEN(en)
  40  	jaExt := ExtractJA(jaToks)
  41  	enExt := ExtractEN(enToks)
  42  	// The gate scores the pair and drops it before anything is stored. It is
  43  	// cmd/iskra/quality.mx passesGate, at QualityThreshold and the 8-token
  44  	// floor; a pair under the floor passes unscored.
  45  	q := ScorePair(ja, en, jaToks, enToks, jaExt, enExt)
  46  	passed := PassesGate(q, jaToks, enToks)
  47  	stats.Quality.RecordQuality(q, jaToks, enToks, passed)
  48  	if !passed {
  49  		stats.Rejected++
  50  		stats.RejectedByGate = append(stats.RejectedByGate, ja)
  51  		return nil
  52  	}
  53  	if len(jaExt.Pattern) > 0 {
  54  		if _, err := t.IngestPattern(LangJA, jaExt); err != nil {
  55  			return err
  56  		}
  57  		stats.Atoms += len(jaExt.Slots)
  58  		stats.Patterns++
  59  	}
  60  	if len(enExt.Pattern) > 0 {
  61  		if _, err := t.IngestPattern(LangEN, enExt); err != nil {
  62  			return err
  63  		}
  64  		stats.Atoms += len(enExt.Slots)
  65  		stats.Patterns++
  66  	}
  67  	if len(jaExt.Pattern) > 0 && len(enExt.Pattern) > 0 {
  68  		if err := t.IngestCrossDomain(LangJA, LangEN, jaExt.Pattern, enExt.Pattern); err != nil {
  69  			return err
  70  		}
  71  		stats.Patterns++
  72  	}
  73  	if len(jaExt.DeepPat) > 0 && len(enExt.DeepPat) > 0 {
  74  		if err := t.IngestDeepPattern(jaExt.DeepPat); err != nil {
  75  			return err
  76  		}
  77  		stats.DeepStaffs++
  78  	}
  79  	// Role-based alignment of slot fillers, first unused equivalent match wins.
  80  	// Each aligned pair is stored under its own (src, dst) key, so an EN
  81  	// lookup for the EN atom finds an edge keyed with the EN atom first. The
  82  	// JA record is inserted first and the EN one mirrors it, which is how
  83  	// BuildAtomIdx sees both languages without a language field on disk.
  84  	enUsed := make([]bool, len(enExt.Slots))
  85  	for i, jaWord := range jaExt.Slots {
  86  		jaRole := int32(HistComplement)
  87  		if i < len(jaExt.Roles) {
  88  			jaRole = jaExt.Roles[i]
  89  		}
  90  		for j, enWord := range enExt.Slots {
  91  			if enUsed[j] {
  92  				continue
  93  			}
  94  			enRole := int32(HistComplement)
  95  			if j < len(enExt.Roles) {
  96  				enRole = enExt.Roles[j]
  97  			}
  98  			if !RoleEquiv(jaRole, enRole) {
  99  				continue
 100  			}
 101  			jaAtom, enAtom := jaWord, enWord
 102  			if i < len(jaExt.Set) && jaExt.Set[i].Atom != "" {
 103  				jaAtom = jaExt.Set[i].Atom
 104  			}
 105  			if j < len(enExt.Set) && enExt.Set[j].Atom != "" {
 106  				enAtom = enExt.Set[j].Atom
 107  			}
 108  			// Both directions, the way the moxie ingest stores every link
 109  			// twice and the atom index dedupes on rebuild. The reverse record is
 110  			// keyed with the destination first and reads back as an EN source,
 111  			// which is what makes a JA-side lookup or an EN-side lookup both
 112  			// find the pair without guessing the other language from the stage
 113  			// tag.
 114  			if err := t.IngestAtomLink(LangJA, LangEN, jaAtom, enAtom, rArch, rDisc); err != nil {
 115  				return err
 116  			}
 117  			if err := t.IngestAtomLink(LangEN, LangJA, enAtom, jaAtom, rArch, rDisc); err != nil {
 118  				return err
 119  			}
 120  			stats.Links++
 121  			enUsed[j] = true
 122  			break
 123  		}
 124  	}
 125  	return nil
 126  }
 127  
 128  // IngestPairs reads tab-separated JA/EN pairs and ingests each one.
 129  func (t *Tree) IngestPairs(tsv string, rArch, rDisc uint8) (*PairStats, error) {
 130  	stats := &PairStats{}
 131  	for _, line := range strings.Split(strings.TrimRight(tsv, "\n"), "\n") {
 132  		if line == "" {
 133  			continue
 134  		}
 135  		f := strings.Split(line, "\t")
 136  		if len(f) < 2 {
 137  			continue
 138  		}
 139  		if err := t.IngestPair(f[0], f[1], rArch, rDisc, stats); err != nil {
 140  			return stats, err
 141  		}
 142  	}
 143  	return stats, nil
 144  }
 145