package iskra // Morph bits on a SetEntry, lemma_en.mx. const ( MetaTensePast uint16 = 1 << 0 MetaAspectProg uint16 = 1 << 1 MetaPolarNeg uint16 = 1 << 2 MetaFormalityPol uint16 = 1 << 3 MetaNumPlural uint16 = 1 << 4 MetaDefDef uint16 = 1 << 5 MetaMoodVol uint16 = 1 << 6 Meta3Sg uint16 = 1 << 7 MetaPassive uint16 = 1 << 8 MetaCausative uint16 = 1 << 9 MetaCompare uint16 = 1 << 10 ) // LemmaResult is a lemma with the morph bits it absorbed. type LemmaResult struct { Lemma string Morph uint16 Class uint8 } // StripJACopula peels でした / だった / です / だ. lemma_ja.mx stripJACopula, // including the guard that keeps 噛んだ, 飛んだ and 死んだ from reading as a // copula. func StripJACopula(word string) (string, bool, uint16) { b := []byte(word) strip := func(suffix string, morph uint16) (string, bool, uint16) { if len(b) > len(suffix) && string(b[len(b)-len(suffix):]) == suffix { return string(b[:len(b)-len(suffix)]), true, morph } return word, false, 0 } if s, ok, m := strip("でした", MetaTensePast|MetaFormalityPol); ok { return s, ok, m } if s, ok, m := strip("だった", MetaTensePast); ok { return s, ok, m } if s, ok, m := strip("です", MetaFormalityPol); ok { return s, ok, m } if len(b) > 3 && string(b[len(b)-3:]) == "だ" { stem := b[:len(b)-3] if len(stem) >= 3 { tail := string(stem[len(stem)-3:]) if tail == "ん" || tail == "っ" { return word, false, 0 } } return string(stem), true, 0 } return word, false, 0 } // EndsIn checks whether s ends in the given rune suffix. func EndsIn(s, suffix string) bool { return len(s) >= len(suffix) && s[len(s)-len(suffix):] == suffix } // EndsInIKana reports a final い. func EndsInIKana(s string) bool { return EndsIn(s, "い") } // EndsInKuKana reports a final く, the く-form adverb ending. func EndsInKuKana(s string) bool { return EndsIn(s, "く") } // EndsInTaKana reports a final た, the past-tense ending. func EndsInTaKana(s string) bool { return EndsIn(s, "た") } // EndsInNaiSuffix reports a final ない. func EndsInNaiSuffix(s string) bool { return EndsIn(s, "ない") } // EndsInTaiSuffix reports a final たい. func EndsInTaiSuffix(s string) bool { return EndsIn(s, "たい") } // IsJABareKanjiAdj reports one of the colour and size kanji used attributively. func IsJABareKanjiAdj(s string) bool { return endsInKanjiIteration(s) } func endsInKanjiIteration(s string) bool { if len(s) != 3 && len([]rune(s)) != 1 { return false } for _, k := range kanjiAdjPrefixes { if s == k { return true } } return false } var ditransitiveJA = []string{"あげ", "や", "くれ", "渡", "送", "教え", "見せ", "売", "買", "貸", "払", "与え"} // IsJADitransitive reports a verb lemma in the closed ditransitive set. func IsJADitransitive(lemma string) bool { for _, v := range ditransitiveJA { if lemma == v { return true } } return false } var temporalNounExtra = []string{"今", "朝", "昼", "夜", "夕方", "いつ"} // IsJATemporalNoun reports a temporal noun that acts as an adverbial. func IsJATemporalNoun(s string) bool { for _, t := range temporalNouns { if s == t { return true } } for _, t := range temporalNounExtra { if s == t { return true } } return false } var relationalNouns = []string{"中", "内", "外", "上", "下", "前", "後", "横", "隣", "間"} // IsJARelationalNoun reports the の-relational-noun-に compound head. func IsJARelationalNoun(s string) bool { for _, r := range relationalNouns { if s == r { return true } } return false } // JARelationalNounOblRole is ORLoc for every relational noun. func JARelationalNounOblRole(s string) uint8 { if IsJARelationalNoun(s) { return ORLoc } return ORNone } // MorphemeEndings are the JA verb and adjective endings a stem stripper peels. // The full conjugation table in lemma_ja.mx has 38 entries, each with a morph // but not with the verb class or the stem rewrite (五段 -k-u to -k-i and the // like). Stripping the suffix and recording its morph is exact; classifying // the verb and rewriting the stem is not, so Class stays VClassNone. var MorphemeEndings = []struct { Suffix string Morph uint16 }{ {"させられた", MetaCausative | MetaPassive | MetaTensePast}, {"させられる", MetaCausative | MetaPassive}, {"させます", MetaCausative | MetaFormalityPol}, {"させる", MetaCausative}, {"られなかった", MetaPassive | MetaPolarNeg | MetaTensePast}, {"られない", MetaPassive | MetaPolarNeg}, {"られます", MetaPassive | MetaFormalityPol}, {"られた", MetaPassive | MetaTensePast}, {"られる", MetaPassive}, {"なかった", MetaPolarNeg | MetaTensePast}, {"ない", MetaPolarNeg}, {"ませんでした", MetaPolarNeg | MetaFormalityPol | MetaTensePast}, {"ません", MetaPolarNeg | MetaFormalityPol}, {"ました", MetaTensePast | MetaFormalityPol}, {"ます", MetaFormalityPol}, {"ています", MetaAspectProg | MetaFormalityPol}, {"ていました", MetaAspectProg | MetaFormalityPol | MetaTensePast}, {"ていなかった", MetaAspectProg | MetaPolarNeg | MetaTensePast}, {"ていない", MetaAspectProg | MetaPolarNeg}, {"ていた", MetaAspectProg | MetaTensePast}, {"ている", MetaAspectProg}, {"たくない", MetaPolarNeg}, {"たい", 0}, {"なかった", MetaPolarNeg | MetaTensePast}, {"すぎた", MetaTensePast}, {"った", MetaTensePast}, {"した", MetaTensePast}, {"かった", MetaTensePast}, } // StripJAConjugation peels the longest matching ending and returns the stem and // the morph bits. ok is false when nothing matched. func StripJAConjugation(word string) (stem string, morph uint16, ok bool) { best := -1 for i, e := range MorphemeEndings { if len(word) > len(e.Suffix) && EndsIn(word, e.Suffix) { if best < 0 || len(e.Suffix) > len(MorphemeEndings[best].Suffix) { best = i } } } if best < 0 { return word, 0, false } e := MorphemeEndings[best] return word[:len(word)-len(e.Suffix)], e.Morph, true } // LemmatizeJA reduces a JA surface form to a stem and the morph bits it // carries: the copula first, then the longest conjugation ending. isVerb is the // extractor's hint that the form is a predicate, which the copula stripper uses // to leave a verb's past form alone. // // Class is always VClassNone: the conjugation table has no verb class and no // stem rewrite (書きます -> 書く, not 書き), so a lemma here is the stem the // endings leave, not the dictionary form. RenderJA has to conjugate from that // stem and needs the class from somewhere else. func LemmatizeJA(word string, isVerb bool) LemmaResult { if word == "" || word == UntranslatedMarker { return LemmaResult{Lemma: word} } if s, ok, m := StripJACopula(word); ok && (!isVerb || m == 0) { return LemmaResult{Lemma: s, Morph: m} } if s, m, ok := StripJAConjugation(word); ok { return LemmaResult{Lemma: s, Morph: m} } if endsInIKanaRune(word) { return LemmaResult{Lemma: word, Class: VClassIAdj} } return LemmaResult{Lemma: word} } // Verb classes, lemma_en.mx lines 40-56. They are what RenderJA conjugates // from; the Go lemmatisers record VClassNone for a verb because neither the // moxie conjugation table nor the ported stem stripper holds the class. const ( VClassNone uint8 = 0 VClassIchidan uint8 = 1 VClassGodanKu uint8 = 2 VClassGodanGu uint8 = 3 VClassGodanSu uint8 = 4 VClassGodanTsu uint8 = 5 VClassGodanNu uint8 = 6 VClassGodanBu uint8 = 7 VClassGodanMu uint8 = 8 VClassGodanRu uint8 = 9 VClassGodanU uint8 = 10 VClassSuru uint8 = 11 VClassKuru uint8 = 12 VClassIAdj uint8 = 13 VClassBare uint8 = 14 ) func endsInIKanaRune(s string) bool { return EndsIn(s, "い") }