morph.go raw

   1  package iskra
   2  
   3  // Morph bits on a SetEntry, lemma_en.mx.
   4  const (
   5  	MetaTensePast    uint16 = 1 << 0
   6  	MetaAspectProg   uint16 = 1 << 1
   7  	MetaPolarNeg     uint16 = 1 << 2
   8  	MetaFormalityPol uint16 = 1 << 3
   9  	MetaNumPlural    uint16 = 1 << 4
  10  	MetaDefDef       uint16 = 1 << 5
  11  	MetaMoodVol      uint16 = 1 << 6
  12  	Meta3Sg          uint16 = 1 << 7
  13  	MetaPassive      uint16 = 1 << 8
  14  	MetaCausative    uint16 = 1 << 9
  15  	MetaCompare      uint16 = 1 << 10
  16  )
  17  
  18  // LemmaResult is a lemma with the morph bits it absorbed.
  19  type LemmaResult struct {
  20  	Lemma string
  21  	Morph uint16
  22  	Class uint8
  23  }
  24  
  25  // StripJACopula peels でした / だった / です / だ. lemma_ja.mx stripJACopula,
  26  // including the guard that keeps 噛んだ, 飛んだ and 死んだ from reading as a
  27  // copula.
  28  func StripJACopula(word string) (string, bool, uint16) {
  29  	b := []byte(word)
  30  	strip := func(suffix string, morph uint16) (string, bool, uint16) {
  31  		if len(b) > len(suffix) && string(b[len(b)-len(suffix):]) == suffix {
  32  			return string(b[:len(b)-len(suffix)]), true, morph
  33  		}
  34  		return word, false, 0
  35  	}
  36  	if s, ok, m := strip("でした", MetaTensePast|MetaFormalityPol); ok {
  37  		return s, ok, m
  38  	}
  39  	if s, ok, m := strip("だった", MetaTensePast); ok {
  40  		return s, ok, m
  41  	}
  42  	if s, ok, m := strip("です", MetaFormalityPol); ok {
  43  		return s, ok, m
  44  	}
  45  	if len(b) > 3 && string(b[len(b)-3:]) == "だ" {
  46  		stem := b[:len(b)-3]
  47  		if len(stem) >= 3 {
  48  			tail := string(stem[len(stem)-3:])
  49  			if tail == "ん" || tail == "っ" {
  50  				return word, false, 0
  51  			}
  52  		}
  53  		return string(stem), true, 0
  54  	}
  55  	return word, false, 0
  56  }
  57  
  58  // EndsIn checks whether s ends in the given rune suffix.
  59  func EndsIn(s, suffix string) bool {
  60  	return len(s) >= len(suffix) && s[len(s)-len(suffix):] == suffix
  61  }
  62  
  63  // EndsInIKana reports a final い.
  64  func EndsInIKana(s string) bool { return EndsIn(s, "い") }
  65  
  66  // EndsInKuKana reports a final く, the く-form adverb ending.
  67  func EndsInKuKana(s string) bool { return EndsIn(s, "く") }
  68  
  69  // EndsInTaKana reports a final た, the past-tense ending.
  70  func EndsInTaKana(s string) bool { return EndsIn(s, "た") }
  71  
  72  // EndsInNaiSuffix reports a final ない.
  73  func EndsInNaiSuffix(s string) bool { return EndsIn(s, "ない") }
  74  
  75  // EndsInTaiSuffix reports a final たい.
  76  func EndsInTaiSuffix(s string) bool { return EndsIn(s, "たい") }
  77  
  78  // IsJABareKanjiAdj reports one of the colour and size kanji used attributively.
  79  func IsJABareKanjiAdj(s string) bool { return endsInKanjiIteration(s) }
  80  
  81  func endsInKanjiIteration(s string) bool {
  82  	if len(s) != 3 && len([]rune(s)) != 1 {
  83  		return false
  84  	}
  85  	for _, k := range kanjiAdjPrefixes {
  86  		if s == k {
  87  			return true
  88  		}
  89  	}
  90  	return false
  91  }
  92  
  93  var ditransitiveJA = []string{"あげ", "や", "くれ", "渡", "送", "教え", "見せ", "売", "買", "貸", "払", "与え"}
  94  
  95  // IsJADitransitive reports a verb lemma in the closed ditransitive set.
  96  func IsJADitransitive(lemma string) bool {
  97  	for _, v := range ditransitiveJA {
  98  		if lemma == v {
  99  			return true
 100  		}
 101  	}
 102  	return false
 103  }
 104  
 105  var temporalNounExtra = []string{"今", "朝", "昼", "夜", "夕方", "いつ"}
 106  
 107  // IsJATemporalNoun reports a temporal noun that acts as an adverbial.
 108  func IsJATemporalNoun(s string) bool {
 109  	for _, t := range temporalNouns {
 110  		if s == t {
 111  			return true
 112  		}
 113  	}
 114  	for _, t := range temporalNounExtra {
 115  		if s == t {
 116  			return true
 117  		}
 118  	}
 119  	return false
 120  }
 121  
 122  var relationalNouns = []string{"中", "内", "外", "上", "下", "前", "後", "横", "隣", "間"}
 123  
 124  // IsJARelationalNoun reports the の-relational-noun-に compound head.
 125  func IsJARelationalNoun(s string) bool {
 126  	for _, r := range relationalNouns {
 127  		if s == r {
 128  			return true
 129  		}
 130  	}
 131  	return false
 132  }
 133  
 134  // JARelationalNounOblRole is ORLoc for every relational noun.
 135  func JARelationalNounOblRole(s string) uint8 {
 136  	if IsJARelationalNoun(s) {
 137  		return ORLoc
 138  	}
 139  	return ORNone
 140  }
 141  
 142  // MorphemeEndings are the JA verb and adjective endings a stem stripper peels.
 143  // The full conjugation table in lemma_ja.mx has 38 entries, each with a morph
 144  // but not with the verb class or the stem rewrite (五段 -k-u to -k-i and the
 145  // like). Stripping the suffix and recording its morph is exact; classifying
 146  // the verb and rewriting the stem is not, so Class stays VClassNone.
 147  var MorphemeEndings = []struct {
 148  	Suffix string
 149  	Morph  uint16
 150  }{
 151  	{"させられた", MetaCausative | MetaPassive | MetaTensePast},
 152  	{"させられる", MetaCausative | MetaPassive},
 153  	{"させます", MetaCausative | MetaFormalityPol},
 154  	{"させる", MetaCausative},
 155  	{"られなかった", MetaPassive | MetaPolarNeg | MetaTensePast},
 156  	{"られない", MetaPassive | MetaPolarNeg},
 157  	{"られます", MetaPassive | MetaFormalityPol},
 158  	{"られた", MetaPassive | MetaTensePast},
 159  	{"られる", MetaPassive},
 160  	{"なかった", MetaPolarNeg | MetaTensePast},
 161  	{"ない", MetaPolarNeg},
 162  	{"ませんでした", MetaPolarNeg | MetaFormalityPol | MetaTensePast},
 163  	{"ません", MetaPolarNeg | MetaFormalityPol},
 164  	{"ました", MetaTensePast | MetaFormalityPol},
 165  	{"ます", MetaFormalityPol},
 166  	{"ています", MetaAspectProg | MetaFormalityPol},
 167  	{"ていました", MetaAspectProg | MetaFormalityPol | MetaTensePast},
 168  	{"ていなかった", MetaAspectProg | MetaPolarNeg | MetaTensePast},
 169  	{"ていない", MetaAspectProg | MetaPolarNeg},
 170  	{"ていた", MetaAspectProg | MetaTensePast},
 171  	{"ている", MetaAspectProg},
 172  	{"たくない", MetaPolarNeg},
 173  	{"たい", 0},
 174  	{"なかった", MetaPolarNeg | MetaTensePast},
 175  	{"すぎた", MetaTensePast},
 176  	{"った", MetaTensePast},
 177  	{"した", MetaTensePast},
 178  	{"かった", MetaTensePast},
 179  }
 180  
 181  // StripJAConjugation peels the longest matching ending and returns the stem and
 182  // the morph bits. ok is false when nothing matched.
 183  func StripJAConjugation(word string) (stem string, morph uint16, ok bool) {
 184  	best := -1
 185  	for i, e := range MorphemeEndings {
 186  		if len(word) > len(e.Suffix) && EndsIn(word, e.Suffix) {
 187  			if best < 0 || len(e.Suffix) > len(MorphemeEndings[best].Suffix) {
 188  				best = i
 189  			}
 190  		}
 191  	}
 192  	if best < 0 {
 193  		return word, 0, false
 194  	}
 195  	e := MorphemeEndings[best]
 196  	return word[:len(word)-len(e.Suffix)], e.Morph, true
 197  }
 198  
 199  // LemmatizeJA reduces a JA surface form to a stem and the morph bits it
 200  // carries: the copula first, then the longest conjugation ending. isVerb is the
 201  // extractor's hint that the form is a predicate, which the copula stripper uses
 202  // to leave a verb's past form alone.
 203  //
 204  // Class is always VClassNone: the conjugation table has no verb class and no
 205  // stem rewrite (書きます -> 書く, not 書き), so a lemma here is the stem the
 206  // endings leave, not the dictionary form. RenderJA has to conjugate from that
 207  // stem and needs the class from somewhere else.
 208  func LemmatizeJA(word string, isVerb bool) LemmaResult {
 209  	if word == "" || word == UntranslatedMarker {
 210  		return LemmaResult{Lemma: word}
 211  	}
 212  	if s, ok, m := StripJACopula(word); ok && (!isVerb || m == 0) {
 213  		return LemmaResult{Lemma: s, Morph: m}
 214  	}
 215  	if s, m, ok := StripJAConjugation(word); ok {
 216  		return LemmaResult{Lemma: s, Morph: m}
 217  	}
 218  	if endsInIKanaRune(word) {
 219  		return LemmaResult{Lemma: word, Class: VClassIAdj}
 220  	}
 221  	return LemmaResult{Lemma: word}
 222  }
 223  
 224  // Verb classes, lemma_en.mx lines 40-56. They are what RenderJA conjugates
 225  // from; the Go lemmatisers record VClassNone for a verb because neither the
 226  // moxie conjugation table nor the ported stem stripper holds the class.
 227  const (
 228  	VClassNone     uint8 = 0
 229  	VClassIchidan  uint8 = 1
 230  	VClassGodanKu  uint8 = 2
 231  	VClassGodanGu  uint8 = 3
 232  	VClassGodanSu  uint8 = 4
 233  	VClassGodanTsu uint8 = 5
 234  	VClassGodanNu  uint8 = 6
 235  	VClassGodanBu  uint8 = 7
 236  	VClassGodanMu  uint8 = 8
 237  	VClassGodanRu  uint8 = 9
 238  	VClassGodanU   uint8 = 10
 239  	VClassSuru     uint8 = 11
 240  	VClassKuru     uint8 = 12
 241  	VClassIAdj     uint8 = 13
 242  	VClassBare     uint8 = 14
 243  )
 244  
 245  func endsInIKanaRune(s string) bool { return EndsIn(s, "い") }
 246