render_ja.go raw

   1  // Package iskra renderer.
   2  package iskra
   3  
   4  // Port of render_ja.mx: Set to JA text, and Discourse to JA text.
   5  //
   6  // Where moxie joins strings with `|` this port joins with `+`. Nothing else in
   7  // the order of operations changes, including the places where the moxie source
   8  // is visibly accidental: RenderJA counts the copulas before it appends them and
   9  // the adjective pass tests `i > 0 || len(copulas) > 0` without having counted
  10  // the copulas either.
  11  
  12  // RenderJADiscourse renders a multi-clause Discourse to JA text.
  13  // Single-clause case renders identically to RenderJA(d[0].Set).
  14  //
  15  // Subordinating prefixes (もし for ClauseIf, なぜなら for ClauseBecause)
  16  // attach to the subordinate clause itself. Peer connectives (、 for AND/OR/
  17  // BUT) join adjacent clauses.
  18  func RenderJADiscourse(d []Clause) string {
  19  	if len(d) == 0 {
  20  		return ""
  21  	}
  22  	out := ""
  23  	for i, c := range d {
  24  		if i > 0 {
  25  			switch c.Relation {
  26  			case ClauseAnd:
  27  				out = out + "、"
  28  			case ClauseOr:
  29  				out = out + "、または"
  30  			case ClauseBut:
  31  				out = out + "、しかし"
  32  			case ClauseIf, ClauseBecause:
  33  				out = out + "、"
  34  			default:
  35  				out = out + "、"
  36  			}
  37  		}
  38  		// Subordinating prefix on this clause.
  39  		switch c.Relation {
  40  		case ClauseIf:
  41  			out = out + "もし"
  42  		case ClauseBecause:
  43  			out = out + "なぜなら"
  44  		}
  45  		out = out + RenderJA(c.Set)
  46  	}
  47  	return out
  48  }
  49  
  50  // RenderJA emits the Set as JA text in SOV order with modifier-aware traversal.
  51  //
  52  // Three groups, emitted in order:
  53  //  1. Top-level non-verb arguments (with their POSS/ATTR modifiers)
  54  //  2. Top-level verbs (with their modifiers)
  55  //  3. Copular predicates (MKCop entries) - emitted last as sentence-final
  56  //     predicate with the copula form (だ/だった/です/でした) per Morph.
  57  //
  58  // POSS/ATTR modifiers attach to non-verb/verb heads (Head points at head).
  59  // MKCop entries have Head pointing at the subject but render at end of clause.
  60  func RenderJA(set []SetEntry) string {
  61  	var topNonVerbs, topVerbs, copulas, adjs []int32
  62  	mods := map[int32][]int32{} // non-COP/ADJ modifiers: headIdx -> [modIdx, ...]
  63  	for i, e := range set {
  64  		if e.ModKind == MKCop {
  65  			copulas = append(copulas, int32(i))
  66  			continue
  67  		}
  68  		if e.ModKind == MKAdj {
  69  			adjs = append(adjs, int32(i))
  70  			continue
  71  		}
  72  		if e.Head >= 0 && int(e.Head) < len(set) {
  73  			mods[int32(e.Head)] = append(mods[int32(e.Head)], int32(i))
  74  			continue
  75  		}
  76  		if e.Role == HistVerb {
  77  			topVerbs = append(topVerbs, int32(i))
  78  		} else {
  79  			topNonVerbs = append(topNonVerbs, int32(i))
  80  		}
  81  	}
  82  
  83  	// Pair each copula/adj predicate with its nearest preceding subject.
  84  	// Head indices are unreliable in flattened multi-clause sets, so we
  85  	// assign subjects to predicates by proximity: each predicate claims
  86  	// the closest unclaimed subject that precedes it in set order.
  87  	predSubj := map[int32]int32{} // predicate idx -> subject idx
  88  	usedSubj := map[int32]bool{}
  89  	// The moxie source allocates allPreds at len(copulas)+len(adjs) and then
  90  	// appends both lists on top of that allocation. The capacity only sizes
  91  	// the buffer; the append length is what the range sees, so this Go
  92  	// version appends exactly what moxie appends.
  93  	var allPreds []int32
  94  	allPreds = append(allPreds, copulas...)
  95  	allPreds = append(allPreds, adjs...)
  96  	for _, pidx := range allPreds {
  97  		best := int32(-1)
  98  		for _, sidx := range topNonVerbs {
  99  			if sidx >= pidx {
 100  				break
 101  			}
 102  			r := set[sidx].Role
 103  			if (r == HistSubject || r == HistTopic) && !usedSubj[sidx] {
 104  				best = sidx
 105  			}
 106  		}
 107  		if best >= 0 {
 108  			predSubj[pidx] = best
 109  			usedSubj[best] = true
 110  		}
 111  	}
 112  
 113  	var out []byte
 114  	sepIfNeeded := func() {
 115  		if len(out) > 0 && !endsWithJAParticle(out) && out[len(out)-1] != ' ' {
 116  			out = append(out, ' ')
 117  		}
 118  	}
 119  	clauseSep := func() {
 120  		if len(out) > 0 {
 121  			out = append(out, 0xe3, 0x80, 0x81) // 、
 122  		}
 123  	}
 124  	for _, idx := range topNonVerbs {
 125  		if usedSubj[idx] {
 126  			continue
 127  		}
 128  		sepIfNeeded()
 129  		emitJANonVerbWithMods(&out, set, idx, mods)
 130  	}
 131  	for _, idx := range topVerbs {
 132  		sepIfNeeded()
 133  		emitJAVerbWithMods(&out, set, idx, mods)
 134  	}
 135  	for i, idx := range copulas {
 136  		if i > 0 {
 137  			clauseSep()
 138  		} else {
 139  			sepIfNeeded()
 140  		}
 141  		if sidx, ok := predSubj[idx]; ok {
 142  			emitJANonVerbWithMods(&out, set, sidx, mods)
 143  		}
 144  		emitJACopula(&out, set, idx, mods)
 145  	}
 146  	for i, idx := range adjs {
 147  		if i > 0 || len(copulas) > 0 {
 148  			clauseSep()
 149  		} else {
 150  			sepIfNeeded()
 151  		}
 152  		if sidx, ok := predSubj[idx]; ok {
 153  			emitJANonVerbWithMods(&out, set, sidx, mods)
 154  		}
 155  		emitJAPredAdj(&out, set, idx, mods)
 156  	}
 157  	return string(out)
 158  }
 159  
 160  // emitJAPredAdj emits a predicative i-adjective as the sentence-final
 161  // predicate. The atom is normally the full i-adj form (面白い); when an
 162  // EN to JA translation returns a stem without the い suffix (e.g. red to 赤),
 163  // append い to restore the predicative-adjective surface. Past/negative
 164  // forms would replace い with かった/くない but only non-past is handled.
 165  // Synthetic 3sg marker (◯) appended after the adjective.
 166  func emitJAPredAdj(out *[]byte, set []SetEntry, idx int32, mods map[int32][]int32) {
 167  	emitJAModifiers(out, set, idx, mods)
 168  	e := set[idx]
 169  	*out = append(*out, []byte(e.Atom)...)
 170  	if !EndsInIKana(e.Atom) {
 171  		*out = append(*out, 0xe3, 0x81, 0x84) // い
 172  	}
 173  	if e.Morph&Meta3Sg != 0 {
 174  		*out = append(*out, []byte(markerToJA[Mk3Sg])...)
 175  	}
 176  }
 177  
 178  // emitJACopula emits a copular predicate: complement_atom + copula form +
 179  // trailing morph markers. The morph markers MUST come AFTER the copula form,
 180  // not between atom and copula, to avoid splitting the copula compound on
 181  // re-extraction (学生◯だ would tokenize as [学生, ◯, だ] with だ as a
 182  // separate slot; 学生だ◯ tokenizes as [学生だ, ◯] which strips cleanly).
 183  //
 184  // Locative variant: when the MKCop entry carries OblRole=ORLoc, emit as
 185  // 〜の中にいる (existence-locative) instead of 〜だ (nominal copula).
 186  func emitJACopula(out *[]byte, set []SetEntry, idx int32, mods map[int32][]int32) {
 187  	emitJAModifiers(out, set, idx, mods)
 188  	e := set[idx]
 189  	// Definiteness and plural markers belong with the noun (before copula).
 190  	*out = append(*out, []byte(e.Atom)...)
 191  	if e.Morph&MetaDefDef != 0 {
 192  		*out = append(*out, []byte(markerToJA[MkDef])...)
 193  	}
 194  	if e.Morph&MetaNumPlural != 0 {
 195  		*out = append(*out, []byte(markerToJA[MkPlural])...)
 196  	}
 197  	if e.OblRole == ORLoc {
 198  		// Locative-existence: 〜の中に + いる.
 199  		*out = append(*out, 0xe3, 0x81, 0xae)                   // の
 200  		*out = append(*out, 0xe4, 0xb8, 0xad)                   // 中
 201  		*out = append(*out, 0xe3, 0x81, 0xab)                   // に
 202  		*out = append(*out, 0xe3, 0x81, 0x84, 0xe3, 0x82, 0x8b) // いる
 203  	} else {
 204  		*out = append(*out, []byte(jaCopulaForm(e.Morph))...)
 205  	}
 206  	// 3sg agreement marker AFTER the copula form so it doesn't split it.
 207  	if e.Morph&Meta3Sg != 0 {
 208  		*out = append(*out, []byte(markerToJA[Mk3Sg])...)
 209  	}
 210  }
 211  
 212  func jaCopulaForm(morph uint16) string {
 213  	past := morph&MetaTensePast != 0
 214  	pol := morph&MetaFormalityPol != 0
 215  	switch {
 216  	case past && pol:
 217  		return "でした"
 218  	case past:
 219  		return "だった"
 220  	case pol:
 221  		return "です"
 222  	default:
 223  		return "だ"
 224  	}
 225  }
 226  
 227  // emitJAModifiers emits pre-head modifiers (POSS, ATTR, ADV) of the entry at
 228  // idx with the appropriate relation marker. MKCoord peers are emitted by the
 229  // caller AFTER the head, so they're skipped here.
 230  func emitJAModifiers(out *[]byte, set []SetEntry, idx int32, mods map[int32][]int32) {
 231  	for _, mIdx := range mods[idx] {
 232  		m := set[mIdx]
 233  		if m.ModKind == MKCoord {
 234  			continue
 235  		}
 236  		// MKRel: modifier is a verb forming a relative clause. JA renders the
 237  		// conjugated verb BEFORE the head noun with no particle between. The
 238  		// verb's morph drives the surface form; no synthetic morph markers
 239  		// (★/☆/◯) because those would split the conjugation boundary.
 240  		if m.ModKind == MKRel {
 241  			form := renderJAVerb(m.Atom, m.Class, m.Morph)
 242  			*out = append(*out, []byte(form)...)
 243  			continue
 244  		}
 245  		*out = append(*out, []byte(m.Atom)...)
 246  		emitMorphMarkersJA(out, m.Morph)
 247  		switch m.ModKind {
 248  		case MKPoss:
 249  			*out = append(*out, []byte(markerToJA[MkNo])...) // の
 250  		case MKAttr:
 251  			// い-adjective sits directly before the head, no particle.
 252  		case MKAdv:
 253  			// Adverbial: space delimiter so tokenizer separates adv from verb.
 254  			*out = append(*out, ' ')
 255  		}
 256  	}
 257  }
 258  
 259  func emitJANonVerbWithMods(out *[]byte, set []SetEntry, idx int32, mods map[int32][]int32) {
 260  	emitJAModifiers(out, set, idx, mods)
 261  	e := set[idx]
 262  	*out = append(*out, []byte(e.Atom)...)
 263  	// Skip morph markers + particles when the atom is untranslated or
 264  	// foreign-language leak; those would be meaningless decorations on a
 265  	// placeholder. Particle is still emitted to preserve sentence shape.
 266  	if e.Atom == UntranslatedMarker || containsASCIIByte(e.Atom) {
 267  		// Still emit the particle so the sentence parses on round-trip.
 268  		particle := ""
 269  		if e.Mark != 0 && e.Mark <= 16 && e.Mark != MkTo {
 270  			particle = markerToJA[e.Mark]
 271  		}
 272  		if particle == "" && e.OblRole != ORNone {
 273  			particle = oblRoleToJaParticle(e.OblRole)
 274  		}
 275  		if particle == "" {
 276  			particle = renderJAParticle(e.Role, e.Mark)
 277  		}
 278  		if particle != "" {
 279  			*out = append(*out, []byte(particle)...)
 280  		}
 281  		return
 282  	}
 283  	emitMorphMarkersJA(out, e.Morph)
 284  	// Coordination peers: emit と + peer-atom for each MKCoord modifier of
 285  	// this head. The peer's own morph markers stick to its atom; the head's
 286  	// particle is emitted AFTER all coord members so it scopes the whole group.
 287  	for _, mIdx := range mods[idx] {
 288  		if set[mIdx].ModKind != MKCoord {
 289  			continue
 290  		}
 291  		peer := set[mIdx]
 292  		*out = append(*out, []byte(markerToJA[MkTo])...) // と
 293  		*out = append(*out, []byte(peer.Atom)...)
 294  		emitMorphMarkersJA(out, peer.Morph)
 295  	}
 296  	// Priority for particle selection: Mark first, then OblRole, then Role default.
 297  	// Mark==MkTo on the head is the coordination signal (consumed above), so
 298  	// fall through to OblRole or Role for the actual scope-particle.
 299  	var particle string
 300  	if e.Mark != 0 && e.Mark <= 16 && e.Mark != MkTo {
 301  		particle = markerToJA[e.Mark]
 302  	}
 303  	if particle == "" && e.OblRole != ORNone {
 304  		particle = oblRoleToJaParticle(e.OblRole)
 305  	}
 306  	if particle == "" {
 307  		particle = renderJAParticle(e.Role, e.Mark)
 308  	}
 309  	if particle != "" {
 310  		*out = append(*out, []byte(particle)...)
 311  	}
 312  }
 313  
 314  func emitJAVerbWithMods(out *[]byte, set []SetEntry, idx int32, mods map[int32][]int32) {
 315  	emitJAModifiers(out, set, idx, mods)
 316  	e := set[idx]
 317  	form := renderJAVerb(e.Atom, e.Class, e.Morph)
 318  	*out = append(*out, []byte(form)...)
 319  	// 3sg marker emitted via emitMorphMarkersJA semantics, but verb-side
 320  	// places it AFTER the conjugated form rather than between atom and particle.
 321  	if e.Morph&Meta3Sg != 0 {
 322  		*out = append(*out, []byte(markerToJA[Mk3Sg])...)
 323  	}
 324  	if isSentenceFinalParticle(e.Mark) {
 325  		*out = append(*out, []byte(markerToJAString(e.Mark))...)
 326  	}
 327  }
 328  
 329  // emitMorphMarkersJA appends synthetic morph markers (★/☆/◯) to the output
 330  // for morph bits that have no native JA surface form. EN-only features like
 331  // definiteness, plural, and 3sg agreement need explicit markers in JA to
 332  // survive the round-trip.
 333  func emitMorphMarkersJA(out *[]byte, morph uint16) {
 334  	if morph&MetaDefDef != 0 {
 335  		*out = append(*out, []byte(markerToJA[MkDef])...)
 336  	}
 337  	if morph&MetaNumPlural != 0 {
 338  		*out = append(*out, []byte(markerToJA[MkPlural])...)
 339  	}
 340  	if morph&Meta3Sg != 0 {
 341  		*out = append(*out, []byte(markerToJA[Mk3Sg])...)
 342  	}
 343  }
 344  
 345  func isSentenceFinalParticle(mk uint8) bool {
 346  	switch mk {
 347  	case MkYo, MkNe, MkKa, MkYori, MkKedo, MkMo:
 348  		return true
 349  	}
 350  	return false
 351  }
 352  
 353  // renderJAParticle picks the JA particle for a slot.
 354  // Priority: OblRole (semantic) > Mark (within-language preserved) > Role default.
 355  func renderJAParticle(role int32, mark uint8) string {
 356  	if mark != 0 && mark <= 16 {
 357  		s := markerToJA[mark]
 358  		if s != "" {
 359  			return s
 360  		}
 361  	}
 362  	switch role {
 363  	case HistTopic:
 364  		return "は"
 365  	case HistSubject:
 366  		return "が"
 367  	case HistObject:
 368  		return "を"
 369  	case HistScope:
 370  		return "に"
 371  	case HistModifier:
 372  		return "で"
 373  	case HistOperator:
 374  		return "の"
 375  	case HistComplement:
 376  		return ""
 377  	}
 378  	return ""
 379  }
 380  
 381  // oblRoleToJaParticle maps an oblique semantic role to the canonical JA particle.
 382  func oblRoleToJaParticle(or uint8) string {
 383  	switch or {
 384  	case ORGoal:
 385  		return "に" // motion goal
 386  	case ORLoc:
 387  		return "で" // location of action
 388  	case ORSource:
 389  		return "から"
 390  	case ORLimit:
 391  		return "まで"
 392  	case ORInstr:
 393  		return "で" // instrumental
 394  	case ORComit:
 395  		return "と"
 396  	case ORBenef:
 397  		return "に" // default; could be のために
 398  	case ORAgent:
 399  		return "に" // passive agent
 400  	case ORRecip:
 401  		return "に" // dative
 402  	case ORPart:
 403  		return "の"
 404  	case ORCompare:
 405  		return "より"
 406  	}
 407  	return ""
 408  }
 409  
 410  // endsWithJAParticle tests the tail of b against every non-empty markerToJA
 411  // entry from index 1. The marker table is fixed at 50 entries in the moxie
 412  // source, so the loop bound is the table itself.
 413  func endsWithJAParticle(b []byte) bool {
 414  	n := len(b)
 415  	if n < 3 {
 416  		return false
 417  	}
 418  	for i := 1; i < len(markerToJA); i++ {
 419  		p := markerToJA[i]
 420  		if p == "" {
 421  			continue
 422  		}
 423  		pb := []byte(p)
 424  		if n >= len(pb) && string(b[n-len(pb):]) == p {
 425  			return true
 426  		}
 427  	}
 428  	return false
 429  }
 430  
 431  func markerToJAString(mk uint8) string {
 432  	if mk >= 1 && mk <= 16 {
 433  		return markerToJA[mk]
 434  	}
 435  	return ""
 436  }
 437  
 438  // renderJAVerb reconstructs the verb surface form from stem, class, and morph.
 439  // Class=0 means "no class detected" - this happens routinely for atoms
 440  // returned by cross-language translation (the lattice atom-link table
 441  // carries the lemma but not its class). Recover by inferring class from
 442  // the lemma's final character + a small lookup table of common verbs.
 443  // Falling back to godan-ru as default for unknown kanji stems produces
 444  // a parseable surface form that round-trips, even when the specific
 445  // class is wrong.
 446  //
 447  // Deviation from the moxie source, required by the Go lemmatiser: the moxie
 448  // lemmatiser records a verb class where it can, so class 0 there means "only
 449  // the cross-language link lost it" and infers from the atom. The Go
 450  // LemmatizeJA always returns VClassNone for verbs (the conjugation table in
 451  // morph.go has no class data and no stem rewrite), so class 0 here means
 452  // "unknown" and the stem is emitted unchanged. VClassBare, VClassSuru,
 453  // VClassKuru and the enumerated godan and ichidan classes conjugate as in the
 454  // source.
 455  func renderJAVerb(stem string, class uint8, morph uint16) string {
 456  	if stem == UntranslatedMarker {
 457  		return stem
 458  	}
 459  	if containsASCIIByte(stem) {
 460  		return stem
 461  	}
 462  	// render_ja.mx inferJAClass is called whenever the atom carries no class:
 463  	// a cross-language atom link stores the lemma but not the verb class, so a
 464  	// translated verb reaches the renderer as class 0 and is classified from its
 465  	// stem here.
 466  	if class == VClassNone {
 467  		class = InferJAClass(stem)
 468  	}
 469  	if class == VClassBare {
 470  		b := []byte(stem)
 471  		if EndsInIKana(stem) && len(b) > 3 && hasKanji(b) {
 472  			return renderJAIAdjVerbSlot(stem, morph)
 473  		}
 474  		return stem
 475  	}
 476  	if class == VClassSuru {
 477  		return conjugateSuru(stem, morph)
 478  	}
 479  	if class == VClassKuru {
 480  		return conjugateKuru(stem, morph)
 481  	}
 482  	// Godan dict-form lemmas (行く, 読む, etc.) include the u-row ending.
 483  	// Strip it before conjugating to avoid double-suffix (行く+く = 行くく).
 484  	stem = stripGodanDictEnding(stem, class)
 485  	connector, suffix := jaConjugate(class, morph)
 486  	return stem + connector + suffix
 487  }
 488  
 489  func renderJAIAdjVerbSlot(stem string, morph uint16) string {
 490  	b := []byte(stem)
 491  	root := string(b[:len(b)-3])
 492  	neg := morph&MetaPolarNeg != 0
 493  	past := morph&MetaTensePast != 0
 494  	if neg && past {
 495  		return root + "くなかった"
 496  	}
 497  	if neg {
 498  		return root + "くない"
 499  	}
 500  	if past {
 501  		return root + "かった"
 502  	}
 503  	return stem
 504  }
 505  
 506  func conjugateSuru(stem string, morph uint16) string {
 507  	// stem is "する" for standalone, or "勉強する" for compounds.
 508  	// Strip する suffix to get the noun-stem (empty for standalone).
 509  	base := ""
 510  	sb := []byte(stem)
 511  	suru := "する"
 512  	if len(sb) > 6 && string(sb[len(sb)-6:]) == suru {
 513  		base = string(sb[:len(sb)-6])
 514  	}
 515  	past := morph&MetaTensePast != 0
 516  	prog := morph&MetaAspectProg != 0
 517  	neg := morph&MetaPolarNeg != 0
 518  	pol := morph&MetaFormalityPol != 0
 519  	pass := morph&MetaPassive != 0
 520  	caus := morph&MetaCausative != 0
 521  	if caus && pass {
 522  		if past {
 523  			return base + "させられた"
 524  		}
 525  		return base + "させられる"
 526  	}
 527  	if pass {
 528  		if past {
 529  			return base + "された"
 530  		}
 531  		return base + "される"
 532  	}
 533  	if caus {
 534  		if past {
 535  			return base + "させた"
 536  		}
 537  		return base + "させる"
 538  	}
 539  	if pol {
 540  		if past && neg {
 541  			return base + "しませんでした"
 542  		}
 543  		if neg {
 544  			return base + "しません"
 545  		}
 546  		if past {
 547  			return base + "しました"
 548  		}
 549  		return base + "します"
 550  	}
 551  	if prog {
 552  		if past {
 553  			return base + "していた"
 554  		}
 555  		return base + "している"
 556  	}
 557  	if neg {
 558  		if past {
 559  			return base + "しなかった"
 560  		}
 561  		return base + "しない"
 562  	}
 563  	if past {
 564  		return base + "した"
 565  	}
 566  	return base + "する"
 567  }
 568  
 569  func conjugateKuru(stem string, morph uint16) string {
 570  	past := morph&MetaTensePast != 0
 571  	prog := morph&MetaAspectProg != 0
 572  	neg := morph&MetaPolarNeg != 0
 573  	pol := morph&MetaFormalityPol != 0
 574  	if pol {
 575  		if past {
 576  			return "来ました"
 577  		}
 578  		if neg {
 579  			return "来ません"
 580  		}
 581  		return "来ます"
 582  	}
 583  	if prog {
 584  		if past {
 585  			return "来ていた"
 586  		}
 587  		return "来ている"
 588  	}
 589  	if neg {
 590  		if past {
 591  			return "来なかった"
 592  		}
 593  		return "来ない"
 594  	}
 595  	if past {
 596  		return "来た"
 597  	}
 598  	return "来る"
 599  }
 600  
 601  func stripGodanDictEnding(stem string, class uint8) string {
 602  	s := godanDictSuffix(class)
 603  	if s == "" {
 604  		return stem
 605  	}
 606  	b := []byte(stem)
 607  	sb := []byte(s)
 608  	if len(b) > len(sb) && string(b[len(b)-len(sb):]) == s {
 609  		return string(b[:len(b)-len(sb)])
 610  	}
 611  	return stem
 612  }
 613  
 614  // containsASCIIByte returns true when at least one byte of s is in the
 615  // ASCII range. Used by the JA renderer to refuse inflection on atoms that
 616  // appear to be foreign (cross-language leakage). Mirrors the EN renderer's
 617  // isASCIIOnly guard but from the JA side.
 618  func containsASCIIByte(s string) bool {
 619  	for i := 0; i < len(s); i++ {
 620  		if s[i] < 0x80 {
 621  			return true
 622  		}
 623  	}
 624  	return false
 625  }
 626  
 627  // rawMorphSuffix emits a class-independent surface for morph bits.
 628  // Used when class detection failed (class=0) - includes the case of a
 629  // cross-language-translated atom that lost its JA class.
 630  // No caller in render_ja.mx reaches this: renderJAVerb infers a class for 0
 631  // and every conjugation path takes the class-aware helpers. It is ported so
 632  // the file has no gaps.
 633  func rawMorphSuffix(morph uint16) string {
 634  	if morph&MetaMoodVol != 0 {
 635  		// Default to ichidan よう - the only class-neutral volitional form
 636  		// available without verb-class knowledge. Re-extraction recognizes
 637  		// よう as ichidan, so this round-trips even if not original-faithful.
 638  		return "よう"
 639  	}
 640  	if morph&MetaPassive != 0 {
 641  		if morph&MetaTensePast != 0 {
 642  			return "られた"
 643  		}
 644  		return "られる"
 645  	}
 646  	if morph&MetaCausative != 0 {
 647  		if morph&MetaTensePast != 0 {
 648  			return "させた"
 649  		}
 650  		return "させる"
 651  	}
 652  	out := ""
 653  	if morph&MetaAspectProg != 0 {
 654  		if morph&MetaTensePast != 0 {
 655  			out = out + "ていた"
 656  		} else {
 657  			out = out + "ている"
 658  		}
 659  		return out
 660  	}
 661  	if morph&MetaPolarNeg != 0 {
 662  		if morph&MetaTensePast != 0 {
 663  			out = out + "なかった"
 664  		} else {
 665  			out = out + "ない"
 666  		}
 667  		return out
 668  	}
 669  	if morph&MetaFormalityPol != 0 {
 670  		if morph&MetaTensePast != 0 {
 671  			return "ました"
 672  		}
 673  		return "ます"
 674  	}
 675  	if morph&MetaTensePast != 0 {
 676  		return "た"
 677  	}
 678  	return ""
 679  }
 680  
 681  // jaConjugate returns (connector, suffix) for a verb of given class and morph.
 682  // connector is the kana between stem and suffix (e.g. for ichidan: empty for past,
 683  // for godan: i-row for polite, etc).
 684  func jaConjugate(class uint8, morph uint16) (string, string) {
 685  	past := morph&MetaTensePast != 0
 686  	prog := morph&MetaAspectProg != 0
 687  	neg := morph&MetaPolarNeg != 0
 688  	pol := morph&MetaFormalityPol != 0
 689  	vol := morph&MetaMoodVol != 0
 690  	pass := morph&MetaPassive != 0
 691  	caus := morph&MetaCausative != 0
 692  
 693  	// Causative-passive (made to V): させられる
 694  	if caus && pass {
 695  		if past {
 696  			return jaCausPassPast(class)
 697  		}
 698  		return jaCausPass(class)
 699  	}
 700  	// Passive alone
 701  	if pass {
 702  		if past {
 703  			return jaPassivePast(class)
 704  		}
 705  		return jaPassive(class)
 706  	}
 707  	// Causative alone
 708  	if caus {
 709  		if past {
 710  			return jaCausativePast(class)
 711  		}
 712  		return jaCausative(class)
 713  	}
 714  
 715  	if vol {
 716  		return jaVolitional(class)
 717  	}
 718  
 719  	// Polite forms: ます, ました, ません
 720  	if pol {
 721  		istem := jaIStem(class)
 722  		if past && neg {
 723  			return istem, "ませんでした" // rare; default to ません
 724  		}
 725  		if neg {
 726  			return istem, "ません"
 727  		}
 728  		if past {
 729  			return istem, "ました"
 730  		}
 731  		return istem, "ます"
 732  	}
 733  
 734  	// Progressive: ている, ていた
 735  	if prog {
 736  		teconn, te := jaTeForm(class)
 737  		if past {
 738  			return teconn, te + "いた" // ていた / でいた
 739  		}
 740  		return teconn, te + "いる" // ている / でいる
 741  	}
 742  
 743  	// Negative
 744  	if neg {
 745  		nstem := jaNegStem(class)
 746  		if past {
 747  			return nstem, "なかった"
 748  		}
 749  		return nstem, "ない"
 750  	}
 751  
 752  	// Past (plain)
 753  	if past {
 754  		return jaPastForm(class)
 755  	}
 756  
 757  	// Dict form (non-past affirmative plain)
 758  	return jaDictForm(class)
 759  }
 760  
 761  // jaDictForm returns the dict-form ending for each class.
 762  // For ichidan, the connector (え/い-row) stays in the stem, suffix is る.
 763  // For godan, the kanji-stem gets the u-row ending appended directly.
 764  func jaDictForm(class uint8) (string, string) {
 765  	switch class {
 766  	case VClassIchidan:
 767  		return "", "る"
 768  	case VClassGodanKu:
 769  		return "", "く"
 770  	case VClassGodanGu:
 771  		return "", "ぐ"
 772  	case VClassGodanSu:
 773  		return "", "す"
 774  	case VClassGodanTsu:
 775  		return "", "つ"
 776  	case VClassGodanNu:
 777  		return "", "ぬ"
 778  	case VClassGodanBu:
 779  		return "", "ぶ"
 780  	case VClassGodanMu:
 781  		return "", "む"
 782  	case VClassGodanRu:
 783  		return "", "る"
 784  	case VClassGodanU:
 785  		return "", "う"
 786  	}
 787  	return "", "る" // default る
 788  }
 789  
 790  // jaPastForm returns (connector, suffix) for past-tense plain form.
 791  // Ichidan: stem + た. Godan: contracted forms.
 792  func jaPastForm(class uint8) (string, string) {
 793  	switch class {
 794  	case VClassIchidan:
 795  		return "", "た"
 796  	case VClassGodanKu:
 797  		return "い", "た" // いた
 798  	case VClassGodanGu:
 799  		return "い", "だ" // いだ
 800  	case VClassGodanSu:
 801  		return "し", "た" // した
 802  	case VClassGodanTsu, VClassGodanRu, VClassGodanU:
 803  		return "っ", "た" // った
 804  	case VClassGodanNu, VClassGodanBu, VClassGodanMu:
 805  		return "ん", "だ" // んだ
 806  	}
 807  	return "", "た"
 808  }
 809  
 810  // jaTeForm returns (connector, te-form-suffix) - basically same as past but te/で.
 811  func jaTeForm(class uint8) (string, string) {
 812  	switch class {
 813  	case VClassIchidan:
 814  		return "", "て"
 815  	case VClassGodanKu:
 816  		return "い", "て" // いて
 817  	case VClassGodanGu:
 818  		return "い", "で" // いで
 819  	case VClassGodanSu:
 820  		return "し", "て" // して
 821  	case VClassGodanTsu, VClassGodanRu, VClassGodanU:
 822  		return "っ", "て" // って
 823  	case VClassGodanNu, VClassGodanBu, VClassGodanMu:
 824  		return "ん", "で" // んで
 825  	}
 826  	return "", "て"
 827  }
 828  
 829  // jaIStem returns the i-row stem connector for polite forms.
 830  func jaIStem(class uint8) string {
 831  	switch class {
 832  	case VClassIchidan:
 833  		return "" // ichidan stem connects directly
 834  	case VClassGodanKu:
 835  		return "き"
 836  	case VClassGodanGu:
 837  		return "ぎ"
 838  	case VClassGodanSu:
 839  		return "し"
 840  	case VClassGodanTsu:
 841  		return "ち"
 842  	case VClassGodanNu:
 843  		return "に"
 844  	case VClassGodanBu:
 845  		return "び"
 846  	case VClassGodanMu:
 847  		return "み"
 848  	case VClassGodanRu:
 849  		return "り"
 850  	case VClassGodanU:
 851  		return "い"
 852  	}
 853  	return ""
 854  }
 855  
 856  // jaNegStem returns the a-row stem connector for negative forms.
 857  func jaNegStem(class uint8) string {
 858  	switch class {
 859  	case VClassIchidan:
 860  		return ""
 861  	case VClassGodanKu:
 862  		return "か"
 863  	case VClassGodanGu:
 864  		return "が"
 865  	case VClassGodanSu:
 866  		return "さ"
 867  	case VClassGodanTsu:
 868  		// The あ-row of つ is た, so 立つ gives 立たない. It is the same kana
 869  		// as the plain past suffix; the suffix decides which reading applies.
 870  		return "た"
 871  	case VClassGodanNu:
 872  		return "な"
 873  	case VClassGodanBu:
 874  		return "ば"
 875  	case VClassGodanMu:
 876  		return "ま"
 877  	case VClassGodanRu:
 878  		return "ら"
 879  	case VClassGodanU:
 880  		return "わ"
 881  	}
 882  	return ""
 883  }
 884  
 885  // jaVolitional returns (connector, suffix) for volitional form (let's...).
 886  func jaVolitional(class uint8) (string, string) {
 887  	switch class {
 888  	case VClassIchidan:
 889  		return "", "よう"
 890  	case VClassGodanKu:
 891  		return "こ", "う" // こう
 892  	case VClassGodanGu:
 893  		return "ご", "う" // ごう
 894  	case VClassGodanSu:
 895  		return "そ", "う" // そう
 896  	case VClassGodanTsu:
 897  		return "と", "う" // とう
 898  	case VClassGodanNu:
 899  		return "の", "う" // のう
 900  	case VClassGodanBu:
 901  		return "ぼ", "う" // ぼう
 902  	case VClassGodanMu:
 903  		return "も", "う" // もう
 904  	case VClassGodanRu:
 905  		return "ろ", "う" // ろう
 906  	case VClassGodanU:
 907  		return "お", "う" // おう
 908  	}
 909  	return "", "よう"
 910  }
 911  
 912  // jaPassive: ichidan stem + られる, godan a-row stem + れる.
 913  func jaPassive(class uint8) (string, string) {
 914  	switch class {
 915  	case VClassIchidan:
 916  		return "", "られる"
 917  	}
 918  	// Godan: a-row connector + れる
 919  	return jaNegStem(class), "れる"
 920  }
 921  
 922  func jaPassivePast(class uint8) (string, string) {
 923  	switch class {
 924  	case VClassIchidan:
 925  		return "", "られた"
 926  	}
 927  	return jaNegStem(class), "れた" // a-row + れた
 928  }
 929  
 930  // jaCausative: ichidan stem + させる, godan a-row stem + せる.
 931  func jaCausative(class uint8) (string, string) {
 932  	switch class {
 933  	case VClassIchidan:
 934  		return "", "させる"
 935  	}
 936  	return jaNegStem(class), "せる" // a-row + せる
 937  }
 938  
 939  func jaCausativePast(class uint8) (string, string) {
 940  	switch class {
 941  	case VClassIchidan:
 942  		return "", "させた"
 943  	}
 944  	return jaNegStem(class), "せた" // a-row + せた
 945  }
 946  
 947  // jaCausPass: ichidan stem + させられる, godan a-row + せられる.
 948  func jaCausPass(class uint8) (string, string) {
 949  	switch class {
 950  	case VClassIchidan:
 951  		return "", "させられる"
 952  	}
 953  	return jaNegStem(class), "せられる" // a-row + せられる
 954  }
 955  
 956  func jaCausPassPast(class uint8) (string, string) {
 957  	switch class {
 958  	case VClassIchidan:
 959  		return "", "させられた"
 960  	}
 961  	return jaNegStem(class), "せられた" // a-row + せられた
 962  }
 963  
 964  // hasKanji reports a byte string that contains at least one CJK ideograph.
 965  func hasKanji(b []byte) bool {
 966  	for i := 0; i+2 < len(b); i++ {
 967  		if b[i] >= 0xe4 && b[i] <= 0xe9 {
 968  			return true
 969  		}
 970  	}
 971  	return false
 972  }
 973  
 974  // godanDictSuffix is the dictionary-form ending of each godan class,
 975  // lemma_ja.mx godanDictSuffix.
 976  func godanDictSuffix(class uint8) string {
 977  	switch class {
 978  	case VClassGodanKu:
 979  		return "く"
 980  	case VClassGodanGu:
 981  		return "ぐ"
 982  	case VClassGodanSu:
 983  		return "す"
 984  	case VClassGodanTsu:
 985  		return "つ"
 986  	case VClassGodanNu:
 987  		return "ぬ"
 988  	case VClassGodanBu:
 989  		return "ぶ"
 990  	case VClassGodanMu:
 991  		return "む"
 992  	case VClassGodanRu:
 993  		return "る"
 994  	case VClassGodanU:
 995  		return "う"
 996  	}
 997  	return ""
 998  }
 999