package iskra import ( "strings" ) // Punctuation classes from the language tokenizers. const ( punctNone uint8 = 0 punctHard uint8 = 1 // sentence terminator: . ! ? 。 ! ? punctSoft uint8 = 2 // clause boundary: , ; 、 : — … punctSkip uint8 = 3 // quotes, brackets, decorative ) // ClauseSplitToken is the synthetic boundary both tokenizers emit for soft and // hard punctuation: the JA comma 、. const ClauseSplitToken = "、" // ClassifyPunct classifies the punctuation run starting at b[i]. func ClassifyPunct(b []byte, i int) (int, uint8) { if i >= len(b) { return 0, punctNone } c := b[i] if c < 0x80 { switch c { case '.', '!', '?': return 1, punctHard case ',', ';', ':': return 1, punctSoft case '"', '(', ')', '[', ']', '{', '}': return 1, punctSkip } return 0, punctNone } if i+2 >= len(b) { return 0, punctNone } b1, b2 := b[i+1], b[i+2] if c == 0xe3 && b1 == 0x80 { switch b2 { case 0x80, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x91: return 3, punctSkip case 0x81: return 3, punctSoft case 0x82: return 3, punctHard } } if c == 0xef && b1 == 0xbc { switch b2 { case 0x81, 0x9f, 0x8e: return 3, punctHard case 0x8c, 0x9b, 0x9a: return 3, punctSoft case 0x88, 0x89: return 3, punctSkip } } if c == 0xe2 && b1 == 0x80 { switch b2 { case 0x94, 0x95, 0xa6: return 3, punctSoft case 0x9c, 0x9d, 0x98, 0x99: return 3, punctSkip } } if c == 0xe2 && b1 == 0x99 { switch b2 { case 0xaa, 0xab, 0xac, 0xad: return 3, punctSoft } } return 0, punctNone } // ClassifyPunctRun returns the byte length and the strongest class of the run // of punctuation starting at b[i]. func ClassifyPunctRun(b []byte, i int) (int, uint8) { total := 0 kind := punctNone for { n, k := ClassifyPunct(b, i+total) if n == 0 { break } total += n if k > kind { kind = k } } return total, kind } // TokenizeEN splits an English sentence, splitting n't contractions and // emitting ClauseSplitToken for soft and hard punctuation. A skip-class run // alone emits nothing: the moxie tokenizer emits the split token for the whole // run whenever any member of it is soft or hard, so "book." splits the clause. func TokenizeEN(s string) []string { b := []byte(s) var tokens []string for i := 0; i < len(b); { if b[i] == ' ' || b[i] == '\t' || b[i] == '\n' || b[i] == '\r' { i++ continue } if b[i] == '[' && i+9 <= len(b) && string(b[i:i+9]) == UntranslatedMarker { tokens = append(tokens, UntranslatedMarker) i += 9 continue } if n, kind := ClassifyPunctRun(b, i); n > 0 { if kind == punctSoft || kind == punctHard { tokens = append(tokens, ClauseSplitToken) } i += n continue } if b[i] == '\'' { tokens = append(tokens, "'") i++ continue } start := i for i < len(b) && b[i] != ' ' && b[i] != '\t' && b[i] != '\n' && b[i] != '\r' { if b[i] == '\'' { if i+1 < len(b) && isLetter(b[i+1]) { i++ continue } break } if n, _ := ClassifyPunct(b, i); n > 0 { break } i++ } if i > start { tok := string(b[start:i]) tl := len(tok) if tl > 3 && (tok[tl-3] == 'n' || tok[tl-3] == 'N') && tok[tl-2] == '\'' && (tok[tl-1] == 't' || tok[tl-1] == 'T') { base := tok[:tl-3] switch strings.ToLower(base) { case "wo": base = "will" case "ca": base = "can" case "sha": base = "shall" } tokens = append(tokens, base, "n't") } else { tokens = append(tokens, tok) } } } return tokens } // jaParticles lists every JA particle the tokenizer splits on, longest first at // each length so から wins over か and まで over ま. func jaParticles() []string { return []string{ "から", "まで", "より", "けど", "は", "が", "を", "に", "で", "へ", "も", "の", "と", "か", "よ", "ね", "★", "☆", "〇", "◯", } } // TokenizeJA splits a Japanese sentence: particles come out as their own // tokens, the content between them accumulates, temporal nouns and kanji // adjective prefixes split off the front of a content run, and soft and hard // punctuation both emit ClauseSplitToken. func TokenizeJA(s string) []string { b := []byte(s) var tokens []string for i := 0; i < len(b); { if b[i] == ' ' || b[i] == '\t' || b[i] == '\n' || b[i] == '\r' { i++ continue } if b[i] == '[' && i+9 <= len(b) && string(b[i:i+9]) == UntranslatedMarker { tokens = append(tokens, UntranslatedMarker) i += 9 continue } if n, kind := ClassifyPunctRun(b, i); n > 0 { if kind == punctSoft || kind == punctHard { tokens = append(tokens, ClauseSplitToken) } i += n continue } if isPartOfMoshiPrefix(b, i) { tokens = append(tokens, "もし") i += 6 continue } matched := false for _, p := range jaParticles() { if strings.HasPrefix(string(b[i:]), p) && jaParticleFits(b, i, p) { tokens = append(tokens, p) i += len(p) matched = true break } } if matched { continue } if tlen := tryMatchJATemporalPrefix(b, i); tlen > 0 { tokens = append(tokens, string(b[i:i+tlen])) i += tlen continue } if isJAKanjiAdjPrefix(b, i) { tokens = append(tokens, string(b[i:i+3])) i += 3 continue } start := i for i < len(b) && !isJASkippable(b, i) { if i+3 <= len(b) && b[i] == 0xe3 && b[i+1] == 0x80 && b[i+2] == 0x81 { break } hit := false for _, p := range jaParticles() { if strings.HasPrefix(string(b[i:]), p) && jaParticleFits(b, i, p) { hit = true break } } if hit { break } if i+6 <= len(b) { curHira := b[i] == 0xe3 && (b[i+1] == 0x81 || b[i+1] == 0x82) nextKanji := b[i+3] >= 0xe4 && b[i+3] <= 0xe9 if curHira && nextKanji { i += 3 break } } i += utf8Len(b, i) } if i > start { tokens = append(tokens, string(b[start:i])) } else { i += utf8Len(b, i) } } return tokens } // jaParticleFits applies the compound suppressors: です/でした for で, volitional // stems for よ/こ/そ/と/の/ぼ/も/ろ/お, the conditional prefix for もし, and the // conjugation compounds for から/か/で/も. func jaParticleFits(b []byte, i int, p string) bool { switch p { case "で": if isPartOfDesuCompound(b, i) { return false } } if isPartOfVolitionalCompound(b, i) { return false } if isPartOfMoshiPrefix(b, i) { return false } return !isPartOfConjugation(b, i, -1) } func isLetter(c byte) bool { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') } func utf8Len(b []byte, i int) int { switch { case b[i] < 0x80: return 1 case b[i] < 0xE0: return 2 case b[i] < 0xF0: return 3 default: return 4 } } // isPartOfVolitionalCompound reports a volitional stem followed by う. func isPartOfVolitionalCompound(b []byte, i int) bool { if i+6 > len(b) || b[i+3] != 0xe3 || b[i+4] != 0x81 || b[i+5] != 0x86 || b[i] != 0xe3 { return false } if b[i+1] == 0x82 && (b[i+2] == 0x88 || b[i+2] == 0x82 || b[i+2] == 0x8d) { return true } if b[i+1] == 0x81 { switch b[i+2] { case 0x93, 0x94, 0x9d, 0xa8, 0xae, 0xbc, 0x8a: return true } } return false } // isPartOfMoshiPrefix reports もし at the start of the sentence. func isPartOfMoshiPrefix(b []byte, i int) bool { if i != 0 || i+6 > len(b) { return false } return b[0] == 0xe3 && b[1] == 0x82 && b[2] == 0x82 && b[3] == 0xe3 && b[4] == 0x81 && b[5] == 0x97 } // isPartOfDesuCompound reports です or でした, where で is not a particle. func isPartOfDesuCompound(b []byte, i int) bool { if i+3 > len(b) || !(b[i] == 0xe3 && b[i+1] == 0x81 && b[i+2] == 0xa7) { return false } if i+6 <= len(b) && b[i+3] == 0xe3 && b[i+4] == 0x81 && b[i+5] == 0x99 { return true } return i+9 <= len(b) && b[i+3] == 0xe3 && b[i+4] == 0x81 && b[i+5] == 0x97 && b[i+6] == 0xe3 && b[i+7] == 0x81 && b[i+8] == 0x9f } // temporalNouns split off the front of a content run. var temporalNouns = []string{ "昨日", "今日", "明日", "昨夜", "今朝", "今晩", "今夜", "来週", "先週", "来月", "先月", "来年", "去年", "毎日", "毎週", "毎月", "毎年", } // tryMatchJATemporalPrefix returns the byte length of a temporal noun at i when // it is followed by more content. func tryMatchJATemporalPrefix(b []byte, i int) int { for _, t := range temporalNouns { if !strings.HasPrefix(string(b[i:]), t) { continue } next := i + len(t) if next >= len(b) { return 0 } if b[next] >= 0xe0 && b[next] < 0xf0 { return len(t) } return 0 } return 0 } // kanjiAdjPrefixes are colour and size kanji that split off before a noun. var kanjiAdjPrefixes = []string{"赤", "白", "黒", "大", "小", "新", "古", "長", "高", "低"} // isJAKanjiAdjPrefix reports one of the adjective kanji followed by a kanji. func isJAKanjiAdjPrefix(b []byte, i int) bool { if i+6 > len(b) || b[i+3] < 0xe4 || b[i+3] > 0xe9 { return false } for _, k := range kanjiAdjPrefixes { if strings.HasPrefix(string(b[i:]), k) { return true } } return false } // isJASkippable reports bytes that end a content run: whitespace and // punctuation other than the clause comma. func isJASkippable(b []byte, i int) bool { if i >= len(b) { return false } switch b[i] { case ' ', '\t', '\n', '\r': return true case '.', ',', '!', '?', ';', ':', '"', '\'', '(', ')', '[', ']', '{', '}': return true } if i+2 < len(b) && b[i] == 0xe3 && b[i+1] == 0x80 { return b[i+2] != 0x81 } return false } // isPartOfConjugation reports particles that are really verb morphology. func isPartOfConjugation(b []byte, i, contentStart int) bool { if i+3 > len(b) || b[i] != 0xe3 { return false } p1, p2 := b[i+1], b[i+2] if p1 == 0x81 && p2 == 0x8b { if i+6 <= len(b) && b[i+3] == 0xe3 && b[i+4] == 0x82 && b[i+5] == 0x89 { if i+9 <= len(b) && b[i+6] == 0xe3 && b[i+7] == 0x81 && b[i+8] == 0xaa { return true } } if i >= 3 && b[i-3] == 0xe3 && b[i-2] == 0x81 && b[i-1] == 0xaa && i+6 <= len(b) { if b[i+3] == 0xe3 && b[i+4] == 0x81 && b[i+5] == 0xa3 { return true } if b[i+3] == 0xe3 && b[i+4] == 0x82 && b[i+5] == 0x8d { return true } } return i+6 <= len(b) && b[i+3] == 0xe3 && b[i+4] == 0x81 && b[i+5] == 0xaa } if p1 == 0x81 && p2 == 0xa7 { if i+6 <= len(b) && b[i+3] == 0xe3 && b[i+4] == 0x81 && b[i+5] == 0x8d { return true } if i >= 6 && b[i-6] == 0xe3 && b[i-5] == 0x81 && b[i-4] == 0xaa && b[i-3] == 0xe3 && b[i-2] == 0x81 && b[i-1] == 0x84 { return true } return i >= 3 && (contentStart < 0 || contentStart <= i-3) && b[i-3] == 0xe3 && b[i-2] == 0x82 && b[i-1] == 0x93 } if p1 == 0x82 && p2 == 0x82 { if i+6 <= len(b) && b[i+3] == 0xe3 && b[i+4] == 0x82 && b[i+5] == 0x89 { return true } return i >= 3 && b[i-3] == 0xe3 && b[i-2] == 0x81 && b[i-1] == 0xa6 } return false }