package iskra import "strings" // ExtractEN takes EN tokens and produces pattern + slots. Port of extract.mx // ExtractEN. Determin ers (the/a) are morph hints, prepositions are role // markers, verb auxiliaries stage copula info, pronouns are content nouns. // EN marker functional classes, extract.mx enMarkerClass. const ( enMarkUnknown = 0 enMarkDeterminer = 1 // the, a, an enMarkPossDet = 2 // my, your, his, her, its, our, their enMarkVerbAux = 3 // is, are, was, do, have... enMarkPreposition = 4 // in, on, at, with, by... enMarkNegation = 5 // not, n't enMarkConjunction = 6 // and, but, or enMarkRelative = 7 // that, which, who enMarkPronoun = 8 // i, you, he, she, it, we, they ) func enMarkerClass(mk uint8) int32 { switch mk { case MkThe, MkA: return enMarkDeterminer case MkMy, MkYour, MkHis, MkHerP, MkIts, MkOurP, MkTheirP: return enMarkPossDet case MkIs, MkAre, MkWas, MkDo: return enMarkVerbAux case MkIn, MkOn, MkAt, MkWith, MkBy, MkFor, MkToEN, MkOf, MkFrom, MkAs, MkThan: return enMarkPreposition case MkNot: return enMarkNegation case MkAnd, MkBut: return enMarkConjunction case MkThat: return enMarkRelative case MkI, MkYou, MkPron3, MkItEN, MkWeEN, MkThey: return enMarkPronoun } return enMarkUnknown } // findSubjectIdx returns the index of the entry that serves as the subject of // a copular predicate: the first entry whose role is Topic or Subject and that // is not itself a modifier. Returns -1 if no candidate found. func findSubjectIdx(roles []int32, heads []int16) int16 { for i, r := range roles { if i >= len(heads) { break } if heads[i] >= 0 { continue } if r == HistTopic || r == HistSubject { return int16(i) } } return -1 } // buildSetFromSlices converts parallel per-slot slices into a []SetEntry. func buildSetFromSlices( slots []string, roles []int32, slotMorphs []uint16, slotMarks, slotOblRoles []uint8, slotHeads []int16, slotModKinds []uint8, ) []SetEntry { // Role propagation before set construction: ATTR copies its head's role, // ADV becomes HistModifier. Mutates roles, the caller's slice. for i := 0; i < len(slots); i++ { if i < len(slotModKinds) && i < len(slotHeads) && slotHeads[i] >= 0 { h := int32(slotHeads[i]) switch slotModKinds[i] { case MKAttr: if int(h) < len(roles) { roles[i] = roles[h] } case MKAdv: roles[i] = HistModifier } } } var set []SetEntry for i, lemma := range slots { role := int32(HistComplement) if i < len(roles) { role = roles[i] } m := uint16(0) if i < len(slotMorphs) { m = slotMorphs[i] } mark := uint8(0) if i < len(slotMarks) { mark = slotMarks[i] } obl := uint8(ORNone) if i < len(slotOblRoles) { obl = slotOblRoles[i] } head := int16(-1) modKind := uint8(MKNone) if i < len(slotHeads) { head = slotHeads[i] } if i < len(slotModKinds) { modKind = slotModKinds[i] } set = append(set, SetEntry{ Role: role, Atom: lemma, Morph: m, Class: 0, Mark: mark, OblRole: obl, Head: head, ModKind: modKind, }) } return set } // hasPredication reports whether the per-slot state holds a completed // predication: a verb slot or a copular complement. func hasPredication(roles []int32, slotModKinds []uint8) bool { for i, r := range roles { if r == HistVerb { return true } if i < len(slotModKinds) && slotModKinds[i] == MKCop { return true } } return false } // looksLikeAdverb reports an -ly suffix or an irregular adverb. func looksLikeAdverb(w string) bool { if len(w) > 3 && HasSuffixEN(w, "ly") { return true } switch w { case "fast", "well", "hard", "here", "there", "now", "then", "today", "yesterday", "tomorrow", "always", "often", "never", "sometimes", "usually", "rarely", "still", "already", "yet", "soon", "later", "early", "late", "ever", "again": return true } return false } // causativeBareVFollows reports a bare infinitive verb within the next four // tokens: "make/let + NP + bare-V". "to" and any preposition or conjunction // before the verb cancel the reading. func causativeBareVFollows(tokens []string, tokIdx int) bool { for k := 1; k <= 4 && tokIdx+k < len(tokens); k++ { t := strings.ToLower(tokens[tokIdx+k]) if t == "to" { return false } if mk, isMk := enWordToMarker()[t]; isMk { switch enMarkerClass(mk) { case enMarkPreposition, enMarkConjunction: return false } continue } if looksLikeVerb(t) { return true } } return false } // isEnDitransitive reports a verb that takes a bare-NP recipient before the // patient: "give X Y". moxie lists "hand" twice in the closed set; Go rejects a // repeated label and the set is the same either way. func isEnDitransitive(lemma string) bool { switch lemma { case "give", "send", "tell", "show", "offer", "hand", "pass", "teach", "write", "read", "sell", "buy", "bring", "mail", "lend", "owe", "pay", "throw", "ask": return true } return false } // looksLikePredicateAdj reports a word that, after a copula, is a predicative // adjective rather than a verb or noun. func looksLikePredicateAdj(w string) bool { if len(w) < 2 { return false } switch w { case "big", "small", "tall", "short", "long", "wide", "narrow", "thick", "thin", "deep", "shallow", "high", "low", "hot", "cold", "warm", "cool", "wet", "dry", "fast", "slow", "quick", "old", "new", "young", "good", "bad", "nice", "fine", "great", "poor", "happy", "sad", "angry", "tired", "hungry", "thirsty", "sleepy", "busy", "lazy", "easy", "hard", "soft", "loud", "quiet", "clean", "dirty", "empty", "full", "rich", "weak", "strong", "smart", "kind", "mean", "red", "blue", "green", "yellow", "white", "black", "pink", "brown", "gray", "grey", "purple", "orange", "heavy", "light", "free", "cheap", "expensive", "safe", "sick", "well", "ill", "ready", "right", "wrong", "true", "false", "real", "fake", "open", "closed", "bright", "dark", "sweet", "sour", "salty", "bitter", "round", "square", "flat", "sharp", "dull", "strange", "weird", "normal", "common", "rare", "important", "famous", "popular", "different", "similar", "alive", "dead", "alone", "together": return true } if len(w) < 4 { return false } switch w { case "interesting", "exciting", "boring", "tiring", "amazing", "frightening", "surprising", "confusing", "disappointing", "satisfying", "encouraging", "pleasing", "fascinating", "depressing", "embarrassing", "shocking", "thrilling", "charming", "annoying", "relaxing", "stunning", "missing", "willing", "outstanding", "promising": return true } if HasSuffixEN(w, "ful") || HasSuffixEN(w, "ous") || HasSuffixEN(w, "ic") || HasSuffixEN(w, "able") || HasSuffixEN(w, "ible") { return true } return false } // possDetSurface is the surface form of an EN possessive determiner. func possDetSurface(mk uint8) string { switch mk { case MkMy: return "my" case MkYour: return "your" case MkHis: return "his" case MkHerP: return "her" case MkIts: return "its" case MkOurP: return "our" case MkTheirP: return "their" } return "" } // isENPunct reports a single-character punctuation token. func isENPunct(s string) bool { if len(s) != 1 { return false } c := s[0] return c == '.' || c == ',' || c == '!' || c == '?' || c == ';' || c == ':' || c == '"' || c == '\'' } // looksLikeVerb is the EN verb heuristic: suffix shapes plus a high-frequency // word set. The length guards keep red/fed/led and sing/king/ring out. func looksLikeVerb(w string) bool { if len(w) < 2 { return false } if (HasSuffixEN(w, "ing") && len(w) > 4) || (HasSuffixEN(w, "ed") && len(w) > 3) || HasSuffixEN(w, "ize") || HasSuffixEN(w, "ise") || HasSuffixEN(w, "ate") { return true } if HasSuffixEN(w, "fy") || (HasSuffixEN(w, "en") && len(w) > 3) { return true } switch w { case "go", "went", "gone", "goes", "eat", "eats", "drink", "drinks", "read", "reads", "write", "writes", "walk", "walks", "talk", "talks", "sleep", "sleeps", "wake", "wakes", "sit", "sits", "stand", "stands", "lie", "lies", "live", "lives", "die", "dies", "chew", "chews", "fly", "flies", "swim", "swims", "jump", "jumps", "throw", "throws", "catch", "catches", "kick", "kicks", "hit", "hits", "push", "pushes", "pull", "pulls", "grab", "grabs", "bite", "bites", "chase", "chases", "get", "got", "gotten", "gets", "make", "made", "makes", "take", "took", "taken", "takes", "come", "came", "comes", "see", "saw", "seen", "sees", "know", "knew", "known", "knows", "give", "gave", "given", "gives", "say", "said", "says", "tell", "told", "tells", "think", "thought", "thinks", "find", "found", "finds", "leave", "left", "leaves", "call", "calls", "ask", "asks", "seem", "seems", "feel", "felt", "feels", "become", "became", "becomes", "keep", "kept", "keeps", "begin", "began", "begun", "begins", "show", "shows", "hear", "heard", "hears", "play", "plays", "move", "moves", "believe", "believes", "hold", "held", "holds", "bring", "brought", "brings", "happen", "happens", "wrote", "provide", "provides", "sat", "stood", "lose", "lost", "loses", "pay", "paid", "pays", "meet", "met", "meets", "include", "includes", "continue", "continues", "learn", "learns", "change", "changes", "lead", "led", "leads", "understand", "understood", "watch", "watches", "follow", "follows", "stop", "stops", "create", "creates", "speak", "spoke", "speaks", "allow", "allows", "add", "adds", "spend", "spent", "spends", "grow", "grew", "grows", "open", "opens", "win", "won", "wins", "teach", "taught", "offer", "offers", "remember", "remembers", "love", "loves", "consider", "considers", "appear", "appears", "buy", "bought", "buys", "wait", "waits", "serve", "serves", "died", "send", "sent", "sends", "expect", "expects", "build", "built", "builds", "stay", "stays", "fall", "fell", "falls", "cut", "cuts", "reach", "reaches", "kill", "kills", "remain", "remains", "suggest", "suggests", "raise", "raises", "pass", "passes", "sell", "sold", "sells", "require", "requires", "report", "reports", "decide", "decides", "develop", "develops", "use", "uses", "put", "puts", "set", "sets", "run", "runs", "let", "lets", "try", "tries", "need", "needs", "want", "wants", "start", "starts", "help", "helps", "turn", "turns", "work", "works", "like", "likes", "look", "looks", "mean", "means", "meant", "can", "could", "will", "would", "shall", "should", "may", "might", "must": return true } return false } // HasSuffixEN reports whether s ends in suffix. func HasSuffixEN(s, suffix string) bool { if len(s) < len(suffix) { return false } return s[len(s)-len(suffix):] == suffix } // buildDeepPat is the canonical role sequence: Topic normalised to Subject, // sorted ascending by insertion. func buildDeepPat(roles []int32) []uint8 { if len(roles) == 0 { return nil } dp := make([]uint8, len(roles)) for i, r := range roles { nr := r if nr == HistTopic { nr = HistSubject } dp[i] = uint8(nr) } for i := 1; i < len(dp); i++ { key := dp[i] j := i - 1 for j >= 0 && dp[j] > key { dp[j+1] = dp[j] j-- } dp[j+1] = key } return dp } // LemmatizeEN reduces an EN surface form to its lemma and morph state. Port of // lemma_en.mx LemmatizeEN: the irregular table first, then -ing, -ed and the // 3rd-singular/plural -s rules. The phrasal-verb registry is not ported; an // unknown word comes back lowercased with zero morph, as the source's default // branch does. func LemmatizeEN(word string) LemmaResult { if r, ok := enIrregular[word]; ok { return r } return enLemmatizeRegular(word) } // enLemmatizeRegular is LemmatizeEN past the irregular table. func enLemmatizeRegular(word string) LemmaResult { if word == UntranslatedMarker { return LemmaResult{Lemma: word, Morph: 0} } low := strings.ToLower(word) if r, ok := enIrregular[low]; ok { return r } if len(low) > 4 && HasSuffixEN(low, "ing") { stem := stripIng(low) if stem != "" { return LemmaResult{Lemma: stem, Morph: MetaAspectProg} } } if len(low) > 3 && HasSuffixEN(low, "ed") { stem := stripEd(low) if stem != "" { return LemmaResult{Lemma: stem, Morph: MetaTensePast} } } if len(low) > 3 && HasSuffixEN(low, "ies") { return LemmaResult{Lemma: low[:len(low)-3] + "y", Morph: Meta3Sg} } if len(low) > 3 && HasSuffixEN(low, "es") { base := low[:len(low)-2] last := base[len(base)-1] if last == 's' || last == 'x' || last == 'z' || HasSuffixEN(base, "sh") || HasSuffixEN(base, "ch") { return LemmaResult{Lemma: base, Morph: Meta3Sg} } return LemmaResult{Lemma: low[:len(low)-1], Morph: Meta3Sg} } // -s is plural here, never 3sg: the 3sg path is the -es rule above. if len(low) > 2 && low[len(low)-1] == 's' && low[len(low)-2] != 's' && low[len(low)-2] != '\'' { c := low[len(low)-2] if c != 'u' && c != 'i' { return LemmaResult{Lemma: low[:len(low)-1], Morph: MetaNumPlural} } } return LemmaResult{Lemma: low, Morph: 0} } func stripIng(word string) string { base := word[:len(word)-3] if len(base) < 3 { return word } last := base[len(base)-1] if len(base) > 2 && base[len(base)-1] == base[len(base)-2] && isConsonant(last) { return base[:len(base)-1] } if !isConsonant(last) || last == 'e' { return base } if last == 'y' { return base } if last == 'c' || last == 'v' || last == 'z' { return base + "e" } if len(base) >= 2 && isConsonant(base[len(base)-2]) { return base } if len(base) >= 3 && !isConsonant(base[len(base)-2]) && !isConsonant(base[len(base)-3]) { return base } return base + "e" } func stripEd(word string) string { base := word[:len(word)-2] last := base[len(base)-1] if last == 'i' && len(base) > 1 { return base[:len(base)-1] + "y" } if len(base) > 2 && base[len(base)-1] == base[len(base)-2] && isConsonant(last) { // Vowel before the double means the double was added by -ed // (stopped); a consonant means it is natural (guessed, passed). if len(base) > 2 && !isConsonant(base[len(base)-3]) { return base[:len(base)-1] } return base } if last == 'e' { return base } if !isConsonant(last) { return base } if last == 'c' || last == 'v' || last == 'z' { return base + "e" } if len(base) >= 2 && isConsonant(base[len(base)-2]) { return base } if len(base) >= 3 && !isConsonant(base[len(base)-2]) && !isConsonant(base[len(base)-3]) { return base } return base + "e" } func isConsonant(c byte) bool { switch c { case 'a', 'e', 'i', 'o', 'u': return false } return c >= 'a' && c <= 'z' } // enIrregular is lemma_en.mx enIrregular. "men" and "women" are plural nouns // here, not forms of "man"/"woman" verbs. var enIrregular = map[string]LemmaResult{ // be "am": {Lemma: "be"}, "is": {Lemma: "be", Morph: Meta3Sg}, "are": {Lemma: "be"}, "was": {Lemma: "be", Morph: MetaTensePast | Meta3Sg}, "were": {Lemma: "be", Morph: MetaTensePast}, "been": {Lemma: "be", Morph: MetaTensePast | MetaAspectProg}, "being": {Lemma: "be", Morph: MetaAspectProg}, // have "has": {Lemma: "have", Morph: Meta3Sg}, "had": {Lemma: "have", Morph: MetaTensePast}, "having": {Lemma: "have", Morph: MetaAspectProg}, // do "does": {Lemma: "do", Morph: Meta3Sg}, "did": {Lemma: "do", Morph: MetaTensePast}, "done": {Lemma: "do", Morph: MetaTensePast}, // go "went": {Lemma: "go", Morph: MetaTensePast}, "gone": {Lemma: "go", Morph: MetaTensePast}, "goes": {Lemma: "go", Morph: Meta3Sg}, // get "got": {Lemma: "get", Morph: MetaTensePast}, "gotten": {Lemma: "get", Morph: MetaTensePast}, // make "made": {Lemma: "make", Morph: MetaTensePast}, // take "took": {Lemma: "take", Morph: MetaTensePast}, "taken": {Lemma: "take", Morph: MetaTensePast}, // come "came": {Lemma: "come", Morph: MetaTensePast}, // see "saw": {Lemma: "see", Morph: MetaTensePast}, "seen": {Lemma: "see", Morph: MetaTensePast}, // know "knew": {Lemma: "know", Morph: MetaTensePast}, "known": {Lemma: "know", Morph: MetaTensePast}, // give "gave": {Lemma: "give", Morph: MetaTensePast}, "given": {Lemma: "give", Morph: MetaTensePast}, // say "said": {Lemma: "say", Morph: MetaTensePast}, // tell "told": {Lemma: "tell", Morph: MetaTensePast}, // think "thought": {Lemma: "think", Morph: MetaTensePast}, // find "found": {Lemma: "find", Morph: MetaTensePast}, // leave "left": {Lemma: "leave", Morph: MetaTensePast}, // feel "felt": {Lemma: "feel", Morph: MetaTensePast}, // become "became": {Lemma: "become", Morph: MetaTensePast}, // keep "kept": {Lemma: "keep", Morph: MetaTensePast}, // begin "began": {Lemma: "begin", Morph: MetaTensePast}, "begun": {Lemma: "begin", Morph: MetaTensePast}, // hear "heard": {Lemma: "hear", Morph: MetaTensePast}, // hold "held": {Lemma: "hold", Morph: MetaTensePast}, // bring "brought": {Lemma: "bring", Morph: MetaTensePast}, // write "wrote": {Lemma: "write", Morph: MetaTensePast}, "written": {Lemma: "write", Morph: MetaTensePast}, // sit "sat": {Lemma: "sit", Morph: MetaTensePast}, // stand "stood": {Lemma: "stand", Morph: MetaTensePast}, // lose "lost": {Lemma: "lose", Morph: MetaTensePast}, // pay "paid": {Lemma: "pay", Morph: MetaTensePast}, // meet "met": {Lemma: "meet", Morph: MetaTensePast}, // lead "led": {Lemma: "lead", Morph: MetaTensePast}, // understand "understood": {Lemma: "understand", Morph: MetaTensePast}, // speak "spoke": {Lemma: "speak", Morph: MetaTensePast}, "spoken": {Lemma: "speak", Morph: MetaTensePast}, // grow "grew": {Lemma: "grow", Morph: MetaTensePast}, "grown": {Lemma: "grow", Morph: MetaTensePast}, // win "won": {Lemma: "win", Morph: MetaTensePast}, // teach "taught": {Lemma: "teach", Morph: MetaTensePast}, // buy "bought": {Lemma: "buy", Morph: MetaTensePast}, // send "sent": {Lemma: "send", Morph: MetaTensePast}, // build "built": {Lemma: "build", Morph: MetaTensePast}, // fall "fell": {Lemma: "fall", Morph: MetaTensePast}, "fallen": {Lemma: "fall", Morph: MetaTensePast}, // sell "sold": {Lemma: "sell", Morph: MetaTensePast}, // run "ran": {Lemma: "run", Morph: MetaTensePast}, // mean "meant": {Lemma: "mean", Morph: MetaTensePast}, // spend "spent": {Lemma: "spend", Morph: MetaTensePast}, // catch "caught": {Lemma: "catch", Morph: MetaTensePast}, // fly "flew": {Lemma: "fly", Morph: MetaTensePast}, "flown": {Lemma: "fly", Morph: MetaTensePast}, // eat "ate": {Lemma: "eat", Morph: MetaTensePast}, "eaten": {Lemma: "eat", Morph: MetaTensePast}, // bite: the participle carries MetaPassive, the simple past does not. "bit": {Lemma: "bite", Morph: MetaTensePast}, "bitten": {Lemma: "bite", Morph: MetaTensePast | MetaPassive}, // drink "drank": {Lemma: "drink", Morph: MetaTensePast}, "drunk": {Lemma: "drink", Morph: MetaTensePast}, // drive "drove": {Lemma: "drive", Morph: MetaTensePast}, "driven": {Lemma: "drive", Morph: MetaTensePast}, // sing "sang": {Lemma: "sing", Morph: MetaTensePast}, "sung": {Lemma: "sing", Morph: MetaTensePast}, // swim "swam": {Lemma: "swim", Morph: MetaTensePast}, "swum": {Lemma: "swim", Morph: MetaTensePast}, // break "broke": {Lemma: "break", Morph: MetaTensePast}, "broken": {Lemma: "break", Morph: MetaTensePast}, // choose "chose": {Lemma: "choose", Morph: MetaTensePast}, "chosen": {Lemma: "choose", Morph: MetaTensePast}, // draw "drew": {Lemma: "draw", Morph: MetaTensePast}, "drawn": {Lemma: "draw", Morph: MetaTensePast}, // forget "forgot": {Lemma: "forget", Morph: MetaTensePast}, "forgotten": {Lemma: "forget", Morph: MetaTensePast}, // freeze "froze": {Lemma: "freeze", Morph: MetaTensePast}, "frozen": {Lemma: "freeze", Morph: MetaTensePast}, // hide "hid": {Lemma: "hide", Morph: MetaTensePast}, "hidden": {Lemma: "hide", Morph: MetaTensePast}, // lie (recline) "lay": {Lemma: "lie", Morph: MetaTensePast}, "lain": {Lemma: "lie", Morph: MetaTensePast}, // ride "rode": {Lemma: "ride", Morph: MetaTensePast}, "ridden": {Lemma: "ride", Morph: MetaTensePast}, // rise "rose": {Lemma: "rise", Morph: MetaTensePast}, "risen": {Lemma: "rise", Morph: MetaTensePast}, // shake "shook": {Lemma: "shake", Morph: MetaTensePast}, "shaken": {Lemma: "shake", Morph: MetaTensePast}, // steal "stole": {Lemma: "steal", Morph: MetaTensePast}, "stolen": {Lemma: "steal", Morph: MetaTensePast}, // tear "tore": {Lemma: "tear", Morph: MetaTensePast}, "torn": {Lemma: "tear", Morph: MetaTensePast}, // throw "threw": {Lemma: "throw", Morph: MetaTensePast}, "thrown": {Lemma: "throw", Morph: MetaTensePast}, // wake "woke": {Lemma: "wake", Morph: MetaTensePast}, "woken": {Lemma: "wake", Morph: MetaTensePast}, // wear "wore": {Lemma: "wear", Morph: MetaTensePast}, "worn": {Lemma: "wear", Morph: MetaTensePast}, // irregular nouns "children": {Lemma: "child", Morph: MetaNumPlural}, "men": {Lemma: "man", Morph: MetaNumPlural}, "women": {Lemma: "woman", Morph: MetaNumPlural}, "people": {Lemma: "person", Morph: MetaNumPlural}, "mice": {Lemma: "mouse", Morph: MetaNumPlural}, "feet": {Lemma: "foot", Morph: MetaNumPlural}, "teeth": {Lemma: "tooth", Morph: MetaNumPlural}, "geese": {Lemma: "goose", Morph: MetaNumPlural}, "lives": {Lemma: "life", Morph: MetaNumPlural}, "wives": {Lemma: "wife", Morph: MetaNumPlural}, "knives": {Lemma: "knife", Morph: MetaNumPlural}, "wolves": {Lemma: "wolf", Morph: MetaNumPlural}, "halves": {Lemma: "half", Morph: MetaNumPlural}, "leaves": {Lemma: "leaf", Morph: MetaNumPlural}, // modals/negative contractions "can't": {Lemma: "can", Morph: MetaPolarNeg}, "won't": {Lemma: "will", Morph: MetaPolarNeg}, "don't": {Lemma: "do", Morph: MetaPolarNeg}, "doesn't": {Lemma: "do", Morph: MetaPolarNeg | Meta3Sg}, "didn't": {Lemma: "do", Morph: MetaTensePast | MetaPolarNeg}, "isn't": {Lemma: "be", Morph: MetaPolarNeg | Meta3Sg}, "aren't": {Lemma: "be", Morph: MetaPolarNeg}, "wasn't": {Lemma: "be", Morph: MetaTensePast | MetaPolarNeg}, "weren't": {Lemma: "be", Morph: MetaTensePast | MetaPolarNeg}, "haven't": {Lemma: "have", Morph: MetaPolarNeg}, "hasn't": {Lemma: "have", Morph: MetaPolarNeg | Meta3Sg}, "hadn't": {Lemma: "have", Morph: MetaTensePast | MetaPolarNeg}, "couldn't": {Lemma: "can", Morph: MetaTensePast | MetaPolarNeg}, "wouldn't": {Lemma: "will", Morph: MetaTensePast | MetaPolarNeg}, "shouldn't": {Lemma: "shall", Morph: MetaTensePast | MetaPolarNeg}, // CVC-rule overrides: multi-syllable verbs whose -ed/-ing forms get a // spurious -e from the CVC heuristic. "happened": {Lemma: "happen", Morph: MetaTensePast}, "happening": {Lemma: "happen", Morph: MetaAspectProg}, "opened": {Lemma: "open", Morph: MetaTensePast}, "opening": {Lemma: "open", Morph: MetaAspectProg}, "listened": {Lemma: "listen", Morph: MetaTensePast}, "listening": {Lemma: "listen", Morph: MetaAspectProg}, "offered": {Lemma: "offer", Morph: MetaTensePast}, "offering": {Lemma: "offer", Morph: MetaAspectProg}, "differed": {Lemma: "differ", Morph: MetaTensePast}, "suffered": {Lemma: "suffer", Morph: MetaTensePast}, "suffering": {Lemma: "suffer", Morph: MetaAspectProg}, "wandered": {Lemma: "wander", Morph: MetaTensePast}, "wandering": {Lemma: "wander", Morph: MetaAspectProg}, "wondered": {Lemma: "wonder", Morph: MetaTensePast}, "wondering": {Lemma: "wonder", Morph: MetaAspectProg}, "ordered": {Lemma: "order", Morph: MetaTensePast}, "ordering": {Lemma: "order", Morph: MetaAspectProg}, "murdered": {Lemma: "murder", Morph: MetaTensePast}, "answered": {Lemma: "answer", Morph: MetaTensePast}, "answering": {Lemma: "answer", Morph: MetaAspectProg}, "entered": {Lemma: "enter", Morph: MetaTensePast}, "entering": {Lemma: "enter", Morph: MetaAspectProg}, "remembered": {Lemma: "remember", Morph: MetaTensePast}, "remembering": {Lemma: "remember", Morph: MetaAspectProg}, "considered": {Lemma: "consider", Morph: MetaTensePast}, "considering": {Lemma: "consider", Morph: MetaAspectProg}, "discovered": {Lemma: "discover", Morph: MetaTensePast}, "discovering": {Lemma: "discover", Morph: MetaAspectProg}, "delivered": {Lemma: "deliver", Morph: MetaTensePast}, "developed": {Lemma: "develop", Morph: MetaTensePast}, "developing": {Lemma: "develop", Morph: MetaAspectProg}, "gathered": {Lemma: "gather", Morph: MetaTensePast}, "gathering": {Lemma: "gather", Morph: MetaAspectProg}, "threatened": {Lemma: "threaten", Morph: MetaTensePast}, "threatening": {Lemma: "threaten", Morph: MetaAspectProg}, "strengthened": {Lemma: "strengthen", Morph: MetaTensePast}, "loosened": {Lemma: "loosen", Morph: MetaTensePast}, "sharpened": {Lemma: "sharpen", Morph: MetaTensePast}, "flattened": {Lemma: "flatten", Morph: MetaTensePast}, "softened": {Lemma: "soften", Morph: MetaTensePast}, "modeled": {Lemma: "model", Morph: MetaTensePast}, "modeling": {Lemma: "model", Morph: MetaAspectProg}, "traveled": {Lemma: "travel", Morph: MetaTensePast}, "traveling": {Lemma: "travel", Morph: MetaAspectProg}, "cancelled": {Lemma: "cancel", Morph: MetaTensePast}, "canceling": {Lemma: "cancel", Morph: MetaAspectProg}, // Pronoun objective case collapses to nominative. Possessives stay out: // they are modifiers and distinct atoms from the subject pronoun. "me": {Lemma: "i"}, "myself": {Lemma: "i"}, "him": {Lemma: "he"}, "himself": {Lemma: "he"}, "herself": {Lemma: "she"}, "us": {Lemma: "we"}, "ourselves": {Lemma: "we"}, "them": {Lemma: "they"}, "themselves": {Lemma: "they"}, "yourself": {Lemma: "you"}, "itself": {Lemma: "it"}, // Pronoun contractions collapse to the pronoun; the auxiliary is // structural and its morph rides on the clause's main verb. "i'm": {Lemma: "i"}, "i've": {Lemma: "i"}, "i'll": {Lemma: "i"}, "i'd": {Lemma: "i"}, "it's": {Lemma: "it"}, "he's": {Lemma: "he"}, "she's": {Lemma: "she"}, "we're": {Lemma: "we"}, "we've": {Lemma: "we"}, "we'll": {Lemma: "we"}, "they're": {Lemma: "they"}, "they've": {Lemma: "they"}, "they'll": {Lemma: "they"}, "you're": {Lemma: "you"}, "you've": {Lemma: "you"}, "you'll": {Lemma: "you"}, "let's": {Lemma: "let", Morph: MetaMoodVol}, "that's": {Lemma: "that"}, "there's": {Lemma: "there"}, "what's": {Lemma: "what"}, "who's": {Lemma: "who"}, "here's": {Lemma: "here"}, // -ing irregulars: ie->y before -ing, and digraph plus silent-e. "dying": {Lemma: "die", Morph: MetaAspectProg}, "lying": {Lemma: "lie", Morph: MetaAspectProg}, "tying": {Lemma: "tie", Morph: MetaAspectProg}, "choosing": {Lemma: "choose", Morph: MetaAspectProg}, "going": {Lemma: "go", Morph: MetaAspectProg}, "doing": {Lemma: "do", Morph: MetaAspectProg}, // Informal contractions "gonna": {Lemma: "go", Morph: MetaMoodVol}, "wanna": {Lemma: "want", Morph: MetaMoodVol}, "gotta": {Lemma: "get", Morph: MetaMoodVol}, "kinda": {Lemma: "kind"}, "sorta": {Lemma: "sort"}, "lemme": {Lemma: "let"}, "gimme": {Lemma: "give"}, // Words that naturally end in -ing "thing": {Lemma: "thing"}, "nothing": {Lemma: "nothing"}, "something": {Lemma: "something"}, "anything": {Lemma: "anything"}, "everything": {Lemma: "everything"}, "morning": {Lemma: "morning"}, "evening": {Lemma: "evening"}, "spring": {Lemma: "spring"}, "string": {Lemma: "string"}, "during": {Lemma: "during"}, "ceiling": {Lemma: "ceiling"}, "king": {Lemma: "king"}, // Adverbs ending in -s, not plurals "always": {Lemma: "always"}, "sometimes": {Lemma: "sometimes"}, "perhaps": {Lemma: "perhaps"}, "thus": {Lemma: "thus"}, "besides": {Lemma: "besides"}, "nowadays": {Lemma: "nowadays"}, "towards": {Lemma: "towards"}, "upstairs": {Lemma: "upstairs"}, "downstairs": {Lemma: "downstairs"}, "outdoors": {Lemma: "outdoors"}, // -ed words with natural double consonants "guessed": {Lemma: "guess", Morph: MetaTensePast}, "passed": {Lemma: "pass", Morph: MetaTensePast}, "missed": {Lemma: "miss", Morph: MetaTensePast}, "kissed": {Lemma: "kiss", Morph: MetaTensePast}, "crossed": {Lemma: "cross", Morph: MetaTensePast}, "dressed": {Lemma: "dress", Morph: MetaTensePast}, "pressed": {Lemma: "press", Morph: MetaTensePast}, "stressed": {Lemma: "stress", Morph: MetaTensePast}, // -ed words needing silent-e restoration "phrased": {Lemma: "phrase", Morph: MetaTensePast}, "increased": {Lemma: "increase", Morph: MetaTensePast}, "decreased": {Lemma: "decrease", Morph: MetaTensePast}, "pleased": {Lemma: "please", Morph: MetaTensePast}, "released": {Lemma: "release", Morph: MetaTensePast}, "surprised": {Lemma: "surprise", Morph: MetaTensePast}, // Comparatives map to base plus MetaCompare. "bigger": {Lemma: "big", Morph: MetaCompare}, "smaller": {Lemma: "small", Morph: MetaCompare}, "taller": {Lemma: "tall", Morph: MetaCompare}, "shorter": {Lemma: "short", Morph: MetaCompare}, "longer": {Lemma: "long", Morph: MetaCompare}, "wider": {Lemma: "wide", Morph: MetaCompare}, "thicker": {Lemma: "thick", Morph: MetaCompare}, "thinner": {Lemma: "thin", Morph: MetaCompare}, "higher": {Lemma: "high", Morph: MetaCompare}, "lower": {Lemma: "low", Morph: MetaCompare}, "hotter": {Lemma: "hot", Morph: MetaCompare}, "colder": {Lemma: "cold", Morph: MetaCompare}, "warmer": {Lemma: "warm", Morph: MetaCompare}, "cooler": {Lemma: "cool", Morph: MetaCompare}, "faster": {Lemma: "fast", Morph: MetaCompare}, "slower": {Lemma: "slow", Morph: MetaCompare}, "older": {Lemma: "old", Morph: MetaCompare}, "younger": {Lemma: "young", Morph: MetaCompare}, "happier": {Lemma: "happy", Morph: MetaCompare}, "sadder": {Lemma: "sad", Morph: MetaCompare}, "easier": {Lemma: "easy", Morph: MetaCompare}, "harder": {Lemma: "hard", Morph: MetaCompare}, "larger": {Lemma: "large", Morph: MetaCompare}, "better": {Lemma: "good", Morph: MetaCompare}, "worse": {Lemma: "bad", Morph: MetaCompare}, "stronger": {Lemma: "strong", Morph: MetaCompare}, "weaker": {Lemma: "weak", Morph: MetaCompare}, "heavier": {Lemma: "heavy", Morph: MetaCompare}, "lighter": {Lemma: "light", Morph: MetaCompare}, "cheaper": {Lemma: "cheap", Morph: MetaCompare}, "richer": {Lemma: "rich", Morph: MetaCompare}, "poorer": {Lemma: "poor", Morph: MetaCompare}, "nicer": {Lemma: "nice", Morph: MetaCompare}, "newer": {Lemma: "new", Morph: MetaCompare}, } // ExtractEN takes EN tokens and produces pattern + slots. Determin ers (the/a) // are morph hints, prepositions are role markers, auxiliaries stage copula // info, pronouns are content nouns. func ExtractEN(tokens []string) ExtractResult { var pat []byte var slots []string var roles []int32 var slotMorphs []uint16 var slotMarks []uint8 var slotOblRoles []uint8 var slotHeads []int16 var slotModKinds []uint8 sawVerb := false contentCount := 0 pendingRole := int32(HistSubject) pendingDef := false pendingNeg := false pendingMark := uint8(0) pendingOblRole := uint8(ORNone) pendingHead := int16(-1) pendingModKind := uint8(MKNone) pendingCop := false pendingCopHead := int16(-1) pendingCopMorph := uint16(0) pendingCopAuxIsBe := false pendingVol := false pendingCoordHead := int16(-1) pendingRelHead := int16(-1) pendingRecipCand := false pendingRecipIdx := int16(-1) pendingCausative := false pendingCausativeMorph := uint16(0) resetPending := func() { pendingRole = HistSubject pendingDef = false pendingNeg = false pendingMark = 0 pendingOblRole = ORNone pendingHead = -1 pendingModKind = MKNone pendingCop = false pendingCopHead = -1 pendingCopMorph = 0 pendingCopAuxIsBe = false pendingVol = false pendingCoordHead = -1 pendingRelHead = -1 pendingRecipCand = false pendingRecipIdx = -1 pendingCausative = false pendingCausativeMorph = 0 } var clauses []Clause clauseRel := uint8(ClauseRoot) clauseParent := int16(-1) for tokIdx, tok := range tokens { // Clause boundary from EN punctuation classification. Finalize the // current clause as a ClauseAnd peer, then reset per-slot state. if tok == ClauseSplitToken { if len(slots) > 0 { clauseSet := buildSetFromSlices( slots, roles, slotMorphs, slotMarks, slotOblRoles, slotHeads, slotModKinds, ) clauseRelLocal := clauseRel if clauseRelLocal == ClauseRoot && len(clauses) > 0 { clauseRelLocal = ClauseAnd } nextParent := int16(len(clauses)) clauses = append(clauses, Clause{ Set: clauseSet, Relation: clauseRelLocal, Parent: clauseParent, HostIdx: -1, }) slots = nil roles = nil slotMorphs = nil slotMarks = nil slotOblRoles = nil slotHeads = nil slotModKinds = nil sawVerb = false contentCount = 0 resetPending() clauseRel = ClauseAnd clauseParent = nextParent - 1 } continue } low := strings.ToLower(tok) mk, isMk := enWordToMarker()[low] if isMk { pat = append(pat, mk) switch enMarkerClass(mk) { case enMarkDeterminer: // In a subordinate clause that already has a verb, a new // determiner starts the main clause: "if it rains the cat // runs" - "the" begins the main clause. if sawVerb && (clauseRel == ClauseIf || clauseRel == ClauseBecause) { clauseSet := buildSetFromSlices( slots, roles, slotMorphs, slotMarks, slotOblRoles, slotHeads, slotModKinds, ) nextParent := int16(len(clauses)) clauses = append(clauses, Clause{ Set: clauseSet, Relation: clauseRel, Parent: nextParent, HostIdx: -1, }) slots = nil roles = nil slotMorphs = nil slotMarks = nil slotOblRoles = nil slotHeads = nil slotModKinds = nil sawVerb = false contentCount = 0 resetPending() clauseRel = ClauseRoot clauseParent = -1 } if mk == MkThe { pendingDef = true } // "a" leaves pendingDef false case enMarkNegation: pendingNeg = true case enMarkPronoun: contentCount++ role := pendingRole if !sawVerb { role = HistSubject } else { role = pendingRole } pat[len(pat)-1] = SlotNoun pronAtom := tok plem := LemmatizeEN(strings.ToLower(tok)) if plem.Lemma != "" { pronAtom = plem.Lemma } slots = append(slots, pronAtom) roles = append(roles, role) m := uint16(0) if pendingDef { m |= MetaDefDef } slotMorphs = append(slotMorphs, m) slotMarks = append(slotMarks, pendingMark) slotOblRoles = append(slotOblRoles, ORNone) slotHeads = append(slotHeads, -1) slotModKinds = append(slotModKinds, MKNone) newPronIdx := int16(len(slots) - 1) // Ditransitive recipient, mirroring the noun emit branch. if pendingRecipCand { if pendingRecipIdx < 0 { if pendingMark == 0 && pendingOblRole == ORNone { pendingRecipIdx = newPronIdx } else { pendingRecipCand = false } } else { slotOblRoles[pendingRecipIdx] = ORRecip roles[pendingRecipIdx] = HistModifier pendingRecipCand = false pendingRecipIdx = -1 } } pendingMark = 0 pendingDef = false case enMarkPossDet: // Possessive determiner: emit as a POSS modifier of the next // noun. The next slot emission resolves the Head pointer. pat[len(pat)-1] = SlotNoun slots = append(slots, possDetSurface(mk)) roles = append(roles, HistOperator) slotMorphs = append(slotMorphs, 0) slotMarks = append(slotMarks, mk) slotOblRoles = append(slotOblRoles, ORNone) slotHeads = append(slotHeads, -1) slotModKinds = append(slotModKinds, MKNone) pendingHead = int16(len(slots) - 1) pendingModKind = MKPoss case enMarkVerbAux: // Auxiliaries emit no verb slot. After a real verb they only // carry morph onto it; otherwise they stage copula info for // the next noun. if sawVerb && len(slotMorphs) > 0 { lem := LemmatizeEN(low) slotMorphs[len(slotMorphs)-1] |= lem.Morph } else { subj := findSubjectIdx(roles, slotHeads) if subj >= 0 { pendingCop = true pendingCopHead = subj lem := LemmatizeEN(low) pendingCopMorph = lem.Morph pendingCopAuxIsBe = lem.Lemma == "be" if pendingNeg { pendingCopMorph |= MetaPolarNeg pendingNeg = false } } pendingRole = HistComplement } case enMarkPreposition: pendingRole = MarkerToRole(mk) pendingMark = mk pendingOblRole = MarkerToOblRole(mk) case enMarkConjunction: // "and"/"or" is clause coordination after a completed // predication and NP coordination before one. if hasPredication(roles, slotModKinds) { clauseSet := buildSetFromSlices( slots, roles, slotMorphs, slotMarks, slotOblRoles, slotHeads, slotModKinds, ) clauseRelLocal := clauseRel clauseParentLocal := clauseParent if clauseRelLocal == ClauseRoot && len(clauses) > 0 { clauseRelLocal = ClauseAnd } nextParent := int16(len(clauses)) clauses = append(clauses, Clause{ Set: clauseSet, Relation: clauseRelLocal, Parent: clauseParentLocal, HostIdx: -1, }) slots = nil roles = nil slotMorphs = nil slotMarks = nil slotOblRoles = nil slotHeads = nil slotModKinds = nil sawVerb = false contentCount = 0 resetPending() if mk == MkBut { clauseRel = ClauseBut } else { clauseRel = ClauseAnd } clauseParent = nextParent - 1 continue } if len(slots) > 0 { prev := int16(len(slots) - 1) if int(prev) < len(slotModKinds) && slotModKinds[prev] == MKCoord { prev = slotHeads[prev] } pendingCoordHead = prev } else { pendingRole = HistSubject sawVerb = false contentCount = 0 } case enMarkRelative: // Intransitive REL only: if a subject follows ("that I saw"), // the verb attaches to that subject and this head does not // fire. if len(slots) > 0 && roles[len(roles)-1] == HistSubject { pendingRelHead = int16(len(slots) - 1) } } continue } if isENPunct(tok) { continue } // "let" is volitional only at clause start; elsewhere it is the verb // meaning allow. "let's" is unambiguously volitional anywhere, // because the renderer emits "we let's go". if !sawVerb && low == "let's" { pendingVol = true continue } if !sawVerb && contentCount == 0 && low == "let" { pendingVol = true continue } if !sawVerb && contentCount == 0 && len(slots) == 0 { switch low { case "if": clauseRel = ClauseIf continue case "because": clauseRel = ClauseBecause continue } } // Adverbs modify the clause verb. Head resolves backward to the last // verb slot, or forward at the next verb emission. A staged copula // takes precedence: "is fast" is the adjective, "runs fast" the adverb. isAdv := looksLikeAdverb(low) if pendingCop && looksLikePredicateAdj(low) { isAdv = false } if isAdv { contentCount++ pat = append(pat, SlotNoun) slots = append(slots, low) roles = append(roles, HistModifier) slotMorphs = append(slotMorphs, 0) slotMarks = append(slotMarks, 0) slotOblRoles = append(slotOblRoles, ORNone) advIdx := int16(len(slots) - 1) boundHead := int16(-1) for i := int32(advIdx) - 1; i >= 0; i-- { if roles[i] == HistVerb { boundHead = int16(i) break } } if boundHead >= 0 { slotHeads = append(slotHeads, boundHead) slotModKinds = append(slotModKinds, MKAdv) } else { slotHeads = append(slotHeads, -1) slotModKinds = append(slotModKinds, MKAdv) } continue } // Content word. contentCount++ isVerb := looksLikeVerb(low) // Second-verb-no-comma boundary: a verb after a verb with no // preposition or conjunction between them starts a new clause. // Suppressed while a causative is staged: that verb is the embedded // action, not a new clause. if sawVerb && isVerb && pendingMark == 0 && pendingOblRole == ORNone && !pendingCausative { clauseSet := buildSetFromSlices( slots, roles, slotMorphs, slotMarks, slotOblRoles, slotHeads, slotModKinds, ) nextParent := int16(len(clauses)) // A condition or cause clause precedes the main clause, so its // Parent points forward to the index the main clause will take. parent := clauseParent if clauseRel == ClauseIf || clauseRel == ClauseBecause { parent = nextParent } clauses = append(clauses, Clause{ Set: clauseSet, Relation: clauseRel, Parent: parent, HostIdx: -1, }) slots = nil roles = nil slotMorphs = nil slotMarks = nil slotOblRoles = nil slotHeads = nil slotModKinds = nil sawVerb = false contentCount = 1 resetPending() clauseRel = ClauseRoot clauseParent = -1 } // ATTR: current and next are content non-verbs and the token after // next is a verb or a marker or end of input, so current modifies // next: "red car runs". isAttr := false if !isVerb && tokIdx+1 < len(tokens) { next := strings.ToLower(tokens[tokIdx+1]) if _, nextIsMk := enWordToMarker()[next]; !nextIsMk && !isENPunct(tokens[tokIdx+1]) && !looksLikeVerb(next) { if tokIdx+2 >= len(tokens) { isAttr = true } else { afterNext := strings.ToLower(tokens[tokIdx+2]) if _, anIsMk := enWordToMarker()[afterNext]; anIsMk { isAttr = true } else if looksLikeVerb(afterNext) { isAttr = true } } } } // A slot awaiting its head, or a staged copula, or a conjunct makes // the current word a predicate or noun, not a verb. prevWasAttrOrPending := pendingHead >= 0 || pendingCop || pendingCoordHead >= 0 if len(slotModKinds) > 0 && slotModKinds[len(slotModKinds)-1] == MKAttr { prevWasAttrOrPending = true } if !isVerb && !sawVerb && contentCount == 2 && pendingRole == HistObject { // pendingRole was set to Object by a preposition; not a verb. } else if !isVerb && !sawVerb && contentCount == 2 && !isAttr && !prevWasAttrOrPending { isVerb = true } // Predicate adjective: with a copula staged, a predicate-adj surface // or a lemma that is one (bigger -> big) becomes HistComplement with // MKAdj on the subject, carrying the copula's morph plus the lemma's. if pendingCop { useLemma := false var lem LemmaResult if !looksLikePredicateAdj(low) { lem = LemmatizeEN(low) if looksLikePredicateAdj(lem.Lemma) { useLemma = true } } if looksLikePredicateAdj(low) || useLemma { pat = append(pat, SlotNoun) atom := low m := pendingCopMorph if useLemma { atom = lem.Lemma m |= lem.Morph } slots = append(slots, atom) roles = append(roles, HistComplement) if pendingNeg { m |= MetaPolarNeg pendingNeg = false } slotMorphs = append(slotMorphs, m) slotMarks = append(slotMarks, 0) slotOblRoles = append(slotOblRoles, ORNone) slotHeads = append(slotHeads, pendingCopHead) slotModKinds = append(slotModKinds, MKAdj) pendingCop = false pendingCopHead = -1 pendingCopMorph = 0 pendingCopAuxIsBe = false sawVerb = true continue } } // REL-intransitive: the verb is a MKRel modifier of the host noun. // sawVerb stays false so the next real verb is still the predicate. if pendingRelHead >= 0 && isVerb { pat = append(pat, SlotVerb) lem := LemmatizeEN(low) slots = append(slots, lem.Lemma) roles = append(roles, HistModifier) m := lem.Morph if m&MetaNumPlural != 0 { m = (m &^ MetaNumPlural) | Meta3Sg } if pendingNeg { m |= MetaPolarNeg pendingNeg = false } slotMorphs = append(slotMorphs, m) slotMarks = append(slotMarks, 0) slotOblRoles = append(slotOblRoles, ORNone) slotHeads = append(slotHeads, pendingRelHead) slotModKinds = append(slotModKinds, MKRel) pendingRelHead = -1 continue } // Causative: the embedded verb carries MetaCausative and the // suppressed auxiliary's morph. if pendingCausative && isVerb { pat = append(pat, SlotVerb) lem := LemmatizeEN(low) slots = append(slots, lem.Lemma) roles = append(roles, HistVerb) m := lem.Morph | MetaCausative | pendingCausativeMorph if m&MetaNumPlural != 0 { m = (m &^ MetaNumPlural) | Meta3Sg } if pendingNeg { m |= MetaPolarNeg pendingNeg = false } slotMorphs = append(slotMorphs, m) slotMarks = append(slotMarks, 0) slotOblRoles = append(slotOblRoles, ORNone) slotHeads = append(slotHeads, -1) slotModKinds = append(slotModKinds, MKNone) pendingCausative = false pendingCausativeMorph = 0 continue } if !sawVerb && isVerb { lem := LemmatizeEN(low) // Causative aux: "make"/"let" + NP + bare-V suppresses the aux // and stages MetaCausative for the next verb. if (lem.Lemma == "make" || lem.Lemma == "let") && causativeBareVFollows(tokens, tokIdx) { pendingCausative = true pendingCausativeMorph = lem.Morph sawVerb = true pendingRole = HistObject continue } pat = append(pat, SlotVerb) slots = append(slots, lem.Lemma) roles = append(roles, HistVerb) m := lem.Morph if m&MetaNumPlural != 0 { m = (m &^ MetaNumPlural) | Meta3Sg } if pendingNeg { m |= MetaPolarNeg pendingNeg = false } if pendingVol { m |= MetaMoodVol pendingVol = false } // A staged copula followed by a real verb makes the auxiliary a // helper: "be + -ing" is progressive, "be + past participle" // passive. Only a form of "be" plus a past non-progressive // participle yields passive. The auxiliary's own tense wins: // "was bitten" is past passive, "is bitten" is non-past passive. if pendingCop { m |= pendingCopMorph if pendingCopAuxIsBe && m&MetaAspectProg == 0 && m&MetaTensePast != 0 { m |= MetaPassive if pendingCopMorph&MetaTensePast == 0 { m &^= MetaTensePast } } pendingCop = false pendingCopHead = -1 pendingCopMorph = 0 pendingCopAuxIsBe = false } slotMorphs = append(slotMorphs, m) slotMarks = append(slotMarks, 0) slotOblRoles = append(slotOblRoles, ORNone) slotHeads = append(slotHeads, -1) slotModKinds = append(slotModKinds, MKNone) verbIdx := int16(len(slots) - 1) for i := 0; i < int(verbIdx); i++ { if slotModKinds[i] == MKAdv && slotHeads[i] < 0 { slotHeads[i] = verbIdx } } // Ditransitive: the first bare NP is a recipient candidate. It // commits to ORRecip only when a second object arrives, so // "read a book" keeps a plain patient. if isEnDitransitive(lem.Lemma) { pendingRecipCand = true pendingRecipIdx = -1 } sawVerb = true pendingRole = HistObject } else { pat = append(pat, SlotNoun) lem := LemmatizeEN(low) slots = append(slots, lem.Lemma) role := pendingRole if !sawVerb && !pendingCop { role = HistSubject } roles = append(roles, role) m := lem.Morph if pendingDef { m |= MetaDefDef pendingDef = false } slotMorphs = append(slotMorphs, m) slotMarks = append(slotMarks, pendingMark) slotOblRoles = append(slotOblRoles, pendingOblRole) slotHeads = append(slotHeads, -1) slotModKinds = append(slotModKinds, MKNone) newIdx := int16(len(slots) - 1) if pendingRecipCand { if pendingRecipIdx < 0 { if pendingMark == 0 && pendingOblRole == ORNone { pendingRecipIdx = newIdx } else { pendingRecipCand = false } } else { slotOblRoles[pendingRecipIdx] = ORRecip roles[pendingRecipIdx] = HistModifier pendingRecipCand = false pendingRecipIdx = -1 } } // A conjunct inherits the role of its coordinate head. if pendingCoordHead >= 0 && pendingCoordHead < newIdx { slotHeads[newIdx] = pendingCoordHead slotModKinds[newIdx] = MKCoord roles[newIdx] = roles[pendingCoordHead] pendingCoordHead = -1 } // A staged copula makes this noun the predicate of the subject. if pendingCop { slotHeads[newIdx] = pendingCopHead slotModKinds[newIdx] = MKCop slotMorphs[newIdx] |= pendingCopMorph pendingCop = false pendingCopHead = -1 pendingCopMorph = 0 pendingCopAuxIsBe = false } // Resolve a pending POSS/ATTR modifier from a determiner or an // ATTR-detected adjective. ATTR moves the phrase-level // definiteness to the head and the head takes the clause role. if pendingHead >= 0 && pendingHead < newIdx { slotHeads[pendingHead] = newIdx slotModKinds[pendingHead] = pendingModKind if pendingModKind == MKAttr { if slotMorphs[pendingHead]&MetaDefDef != 0 { slotMorphs[newIdx] |= MetaDefDef slotMorphs[pendingHead] &^= MetaDefDef } if roles[pendingHead] == HistSubject { roles[newIdx] = HistSubject } } pendingHead = -1 pendingModKind = MKNone } if isAttr { pendingHead = newIdx pendingModKind = MKAttr } pendingMark = 0 pendingOblRole = ORNone if sawVerb && pendingRole == HistObject { pendingRole = HistComplement } } } // Finalize the last clause. finalSet := buildSetFromSlices( slots, roles, slotMorphs, slotMarks, slotOblRoles, slotHeads, slotModKinds, ) clauses = append(clauses, Clause{ Set: finalSet, Relation: clauseRel, Parent: clauseParent, HostIdx: -1, }) // Flatten Slots/Roles across all clauses so the atom-link layer sees // every word; mid-input clause finalization would otherwise hide all but // the last clause from ExtractResult.Slots. flatSlots := make([]string, 0, len(slots)) flatRoles := make([]int32, 0, len(slots)) for _, c := range clauses { for _, e := range c.Set { flatSlots = append(flatSlots, e.Atom) flatRoles = append(flatRoles, e.Role) } } return ExtractResult{ Pattern: pat, Slots: flatSlots, Roles: flatRoles, DeepPat: buildDeepPat(flatRoles), Set: clauses[0].Set, Discourse: clauses, } }