extract_en.go raw

   1  package iskra
   2  
   3  import "strings"
   4  
   5  // ExtractEN takes EN tokens and produces pattern + slots. Port of extract.mx
   6  // ExtractEN. Determin ers (the/a) are morph hints, prepositions are role
   7  // markers, verb auxiliaries stage copula info, pronouns are content nouns.
   8  
   9  // EN marker functional classes, extract.mx enMarkerClass.
  10  const (
  11  	enMarkUnknown     = 0
  12  	enMarkDeterminer  = 1 // the, a, an
  13  	enMarkPossDet     = 2 // my, your, his, her, its, our, their
  14  	enMarkVerbAux     = 3 // is, are, was, do, have...
  15  	enMarkPreposition = 4 // in, on, at, with, by...
  16  	enMarkNegation    = 5 // not, n't
  17  	enMarkConjunction = 6 // and, but, or
  18  	enMarkRelative    = 7 // that, which, who
  19  	enMarkPronoun     = 8 // i, you, he, she, it, we, they
  20  )
  21  
  22  func enMarkerClass(mk uint8) int32 {
  23  	switch mk {
  24  	case MkThe, MkA:
  25  		return enMarkDeterminer
  26  	case MkMy, MkYour, MkHis, MkHerP, MkIts, MkOurP, MkTheirP:
  27  		return enMarkPossDet
  28  	case MkIs, MkAre, MkWas, MkDo:
  29  		return enMarkVerbAux
  30  	case MkIn, MkOn, MkAt, MkWith, MkBy, MkFor, MkToEN, MkOf, MkFrom, MkAs, MkThan:
  31  		return enMarkPreposition
  32  	case MkNot:
  33  		return enMarkNegation
  34  	case MkAnd, MkBut:
  35  		return enMarkConjunction
  36  	case MkThat:
  37  		return enMarkRelative
  38  	case MkI, MkYou, MkPron3, MkItEN, MkWeEN, MkThey:
  39  		return enMarkPronoun
  40  	}
  41  	return enMarkUnknown
  42  }
  43  
  44  // findSubjectIdx returns the index of the entry that serves as the subject of
  45  // a copular predicate: the first entry whose role is Topic or Subject and that
  46  // is not itself a modifier. Returns -1 if no candidate found.
  47  func findSubjectIdx(roles []int32, heads []int16) int16 {
  48  	for i, r := range roles {
  49  		if i >= len(heads) {
  50  			break
  51  		}
  52  		if heads[i] >= 0 {
  53  			continue
  54  		}
  55  		if r == HistTopic || r == HistSubject {
  56  			return int16(i)
  57  		}
  58  	}
  59  	return -1
  60  }
  61  
  62  // buildSetFromSlices converts parallel per-slot slices into a []SetEntry.
  63  func buildSetFromSlices(
  64  	slots []string, roles []int32, slotMorphs []uint16,
  65  	slotMarks, slotOblRoles []uint8, slotHeads []int16, slotModKinds []uint8,
  66  ) []SetEntry {
  67  	// Role propagation before set construction: ATTR copies its head's role,
  68  	// ADV becomes HistModifier. Mutates roles, the caller's slice.
  69  	for i := 0; i < len(slots); i++ {
  70  		if i < len(slotModKinds) && i < len(slotHeads) && slotHeads[i] >= 0 {
  71  			h := int32(slotHeads[i])
  72  			switch slotModKinds[i] {
  73  			case MKAttr:
  74  				if int(h) < len(roles) {
  75  					roles[i] = roles[h]
  76  				}
  77  			case MKAdv:
  78  				roles[i] = HistModifier
  79  			}
  80  		}
  81  	}
  82  	var set []SetEntry
  83  	for i, lemma := range slots {
  84  		role := int32(HistComplement)
  85  		if i < len(roles) {
  86  			role = roles[i]
  87  		}
  88  		m := uint16(0)
  89  		if i < len(slotMorphs) {
  90  			m = slotMorphs[i]
  91  		}
  92  		mark := uint8(0)
  93  		if i < len(slotMarks) {
  94  			mark = slotMarks[i]
  95  		}
  96  		obl := uint8(ORNone)
  97  		if i < len(slotOblRoles) {
  98  			obl = slotOblRoles[i]
  99  		}
 100  		head := int16(-1)
 101  		modKind := uint8(MKNone)
 102  		if i < len(slotHeads) {
 103  			head = slotHeads[i]
 104  		}
 105  		if i < len(slotModKinds) {
 106  			modKind = slotModKinds[i]
 107  		}
 108  		set = append(set, SetEntry{
 109  			Role: role, Atom: lemma, Morph: m, Class: 0, Mark: mark, OblRole: obl,
 110  			Head: head, ModKind: modKind,
 111  		})
 112  	}
 113  	return set
 114  }
 115  
 116  // hasPredication reports whether the per-slot state holds a completed
 117  // predication: a verb slot or a copular complement.
 118  func hasPredication(roles []int32, slotModKinds []uint8) bool {
 119  	for i, r := range roles {
 120  		if r == HistVerb {
 121  			return true
 122  		}
 123  		if i < len(slotModKinds) && slotModKinds[i] == MKCop {
 124  			return true
 125  		}
 126  	}
 127  	return false
 128  }
 129  
 130  // looksLikeAdverb reports an -ly suffix or an irregular adverb.
 131  func looksLikeAdverb(w string) bool {
 132  	if len(w) > 3 && HasSuffixEN(w, "ly") {
 133  		return true
 134  	}
 135  	switch w {
 136  	case "fast", "well", "hard", "here", "there", "now", "then",
 137  		"today", "yesterday", "tomorrow", "always", "often", "never",
 138  		"sometimes", "usually", "rarely", "still", "already", "yet",
 139  		"soon", "later", "early", "late", "ever", "again":
 140  		return true
 141  	}
 142  	return false
 143  }
 144  
 145  // causativeBareVFollows reports a bare infinitive verb within the next four
 146  // tokens: "make/let + NP + bare-V". "to" and any preposition or conjunction
 147  // before the verb cancel the reading.
 148  func causativeBareVFollows(tokens []string, tokIdx int) bool {
 149  	for k := 1; k <= 4 && tokIdx+k < len(tokens); k++ {
 150  		t := strings.ToLower(tokens[tokIdx+k])
 151  		if t == "to" {
 152  			return false
 153  		}
 154  		if mk, isMk := enWordToMarker()[t]; isMk {
 155  			switch enMarkerClass(mk) {
 156  			case enMarkPreposition, enMarkConjunction:
 157  				return false
 158  			}
 159  			continue
 160  		}
 161  		if looksLikeVerb(t) {
 162  			return true
 163  		}
 164  	}
 165  	return false
 166  }
 167  
 168  // isEnDitransitive reports a verb that takes a bare-NP recipient before the
 169  // patient: "give X Y". moxie lists "hand" twice in the closed set; Go rejects a
 170  // repeated label and the set is the same either way.
 171  func isEnDitransitive(lemma string) bool {
 172  	switch lemma {
 173  	case "give", "send", "tell", "show", "offer", "hand", "pass",
 174  		"teach", "write", "read", "sell", "buy", "bring",
 175  		"mail", "lend", "owe", "pay", "throw", "ask":
 176  		return true
 177  	}
 178  	return false
 179  }
 180  
 181  // looksLikePredicateAdj reports a word that, after a copula, is a predicative
 182  // adjective rather than a verb or noun.
 183  func looksLikePredicateAdj(w string) bool {
 184  	if len(w) < 2 {
 185  		return false
 186  	}
 187  	switch w {
 188  	case "big", "small", "tall", "short", "long", "wide", "narrow",
 189  		"thick", "thin", "deep", "shallow", "high", "low",
 190  		"hot", "cold", "warm", "cool", "wet", "dry",
 191  		"fast", "slow", "quick", "old", "new", "young",
 192  		"good", "bad", "nice", "fine", "great", "poor",
 193  		"happy", "sad", "angry", "tired", "hungry", "thirsty",
 194  		"sleepy", "busy", "lazy", "easy", "hard", "soft",
 195  		"loud", "quiet", "clean", "dirty", "empty", "full",
 196  		"rich", "weak", "strong", "smart", "kind", "mean",
 197  		"red", "blue", "green", "yellow", "white", "black",
 198  		"pink", "brown", "gray", "grey", "purple", "orange",
 199  		"heavy", "light", "free", "cheap", "expensive",
 200  		"safe", "sick", "well", "ill", "ready", "right", "wrong",
 201  		"true", "false", "real", "fake", "open", "closed",
 202  		"bright", "dark", "sweet", "sour", "salty", "bitter",
 203  		"round", "square", "flat", "sharp", "dull",
 204  		"strange", "weird", "normal", "common", "rare",
 205  		"important", "famous", "popular", "different", "similar",
 206  		"alive", "dead", "alone", "together":
 207  		return true
 208  	}
 209  	if len(w) < 4 {
 210  		return false
 211  	}
 212  	switch w {
 213  	case "interesting", "exciting", "boring", "tiring", "amazing",
 214  		"frightening", "surprising", "confusing", "disappointing",
 215  		"satisfying", "encouraging", "pleasing", "fascinating",
 216  		"depressing", "embarrassing", "shocking", "thrilling",
 217  		"charming", "annoying", "relaxing", "stunning",
 218  		"missing", "willing", "outstanding", "promising":
 219  		return true
 220  	}
 221  	if HasSuffixEN(w, "ful") || HasSuffixEN(w, "ous") || HasSuffixEN(w, "ic") ||
 222  		HasSuffixEN(w, "able") || HasSuffixEN(w, "ible") {
 223  		return true
 224  	}
 225  	return false
 226  }
 227  
 228  // possDetSurface is the surface form of an EN possessive determiner.
 229  func possDetSurface(mk uint8) string {
 230  	switch mk {
 231  	case MkMy:
 232  		return "my"
 233  	case MkYour:
 234  		return "your"
 235  	case MkHis:
 236  		return "his"
 237  	case MkHerP:
 238  		return "her"
 239  	case MkIts:
 240  		return "its"
 241  	case MkOurP:
 242  		return "our"
 243  	case MkTheirP:
 244  		return "their"
 245  	}
 246  	return ""
 247  }
 248  
 249  // isENPunct reports a single-character punctuation token.
 250  func isENPunct(s string) bool {
 251  	if len(s) != 1 {
 252  		return false
 253  	}
 254  	c := s[0]
 255  	return c == '.' || c == ',' || c == '!' || c == '?' || c == ';' || c == ':' || c == '"' || c == '\''
 256  }
 257  
 258  // looksLikeVerb is the EN verb heuristic: suffix shapes plus a high-frequency
 259  // word set. The length guards keep red/fed/led and sing/king/ring out.
 260  func looksLikeVerb(w string) bool {
 261  	if len(w) < 2 {
 262  		return false
 263  	}
 264  	if (HasSuffixEN(w, "ing") && len(w) > 4) ||
 265  		(HasSuffixEN(w, "ed") && len(w) > 3) ||
 266  		HasSuffixEN(w, "ize") || HasSuffixEN(w, "ise") || HasSuffixEN(w, "ate") {
 267  		return true
 268  	}
 269  	if HasSuffixEN(w, "fy") || (HasSuffixEN(w, "en") && len(w) > 3) {
 270  		return true
 271  	}
 272  	switch w {
 273  	case
 274  		"go", "went", "gone", "goes",
 275  		"eat", "eats", "drink", "drinks", "read", "reads",
 276  		"write", "writes", "walk", "walks", "talk", "talks",
 277  		"sleep", "sleeps", "wake", "wakes", "sit", "sits",
 278  		"stand", "stands", "lie", "lies", "live", "lives",
 279  		"die", "dies", "chew", "chews", "fly", "flies",
 280  		"swim", "swims", "jump", "jumps", "throw", "throws",
 281  		"catch", "catches", "kick", "kicks", "hit", "hits",
 282  		"push", "pushes", "pull", "pulls", "grab", "grabs",
 283  		"bite", "bites", "chase", "chases",
 284  		"get", "got", "gotten", "gets",
 285  		"make", "made", "makes",
 286  		"take", "took", "taken", "takes",
 287  		"come", "came", "comes",
 288  		"see", "saw", "seen", "sees",
 289  		"know", "knew", "known", "knows",
 290  		"give", "gave", "given", "gives",
 291  		"say", "said", "says",
 292  		"tell", "told", "tells",
 293  		"think", "thought", "thinks",
 294  		"find", "found", "finds",
 295  		"leave", "left", "leaves",
 296  		"call", "calls",
 297  		"ask", "asks",
 298  		"seem", "seems",
 299  		"feel", "felt", "feels",
 300  		"become", "became", "becomes",
 301  		"keep", "kept", "keeps",
 302  		"begin", "began", "begun", "begins",
 303  		"show", "shows",
 304  		"hear", "heard", "hears",
 305  		"play", "plays",
 306  		"move", "moves",
 307  		"believe", "believes",
 308  		"hold", "held", "holds",
 309  		"bring", "brought", "brings",
 310  		"happen", "happens",
 311  		"wrote",
 312  		"provide", "provides",
 313  		"sat",
 314  		"stood",
 315  		"lose", "lost", "loses",
 316  		"pay", "paid", "pays",
 317  		"meet", "met", "meets",
 318  		"include", "includes",
 319  		"continue", "continues",
 320  		"learn", "learns",
 321  		"change", "changes",
 322  		"lead", "led", "leads",
 323  		"understand", "understood",
 324  		"watch", "watches",
 325  		"follow", "follows",
 326  		"stop", "stops",
 327  		"create", "creates",
 328  		"speak", "spoke", "speaks",
 329  		"allow", "allows",
 330  		"add", "adds",
 331  		"spend", "spent", "spends",
 332  		"grow", "grew", "grows",
 333  		"open", "opens",
 334  		"win", "won", "wins",
 335  		"teach", "taught",
 336  		"offer", "offers",
 337  		"remember", "remembers",
 338  		"love", "loves",
 339  		"consider", "considers",
 340  		"appear", "appears",
 341  		"buy", "bought", "buys",
 342  		"wait", "waits",
 343  		"serve", "serves",
 344  		"died",
 345  		"send", "sent", "sends",
 346  		"expect", "expects",
 347  		"build", "built", "builds",
 348  		"stay", "stays",
 349  		"fall", "fell", "falls",
 350  		"cut", "cuts",
 351  		"reach", "reaches",
 352  		"kill", "kills",
 353  		"remain", "remains",
 354  		"suggest", "suggests",
 355  		"raise", "raises",
 356  		"pass", "passes",
 357  		"sell", "sold", "sells",
 358  		"require", "requires",
 359  		"report", "reports",
 360  		"decide", "decides",
 361  		"develop", "develops",
 362  		"use", "uses", "put", "puts", "set", "sets", "run", "runs",
 363  		"let", "lets", "try", "tries", "need", "needs", "want", "wants",
 364  		"start", "starts",
 365  		"help", "helps",
 366  		"turn", "turns",
 367  		"work", "works",
 368  		"like", "likes",
 369  		"look", "looks",
 370  		"mean", "means", "meant",
 371  		"can", "could", "will", "would", "shall", "should", "may", "might", "must":
 372  		return true
 373  	}
 374  	return false
 375  }
 376  
 377  // HasSuffixEN reports whether s ends in suffix.
 378  func HasSuffixEN(s, suffix string) bool {
 379  	if len(s) < len(suffix) {
 380  		return false
 381  	}
 382  	return s[len(s)-len(suffix):] == suffix
 383  }
 384  
 385  // buildDeepPat is the canonical role sequence: Topic normalised to Subject,
 386  // sorted ascending by insertion.
 387  func buildDeepPat(roles []int32) []uint8 {
 388  	if len(roles) == 0 {
 389  		return nil
 390  	}
 391  	dp := make([]uint8, len(roles))
 392  	for i, r := range roles {
 393  		nr := r
 394  		if nr == HistTopic {
 395  			nr = HistSubject
 396  		}
 397  		dp[i] = uint8(nr)
 398  	}
 399  	for i := 1; i < len(dp); i++ {
 400  		key := dp[i]
 401  		j := i - 1
 402  		for j >= 0 && dp[j] > key {
 403  			dp[j+1] = dp[j]
 404  			j--
 405  		}
 406  		dp[j+1] = key
 407  	}
 408  	return dp
 409  }
 410  
 411  // LemmatizeEN reduces an EN surface form to its lemma and morph state. Port of
 412  // lemma_en.mx LemmatizeEN: the irregular table first, then -ing, -ed and the
 413  // 3rd-singular/plural -s rules. The phrasal-verb registry is not ported; an
 414  // unknown word comes back lowercased with zero morph, as the source's default
 415  // branch does.
 416  func LemmatizeEN(word string) LemmaResult {
 417  	if r, ok := enIrregular[word]; ok {
 418  		return r
 419  	}
 420  	return enLemmatizeRegular(word)
 421  }
 422  
 423  // enLemmatizeRegular is LemmatizeEN past the irregular table.
 424  func enLemmatizeRegular(word string) LemmaResult {
 425  	if word == UntranslatedMarker {
 426  		return LemmaResult{Lemma: word, Morph: 0}
 427  	}
 428  	low := strings.ToLower(word)
 429  
 430  	if r, ok := enIrregular[low]; ok {
 431  		return r
 432  	}
 433  
 434  	if len(low) > 4 && HasSuffixEN(low, "ing") {
 435  		stem := stripIng(low)
 436  		if stem != "" {
 437  			return LemmaResult{Lemma: stem, Morph: MetaAspectProg}
 438  		}
 439  	}
 440  
 441  	if len(low) > 3 && HasSuffixEN(low, "ed") {
 442  		stem := stripEd(low)
 443  		if stem != "" {
 444  			return LemmaResult{Lemma: stem, Morph: MetaTensePast}
 445  		}
 446  	}
 447  
 448  	if len(low) > 3 && HasSuffixEN(low, "ies") {
 449  		return LemmaResult{Lemma: low[:len(low)-3] + "y", Morph: Meta3Sg}
 450  	}
 451  	if len(low) > 3 && HasSuffixEN(low, "es") {
 452  		base := low[:len(low)-2]
 453  		last := base[len(base)-1]
 454  		if last == 's' || last == 'x' || last == 'z' || HasSuffixEN(base, "sh") || HasSuffixEN(base, "ch") {
 455  			return LemmaResult{Lemma: base, Morph: Meta3Sg}
 456  		}
 457  		return LemmaResult{Lemma: low[:len(low)-1], Morph: Meta3Sg}
 458  	}
 459  	// -s is plural here, never 3sg: the 3sg path is the -es rule above.
 460  	if len(low) > 2 && low[len(low)-1] == 's' && low[len(low)-2] != 's' &&
 461  		low[len(low)-2] != '\'' {
 462  		c := low[len(low)-2]
 463  		if c != 'u' && c != 'i' {
 464  			return LemmaResult{Lemma: low[:len(low)-1], Morph: MetaNumPlural}
 465  		}
 466  	}
 467  
 468  	return LemmaResult{Lemma: low, Morph: 0}
 469  }
 470  
 471  func stripIng(word string) string {
 472  	base := word[:len(word)-3]
 473  	if len(base) < 3 {
 474  		return word
 475  	}
 476  	last := base[len(base)-1]
 477  
 478  	if len(base) > 2 && base[len(base)-1] == base[len(base)-2] && isConsonant(last) {
 479  		return base[:len(base)-1]
 480  	}
 481  
 482  	if !isConsonant(last) || last == 'e' {
 483  		return base
 484  	}
 485  
 486  	if last == 'y' {
 487  		return base
 488  	}
 489  
 490  	if last == 'c' || last == 'v' || last == 'z' {
 491  		return base + "e"
 492  	}
 493  
 494  	if len(base) >= 2 && isConsonant(base[len(base)-2]) {
 495  		return base
 496  	}
 497  
 498  	if len(base) >= 3 && !isConsonant(base[len(base)-2]) && !isConsonant(base[len(base)-3]) {
 499  		return base
 500  	}
 501  
 502  	return base + "e"
 503  }
 504  
 505  func stripEd(word string) string {
 506  	base := word[:len(word)-2]
 507  	last := base[len(base)-1]
 508  
 509  	if last == 'i' && len(base) > 1 {
 510  		return base[:len(base)-1] + "y"
 511  	}
 512  
 513  	if len(base) > 2 && base[len(base)-1] == base[len(base)-2] && isConsonant(last) {
 514  		// Vowel before the double means the double was added by -ed
 515  		// (stopped); a consonant means it is natural (guessed, passed).
 516  		if len(base) > 2 && !isConsonant(base[len(base)-3]) {
 517  			return base[:len(base)-1]
 518  		}
 519  		return base
 520  	}
 521  
 522  	if last == 'e' {
 523  		return base
 524  	}
 525  
 526  	if !isConsonant(last) {
 527  		return base
 528  	}
 529  
 530  	if last == 'c' || last == 'v' || last == 'z' {
 531  		return base + "e"
 532  	}
 533  
 534  	if len(base) >= 2 && isConsonant(base[len(base)-2]) {
 535  		return base
 536  	}
 537  
 538  	if len(base) >= 3 && !isConsonant(base[len(base)-2]) && !isConsonant(base[len(base)-3]) {
 539  		return base
 540  	}
 541  
 542  	return base + "e"
 543  }
 544  
 545  func isConsonant(c byte) bool {
 546  	switch c {
 547  	case 'a', 'e', 'i', 'o', 'u':
 548  		return false
 549  	}
 550  	return c >= 'a' && c <= 'z'
 551  }
 552  
 553  // enIrregular is lemma_en.mx enIrregular. "men" and "women" are plural nouns
 554  // here, not forms of "man"/"woman" verbs.
 555  var enIrregular = map[string]LemmaResult{
 556  	// be
 557  	"am":    {Lemma: "be"},
 558  	"is":    {Lemma: "be", Morph: Meta3Sg},
 559  	"are":   {Lemma: "be"},
 560  	"was":   {Lemma: "be", Morph: MetaTensePast | Meta3Sg},
 561  	"were":  {Lemma: "be", Morph: MetaTensePast},
 562  	"been":  {Lemma: "be", Morph: MetaTensePast | MetaAspectProg},
 563  	"being": {Lemma: "be", Morph: MetaAspectProg},
 564  	// have
 565  	"has":    {Lemma: "have", Morph: Meta3Sg},
 566  	"had":    {Lemma: "have", Morph: MetaTensePast},
 567  	"having": {Lemma: "have", Morph: MetaAspectProg},
 568  	// do
 569  	"does": {Lemma: "do", Morph: Meta3Sg},
 570  	"did":  {Lemma: "do", Morph: MetaTensePast},
 571  	"done": {Lemma: "do", Morph: MetaTensePast},
 572  	// go
 573  	"went": {Lemma: "go", Morph: MetaTensePast},
 574  	"gone": {Lemma: "go", Morph: MetaTensePast},
 575  	"goes": {Lemma: "go", Morph: Meta3Sg},
 576  	// get
 577  	"got":    {Lemma: "get", Morph: MetaTensePast},
 578  	"gotten": {Lemma: "get", Morph: MetaTensePast},
 579  	// make
 580  	"made": {Lemma: "make", Morph: MetaTensePast},
 581  	// take
 582  	"took":  {Lemma: "take", Morph: MetaTensePast},
 583  	"taken": {Lemma: "take", Morph: MetaTensePast},
 584  	// come
 585  	"came": {Lemma: "come", Morph: MetaTensePast},
 586  	// see
 587  	"saw":  {Lemma: "see", Morph: MetaTensePast},
 588  	"seen": {Lemma: "see", Morph: MetaTensePast},
 589  	// know
 590  	"knew":  {Lemma: "know", Morph: MetaTensePast},
 591  	"known": {Lemma: "know", Morph: MetaTensePast},
 592  	// give
 593  	"gave":  {Lemma: "give", Morph: MetaTensePast},
 594  	"given": {Lemma: "give", Morph: MetaTensePast},
 595  	// say
 596  	"said": {Lemma: "say", Morph: MetaTensePast},
 597  	// tell
 598  	"told": {Lemma: "tell", Morph: MetaTensePast},
 599  	// think
 600  	"thought": {Lemma: "think", Morph: MetaTensePast},
 601  	// find
 602  	"found": {Lemma: "find", Morph: MetaTensePast},
 603  	// leave
 604  	"left": {Lemma: "leave", Morph: MetaTensePast},
 605  	// feel
 606  	"felt": {Lemma: "feel", Morph: MetaTensePast},
 607  	// become
 608  	"became": {Lemma: "become", Morph: MetaTensePast},
 609  	// keep
 610  	"kept": {Lemma: "keep", Morph: MetaTensePast},
 611  	// begin
 612  	"began": {Lemma: "begin", Morph: MetaTensePast},
 613  	"begun": {Lemma: "begin", Morph: MetaTensePast},
 614  	// hear
 615  	"heard": {Lemma: "hear", Morph: MetaTensePast},
 616  	// hold
 617  	"held": {Lemma: "hold", Morph: MetaTensePast},
 618  	// bring
 619  	"brought": {Lemma: "bring", Morph: MetaTensePast},
 620  	// write
 621  	"wrote":   {Lemma: "write", Morph: MetaTensePast},
 622  	"written": {Lemma: "write", Morph: MetaTensePast},
 623  	// sit
 624  	"sat": {Lemma: "sit", Morph: MetaTensePast},
 625  	// stand
 626  	"stood": {Lemma: "stand", Morph: MetaTensePast},
 627  	// lose
 628  	"lost": {Lemma: "lose", Morph: MetaTensePast},
 629  	// pay
 630  	"paid": {Lemma: "pay", Morph: MetaTensePast},
 631  	// meet
 632  	"met": {Lemma: "meet", Morph: MetaTensePast},
 633  	// lead
 634  	"led": {Lemma: "lead", Morph: MetaTensePast},
 635  	// understand
 636  	"understood": {Lemma: "understand", Morph: MetaTensePast},
 637  	// speak
 638  	"spoke":  {Lemma: "speak", Morph: MetaTensePast},
 639  	"spoken": {Lemma: "speak", Morph: MetaTensePast},
 640  	// grow
 641  	"grew":  {Lemma: "grow", Morph: MetaTensePast},
 642  	"grown": {Lemma: "grow", Morph: MetaTensePast},
 643  	// win
 644  	"won": {Lemma: "win", Morph: MetaTensePast},
 645  	// teach
 646  	"taught": {Lemma: "teach", Morph: MetaTensePast},
 647  	// buy
 648  	"bought": {Lemma: "buy", Morph: MetaTensePast},
 649  	// send
 650  	"sent": {Lemma: "send", Morph: MetaTensePast},
 651  	// build
 652  	"built": {Lemma: "build", Morph: MetaTensePast},
 653  	// fall
 654  	"fell":   {Lemma: "fall", Morph: MetaTensePast},
 655  	"fallen": {Lemma: "fall", Morph: MetaTensePast},
 656  	// sell
 657  	"sold": {Lemma: "sell", Morph: MetaTensePast},
 658  	// run
 659  	"ran": {Lemma: "run", Morph: MetaTensePast},
 660  	// mean
 661  	"meant": {Lemma: "mean", Morph: MetaTensePast},
 662  	// spend
 663  	"spent": {Lemma: "spend", Morph: MetaTensePast},
 664  	// catch
 665  	"caught": {Lemma: "catch", Morph: MetaTensePast},
 666  	// fly
 667  	"flew":  {Lemma: "fly", Morph: MetaTensePast},
 668  	"flown": {Lemma: "fly", Morph: MetaTensePast},
 669  	// eat
 670  	"ate":   {Lemma: "eat", Morph: MetaTensePast},
 671  	"eaten": {Lemma: "eat", Morph: MetaTensePast},
 672  	// bite: the participle carries MetaPassive, the simple past does not.
 673  	"bit":    {Lemma: "bite", Morph: MetaTensePast},
 674  	"bitten": {Lemma: "bite", Morph: MetaTensePast | MetaPassive},
 675  	// drink
 676  	"drank": {Lemma: "drink", Morph: MetaTensePast},
 677  	"drunk": {Lemma: "drink", Morph: MetaTensePast},
 678  	// drive
 679  	"drove":  {Lemma: "drive", Morph: MetaTensePast},
 680  	"driven": {Lemma: "drive", Morph: MetaTensePast},
 681  	// sing
 682  	"sang": {Lemma: "sing", Morph: MetaTensePast},
 683  	"sung": {Lemma: "sing", Morph: MetaTensePast},
 684  	// swim
 685  	"swam": {Lemma: "swim", Morph: MetaTensePast},
 686  	"swum": {Lemma: "swim", Morph: MetaTensePast},
 687  	// break
 688  	"broke":  {Lemma: "break", Morph: MetaTensePast},
 689  	"broken": {Lemma: "break", Morph: MetaTensePast},
 690  	// choose
 691  	"chose":  {Lemma: "choose", Morph: MetaTensePast},
 692  	"chosen": {Lemma: "choose", Morph: MetaTensePast},
 693  	// draw
 694  	"drew":  {Lemma: "draw", Morph: MetaTensePast},
 695  	"drawn": {Lemma: "draw", Morph: MetaTensePast},
 696  	// forget
 697  	"forgot":    {Lemma: "forget", Morph: MetaTensePast},
 698  	"forgotten": {Lemma: "forget", Morph: MetaTensePast},
 699  	// freeze
 700  	"froze":  {Lemma: "freeze", Morph: MetaTensePast},
 701  	"frozen": {Lemma: "freeze", Morph: MetaTensePast},
 702  	// hide
 703  	"hid":    {Lemma: "hide", Morph: MetaTensePast},
 704  	"hidden": {Lemma: "hide", Morph: MetaTensePast},
 705  	// lie (recline)
 706  	"lay":  {Lemma: "lie", Morph: MetaTensePast},
 707  	"lain": {Lemma: "lie", Morph: MetaTensePast},
 708  	// ride
 709  	"rode":   {Lemma: "ride", Morph: MetaTensePast},
 710  	"ridden": {Lemma: "ride", Morph: MetaTensePast},
 711  	// rise
 712  	"rose":  {Lemma: "rise", Morph: MetaTensePast},
 713  	"risen": {Lemma: "rise", Morph: MetaTensePast},
 714  	// shake
 715  	"shook":  {Lemma: "shake", Morph: MetaTensePast},
 716  	"shaken": {Lemma: "shake", Morph: MetaTensePast},
 717  	// steal
 718  	"stole":  {Lemma: "steal", Morph: MetaTensePast},
 719  	"stolen": {Lemma: "steal", Morph: MetaTensePast},
 720  	// tear
 721  	"tore": {Lemma: "tear", Morph: MetaTensePast},
 722  	"torn": {Lemma: "tear", Morph: MetaTensePast},
 723  	// throw
 724  	"threw":  {Lemma: "throw", Morph: MetaTensePast},
 725  	"thrown": {Lemma: "throw", Morph: MetaTensePast},
 726  	// wake
 727  	"woke":  {Lemma: "wake", Morph: MetaTensePast},
 728  	"woken": {Lemma: "wake", Morph: MetaTensePast},
 729  	// wear
 730  	"wore": {Lemma: "wear", Morph: MetaTensePast},
 731  	"worn": {Lemma: "wear", Morph: MetaTensePast},
 732  	// irregular nouns
 733  	"children": {Lemma: "child", Morph: MetaNumPlural},
 734  	"men":      {Lemma: "man", Morph: MetaNumPlural},
 735  	"women":    {Lemma: "woman", Morph: MetaNumPlural},
 736  	"people":   {Lemma: "person", Morph: MetaNumPlural},
 737  	"mice":     {Lemma: "mouse", Morph: MetaNumPlural},
 738  	"feet":     {Lemma: "foot", Morph: MetaNumPlural},
 739  	"teeth":    {Lemma: "tooth", Morph: MetaNumPlural},
 740  	"geese":    {Lemma: "goose", Morph: MetaNumPlural},
 741  	"lives":    {Lemma: "life", Morph: MetaNumPlural},
 742  	"wives":    {Lemma: "wife", Morph: MetaNumPlural},
 743  	"knives":   {Lemma: "knife", Morph: MetaNumPlural},
 744  	"wolves":   {Lemma: "wolf", Morph: MetaNumPlural},
 745  	"halves":   {Lemma: "half", Morph: MetaNumPlural},
 746  	"leaves":   {Lemma: "leaf", Morph: MetaNumPlural},
 747  	// modals/negative contractions
 748  	"can't":     {Lemma: "can", Morph: MetaPolarNeg},
 749  	"won't":     {Lemma: "will", Morph: MetaPolarNeg},
 750  	"don't":     {Lemma: "do", Morph: MetaPolarNeg},
 751  	"doesn't":   {Lemma: "do", Morph: MetaPolarNeg | Meta3Sg},
 752  	"didn't":    {Lemma: "do", Morph: MetaTensePast | MetaPolarNeg},
 753  	"isn't":     {Lemma: "be", Morph: MetaPolarNeg | Meta3Sg},
 754  	"aren't":    {Lemma: "be", Morph: MetaPolarNeg},
 755  	"wasn't":    {Lemma: "be", Morph: MetaTensePast | MetaPolarNeg},
 756  	"weren't":   {Lemma: "be", Morph: MetaTensePast | MetaPolarNeg},
 757  	"haven't":   {Lemma: "have", Morph: MetaPolarNeg},
 758  	"hasn't":    {Lemma: "have", Morph: MetaPolarNeg | Meta3Sg},
 759  	"hadn't":    {Lemma: "have", Morph: MetaTensePast | MetaPolarNeg},
 760  	"couldn't":  {Lemma: "can", Morph: MetaTensePast | MetaPolarNeg},
 761  	"wouldn't":  {Lemma: "will", Morph: MetaTensePast | MetaPolarNeg},
 762  	"shouldn't": {Lemma: "shall", Morph: MetaTensePast | MetaPolarNeg},
 763  
 764  	// CVC-rule overrides: multi-syllable verbs whose -ed/-ing forms get a
 765  	// spurious -e from the CVC heuristic.
 766  	"happened":     {Lemma: "happen", Morph: MetaTensePast},
 767  	"happening":    {Lemma: "happen", Morph: MetaAspectProg},
 768  	"opened":       {Lemma: "open", Morph: MetaTensePast},
 769  	"opening":      {Lemma: "open", Morph: MetaAspectProg},
 770  	"listened":     {Lemma: "listen", Morph: MetaTensePast},
 771  	"listening":    {Lemma: "listen", Morph: MetaAspectProg},
 772  	"offered":      {Lemma: "offer", Morph: MetaTensePast},
 773  	"offering":     {Lemma: "offer", Morph: MetaAspectProg},
 774  	"differed":     {Lemma: "differ", Morph: MetaTensePast},
 775  	"suffered":     {Lemma: "suffer", Morph: MetaTensePast},
 776  	"suffering":    {Lemma: "suffer", Morph: MetaAspectProg},
 777  	"wandered":     {Lemma: "wander", Morph: MetaTensePast},
 778  	"wandering":    {Lemma: "wander", Morph: MetaAspectProg},
 779  	"wondered":     {Lemma: "wonder", Morph: MetaTensePast},
 780  	"wondering":    {Lemma: "wonder", Morph: MetaAspectProg},
 781  	"ordered":      {Lemma: "order", Morph: MetaTensePast},
 782  	"ordering":     {Lemma: "order", Morph: MetaAspectProg},
 783  	"murdered":     {Lemma: "murder", Morph: MetaTensePast},
 784  	"answered":     {Lemma: "answer", Morph: MetaTensePast},
 785  	"answering":    {Lemma: "answer", Morph: MetaAspectProg},
 786  	"entered":      {Lemma: "enter", Morph: MetaTensePast},
 787  	"entering":     {Lemma: "enter", Morph: MetaAspectProg},
 788  	"remembered":   {Lemma: "remember", Morph: MetaTensePast},
 789  	"remembering":  {Lemma: "remember", Morph: MetaAspectProg},
 790  	"considered":   {Lemma: "consider", Morph: MetaTensePast},
 791  	"considering":  {Lemma: "consider", Morph: MetaAspectProg},
 792  	"discovered":   {Lemma: "discover", Morph: MetaTensePast},
 793  	"discovering":  {Lemma: "discover", Morph: MetaAspectProg},
 794  	"delivered":    {Lemma: "deliver", Morph: MetaTensePast},
 795  	"developed":    {Lemma: "develop", Morph: MetaTensePast},
 796  	"developing":   {Lemma: "develop", Morph: MetaAspectProg},
 797  	"gathered":     {Lemma: "gather", Morph: MetaTensePast},
 798  	"gathering":    {Lemma: "gather", Morph: MetaAspectProg},
 799  	"threatened":   {Lemma: "threaten", Morph: MetaTensePast},
 800  	"threatening":  {Lemma: "threaten", Morph: MetaAspectProg},
 801  	"strengthened": {Lemma: "strengthen", Morph: MetaTensePast},
 802  	"loosened":     {Lemma: "loosen", Morph: MetaTensePast},
 803  	"sharpened":    {Lemma: "sharpen", Morph: MetaTensePast},
 804  	"flattened":    {Lemma: "flatten", Morph: MetaTensePast},
 805  	"softened":     {Lemma: "soften", Morph: MetaTensePast},
 806  	"modeled":      {Lemma: "model", Morph: MetaTensePast},
 807  	"modeling":     {Lemma: "model", Morph: MetaAspectProg},
 808  	"traveled":     {Lemma: "travel", Morph: MetaTensePast},
 809  	"traveling":    {Lemma: "travel", Morph: MetaAspectProg},
 810  	"cancelled":    {Lemma: "cancel", Morph: MetaTensePast},
 811  	"canceling":    {Lemma: "cancel", Morph: MetaAspectProg},
 812  
 813  	// Pronoun objective case collapses to nominative. Possessives stay out:
 814  	// they are modifiers and distinct atoms from the subject pronoun.
 815  	"me":         {Lemma: "i"},
 816  	"myself":     {Lemma: "i"},
 817  	"him":        {Lemma: "he"},
 818  	"himself":    {Lemma: "he"},
 819  	"herself":    {Lemma: "she"},
 820  	"us":         {Lemma: "we"},
 821  	"ourselves":  {Lemma: "we"},
 822  	"them":       {Lemma: "they"},
 823  	"themselves": {Lemma: "they"},
 824  	"yourself":   {Lemma: "you"},
 825  	"itself":     {Lemma: "it"},
 826  	// Pronoun contractions collapse to the pronoun; the auxiliary is
 827  	// structural and its morph rides on the clause's main verb.
 828  	"i'm":     {Lemma: "i"},
 829  	"i've":    {Lemma: "i"},
 830  	"i'll":    {Lemma: "i"},
 831  	"i'd":     {Lemma: "i"},
 832  	"it's":    {Lemma: "it"},
 833  	"he's":    {Lemma: "he"},
 834  	"she's":   {Lemma: "she"},
 835  	"we're":   {Lemma: "we"},
 836  	"we've":   {Lemma: "we"},
 837  	"we'll":   {Lemma: "we"},
 838  	"they're": {Lemma: "they"},
 839  	"they've": {Lemma: "they"},
 840  	"they'll": {Lemma: "they"},
 841  	"you're":  {Lemma: "you"},
 842  	"you've":  {Lemma: "you"},
 843  	"you'll":  {Lemma: "you"},
 844  	"let's":   {Lemma: "let", Morph: MetaMoodVol},
 845  	"that's":  {Lemma: "that"},
 846  	"there's": {Lemma: "there"},
 847  	"what's":  {Lemma: "what"},
 848  	"who's":   {Lemma: "who"},
 849  	"here's":  {Lemma: "here"},
 850  
 851  	// -ing irregulars: ie->y before -ing, and digraph plus silent-e.
 852  	"dying":    {Lemma: "die", Morph: MetaAspectProg},
 853  	"lying":    {Lemma: "lie", Morph: MetaAspectProg},
 854  	"tying":    {Lemma: "tie", Morph: MetaAspectProg},
 855  	"choosing": {Lemma: "choose", Morph: MetaAspectProg},
 856  	"going":    {Lemma: "go", Morph: MetaAspectProg},
 857  	"doing":    {Lemma: "do", Morph: MetaAspectProg},
 858  	// Informal contractions
 859  	"gonna": {Lemma: "go", Morph: MetaMoodVol},
 860  	"wanna": {Lemma: "want", Morph: MetaMoodVol},
 861  	"gotta": {Lemma: "get", Morph: MetaMoodVol},
 862  	"kinda": {Lemma: "kind"},
 863  	"sorta": {Lemma: "sort"},
 864  	"lemme": {Lemma: "let"},
 865  	"gimme": {Lemma: "give"},
 866  	// Words that naturally end in -ing
 867  	"thing":      {Lemma: "thing"},
 868  	"nothing":    {Lemma: "nothing"},
 869  	"something":  {Lemma: "something"},
 870  	"anything":   {Lemma: "anything"},
 871  	"everything": {Lemma: "everything"},
 872  	"morning":    {Lemma: "morning"},
 873  	"evening":    {Lemma: "evening"},
 874  	"spring":     {Lemma: "spring"},
 875  	"string":     {Lemma: "string"},
 876  	"during":     {Lemma: "during"},
 877  	"ceiling":    {Lemma: "ceiling"},
 878  	"king":       {Lemma: "king"},
 879  	// Adverbs ending in -s, not plurals
 880  	"always":     {Lemma: "always"},
 881  	"sometimes":  {Lemma: "sometimes"},
 882  	"perhaps":    {Lemma: "perhaps"},
 883  	"thus":       {Lemma: "thus"},
 884  	"besides":    {Lemma: "besides"},
 885  	"nowadays":   {Lemma: "nowadays"},
 886  	"towards":    {Lemma: "towards"},
 887  	"upstairs":   {Lemma: "upstairs"},
 888  	"downstairs": {Lemma: "downstairs"},
 889  	"outdoors":   {Lemma: "outdoors"},
 890  	// -ed words with natural double consonants
 891  	"guessed":  {Lemma: "guess", Morph: MetaTensePast},
 892  	"passed":   {Lemma: "pass", Morph: MetaTensePast},
 893  	"missed":   {Lemma: "miss", Morph: MetaTensePast},
 894  	"kissed":   {Lemma: "kiss", Morph: MetaTensePast},
 895  	"crossed":  {Lemma: "cross", Morph: MetaTensePast},
 896  	"dressed":  {Lemma: "dress", Morph: MetaTensePast},
 897  	"pressed":  {Lemma: "press", Morph: MetaTensePast},
 898  	"stressed": {Lemma: "stress", Morph: MetaTensePast},
 899  	// -ed words needing silent-e restoration
 900  	"phrased":   {Lemma: "phrase", Morph: MetaTensePast},
 901  	"increased": {Lemma: "increase", Morph: MetaTensePast},
 902  	"decreased": {Lemma: "decrease", Morph: MetaTensePast},
 903  	"pleased":   {Lemma: "please", Morph: MetaTensePast},
 904  	"released":  {Lemma: "release", Morph: MetaTensePast},
 905  	"surprised": {Lemma: "surprise", Morph: MetaTensePast},
 906  
 907  	// Comparatives map to base plus MetaCompare.
 908  	"bigger":   {Lemma: "big", Morph: MetaCompare},
 909  	"smaller":  {Lemma: "small", Morph: MetaCompare},
 910  	"taller":   {Lemma: "tall", Morph: MetaCompare},
 911  	"shorter":  {Lemma: "short", Morph: MetaCompare},
 912  	"longer":   {Lemma: "long", Morph: MetaCompare},
 913  	"wider":    {Lemma: "wide", Morph: MetaCompare},
 914  	"thicker":  {Lemma: "thick", Morph: MetaCompare},
 915  	"thinner":  {Lemma: "thin", Morph: MetaCompare},
 916  	"higher":   {Lemma: "high", Morph: MetaCompare},
 917  	"lower":    {Lemma: "low", Morph: MetaCompare},
 918  	"hotter":   {Lemma: "hot", Morph: MetaCompare},
 919  	"colder":   {Lemma: "cold", Morph: MetaCompare},
 920  	"warmer":   {Lemma: "warm", Morph: MetaCompare},
 921  	"cooler":   {Lemma: "cool", Morph: MetaCompare},
 922  	"faster":   {Lemma: "fast", Morph: MetaCompare},
 923  	"slower":   {Lemma: "slow", Morph: MetaCompare},
 924  	"older":    {Lemma: "old", Morph: MetaCompare},
 925  	"younger":  {Lemma: "young", Morph: MetaCompare},
 926  	"happier":  {Lemma: "happy", Morph: MetaCompare},
 927  	"sadder":   {Lemma: "sad", Morph: MetaCompare},
 928  	"easier":   {Lemma: "easy", Morph: MetaCompare},
 929  	"harder":   {Lemma: "hard", Morph: MetaCompare},
 930  	"larger":   {Lemma: "large", Morph: MetaCompare},
 931  	"better":   {Lemma: "good", Morph: MetaCompare},
 932  	"worse":    {Lemma: "bad", Morph: MetaCompare},
 933  	"stronger": {Lemma: "strong", Morph: MetaCompare},
 934  	"weaker":   {Lemma: "weak", Morph: MetaCompare},
 935  	"heavier":  {Lemma: "heavy", Morph: MetaCompare},
 936  	"lighter":  {Lemma: "light", Morph: MetaCompare},
 937  	"cheaper":  {Lemma: "cheap", Morph: MetaCompare},
 938  	"richer":   {Lemma: "rich", Morph: MetaCompare},
 939  	"poorer":   {Lemma: "poor", Morph: MetaCompare},
 940  	"nicer":    {Lemma: "nice", Morph: MetaCompare},
 941  	"newer":    {Lemma: "new", Morph: MetaCompare},
 942  }
 943  
 944  // ExtractEN takes EN tokens and produces pattern + slots. Determin ers (the/a)
 945  // are morph hints, prepositions are role markers, auxiliaries stage copula
 946  // info, pronouns are content nouns.
 947  func ExtractEN(tokens []string) ExtractResult {
 948  	var pat []byte
 949  	var slots []string
 950  	var roles []int32
 951  	var slotMorphs []uint16
 952  	var slotMarks []uint8
 953  	var slotOblRoles []uint8
 954  	var slotHeads []int16
 955  	var slotModKinds []uint8
 956  
 957  	sawVerb := false
 958  	contentCount := 0
 959  	pendingRole := int32(HistSubject)
 960  	pendingDef := false
 961  	pendingNeg := false
 962  	pendingMark := uint8(0)
 963  	pendingOblRole := uint8(ORNone)
 964  	pendingHead := int16(-1)
 965  	pendingModKind := uint8(MKNone)
 966  	pendingCop := false
 967  	pendingCopHead := int16(-1)
 968  	pendingCopMorph := uint16(0)
 969  	pendingCopAuxIsBe := false
 970  	pendingVol := false
 971  	pendingCoordHead := int16(-1)
 972  	pendingRelHead := int16(-1)
 973  	pendingRecipCand := false
 974  	pendingRecipIdx := int16(-1)
 975  	pendingCausative := false
 976  	pendingCausativeMorph := uint16(0)
 977  
 978  	resetPending := func() {
 979  		pendingRole = HistSubject
 980  		pendingDef = false
 981  		pendingNeg = false
 982  		pendingMark = 0
 983  		pendingOblRole = ORNone
 984  		pendingHead = -1
 985  		pendingModKind = MKNone
 986  		pendingCop = false
 987  		pendingCopHead = -1
 988  		pendingCopMorph = 0
 989  		pendingCopAuxIsBe = false
 990  		pendingVol = false
 991  		pendingCoordHead = -1
 992  		pendingRelHead = -1
 993  		pendingRecipCand = false
 994  		pendingRecipIdx = -1
 995  		pendingCausative = false
 996  		pendingCausativeMorph = 0
 997  	}
 998  
 999  	var clauses []Clause
1000  	clauseRel := uint8(ClauseRoot)
1001  	clauseParent := int16(-1)
1002  
1003  	for tokIdx, tok := range tokens {
1004  		// Clause boundary from EN punctuation classification. Finalize the
1005  		// current clause as a ClauseAnd peer, then reset per-slot state.
1006  		if tok == ClauseSplitToken {
1007  			if len(slots) > 0 {
1008  				clauseSet := buildSetFromSlices(
1009  					slots, roles, slotMorphs,
1010  					slotMarks, slotOblRoles, slotHeads, slotModKinds,
1011  				)
1012  				clauseRelLocal := clauseRel
1013  				if clauseRelLocal == ClauseRoot && len(clauses) > 0 {
1014  					clauseRelLocal = ClauseAnd
1015  				}
1016  				nextParent := int16(len(clauses))
1017  				clauses = append(clauses, Clause{
1018  					Set: clauseSet, Relation: clauseRelLocal,
1019  					Parent: clauseParent, HostIdx: -1,
1020  				})
1021  				slots = nil
1022  				roles = nil
1023  				slotMorphs = nil
1024  				slotMarks = nil
1025  				slotOblRoles = nil
1026  				slotHeads = nil
1027  				slotModKinds = nil
1028  				sawVerb = false
1029  				contentCount = 0
1030  				resetPending()
1031  				clauseRel = ClauseAnd
1032  				clauseParent = nextParent - 1
1033  			}
1034  			continue
1035  		}
1036  		low := strings.ToLower(tok)
1037  		mk, isMk := enWordToMarker()[low]
1038  		if isMk {
1039  			pat = append(pat, mk)
1040  			switch enMarkerClass(mk) {
1041  			case enMarkDeterminer:
1042  				// In a subordinate clause that already has a verb, a new
1043  				// determiner starts the main clause: "if it rains the cat
1044  				// runs" - "the" begins the main clause.
1045  				if sawVerb && (clauseRel == ClauseIf || clauseRel == ClauseBecause) {
1046  					clauseSet := buildSetFromSlices(
1047  						slots, roles, slotMorphs,
1048  						slotMarks, slotOblRoles, slotHeads, slotModKinds,
1049  					)
1050  					nextParent := int16(len(clauses))
1051  					clauses = append(clauses, Clause{
1052  						Set: clauseSet, Relation: clauseRel,
1053  						Parent: nextParent, HostIdx: -1,
1054  					})
1055  					slots = nil
1056  					roles = nil
1057  					slotMorphs = nil
1058  					slotMarks = nil
1059  					slotOblRoles = nil
1060  					slotHeads = nil
1061  					slotModKinds = nil
1062  					sawVerb = false
1063  					contentCount = 0
1064  					resetPending()
1065  					clauseRel = ClauseRoot
1066  					clauseParent = -1
1067  				}
1068  				if mk == MkThe {
1069  					pendingDef = true
1070  				} // "a" leaves pendingDef false
1071  			case enMarkNegation:
1072  				pendingNeg = true
1073  			case enMarkPronoun:
1074  				contentCount++
1075  				role := pendingRole
1076  				if !sawVerb {
1077  					role = HistSubject
1078  				} else {
1079  					role = pendingRole
1080  				}
1081  				pat[len(pat)-1] = SlotNoun
1082  				pronAtom := tok
1083  				plem := LemmatizeEN(strings.ToLower(tok))
1084  				if plem.Lemma != "" {
1085  					pronAtom = plem.Lemma
1086  				}
1087  				slots = append(slots, pronAtom)
1088  				roles = append(roles, role)
1089  				m := uint16(0)
1090  				if pendingDef {
1091  					m |= MetaDefDef
1092  				}
1093  				slotMorphs = append(slotMorphs, m)
1094  				slotMarks = append(slotMarks, pendingMark)
1095  				slotOblRoles = append(slotOblRoles, ORNone)
1096  				slotHeads = append(slotHeads, -1)
1097  				slotModKinds = append(slotModKinds, MKNone)
1098  				newPronIdx := int16(len(slots) - 1)
1099  				// Ditransitive recipient, mirroring the noun emit branch.
1100  				if pendingRecipCand {
1101  					if pendingRecipIdx < 0 {
1102  						if pendingMark == 0 && pendingOblRole == ORNone {
1103  							pendingRecipIdx = newPronIdx
1104  						} else {
1105  							pendingRecipCand = false
1106  						}
1107  					} else {
1108  						slotOblRoles[pendingRecipIdx] = ORRecip
1109  						roles[pendingRecipIdx] = HistModifier
1110  						pendingRecipCand = false
1111  						pendingRecipIdx = -1
1112  					}
1113  				}
1114  				pendingMark = 0
1115  				pendingDef = false
1116  			case enMarkPossDet:
1117  				// Possessive determiner: emit as a POSS modifier of the next
1118  				// noun. The next slot emission resolves the Head pointer.
1119  				pat[len(pat)-1] = SlotNoun
1120  				slots = append(slots, possDetSurface(mk))
1121  				roles = append(roles, HistOperator)
1122  				slotMorphs = append(slotMorphs, 0)
1123  				slotMarks = append(slotMarks, mk)
1124  				slotOblRoles = append(slotOblRoles, ORNone)
1125  				slotHeads = append(slotHeads, -1)
1126  				slotModKinds = append(slotModKinds, MKNone)
1127  				pendingHead = int16(len(slots) - 1)
1128  				pendingModKind = MKPoss
1129  			case enMarkVerbAux:
1130  				// Auxiliaries emit no verb slot. After a real verb they only
1131  				// carry morph onto it; otherwise they stage copula info for
1132  				// the next noun.
1133  				if sawVerb && len(slotMorphs) > 0 {
1134  					lem := LemmatizeEN(low)
1135  					slotMorphs[len(slotMorphs)-1] |= lem.Morph
1136  				} else {
1137  					subj := findSubjectIdx(roles, slotHeads)
1138  					if subj >= 0 {
1139  						pendingCop = true
1140  						pendingCopHead = subj
1141  						lem := LemmatizeEN(low)
1142  						pendingCopMorph = lem.Morph
1143  						pendingCopAuxIsBe = lem.Lemma == "be"
1144  						if pendingNeg {
1145  							pendingCopMorph |= MetaPolarNeg
1146  							pendingNeg = false
1147  						}
1148  					}
1149  					pendingRole = HistComplement
1150  				}
1151  			case enMarkPreposition:
1152  				pendingRole = MarkerToRole(mk)
1153  				pendingMark = mk
1154  				pendingOblRole = MarkerToOblRole(mk)
1155  			case enMarkConjunction:
1156  				// "and"/"or" is clause coordination after a completed
1157  				// predication and NP coordination before one.
1158  				if hasPredication(roles, slotModKinds) {
1159  					clauseSet := buildSetFromSlices(
1160  						slots, roles, slotMorphs,
1161  						slotMarks, slotOblRoles, slotHeads, slotModKinds,
1162  					)
1163  					clauseRelLocal := clauseRel
1164  					clauseParentLocal := clauseParent
1165  					if clauseRelLocal == ClauseRoot && len(clauses) > 0 {
1166  						clauseRelLocal = ClauseAnd
1167  					}
1168  					nextParent := int16(len(clauses))
1169  					clauses = append(clauses, Clause{
1170  						Set: clauseSet, Relation: clauseRelLocal,
1171  						Parent: clauseParentLocal, HostIdx: -1,
1172  					})
1173  					slots = nil
1174  					roles = nil
1175  					slotMorphs = nil
1176  					slotMarks = nil
1177  					slotOblRoles = nil
1178  					slotHeads = nil
1179  					slotModKinds = nil
1180  					sawVerb = false
1181  					contentCount = 0
1182  					resetPending()
1183  					if mk == MkBut {
1184  						clauseRel = ClauseBut
1185  					} else {
1186  						clauseRel = ClauseAnd
1187  					}
1188  					clauseParent = nextParent - 1
1189  					continue
1190  				}
1191  				if len(slots) > 0 {
1192  					prev := int16(len(slots) - 1)
1193  					if int(prev) < len(slotModKinds) && slotModKinds[prev] == MKCoord {
1194  						prev = slotHeads[prev]
1195  					}
1196  					pendingCoordHead = prev
1197  				} else {
1198  					pendingRole = HistSubject
1199  					sawVerb = false
1200  					contentCount = 0
1201  				}
1202  			case enMarkRelative:
1203  				// Intransitive REL only: if a subject follows ("that I saw"),
1204  				// the verb attaches to that subject and this head does not
1205  				// fire.
1206  				if len(slots) > 0 && roles[len(roles)-1] == HistSubject {
1207  					pendingRelHead = int16(len(slots) - 1)
1208  				}
1209  			}
1210  			continue
1211  		}
1212  		if isENPunct(tok) {
1213  			continue
1214  		}
1215  
1216  		// "let" is volitional only at clause start; elsewhere it is the verb
1217  		// meaning allow. "let's" is unambiguously volitional anywhere,
1218  		// because the renderer emits "we let's go".
1219  		if !sawVerb && low == "let's" {
1220  			pendingVol = true
1221  			continue
1222  		}
1223  		if !sawVerb && contentCount == 0 && low == "let" {
1224  			pendingVol = true
1225  			continue
1226  		}
1227  
1228  		if !sawVerb && contentCount == 0 && len(slots) == 0 {
1229  			switch low {
1230  			case "if":
1231  				clauseRel = ClauseIf
1232  				continue
1233  			case "because":
1234  				clauseRel = ClauseBecause
1235  				continue
1236  			}
1237  		}
1238  
1239  		// Adverbs modify the clause verb. Head resolves backward to the last
1240  		// verb slot, or forward at the next verb emission. A staged copula
1241  		// takes precedence: "is fast" is the adjective, "runs fast" the adverb.
1242  		isAdv := looksLikeAdverb(low)
1243  		if pendingCop && looksLikePredicateAdj(low) {
1244  			isAdv = false
1245  		}
1246  		if isAdv {
1247  			contentCount++
1248  			pat = append(pat, SlotNoun)
1249  			slots = append(slots, low)
1250  			roles = append(roles, HistModifier)
1251  			slotMorphs = append(slotMorphs, 0)
1252  			slotMarks = append(slotMarks, 0)
1253  			slotOblRoles = append(slotOblRoles, ORNone)
1254  			advIdx := int16(len(slots) - 1)
1255  			boundHead := int16(-1)
1256  			for i := int32(advIdx) - 1; i >= 0; i-- {
1257  				if roles[i] == HistVerb {
1258  					boundHead = int16(i)
1259  					break
1260  				}
1261  			}
1262  			if boundHead >= 0 {
1263  				slotHeads = append(slotHeads, boundHead)
1264  				slotModKinds = append(slotModKinds, MKAdv)
1265  			} else {
1266  				slotHeads = append(slotHeads, -1)
1267  				slotModKinds = append(slotModKinds, MKAdv)
1268  			}
1269  			continue
1270  		}
1271  
1272  		// Content word.
1273  		contentCount++
1274  		isVerb := looksLikeVerb(low)
1275  		// Second-verb-no-comma boundary: a verb after a verb with no
1276  		// preposition or conjunction between them starts a new clause.
1277  		// Suppressed while a causative is staged: that verb is the embedded
1278  		// action, not a new clause.
1279  		if sawVerb && isVerb && pendingMark == 0 && pendingOblRole == ORNone &&
1280  			!pendingCausative {
1281  			clauseSet := buildSetFromSlices(
1282  				slots, roles, slotMorphs,
1283  				slotMarks, slotOblRoles, slotHeads, slotModKinds,
1284  			)
1285  			nextParent := int16(len(clauses))
1286  			// A condition or cause clause precedes the main clause, so its
1287  			// Parent points forward to the index the main clause will take.
1288  			parent := clauseParent
1289  			if clauseRel == ClauseIf || clauseRel == ClauseBecause {
1290  				parent = nextParent
1291  			}
1292  			clauses = append(clauses, Clause{
1293  				Set: clauseSet, Relation: clauseRel,
1294  				Parent: parent, HostIdx: -1,
1295  			})
1296  			slots = nil
1297  			roles = nil
1298  			slotMorphs = nil
1299  			slotMarks = nil
1300  			slotOblRoles = nil
1301  			slotHeads = nil
1302  			slotModKinds = nil
1303  			sawVerb = false
1304  			contentCount = 1
1305  			resetPending()
1306  			clauseRel = ClauseRoot
1307  			clauseParent = -1
1308  		}
1309  		// ATTR: current and next are content non-verbs and the token after
1310  		// next is a verb or a marker or end of input, so current modifies
1311  		// next: "red car runs".
1312  		isAttr := false
1313  		if !isVerb && tokIdx+1 < len(tokens) {
1314  			next := strings.ToLower(tokens[tokIdx+1])
1315  			if _, nextIsMk := enWordToMarker()[next]; !nextIsMk && !isENPunct(tokens[tokIdx+1]) && !looksLikeVerb(next) {
1316  				if tokIdx+2 >= len(tokens) {
1317  					isAttr = true
1318  				} else {
1319  					afterNext := strings.ToLower(tokens[tokIdx+2])
1320  					if _, anIsMk := enWordToMarker()[afterNext]; anIsMk {
1321  						isAttr = true
1322  					} else if looksLikeVerb(afterNext) {
1323  						isAttr = true
1324  					}
1325  				}
1326  			}
1327  		}
1328  		// A slot awaiting its head, or a staged copula, or a conjunct makes
1329  		// the current word a predicate or noun, not a verb.
1330  		prevWasAttrOrPending := pendingHead >= 0 || pendingCop || pendingCoordHead >= 0
1331  		if len(slotModKinds) > 0 && slotModKinds[len(slotModKinds)-1] == MKAttr {
1332  			prevWasAttrOrPending = true
1333  		}
1334  		if !isVerb && !sawVerb && contentCount == 2 && pendingRole == HistObject {
1335  			// pendingRole was set to Object by a preposition; not a verb.
1336  		} else if !isVerb && !sawVerb && contentCount == 2 && !isAttr && !prevWasAttrOrPending {
1337  			isVerb = true
1338  		}
1339  		// Predicate adjective: with a copula staged, a predicate-adj surface
1340  		// or a lemma that is one (bigger -> big) becomes HistComplement with
1341  		// MKAdj on the subject, carrying the copula's morph plus the lemma's.
1342  		if pendingCop {
1343  			useLemma := false
1344  			var lem LemmaResult
1345  			if !looksLikePredicateAdj(low) {
1346  				lem = LemmatizeEN(low)
1347  				if looksLikePredicateAdj(lem.Lemma) {
1348  					useLemma = true
1349  				}
1350  			}
1351  			if looksLikePredicateAdj(low) || useLemma {
1352  				pat = append(pat, SlotNoun)
1353  				atom := low
1354  				m := pendingCopMorph
1355  				if useLemma {
1356  					atom = lem.Lemma
1357  					m |= lem.Morph
1358  				}
1359  				slots = append(slots, atom)
1360  				roles = append(roles, HistComplement)
1361  				if pendingNeg {
1362  					m |= MetaPolarNeg
1363  					pendingNeg = false
1364  				}
1365  				slotMorphs = append(slotMorphs, m)
1366  				slotMarks = append(slotMarks, 0)
1367  				slotOblRoles = append(slotOblRoles, ORNone)
1368  				slotHeads = append(slotHeads, pendingCopHead)
1369  				slotModKinds = append(slotModKinds, MKAdj)
1370  				pendingCop = false
1371  				pendingCopHead = -1
1372  				pendingCopMorph = 0
1373  				pendingCopAuxIsBe = false
1374  				sawVerb = true
1375  				continue
1376  			}
1377  		}
1378  		// REL-intransitive: the verb is a MKRel modifier of the host noun.
1379  		// sawVerb stays false so the next real verb is still the predicate.
1380  		if pendingRelHead >= 0 && isVerb {
1381  			pat = append(pat, SlotVerb)
1382  			lem := LemmatizeEN(low)
1383  			slots = append(slots, lem.Lemma)
1384  			roles = append(roles, HistModifier)
1385  			m := lem.Morph
1386  			if m&MetaNumPlural != 0 {
1387  				m = (m &^ MetaNumPlural) | Meta3Sg
1388  			}
1389  			if pendingNeg {
1390  				m |= MetaPolarNeg
1391  				pendingNeg = false
1392  			}
1393  			slotMorphs = append(slotMorphs, m)
1394  			slotMarks = append(slotMarks, 0)
1395  			slotOblRoles = append(slotOblRoles, ORNone)
1396  			slotHeads = append(slotHeads, pendingRelHead)
1397  			slotModKinds = append(slotModKinds, MKRel)
1398  			pendingRelHead = -1
1399  			continue
1400  		}
1401  		// Causative: the embedded verb carries MetaCausative and the
1402  		// suppressed auxiliary's morph.
1403  		if pendingCausative && isVerb {
1404  			pat = append(pat, SlotVerb)
1405  			lem := LemmatizeEN(low)
1406  			slots = append(slots, lem.Lemma)
1407  			roles = append(roles, HistVerb)
1408  			m := lem.Morph | MetaCausative | pendingCausativeMorph
1409  			if m&MetaNumPlural != 0 {
1410  				m = (m &^ MetaNumPlural) | Meta3Sg
1411  			}
1412  			if pendingNeg {
1413  				m |= MetaPolarNeg
1414  				pendingNeg = false
1415  			}
1416  			slotMorphs = append(slotMorphs, m)
1417  			slotMarks = append(slotMarks, 0)
1418  			slotOblRoles = append(slotOblRoles, ORNone)
1419  			slotHeads = append(slotHeads, -1)
1420  			slotModKinds = append(slotModKinds, MKNone)
1421  			pendingCausative = false
1422  			pendingCausativeMorph = 0
1423  			continue
1424  		}
1425  		if !sawVerb && isVerb {
1426  			lem := LemmatizeEN(low)
1427  			// Causative aux: "make"/"let" + NP + bare-V suppresses the aux
1428  			// and stages MetaCausative for the next verb.
1429  			if (lem.Lemma == "make" || lem.Lemma == "let") &&
1430  				causativeBareVFollows(tokens, tokIdx) {
1431  				pendingCausative = true
1432  				pendingCausativeMorph = lem.Morph
1433  				sawVerb = true
1434  				pendingRole = HistObject
1435  				continue
1436  			}
1437  			pat = append(pat, SlotVerb)
1438  			slots = append(slots, lem.Lemma)
1439  			roles = append(roles, HistVerb)
1440  			m := lem.Morph
1441  			if m&MetaNumPlural != 0 {
1442  				m = (m &^ MetaNumPlural) | Meta3Sg
1443  			}
1444  			if pendingNeg {
1445  				m |= MetaPolarNeg
1446  				pendingNeg = false
1447  			}
1448  			if pendingVol {
1449  				m |= MetaMoodVol
1450  				pendingVol = false
1451  			}
1452  			// A staged copula followed by a real verb makes the auxiliary a
1453  			// helper: "be + -ing" is progressive, "be + past participle"
1454  			// passive. Only a form of "be" plus a past non-progressive
1455  			// participle yields passive. The auxiliary's own tense wins:
1456  			// "was bitten" is past passive, "is bitten" is non-past passive.
1457  			if pendingCop {
1458  				m |= pendingCopMorph
1459  				if pendingCopAuxIsBe &&
1460  					m&MetaAspectProg == 0 && m&MetaTensePast != 0 {
1461  					m |= MetaPassive
1462  					if pendingCopMorph&MetaTensePast == 0 {
1463  						m &^= MetaTensePast
1464  					}
1465  				}
1466  				pendingCop = false
1467  				pendingCopHead = -1
1468  				pendingCopMorph = 0
1469  				pendingCopAuxIsBe = false
1470  			}
1471  			slotMorphs = append(slotMorphs, m)
1472  			slotMarks = append(slotMarks, 0)
1473  			slotOblRoles = append(slotOblRoles, ORNone)
1474  			slotHeads = append(slotHeads, -1)
1475  			slotModKinds = append(slotModKinds, MKNone)
1476  			verbIdx := int16(len(slots) - 1)
1477  			for i := 0; i < int(verbIdx); i++ {
1478  				if slotModKinds[i] == MKAdv && slotHeads[i] < 0 {
1479  					slotHeads[i] = verbIdx
1480  				}
1481  			}
1482  			// Ditransitive: the first bare NP is a recipient candidate. It
1483  			// commits to ORRecip only when a second object arrives, so
1484  			// "read a book" keeps a plain patient.
1485  			if isEnDitransitive(lem.Lemma) {
1486  				pendingRecipCand = true
1487  				pendingRecipIdx = -1
1488  			}
1489  			sawVerb = true
1490  			pendingRole = HistObject
1491  		} else {
1492  			pat = append(pat, SlotNoun)
1493  			lem := LemmatizeEN(low)
1494  			slots = append(slots, lem.Lemma)
1495  			role := pendingRole
1496  			if !sawVerb && !pendingCop {
1497  				role = HistSubject
1498  			}
1499  			roles = append(roles, role)
1500  			m := lem.Morph
1501  			if pendingDef {
1502  				m |= MetaDefDef
1503  				pendingDef = false
1504  			}
1505  			slotMorphs = append(slotMorphs, m)
1506  			slotMarks = append(slotMarks, pendingMark)
1507  			slotOblRoles = append(slotOblRoles, pendingOblRole)
1508  			slotHeads = append(slotHeads, -1)
1509  			slotModKinds = append(slotModKinds, MKNone)
1510  			newIdx := int16(len(slots) - 1)
1511  			if pendingRecipCand {
1512  				if pendingRecipIdx < 0 {
1513  					if pendingMark == 0 && pendingOblRole == ORNone {
1514  						pendingRecipIdx = newIdx
1515  					} else {
1516  						pendingRecipCand = false
1517  					}
1518  				} else {
1519  					slotOblRoles[pendingRecipIdx] = ORRecip
1520  					roles[pendingRecipIdx] = HistModifier
1521  					pendingRecipCand = false
1522  					pendingRecipIdx = -1
1523  				}
1524  			}
1525  			// A conjunct inherits the role of its coordinate head.
1526  			if pendingCoordHead >= 0 && pendingCoordHead < newIdx {
1527  				slotHeads[newIdx] = pendingCoordHead
1528  				slotModKinds[newIdx] = MKCoord
1529  				roles[newIdx] = roles[pendingCoordHead]
1530  				pendingCoordHead = -1
1531  			}
1532  			// A staged copula makes this noun the predicate of the subject.
1533  			if pendingCop {
1534  				slotHeads[newIdx] = pendingCopHead
1535  				slotModKinds[newIdx] = MKCop
1536  				slotMorphs[newIdx] |= pendingCopMorph
1537  				pendingCop = false
1538  				pendingCopHead = -1
1539  				pendingCopMorph = 0
1540  				pendingCopAuxIsBe = false
1541  			}
1542  			// Resolve a pending POSS/ATTR modifier from a determiner or an
1543  			// ATTR-detected adjective. ATTR moves the phrase-level
1544  			// definiteness to the head and the head takes the clause role.
1545  			if pendingHead >= 0 && pendingHead < newIdx {
1546  				slotHeads[pendingHead] = newIdx
1547  				slotModKinds[pendingHead] = pendingModKind
1548  				if pendingModKind == MKAttr {
1549  					if slotMorphs[pendingHead]&MetaDefDef != 0 {
1550  						slotMorphs[newIdx] |= MetaDefDef
1551  						slotMorphs[pendingHead] &^= MetaDefDef
1552  					}
1553  					if roles[pendingHead] == HistSubject {
1554  						roles[newIdx] = HistSubject
1555  					}
1556  				}
1557  				pendingHead = -1
1558  				pendingModKind = MKNone
1559  			}
1560  			if isAttr {
1561  				pendingHead = newIdx
1562  				pendingModKind = MKAttr
1563  			}
1564  			pendingMark = 0
1565  			pendingOblRole = ORNone
1566  			if sawVerb && pendingRole == HistObject {
1567  				pendingRole = HistComplement
1568  			}
1569  		}
1570  	}
1571  
1572  	// Finalize the last clause.
1573  	finalSet := buildSetFromSlices(
1574  		slots, roles, slotMorphs,
1575  		slotMarks, slotOblRoles, slotHeads, slotModKinds,
1576  	)
1577  	clauses = append(clauses, Clause{
1578  		Set: finalSet, Relation: clauseRel,
1579  		Parent: clauseParent, HostIdx: -1,
1580  	})
1581  
1582  	// Flatten Slots/Roles across all clauses so the atom-link layer sees
1583  	// every word; mid-input clause finalization would otherwise hide all but
1584  	// the last clause from ExtractResult.Slots.
1585  	flatSlots := make([]string, 0, len(slots))
1586  	flatRoles := make([]int32, 0, len(slots))
1587  	for _, c := range clauses {
1588  		for _, e := range c.Set {
1589  			flatSlots = append(flatSlots, e.Atom)
1590  			flatRoles = append(flatRoles, e.Role)
1591  		}
1592  	}
1593  
1594  	return ExtractResult{
1595  		Pattern: pat, Slots: flatSlots, Roles: flatRoles,
1596  		DeepPat: buildDeepPat(flatRoles), Set: clauses[0].Set,
1597  		Discourse: clauses,
1598  	}
1599  }
1600