package iskra import ( "strings" ) // langJA and langEN are the domains the JA-EN pair ingest and the round trip // use. cmd/iskra/ja.mx: LangEN 0x01, LangJA 0x02. const ( LangEN uint8 = 0x01 LangJA uint8 = 0x02 LangKO uint8 = 0x03 LangZH uint8 = 0x04 ) // PairStats counts what an ingest run put in the lattice. type PairStats struct { Pairs int Atoms int Patterns int Links int DeepStaffs int Rejected int Quality QualitySummary RejectedByGate []string // the pairs the gate dropped, JA side } // IngestPair stores one JA/EN pair: both extractions' patterns and atoms, the // cross-domain pattern link, the deep pattern, and a role-matched atom link for // every pair of slots whose roles are equivalent. // // cmd/iskra/ja.mx ingestPair with its quality gate. The versioned contexted // atom link (GenContexted, the AtomLinkKey form with per-slot contexts) is not // stored: the legacy bilateral link is what makes roundtrip_cross's lookups // resolve, and the contexted generation is a scoring refinement over it. func (t *Tree) IngestPair(ja, en string, rArch, rDisc uint8, stats *PairStats) error { stats.Pairs++ jaToks := TokenizeJA(ja) enToks := TokenizeEN(en) jaExt := ExtractJA(jaToks) enExt := ExtractEN(enToks) // The gate scores the pair and drops it before anything is stored. It is // cmd/iskra/quality.mx passesGate, at QualityThreshold and the 8-token // floor; a pair under the floor passes unscored. q := ScorePair(ja, en, jaToks, enToks, jaExt, enExt) passed := PassesGate(q, jaToks, enToks) stats.Quality.RecordQuality(q, jaToks, enToks, passed) if !passed { stats.Rejected++ stats.RejectedByGate = append(stats.RejectedByGate, ja) return nil } if len(jaExt.Pattern) > 0 { if _, err := t.IngestPattern(LangJA, jaExt); err != nil { return err } stats.Atoms += len(jaExt.Slots) stats.Patterns++ } if len(enExt.Pattern) > 0 { if _, err := t.IngestPattern(LangEN, enExt); err != nil { return err } stats.Atoms += len(enExt.Slots) stats.Patterns++ } if len(jaExt.Pattern) > 0 && len(enExt.Pattern) > 0 { if err := t.IngestCrossDomain(LangJA, LangEN, jaExt.Pattern, enExt.Pattern); err != nil { return err } stats.Patterns++ } if len(jaExt.DeepPat) > 0 && len(enExt.DeepPat) > 0 { if err := t.IngestDeepPattern(jaExt.DeepPat); err != nil { return err } stats.DeepStaffs++ } // Role-based alignment of slot fillers, first unused equivalent match wins. // Each aligned pair is stored under its own (src, dst) key, so an EN // lookup for the EN atom finds an edge keyed with the EN atom first. The // JA record is inserted first and the EN one mirrors it, which is how // BuildAtomIdx sees both languages without a language field on disk. enUsed := make([]bool, len(enExt.Slots)) for i, jaWord := range jaExt.Slots { jaRole := int32(HistComplement) if i < len(jaExt.Roles) { jaRole = jaExt.Roles[i] } for j, enWord := range enExt.Slots { if enUsed[j] { continue } enRole := int32(HistComplement) if j < len(enExt.Roles) { enRole = enExt.Roles[j] } if !RoleEquiv(jaRole, enRole) { continue } jaAtom, enAtom := jaWord, enWord if i < len(jaExt.Set) && jaExt.Set[i].Atom != "" { jaAtom = jaExt.Set[i].Atom } if j < len(enExt.Set) && enExt.Set[j].Atom != "" { enAtom = enExt.Set[j].Atom } // Both directions, the way the moxie ingest stores every link // twice and the atom index dedupes on rebuild. The reverse record is // keyed with the destination first and reads back as an EN source, // which is what makes a JA-side lookup or an EN-side lookup both // find the pair without guessing the other language from the stage // tag. if err := t.IngestAtomLink(LangJA, LangEN, jaAtom, enAtom, rArch, rDisc); err != nil { return err } if err := t.IngestAtomLink(LangEN, LangJA, enAtom, jaAtom, rArch, rDisc); err != nil { return err } stats.Links++ enUsed[j] = true break } } return nil } // IngestPairs reads tab-separated JA/EN pairs and ingests each one. func (t *Tree) IngestPairs(tsv string, rArch, rDisc uint8) (*PairStats, error) { stats := &PairStats{} for _, line := range strings.Split(strings.TrimRight(tsv, "\n"), "\n") { if line == "" { continue } f := strings.Split(line, "\t") if len(f) < 2 { continue } if err := t.IngestPair(strings.TrimSpace(f[0]), strings.TrimSpace(f[1]), rArch, rDisc, stats); err != nil { return stats, err } } return stats, nil }