Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0258a40b0d | |||
| f6a8752d00 |
+56
-36
@@ -11,6 +11,7 @@ import (
|
||||
"unicode"
|
||||
|
||||
"github.com/kami/maven/internal/ipc"
|
||||
"github.com/kami/maven/internal/morph"
|
||||
"github.com/kami/maven/internal/phraser"
|
||||
)
|
||||
|
||||
@@ -52,29 +53,39 @@ func (h *reactiveHandler) resolveQuietToggle(ctx context.Context, text string, s
|
||||
return reply, true
|
||||
}
|
||||
|
||||
// quietInflections — the inflectional endings a stem may carry and still be
|
||||
// the same word. Adjective/adverb/noun/verb endings, all ≤3 letters. This is
|
||||
// what separates "тихий"/"тихом"/"тихо" (stem "тих" + a real ending) from
|
||||
// "тихонько"/"потихоньку", which are different words: "онько" is not an
|
||||
// ending, and "потихоньку" doesn't start with the stem at all.
|
||||
var quietInflections = []string{
|
||||
"", "а", "е", "и", "й", "о", "у", "ы", "ю", "я",
|
||||
"ая", "ее", "ей", "ем", "ие", "ий", "им", "их", "ия", "ию", "ое", "ой", "ом", "ую", "ые", "ый", "ым", "ых", "ья",
|
||||
"ами", "ого", "ому", "ыми", "ать", "ить", "ять",
|
||||
}
|
||||
|
||||
// quietStem reports whether tok is the given stem carrying at most one
|
||||
// inflectional ending. Word boundaries come from tokenisation (see
|
||||
// quietTokens), not from a regexp — Go's \b is ASCII-oriented and treats every
|
||||
// Cyrillic letter as a non-word character, so `\bтих\b` would happily match
|
||||
// inside "тихонько". Comparing whole tokens sidesteps that entirely.
|
||||
func quietStem(tok, stem string) bool {
|
||||
if !strings.HasPrefix(tok, stem) {
|
||||
return false
|
||||
}
|
||||
suffix := tok[len(stem):]
|
||||
for _, e := range quietInflections {
|
||||
if suffix == e {
|
||||
// quietStem reports whether tok is one of the words a vocabulary slot accepts.
|
||||
// A slot is written as alternatives joined by "|", and an alternative comes in
|
||||
// two flavours:
|
||||
//
|
||||
// - a dictionary form, matched through the dictionary, so every case and
|
||||
// gender of it counts. This is what the nouns and adjectives want: "тихий",
|
||||
// "тихом", "тихо" and "тише" are one word.
|
||||
// - a form prefixed with "=", matched as the exact token. This is what the
|
||||
// VERBS want, and it is not a shortcut. A command is an imperative, and the
|
||||
// dictionary quite correctly files "говори" and "говорил" under one lemma —
|
||||
// so lemma-matching a verb slot read "он говорил тихим голосом весь вечер",
|
||||
// a remark about his evening, as an order to go quiet. Aspect pairs are two
|
||||
// separate verbs, which is why several imperatives are listed by hand.
|
||||
//
|
||||
// Word boundaries come from tokenisation (see quietTokens), not from a regexp —
|
||||
// Go's \b is ASCII-oriented and treats every Cyrillic letter as a non-word
|
||||
// character, so `\bтих\b` would happily match inside "тихонько". Comparing whole
|
||||
// tokens sidesteps that entirely.
|
||||
//
|
||||
// The comparison is a dictionary lookup, not a stem plus a list of 36 endings
|
||||
// (Vikunja #526). The distinction the old comment described is exactly the one a
|
||||
// dictionary makes: "тихий", "тихом", "тихо" and "тише" are one word inflected,
|
||||
// while "тихонько" and "потихоньку" are different words — and the dictionary
|
||||
// knows that without anybody deciding that "онько" is not an ending.
|
||||
func quietStem(tok, slot string) bool {
|
||||
for _, form := range strings.Split(slot, "|") {
|
||||
if exact, ok := strings.CutPrefix(form, "="); ok {
|
||||
if tok == exact {
|
||||
return true
|
||||
}
|
||||
continue
|
||||
}
|
||||
if morph.SameWord(tok, form) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -117,7 +128,10 @@ func quietPhrase(tokens, pattern []string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// quietOffPhrases / quietOnPhrases — the toggle vocabulary, as stem sequences.
|
||||
// quietOffPhrases / quietOnPhrases — the toggle vocabulary, as sequences of
|
||||
// dictionary forms. They used to be truncated stems ("тих", "выключ"), which is
|
||||
// what the ending list existed to complete; a dictionary form needs no
|
||||
// completing (Vikunja #526).
|
||||
//
|
||||
// Note what is NOT here any more: the OFF list used to carry {"не", "тих"} and
|
||||
// the ON list {"не", "шум"} / {"не", "беспоко"}. Both were adjacency patterns,
|
||||
@@ -128,36 +142,42 @@ func quietPhrase(tokens, pattern []string) bool {
|
||||
var (
|
||||
quietOffPhrases = [][]string{
|
||||
{"quiet", "off"}, {"quiet", "end"},
|
||||
{"громк", "режим"}, {"шумн", "режим"},
|
||||
{"отмен", "тих"}, {"выключ", "тих"},
|
||||
{"громкий", "режим"}, {"шумный", "режим"},
|
||||
{"=отмени|=отменяй|=отменить", "тихий"},
|
||||
{"=выключи|=выключай|=выключить", "тихий"},
|
||||
}
|
||||
quietOnPhrases = [][]string{
|
||||
{"quiet", "on"}, {"quiet", "mode"},
|
||||
{"тих", "режим"}, {"не", "шум"}, {"не", "беспоко"},
|
||||
{"тихий", "режим"}, {"не", "=шуми|=шумите"}, {"не", "=беспокой|=беспокоить"},
|
||||
// The noun form and the comparative. "режим тишины" is how the
|
||||
// setting is named half the time, and "сделай потише" is how it is
|
||||
// actually asked for out loud. Both used to fall through to the
|
||||
// router, which has no quiet intent, so the command did nothing.
|
||||
{"режим", "тишин"}, {"сделай", "тише"}, {"сделай", "потише"},
|
||||
{"говори", "тише"}, {"будь", "потише"},
|
||||
{"тих"}, {"потише"},
|
||||
{"режим", "тишина"}, {"=сделай", "тихий"}, {"=сделай", "потише"},
|
||||
{"=говори", "тихий"}, {"=будь", "потише"},
|
||||
{"тихий"}, {"потише"},
|
||||
}
|
||||
)
|
||||
|
||||
// quietWordStems — every stem that names the setting. Used by the
|
||||
// quietWordStems — every word that names the setting. Used by the
|
||||
// negated-but-unmatched fallback in classifyQuietToggle, which has to
|
||||
// recognise "хватит тишины" without an ON phrase having matched.
|
||||
var quietWordStems = []string{"тих", "тишин", "потише"}
|
||||
var quietWordStems = []string{"тихий", "тишина", "потише"}
|
||||
|
||||
// quietNegatorWords — negators that are whole words with no useful stem.
|
||||
var quietNegatorWords = map[string]bool{
|
||||
"не": true, "нет": true, "хватит": true, "no": true, "not": true, "off": true,
|
||||
}
|
||||
|
||||
// quietNegatorStems — negators that inflect. Matched through quietStem, the
|
||||
// same one-ending rule the toggle vocabulary uses, so "выключи", "выключить"
|
||||
// and "выключай" all count and "выключатель" does not.
|
||||
var quietNegatorStems = []string{"выключ", "отмен", "прекрат", "убер", "stop", "cancel", "disable"}
|
||||
// quietNegatorStems — negators that inflect. Imperatives, matched exactly for
|
||||
// the reason quietStem gives: "выключи" is a command and "выключил" is a report
|
||||
// about earlier, and one lemma covers both. "выключатель" was never a negator
|
||||
// and is not one now.
|
||||
var quietNegatorStems = []string{
|
||||
"=выключи|=выключай|=выключить", "=отмени|=отменяй|=отменить",
|
||||
"=прекрати|=прекращай|=прекратить", "=убери|=убирай|=убрать",
|
||||
"stop", "cancel", "disable",
|
||||
}
|
||||
|
||||
// quietNegated reports whether the utterance carries a negator. Two ON phrases
|
||||
// are themselves built on "не" — "не шуми", "не беспокой" — and those are
|
||||
|
||||
+14
-16
@@ -16,18 +16,14 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/kami/maven/internal/lexicon"
|
||||
"github.com/kami/maven/internal/say"
|
||||
)
|
||||
|
||||
var ruWeekdays = []string{
|
||||
"воскресенье", "понедельник", "вторник", "среда",
|
||||
"четверг", "пятница", "суббота",
|
||||
}
|
||||
|
||||
var ruMonths = []string{
|
||||
"января", "февраля", "марта", "апреля", "мая", "июня",
|
||||
"июля", "августа", "сентября", "октября", "ноября", "декабря",
|
||||
}
|
||||
// Weekday and month names are a closed class — the language has seven and
|
||||
// twelve — so they live complete in internal/lexicon, where internal/ttsnorm
|
||||
// reads the same twelve month names instead of keeping a second copy
|
||||
// (Vikunja #525).
|
||||
|
||||
// onlyLocalTimeReply — the honest answer when the user asks the time somewhere
|
||||
// other than here. She only keeps one clock, and saying so is better than
|
||||
@@ -39,13 +35,15 @@ var ruMonths = []string{
|
||||
const onlyLocalTimeReply = "я знаю только местное время, про другие города пока не скажу."
|
||||
|
||||
// notPlaceAfterV — words that follow "в" without naming a place, so
|
||||
// mentionsUnknownPlace does not mistake them for a city.
|
||||
var notPlaceAfterV = map[string]bool{
|
||||
"данный": true, "данную": true, "этот": true, "эту": true,
|
||||
"котором": true, "какое": true, "какой": true, "который": true,
|
||||
"общем": true, "точности": true, "курсе": true, "сутках": true,
|
||||
"часах": true, "минутах": true, "секундах": true, "неделе": true,
|
||||
}
|
||||
// mentionsUnknownPlace does not mistake them for a city. Closed set, kept
|
||||
// complete in internal/lexicon.
|
||||
var notPlaceAfterV = func() map[string]bool {
|
||||
m := map[string]bool{}
|
||||
for _, w := range lexicon.NotPlaceAfterV() {
|
||||
m[w] = true
|
||||
}
|
||||
return m
|
||||
}()
|
||||
|
||||
// mentionsUnknownPlace reports whether the question has a "в <слово>" phrase
|
||||
// that looks like a place we do not know ("который час в киеве"). Used only to
|
||||
|
||||
+12
-4
@@ -82,15 +82,23 @@ func (h *reactiveHandler) pendingNudge(ctx context.Context, now time.Time) (ipc.
|
||||
return ipc.Nudge{}, false
|
||||
}
|
||||
|
||||
// snoozePhrases — the deferral vocabulary, as stem sequences. Matched by
|
||||
// quietPhrase (quiet_toggle.go), which carries the rule that matters here:
|
||||
// snoozePhrases — the deferral vocabulary. Matched by quietPhrase and quietStem
|
||||
// (quiet_toggle.go), so an adverb is matched through the dictionary and a verb
|
||||
// exactly, prefixed with "=": "напомни" is a request and "напомнил" is a report
|
||||
// about earlier, and the dictionary files both under напомнить (Vikunja #526).
|
||||
// The verbs used to be truncated stems — "напомн", "отлож" — which is what the
|
||||
// deleted ending list existed to complete.
|
||||
//
|
||||
// quietPhrase carries the rule that matters here:
|
||||
// a single-word pattern matches only a single-word utterance. Bare "потом" is
|
||||
// an answer; "потом схожу за водой" is a plan, and reporting a plan must not
|
||||
// silence the rule that prompted it.
|
||||
var snoozePhrases = [][]string{
|
||||
{"не", "сейчас"}, {"не", "могу", "сейчас"}, {"не", "до", "этого"},
|
||||
{"напомн", "позже"}, {"напомн", "потом"}, {"спрос", "позже"},
|
||||
{"отлож"}, {"позже"}, {"потом"}, {"попозже"}, {"погоди"},
|
||||
{"=напомни|=напоминай", "позже"}, {"=напомни|=напоминай", "потом"},
|
||||
{"=спроси|=спрашивай", "позже"},
|
||||
{"=отложи|=отложим|=откладывай"}, {"позже"}, {"потом"}, {"попозже"},
|
||||
{"=погоди|=погодите"},
|
||||
{"not", "now"}, {"later"}, {"snooze"}, {"remind", "me", "later"},
|
||||
}
|
||||
|
||||
|
||||
+3
-2
@@ -55,6 +55,7 @@ import (
|
||||
"github.com/kami/maven/internal/crawl"
|
||||
"github.com/kami/maven/internal/dialogue"
|
||||
"github.com/kami/maven/internal/ipc"
|
||||
"github.com/kami/maven/internal/lexicon"
|
||||
"github.com/kami/maven/internal/memory"
|
||||
"github.com/kami/maven/internal/phraser"
|
||||
"github.com/kami/maven/internal/router"
|
||||
@@ -452,8 +453,8 @@ func (h *reactiveHandler) replySystem(ctx context.Context, dec router.Decision)
|
||||
// this arm was fixed for, so say what she can do instead.
|
||||
return onlyNearDaysReply
|
||||
}
|
||||
dow := ruWeekdays[day.Weekday()]
|
||||
month := ruMonths[day.Month()-1]
|
||||
dow := lexicon.Weekday(int(day.Weekday()))
|
||||
month := lexicon.MonthGenitive(int(day.Month()))
|
||||
return fmt.Sprintf("%s %s, %d %s %d года", prefix, dow, day.Day(), month, day.Year())
|
||||
case strings.Contains(u, "кто дома") || strings.Contains(u, "человек дома"):
|
||||
return "присутствие пока не подключено к голосовому запросу."
|
||||
|
||||
@@ -10,12 +10,15 @@ require (
|
||||
modernc.org/sqlite v1.54.0
|
||||
)
|
||||
|
||||
require github.com/kami/hexis v0.0.0
|
||||
require (
|
||||
github.com/aaaton/golem/v4 v4.0.2
|
||||
github.com/aaaton/golem/v4/dicts/ru v0.0.0-20250408131944-3488790fc110
|
||||
github.com/kami/hexis v0.0.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/kami/praxis v0.0.0
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/ncruces/go-strftime v1.0.0 // indirect
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
github.com/aaaton/golem/v4 v4.0.0/go.mod h1:OfK/S5v9Exsx1yO21WorREuIVV+Y5K2hygP0A9oJCCI=
|
||||
github.com/aaaton/golem/v4 v4.0.2 h1:m4FvpSL8Zcv7XjmrKiBP7dp5FzhPCji9FcQRcH6T23k=
|
||||
github.com/aaaton/golem/v4 v4.0.2/go.mod h1:OfK/S5v9Exsx1yO21WorREuIVV+Y5K2hygP0A9oJCCI=
|
||||
github.com/aaaton/golem/v4/dicts/ru v0.0.0-20250408131944-3488790fc110 h1:aRLhKltXUyZg/7DoZE5PcNhETfTjMBXBEzD/JVRVVN4=
|
||||
github.com/aaaton/golem/v4/dicts/ru v0.0.0-20250408131944-3488790fc110/go.mod h1:n14MqOgbLBidXRIvLw9H3/vFyE4+PcjVxYOu05f55R4=
|
||||
github.com/coder/websocket v1.8.12 h1:5bUXkEPPIbewrnkU8LTCLVaxi4N4J8ahufH2vlo4NAo=
|
||||
github.com/coder/websocket v1.8.12/go.mod h1:LNVeNrXQZfe5qhS9ALED3uA+l5pPqvwXg3CKoDBB2gs=
|
||||
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||
|
||||
@@ -4,6 +4,8 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
"unicode"
|
||||
|
||||
"github.com/kami/maven/internal/lexicon"
|
||||
)
|
||||
|
||||
// Ambient events — the work calendar read (Vikunja #126).
|
||||
@@ -48,18 +50,6 @@ type Notification struct {
|
||||
// reposting a notification for a meeting already under way.
|
||||
const ambientPastGrace = 2 * time.Hour
|
||||
|
||||
// dayWords maps the words that move a notification off Posted's day. Only
|
||||
// explicit ones: an offset is a claim about which day, and guessing which day
|
||||
// is exactly the guess this parse refuses to make.
|
||||
var dayWords = map[string]int{
|
||||
"завтра": 1,
|
||||
"tomorrow": 1,
|
||||
"сегодня": 0,
|
||||
"today": 0,
|
||||
"tonight": 0,
|
||||
"послезавтра": 2,
|
||||
}
|
||||
|
||||
// EventFromNotification turns a notification into the event it describes, or
|
||||
// reports false when it does not clearly describe one.
|
||||
//
|
||||
@@ -104,11 +94,16 @@ func EventFromNotification(n Notification) (Event, bool) {
|
||||
}
|
||||
|
||||
// dayOffset reports how many days off Posted's day the notification puts the
|
||||
// event. Words are matched whole, so "послезавтра" is not read as "завтра".
|
||||
// event. Only explicit words move it: an offset is a claim about which day, and
|
||||
// guessing which day is exactly the guess this parse refuses to make.
|
||||
//
|
||||
// The words are a closed class and live complete in internal/lexicon (Vikunja
|
||||
// #525), which is how "позавчера" resolves here now — it never did while the map
|
||||
// was inline. Matched whole, so "послезавтра" is not read as "завтра".
|
||||
func dayOffset(line string) int {
|
||||
for _, f := range strings.Fields(strings.ToLower(line)) {
|
||||
f = strings.Trim(f, ".,;:!?—–-()\"'«»")
|
||||
if off, ok := dayWords[f]; ok {
|
||||
if off, ok := lexicon.DayOffset(f); ok {
|
||||
return off
|
||||
}
|
||||
}
|
||||
@@ -121,7 +116,7 @@ func dayOffset(line string) int {
|
||||
func stripDayWords(s string) string {
|
||||
out := make([]string, 0, 8)
|
||||
for _, f := range strings.Fields(s) {
|
||||
if _, ok := dayWords[strings.Trim(strings.ToLower(f), ".,;:!?—–-()\"'«»")]; ok {
|
||||
if _, ok := lexicon.DayOffset(strings.Trim(f, ".,;:!?—–-()\"'«»")); ok {
|
||||
continue
|
||||
}
|
||||
out = append(out, f)
|
||||
|
||||
@@ -0,0 +1,204 @@
|
||||
// Package lexicon holds the Russian word sets that can be finished.
|
||||
//
|
||||
// A closed class has a fixed number of members: the language has as many
|
||||
// interrogative pronouns as it has, and no utterance will ever contain a
|
||||
// thirteenth month. Those sets belong in a data file, complete, and that is what
|
||||
// this package is (Vikunja #522, owner's call 2026-08-04 — "not pattern, 100%").
|
||||
//
|
||||
// It is the first of three mechanisms that replaced hand-written Russian
|
||||
// patterns, and the only one that answers with certainty. The other two are the
|
||||
// embedder, for recognising an open set of phrasings, and a morphological
|
||||
// dictionary, for questions about grammar. A list that can never be finished is
|
||||
// a guess dressed as a rule and does not go here.
|
||||
//
|
||||
// What this package does NOT do is match. It hands out sets and lookups; the
|
||||
// caller decides what to do with a hit, because "this token is an interrogative"
|
||||
// and "this utterance is a question" are different claims.
|
||||
package lexicon
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
//go:embed lexicon_ru_v1.json
|
||||
var files embed.FS
|
||||
|
||||
// ruFile is the versioned file this package reads. A new version is a new file,
|
||||
// not an edit to this one, so a caller pinned to v1 keeps the words it was
|
||||
// measured against.
|
||||
const ruFile = "lexicon_ru_v1.json"
|
||||
|
||||
type lexiconFile struct {
|
||||
SchemaVersion int `json:"schema_version"`
|
||||
Name string `json:"name"`
|
||||
Sets map[string]lexiSet `json:"sets"`
|
||||
}
|
||||
|
||||
type lexiSet struct {
|
||||
Note string `json:"note"`
|
||||
Words []string `json:"words"`
|
||||
Values map[string]int `json:"values"`
|
||||
}
|
||||
|
||||
// ru is parsed once at init. A malformed embedded file is a build-time mistake
|
||||
// that survived to runtime, and there is no sane degraded behaviour for "the
|
||||
// months are missing", so it panics rather than answering with an empty set.
|
||||
var ru = mustLoad()
|
||||
|
||||
func mustLoad() lexiconFile {
|
||||
data, err := files.ReadFile(ruFile)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("lexicon: read %s: %v", ruFile, err))
|
||||
}
|
||||
var f lexiconFile
|
||||
if err := json.Unmarshal(data, &f); err != nil {
|
||||
panic(fmt.Sprintf("lexicon: parse %s: %v", ruFile, err))
|
||||
}
|
||||
for _, name := range []string{
|
||||
"interrogatives", "capture_verbs", "narrative_requests", "cardinals",
|
||||
"day_offsets", "weekdays", "months_genitive", "hours_spoken",
|
||||
"not_place_after_v",
|
||||
} {
|
||||
s, ok := f.Sets[name]
|
||||
if !ok || (len(s.Words) == 0 && len(s.Values) == 0) {
|
||||
panic(fmt.Sprintf("lexicon: %s has no set %q", ruFile, name))
|
||||
}
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
// words returns a copy of a word set, so a caller cannot edit the lexicon by
|
||||
// holding onto what it was given.
|
||||
func words(set string) []string {
|
||||
src := ru.Sets[set].Words
|
||||
out := make([]string, len(src))
|
||||
copy(out, src)
|
||||
return out
|
||||
}
|
||||
|
||||
// Interrogatives returns the question words, Russian and English.
|
||||
func Interrogatives() []string { return words("interrogatives") }
|
||||
|
||||
// CaptureVerbs returns the imperatives that mean "record this".
|
||||
func CaptureVerbs() []string { return words("capture_verbs") }
|
||||
|
||||
// NarrativeRequests returns the imperatives that mean "tell me about".
|
||||
func NarrativeRequests() []string { return words("narrative_requests") }
|
||||
|
||||
// NotPlaceAfterV returns the words that follow "в" without naming a place.
|
||||
func NotPlaceAfterV() []string { return words("not_place_after_v") }
|
||||
|
||||
// Cardinal reports the value of a spoken number word. The word is compared
|
||||
// lowercased and trimmed, because it arrives from a tokenizer that may not have
|
||||
// done either.
|
||||
func Cardinal(word string) (int, bool) {
|
||||
n, ok := ru.Sets["cardinals"].Values[norm(word)]
|
||||
return n, ok
|
||||
}
|
||||
|
||||
// DayOffset reports how many days a relative day word moves from today.
|
||||
//
|
||||
// The zero value is a real answer here — "сегодня" is offset 0 — so the second
|
||||
// return is the only way to tell a hit from a miss. Callers that used to switch
|
||||
// on strings.Contains had to order "послезавтра" before "завтра" by hand,
|
||||
// because one contains the other; a lookup has no such trap.
|
||||
func DayOffset(word string) (int, bool) {
|
||||
n, ok := ru.Sets["day_offsets"].Values[norm(word)]
|
||||
return n, ok
|
||||
}
|
||||
|
||||
// DayOffsetIn finds a relative day word anywhere in a phrase and reports its
|
||||
// offset. Where two words appear, the one that moves furthest from today wins in
|
||||
// absolute terms: "не сегодня, а послезавтра" is about the day after tomorrow,
|
||||
// and the longest-match rule that picking the first word would need is exactly
|
||||
// what the old Contains switch got wrong.
|
||||
func DayOffsetIn(text string) (int, bool) {
|
||||
lower := norm(text)
|
||||
best, found := 0, false
|
||||
for word, n := range ru.Sets["day_offsets"].Values {
|
||||
if !containsWord(lower, word) {
|
||||
continue
|
||||
}
|
||||
if !found || abs(n) > abs(best) {
|
||||
best, found = n, true
|
||||
}
|
||||
}
|
||||
return best, found
|
||||
}
|
||||
|
||||
// Weekday returns the Russian name of a weekday index, Sunday first, matching
|
||||
// Go's time.Weekday. An index off the end returns "".
|
||||
func Weekday(i int) string { return at("weekdays", i) }
|
||||
|
||||
// MonthGenitive returns the month name a date takes — "10 июля", not "июль".
|
||||
// The set is 1-indexed, so MonthGenitive(int(t.Month())) is the whole call.
|
||||
func MonthGenitive(m int) string { return at("months_genitive", m) }
|
||||
|
||||
// HourSpoken returns an hour spelled out for the voice. 0 to 23.
|
||||
func HourSpoken(h int) string { return at("hours_spoken", h) }
|
||||
|
||||
func at(set string, i int) string {
|
||||
w := ru.Sets[set].Words
|
||||
if i < 0 || i >= len(w) {
|
||||
return ""
|
||||
}
|
||||
return w[i]
|
||||
}
|
||||
|
||||
func norm(s string) string { return strings.ToLower(strings.TrimSpace(s)) }
|
||||
|
||||
func abs(n int) int {
|
||||
if n < 0 {
|
||||
return -n
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// containsWord reports whether haystack holds needle on word boundaries. Go's
|
||||
// \b is ASCII-only and never fires after a Cyrillic letter, so the boundary is
|
||||
// checked here instead: a rune on either side must not be a letter or a digit.
|
||||
func containsWord(haystack, needle string) bool {
|
||||
if needle == "" {
|
||||
return false
|
||||
}
|
||||
from := 0
|
||||
for {
|
||||
i := strings.Index(haystack[from:], needle)
|
||||
if i < 0 {
|
||||
return false
|
||||
}
|
||||
i += from
|
||||
if boundaryBefore(haystack, i) && boundaryAfter(haystack, i+len(needle)) {
|
||||
return true
|
||||
}
|
||||
from = i + len(needle)
|
||||
if from >= len(haystack) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func boundaryBefore(s string, i int) bool {
|
||||
if i == 0 {
|
||||
return true
|
||||
}
|
||||
r, _ := utf8.DecodeLastRuneInString(s[:i])
|
||||
return !wordRune(r)
|
||||
}
|
||||
|
||||
func boundaryAfter(s string, i int) bool {
|
||||
if i >= len(s) {
|
||||
return true
|
||||
}
|
||||
r, _ := utf8.DecodeRuneInString(s[i:])
|
||||
return !wordRune(r)
|
||||
}
|
||||
|
||||
func wordRune(r rune) bool {
|
||||
return unicode.IsLetter(r) || unicode.IsDigit(r) || r == '_'
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
{
|
||||
"schema_version": 1,
|
||||
"name": "russian closed-class lexicons v1",
|
||||
"notes": [
|
||||
"Every set here is a CLOSED CLASS: the language has a fixed number of members and the list can be finished. That is why it is a list at all. A word list that can never be finished is a guess dressed as a rule, and it belongs with the embedder, not here (Vikunja #522).",
|
||||
"Editing a word is a data change. No Go change, no rebuild of a pattern, no second copy to keep in step — month names used to live in two files and day offsets in three.",
|
||||
"Interrogatives and capture verbs carry their English members too. He speaks both languages in one sentence and the router sees one utterance.",
|
||||
"The sets are matched over tokens, never as substrings: \"что\" inside \"чтобы\" and \"как\" inside \"какао\" are not questions.",
|
||||
"Order matters in weekdays, months and hours, and nowhere else. weekdays starts at Sunday because Go's time.Weekday does. months is 1-indexed with an empty slot at 0 for the same reason. hours is indexed by the hour itself.",
|
||||
"A form missing from a closed set is a bug report, not a judgement call. Add it."
|
||||
],
|
||||
"sets": {
|
||||
"interrogatives": {
|
||||
"note": "The Russian interrogative pronouns and adverbs, declined, plus the English ones. Closed class: this is the whole list, and a question word outside it does not exist.",
|
||||
"words": [
|
||||
"что", "чего", "чему", "чем", "чём",
|
||||
"кто", "кого", "кому", "кем", "ком",
|
||||
"какой", "какая", "какое", "какие", "какого", "какому", "каким", "каких", "какими", "каком",
|
||||
"который", "которая", "которое", "которые", "которого", "котором",
|
||||
"чей", "чья", "чьё", "чьи",
|
||||
"где", "куда", "откуда", "когда", "докуда",
|
||||
"почему", "зачем", "отчего", "как", "сколько", "насколько", "каково",
|
||||
"what", "who", "whom", "whose", "why", "when", "where", "which", "how"
|
||||
]
|
||||
},
|
||||
"capture_verbs": {
|
||||
"note": "An explicit instruction to record something, in the imperative he actually speaks. Not a closed class in the grammatical sense, but a closed set of the commands Maven answers to — it is her vocabulary, and its members are decided here rather than discovered.",
|
||||
"words": [
|
||||
"запиши", "запомни", "отметь", "заметь", "добавь", "сохрани", "занеси", "внеси",
|
||||
"note", "remember", "log", "save", "add"
|
||||
]
|
||||
},
|
||||
"narrative_requests": {
|
||||
"note": "\"Tell me about X\" asks for knowledge Maven does not hold about him. It carries no question mark and no interrogative, which is how \"расскажи про битву при Ватерлоо\" reached the fact store (#470).",
|
||||
"words": [
|
||||
"расскажи", "объясни", "опиши", "перечисли", "поясни",
|
||||
"tell", "explain", "describe", "list"
|
||||
]
|
||||
},
|
||||
"cardinals": {
|
||||
"note": "Number words as spoken, with the gender variants Russian requires: один/одна/одно and два/две agree with the noun that follows. Values are the number itself. Twenties and up are compounds and are read as their parts, so only the round members are listed.",
|
||||
"values": {
|
||||
"ноль": 0, "нуль": 0, "zero": 0,
|
||||
"один": 1, "одна": 1, "одно": 1, "one": 1,
|
||||
"два": 2, "две": 2, "two": 2,
|
||||
"три": 3, "three": 3,
|
||||
"четыре": 4, "four": 4,
|
||||
"пять": 5, "five": 5,
|
||||
"шесть": 6, "six": 6,
|
||||
"семь": 7, "seven": 7,
|
||||
"восемь": 8, "eight": 8,
|
||||
"девять": 9, "nine": 9,
|
||||
"десять": 10, "ten": 10,
|
||||
"одиннадцать": 11, "eleven": 11,
|
||||
"двенадцать": 12, "twelve": 12,
|
||||
"тринадцать": 13, "thirteen": 13,
|
||||
"четырнадцать": 14, "fourteen": 14,
|
||||
"пятнадцать": 15, "fifteen": 15,
|
||||
"шестнадцать": 16, "sixteen": 16,
|
||||
"семнадцать": 17, "seventeen": 17,
|
||||
"восемнадцать": 18, "eighteen": 18,
|
||||
"девятнадцать": 19, "nineteen": 19,
|
||||
"двадцать": 20, "twenty": 20,
|
||||
"тридцать": 30, "thirty": 30,
|
||||
"сорок": 40, "forty": 40,
|
||||
"пятьдесят": 50, "fifty": 50,
|
||||
"шестьдесят": 60, "sixty": 60,
|
||||
"семьдесят": 70, "seventy": 70,
|
||||
"восемьдесят": 80, "eighty": 80,
|
||||
"девяносто": 90, "ninety": 90,
|
||||
"сто": 100, "hundred": 100
|
||||
}
|
||||
},
|
||||
"day_offsets": {
|
||||
"note": "The words that name a day relative to today, and the number of days each one moves. Only explicit ones: an offset is a claim about which day, and guessing which day is the guess these callers refuse to make. Multi-word members are matched as a phrase.",
|
||||
"values": {
|
||||
"позавчера": -2,
|
||||
"вчера": -1,
|
||||
"yesterday": -1,
|
||||
"сегодня": 0,
|
||||
"today": 0,
|
||||
"tonight": 0,
|
||||
"завтра": 1,
|
||||
"tomorrow": 1,
|
||||
"послезавтра": 2,
|
||||
"day after tomorrow": 2
|
||||
}
|
||||
},
|
||||
"weekdays": {
|
||||
"note": "Nominative, starting at Sunday so the index is Go's time.Weekday.",
|
||||
"words": [
|
||||
"воскресенье", "понедельник", "вторник", "среда",
|
||||
"четверг", "пятница", "суббота"
|
||||
]
|
||||
},
|
||||
"months_genitive": {
|
||||
"note": "The form a date takes: \"10 июля\", not \"июль\". 1-indexed, so slot 0 is empty and month numbers need no arithmetic.",
|
||||
"words": [
|
||||
"", "января", "февраля", "марта", "апреля", "мая", "июня",
|
||||
"июля", "августа", "сентября", "октября", "ноября", "декабря"
|
||||
]
|
||||
},
|
||||
"hours_spoken": {
|
||||
"note": "Hours spelled out for the voice: \"3 ч\" is fine on a screen and wrong out loud. Indexed by the hour, 0 to 23.",
|
||||
"words": [
|
||||
"ноль", "один", "два", "три", "четыре", "пять", "шесть", "семь", "восемь",
|
||||
"девять", "десять", "одиннадцать", "двенадцать", "тринадцать",
|
||||
"четырнадцать", "пятнадцать", "шестнадцать", "семнадцать", "восемнадцать",
|
||||
"девятнадцать", "двадцать", "двадцать один", "двадцать два", "двадцать три"
|
||||
]
|
||||
},
|
||||
"not_place_after_v": {
|
||||
"note": "Words that follow the preposition \"в\" without naming a place, so \"в общем\" and \"в котором часу\" are not read as a city we do not know.",
|
||||
"words": [
|
||||
"данный", "данную", "данное", "этот", "эту", "это", "том", "той",
|
||||
"котором", "которой", "какое", "какой", "который", "каком",
|
||||
"общем", "точности", "курсе", "принципе", "итоге", "целом",
|
||||
"сутках", "часах", "минутах", "секундах", "неделе", "месяце", "году",
|
||||
"начале", "конце", "середине", "течение", "течении"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
package lexicon
|
||||
|
||||
import "testing"
|
||||
|
||||
// TestClosedSetsAreComplete — the point of the package. A closed class can be
|
||||
// finished, so the test names the members that were MISSING from the inline Go
|
||||
// lists this package replaced (Vikunja #525) and every one of them has to be
|
||||
// there. Add to this list when a form turns up unhandled.
|
||||
func TestClosedSetsAreComplete(t *testing.T) {
|
||||
inter := map[string]bool{}
|
||||
for _, w := range Interrogatives() {
|
||||
inter[w] = true
|
||||
}
|
||||
// Instrumental and prepositional cases of что and кто, and the declined
|
||||
// какой. The old list had что/чего and nothing else, so "чем ты занята" and
|
||||
// "в каком часу" carried no interrogative at all.
|
||||
for _, w := range []string{"чем", "чём", "чему", "кем", "ком", "каком", "какими", "насколько"} {
|
||||
if !inter[w] {
|
||||
t.Errorf("interrogatives is missing %q", w)
|
||||
}
|
||||
}
|
||||
|
||||
for _, tc := range []struct {
|
||||
word string
|
||||
want int
|
||||
}{
|
||||
{"ноль", 0}, {"одна", 1}, {"две", 2}, {"одиннадцать", 11},
|
||||
{"пятнадцать", 15}, {"двадцать", 20}, {"сорок", 40}, {"девяносто", 90},
|
||||
{"сто", 100}, {"twelve", 12},
|
||||
} {
|
||||
got, ok := Cardinal(tc.word)
|
||||
if !ok || got != tc.want {
|
||||
t.Errorf("Cardinal(%q) = %d, %v; want %d, true", tc.word, got, ok, tc.want)
|
||||
}
|
||||
}
|
||||
if _, ok := Cardinal("бэкап"); ok {
|
||||
t.Error("Cardinal must not answer for a word that is not a number")
|
||||
}
|
||||
}
|
||||
|
||||
// TestDayOffsetHasNoOrderingTrap — the defect a lookup removes. The callers this
|
||||
// replaced used strings.Contains in a switch, so "послезавтра" had to be tested
|
||||
// before "завтра" by hand or the day after tomorrow read as tomorrow.
|
||||
func TestDayOffsetHasNoOrderingTrap(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
text string
|
||||
want int
|
||||
ok bool
|
||||
}{
|
||||
{"послезавтра", 2, true},
|
||||
{"завтра", 1, true},
|
||||
{"сегодня", 0, true},
|
||||
{"вчера", -1, true},
|
||||
{"позавчера", -2, true},
|
||||
{"встреча послезавтра в 14:30", 2, true},
|
||||
{"Tomorrow at 09:00", 1, true},
|
||||
{"не сегодня, а послезавтра", 2, true},
|
||||
{"в четверг", 0, false},
|
||||
{"завтраком", 0, false},
|
||||
} {
|
||||
got, ok := DayOffsetIn(tc.text)
|
||||
if ok != tc.ok || (ok && got != tc.want) {
|
||||
t.Errorf("DayOffsetIn(%q) = %d, %v; want %d, %v", tc.text, got, ok, tc.want, tc.ok)
|
||||
}
|
||||
}
|
||||
// "сегодня" is offset 0, which is also the zero value, so the second return
|
||||
// is the only thing that separates a hit from a miss.
|
||||
if n, ok := DayOffset("сегодня"); n != 0 || !ok {
|
||||
t.Errorf("DayOffset(сегодня) = %d, %v; want 0, true", n, ok)
|
||||
}
|
||||
}
|
||||
|
||||
// TestIndexedSetsLineUpWithTheirCallers — weekdays start at Sunday because Go's
|
||||
// time.Weekday does, and months are 1-indexed so a month number needs no
|
||||
// arithmetic. Off-by-one here is a wrong date spoken out loud.
|
||||
func TestIndexedSetsLineUpWithTheirCallers(t *testing.T) {
|
||||
if got := Weekday(0); got != "воскресенье" {
|
||||
t.Errorf("Weekday(0) = %q, want воскресенье", got)
|
||||
}
|
||||
if got := Weekday(1); got != "понедельник" {
|
||||
t.Errorf("Weekday(1) = %q, want понедельник", got)
|
||||
}
|
||||
if got := MonthGenitive(1); got != "января" {
|
||||
t.Errorf("MonthGenitive(1) = %q, want января", got)
|
||||
}
|
||||
if got := MonthGenitive(12); got != "декабря" {
|
||||
t.Errorf("MonthGenitive(12) = %q, want декабря", got)
|
||||
}
|
||||
if got := MonthGenitive(0); got != "" {
|
||||
t.Errorf("MonthGenitive(0) = %q, want the empty slot", got)
|
||||
}
|
||||
if got := HourSpoken(23); got != "двадцать три" {
|
||||
t.Errorf("HourSpoken(23) = %q, want двадцать три", got)
|
||||
}
|
||||
for _, i := range []int{-1, 7, 13, 24} {
|
||||
if got := Weekday(i); i >= 7 && got != "" {
|
||||
t.Errorf("Weekday(%d) = %q, want empty", i, got)
|
||||
}
|
||||
}
|
||||
if got := HourSpoken(24); got != "" {
|
||||
t.Errorf("HourSpoken(24) = %q, want empty", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCallerCannotEditTheLexicon — the sets are handed out as copies. A caller
|
||||
// that sorted the slice it was given would otherwise reorder weekdays for
|
||||
// everybody.
|
||||
func TestCallerCannotEditTheLexicon(t *testing.T) {
|
||||
first := Interrogatives()
|
||||
first[0] = "мутировало"
|
||||
if again := Interrogatives(); again[0] == "мутировало" {
|
||||
t.Fatal("the lexicon handed out its own backing array")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
// Package morph answers questions about Russian grammar from a dictionary.
|
||||
//
|
||||
// The second of the three mechanisms replacing hand-written Russian patterns
|
||||
// (Vikunja #522, owner's call 2026-08-04). internal/lexicon holds the sets that
|
||||
// can be finished; the embedder recognises an open set of phrasings; and this
|
||||
// package answers the questions that are about grammar rather than meaning:
|
||||
//
|
||||
// - is this word a form of a verb, so it carries its own subject?
|
||||
// - are these two tokens the same word in different cases?
|
||||
//
|
||||
// Three places used to answer those from a list of letter endings, and each list
|
||||
// was wrong in a way its own comment admitted. "канал" read as a past-tense verb
|
||||
// because it ends in -ал. A list of nineteen nouns ending in л existed only to
|
||||
// suppress the false positives of "ends in л means masculine past tense", which
|
||||
// is a pattern conceding it is wrong. Grammar is what a dictionary is for.
|
||||
//
|
||||
// Not the resident model. This has to be right every time, offline, in
|
||||
// microseconds, and a 1.7B is neither reliable enough nor fast enough to ask.
|
||||
//
|
||||
// The dictionary is github.com/aaaton/golem's Russian data, vendored. It is
|
||||
// embedded in the module, so a load failure is not a network problem and not a
|
||||
// config problem — it is corrupt data that got past the build. Every function
|
||||
// answers conservatively in that case rather than failing the turn, and says so
|
||||
// in its own doc comment.
|
||||
package morph
|
||||
|
||||
import (
|
||||
"log"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/aaaton/golem/v4"
|
||||
"github.com/aaaton/golem/v4/dicts/ru"
|
||||
)
|
||||
|
||||
var (
|
||||
once sync.Once
|
||||
lemma *golem.Lemmatizer
|
||||
loadErr error
|
||||
)
|
||||
|
||||
// dict loads the lemmatizer on first use. Loading costs a few megabytes of maps,
|
||||
// which is why it is not done at init: a daemon that never sees Russian never
|
||||
// pays for it.
|
||||
func dict() *golem.Lemmatizer {
|
||||
once.Do(func() {
|
||||
lemma, loadErr = golem.New(ru.New())
|
||||
if loadErr != nil {
|
||||
// Once, not per call: this is a permanent condition and a voice loop
|
||||
// would otherwise fill the log with it at speech rate.
|
||||
log.Printf("morph: russian dictionary unavailable, answering conservatively: %v", loadErr)
|
||||
}
|
||||
})
|
||||
return lemma
|
||||
}
|
||||
|
||||
// Available reports whether the dictionary loaded. Callers do not need it to be
|
||||
// correct — every function below has a defined answer without it — but a test
|
||||
// that means to measure the dictionary should skip rather than pass vacuously.
|
||||
func Available() bool {
|
||||
dict()
|
||||
return loadErr == nil
|
||||
}
|
||||
|
||||
// Lemma returns the dictionary form of a word, or the word itself when the
|
||||
// dictionary does not know it or could not load. An unknown word is its own
|
||||
// lemma: "бэкап" is not in the dictionary and there is nothing better to say
|
||||
// about it than what he said.
|
||||
func Lemma(word string) string {
|
||||
w := strings.ToLower(strings.TrimSpace(word))
|
||||
if w == "" {
|
||||
return ""
|
||||
}
|
||||
l := dict()
|
||||
if l == nil {
|
||||
return w
|
||||
}
|
||||
if got := l.Lemma(w); got != "" {
|
||||
return got
|
||||
}
|
||||
return w
|
||||
}
|
||||
|
||||
// infinitiveEndings — how a Russian infinitive ends. This is not a stem pattern:
|
||||
// it is applied to a LEMMA the dictionary returned, where the infinitive is the
|
||||
// dictionary form of every verb by definition, so the test is about the
|
||||
// dictionary's own output and not about the word he said.
|
||||
//
|
||||
// The reflexive forms are listed because a reflexive lemma keeps its particle:
|
||||
// "тренировался" lemmatises to "тренироваться", which ends in "ся" rather than
|
||||
// "ть".
|
||||
var infinitiveEndings = []string{"ться", "тись", "чься", "ть", "ти", "чь"}
|
||||
|
||||
// IsVerbForm reports whether a word is some form of a verb — past tense, present,
|
||||
// imperative, reflexive, participle. A verb carries its own subject and tense, so
|
||||
// in Russian one verb is a whole sentence, which is what the callers care about.
|
||||
//
|
||||
// Without the dictionary this answers false: not knowing is not evidence that a
|
||||
// word IS a verb, and the callers all treat false as the cautious direction.
|
||||
func IsVerbForm(word string) bool {
|
||||
if dict() == nil {
|
||||
return false
|
||||
}
|
||||
l := Lemma(word)
|
||||
for _, e := range infinitiveEndings {
|
||||
if strings.HasSuffix(l, e) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// SameWord reports whether two tokens are the same word in different cases —
|
||||
// "режим" and "режиме", "тихий" and "тихо". It is the test a stem-plus-endings
|
||||
// comparison was approximating, and it draws the line the ending list could not:
|
||||
// "тихонько" and "потихоньку" are different words, and the dictionary says so
|
||||
// because it has never heard of either.
|
||||
//
|
||||
// Without the dictionary this falls back to exact equality, which is the
|
||||
// narrowest honest answer.
|
||||
func SameWord(a, b string) bool {
|
||||
la, lb := Lemma(a), Lemma(b)
|
||||
if la == "" || lb == "" {
|
||||
return false
|
||||
}
|
||||
return la == lb
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package morph
|
||||
|
||||
import "testing"
|
||||
|
||||
// TestVerbFormsAreVerbs — the question internal/router/singletoken.go asks. Every
|
||||
// one of these is a whole sentence in Russian, because the verb carries its own
|
||||
// subject, tense and gender.
|
||||
func TestVerbFormsAreVerbs(t *testing.T) {
|
||||
if !Available() {
|
||||
t.Skip("russian dictionary unavailable")
|
||||
}
|
||||
for _, w := range []string{
|
||||
"поужинал", "сходил", "выпил", "напомнил", "поняла", "сделала",
|
||||
"пришёл", "начал", "работаешь", "занимаюсь",
|
||||
// Reflexive: the lemma keeps its particle, so "тренироваться" ends in
|
||||
// "ся" and not "ть". That is why the ending list carries both.
|
||||
"тренировался", "проснулся",
|
||||
} {
|
||||
if !IsVerbForm(w) {
|
||||
t.Errorf("IsVerbForm(%q) = false, want true (lemma %q)", w, Lemma(w))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestNounsEndingInLAreNotVerbs — the list this package deleted. Nineteen nouns
|
||||
// lived in internal/phraser/eval/checks.go as exceptions to "ends in л means
|
||||
// masculine past tense", plus the ones internal/router/singletoken.go named as
|
||||
// its own known errors. A list of exceptions to a pattern is the pattern
|
||||
// conceding it is wrong, so all of them are here and none may be a verb.
|
||||
func TestNounsEndingInLAreNotVerbs(t *testing.T) {
|
||||
if !Available() {
|
||||
t.Skip("russian dictionary unavailable")
|
||||
}
|
||||
for _, w := range []string{
|
||||
"стол", "стул", "пол", "зал", "гол", "узел", "отдел", "файл", "канал",
|
||||
"угол", "футбол", "вокзал", "металл", "интервал", "уровень", "мускул",
|
||||
"апрель", "июль", "рубль",
|
||||
// singletoken.go named these: "канал" read as past tense, and short
|
||||
// nouns needed a length exemption to survive a two-letter suffix test.
|
||||
"нос", "лес", "газ", "вода", "бэкап",
|
||||
} {
|
||||
if IsVerbForm(w) {
|
||||
t.Errorf("IsVerbForm(%q) = true, want false (lemma %q)", w, Lemma(w))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestSameWordDrawsTheLineTheEndingListCouldNot — the question
|
||||
// cmd/mavend/quiet_toggle.go asks. Its comment describes exactly this: "тихий",
|
||||
// "тихом" and "тихо" are one word inflected, while "тихонько" and "потихоньку"
|
||||
// are different words. The dictionary says so; a list of 36 endings approximated
|
||||
// it.
|
||||
func TestSameWordDrawsTheLineTheEndingListCouldNot(t *testing.T) {
|
||||
if !Available() {
|
||||
t.Skip("russian dictionary unavailable")
|
||||
}
|
||||
for _, w := range []string{"тихий", "тихом", "тихо", "тише"} {
|
||||
if !SameWord(w, "тихий") {
|
||||
t.Errorf("SameWord(%q, тихий) = false, want true (lemma %q)", w, Lemma(w))
|
||||
}
|
||||
}
|
||||
for _, w := range []string{"тихонько", "потихоньку"} {
|
||||
if SameWord(w, "тихий") {
|
||||
t.Errorf("SameWord(%q, тихий) = true, want false", w)
|
||||
}
|
||||
}
|
||||
for _, w := range []string{"режим", "режима", "режиме", "режимы"} {
|
||||
if !SameWord(w, "режим") {
|
||||
t.Errorf("SameWord(%q, режим) = false, want true (lemma %q)", w, Lemma(w))
|
||||
}
|
||||
}
|
||||
if SameWord("режим", "тихий") {
|
||||
t.Error("SameWord matched two unrelated words")
|
||||
}
|
||||
}
|
||||
|
||||
// TestUnknownWordIsItsOwnLemma — "бэкап" is not in the dictionary, and there is
|
||||
// nothing better to say about it than what he said. Two spellings of an unknown
|
||||
// word still compare equal, which is what the exact-equality fallback rests on.
|
||||
func TestUnknownWordIsItsOwnLemma(t *testing.T) {
|
||||
if got := Lemma("бэкап"); got != "бэкап" {
|
||||
t.Errorf("Lemma(бэкап) = %q, want бэкап", got)
|
||||
}
|
||||
if got := Lemma(" БЭКАП "); got != "бэкап" {
|
||||
t.Errorf("Lemma trims and lowercases: got %q", got)
|
||||
}
|
||||
if !SameWord("бэкап", "БЭКАП") {
|
||||
t.Error("SameWord must still compare an unknown word with itself")
|
||||
}
|
||||
if got := Lemma(""); got != "" {
|
||||
t.Errorf("Lemma(empty) = %q, want empty", got)
|
||||
}
|
||||
if SameWord("", "") {
|
||||
t.Error("two empty tokens are not a word")
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,8 @@ import (
|
||||
"strings"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/kami/maven/internal/morph"
|
||||
)
|
||||
|
||||
// The check names, in report order. Every check is a string or length test — no
|
||||
@@ -140,24 +142,25 @@ var masculinePredicative = map[string]bool{
|
||||
"обязан": true, "сам": true, "занят": true, "прав": true,
|
||||
}
|
||||
|
||||
// nounsEndingInL — the false positives of "ends in л ⇒ masculine past tense".
|
||||
// Small on purpose: it only has to cover nouns a nudge might actually use.
|
||||
var nounsEndingInL = map[string]bool{
|
||||
"стол": true, "стул": true, "пол": true, "зал": true, "гол": true,
|
||||
"узел": true, "отдел": true, "файл": true, "канал": true, "угол": true,
|
||||
"футбол": true, "вокзал": true, "металл": true, "интервал": true,
|
||||
"уровень": true, "мускул": true, "апрель": true, "июль": true, "рубль": true,
|
||||
}
|
||||
|
||||
// masculinePast reports whether a word looks like a masculine past-tense verb.
|
||||
// Russian past tense is gendered by suffix: -л (m), -ла (f). A 0.8B with weak
|
||||
// Russian defaults to the masculine form, which is the exact drift being
|
||||
// measured.
|
||||
// masculinePast reports whether a word is a masculine past-tense verb. Russian
|
||||
// past tense is gendered by suffix: -л for him, -ла for her. A small model with
|
||||
// weak Russian defaults to the masculine form, which is the exact drift this
|
||||
// check measures.
|
||||
//
|
||||
// The ending is only half the test, and the other half used to be a hand list of
|
||||
// nineteen nouns that end in л — стол, файл, апрель — kept "small on purpose",
|
||||
// which means incomplete on purpose. A list of exceptions to a pattern is the
|
||||
// pattern conceding it is wrong, so the second half is now a dictionary lookup:
|
||||
// ends in -л AND is a form of a verb (Vikunja #526). Every noun the list held is
|
||||
// correctly not a verb, and so are the ones it had not got round to.
|
||||
func masculinePast(w string) bool {
|
||||
if len([]rune(w)) < 3 || nounsEndingInL[w] {
|
||||
if len([]rune(w)) < 3 {
|
||||
return false
|
||||
}
|
||||
return strings.HasSuffix(w, "л") || strings.HasSuffix(w, "лся")
|
||||
if !strings.HasSuffix(w, "л") && !strings.HasSuffix(w, "лся") {
|
||||
return false
|
||||
}
|
||||
return morph.IsVerbForm(w)
|
||||
}
|
||||
|
||||
func checkFeminine(body string) Result {
|
||||
|
||||
@@ -18,6 +18,8 @@ import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"regexp"
|
||||
|
||||
"github.com/kami/maven/internal/lexicon"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -210,13 +212,11 @@ func capitalizeFirst(s string) string {
|
||||
}
|
||||
|
||||
// hourWords — hours spelled out. "3 ч" is fine on a screen and wrong in a
|
||||
// Russian voice, so the number goes out as words.
|
||||
var hourWords = []string{
|
||||
"ноль", "один", "два", "три", "четыре", "пять", "шесть", "семь", "восемь",
|
||||
"девять", "десять", "одиннадцать", "двенадцать", "тринадцать",
|
||||
"четырнадцать", "пятнадцать", "шестнадцать", "семнадцать", "восемнадцать",
|
||||
"девятнадцать", "двадцать", "двадцать один", "двадцать два", "двадцать три",
|
||||
}
|
||||
// Russian voice, so the number goes out as words. Closed set, indexed by the
|
||||
// hour, kept in internal/lexicon (Vikunja #525).
|
||||
func hourWord(h int) string { return lexicon.HourSpoken(h) }
|
||||
|
||||
const hoursSpoken = 24
|
||||
|
||||
// hourPlural — час / часа / часов by Russian counting rules.
|
||||
func hourPlural(h int) string {
|
||||
@@ -245,7 +245,7 @@ func ruSinceWords(d time.Duration) string {
|
||||
h++
|
||||
m = 0
|
||||
}
|
||||
if h >= len(hourWords) {
|
||||
if h >= hoursSpoken {
|
||||
return "больше суток"
|
||||
}
|
||||
if h == 1 {
|
||||
@@ -255,7 +255,7 @@ func ruSinceWords(d time.Duration) string {
|
||||
return "час"
|
||||
}
|
||||
if m >= 15 {
|
||||
return hourWords[h] + " с половиной часа"
|
||||
return hourWord(h) + " с половиной часа"
|
||||
}
|
||||
return hourWords[h] + " " + hourPlural(h)
|
||||
return hourWord(h) + " " + hourPlural(h)
|
||||
}
|
||||
|
||||
+23
-25
@@ -1,32 +1,30 @@
|
||||
package router
|
||||
|
||||
import "strings"
|
||||
import (
|
||||
"strings"
|
||||
|
||||
// interrogatives — the question words that mark an utterance as asking rather
|
||||
// than telling. Tokenized, never substring: "что" inside "чтобы" and "как"
|
||||
// inside "какао" are not questions.
|
||||
var interrogatives = []string{
|
||||
"что", "чего", "какой", "какая", "какое", "какие", "каких",
|
||||
"кто", "кого", "кому", "чей", "почему", "зачем", "отчего",
|
||||
"где", "куда", "откуда", "когда", "сколько", "как",
|
||||
"what", "who", "whom", "why", "when", "where", "which", "how",
|
||||
}
|
||||
"github.com/kami/maven/internal/lexicon"
|
||||
)
|
||||
|
||||
// narrativeRequests — "tell me about X" asks for knowledge Maven does not
|
||||
// hold about him. It carries no question mark and no interrogative, which is
|
||||
// how "расскажи про битву при Ватерлоо" reached the fact store (#470).
|
||||
var narrativeRequests = []string{
|
||||
"расскажи", "объясни", "опиши", "перечисли",
|
||||
"tell", "explain", "describe",
|
||||
}
|
||||
|
||||
// captureVerbs — an explicit instruction to record something. These win over
|
||||
// every test below, because "запиши что я пил воду" contains an interrogative
|
||||
// and is still a capture: the word he said is "запиши".
|
||||
var captureVerbs = []string{
|
||||
"запиши", "запомни", "отметь", "заметь", "добавь", "сохрани",
|
||||
"note", "remember", "log", "save",
|
||||
}
|
||||
// The three word sets this file tests against are closed classes, so they live
|
||||
// complete in internal/lexicon rather than inline here (Vikunja #525). The
|
||||
// inline lists were short: no "чем", no "чём", no "кем", no declined "какой",
|
||||
// so "чем ты занята" carried no interrogative at all and read as a statement.
|
||||
//
|
||||
// interrogatives mark an utterance as asking rather than telling.
|
||||
// narrativeRequests are "tell me about X", which asks for knowledge Maven does
|
||||
// not hold about him and carries neither a question mark nor an interrogative —
|
||||
// that is how "расскажи про битву при Ватерлоо" reached the fact store (#470).
|
||||
// captureVerbs win over both, because "запиши что я пил воду" contains an
|
||||
// interrogative and is still a capture: the word he said is "запиши".
|
||||
//
|
||||
// All three are matched over tokens, never as substrings: "что" inside "чтобы"
|
||||
// and "как" inside "какао" are not questions.
|
||||
var (
|
||||
interrogatives = lexicon.Interrogatives()
|
||||
narrativeRequests = lexicon.NarrativeRequests()
|
||||
captureVerbs = lexicon.CaptureVerbs()
|
||||
)
|
||||
|
||||
// IsQuestionShaped reports whether text asks for something rather than
|
||||
// records it. It is a deterministic offline test over tokens, so it costs
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package router
|
||||
|
||||
import "strings"
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/kami/maven/internal/morph"
|
||||
)
|
||||
|
||||
// thinSingleToken — is a one-word utterance thin evidence, or is it a whole
|
||||
// sentence?
|
||||
@@ -17,13 +21,15 @@ import "strings"
|
||||
//
|
||||
// - a closed lexicon of social and command singles, which are complete by
|
||||
// definition ("привет", "спасибо", "стоп", "yes");
|
||||
// - a suffix test for an inflected predicate — past tense, 2nd person,
|
||||
// reflexive. Verbs carry their own subject, so a verb IS a sentence.
|
||||
// - a dictionary lookup for a verb form. A verb carries its own subject,
|
||||
// tense and gender, so a verb IS a sentence.
|
||||
//
|
||||
// The suffix test is deliberately loose about nouns that happen to end the
|
||||
// same way ("канал" reads as past tense here). That direction of error only
|
||||
// costs a clarify we would not have asked for; the other direction — treating
|
||||
// a real report as thin — is the bug being fixed.
|
||||
// The dictionary lookup replaced a list of 24 letter endings (Vikunja #526). The
|
||||
// list was loose in a direction its own comment named: "канал" ends in -ал and
|
||||
// read as past tense, and short words needed a length exemption so "нос" and
|
||||
// "лес" would survive a two-letter suffix. Asking a morphological dictionary
|
||||
// costs one map lookup and has no such errors — grammar is what a dictionary is
|
||||
// for.
|
||||
func thinSingleToken(utterance string) bool {
|
||||
f := strings.Fields(utterance)
|
||||
if len(f) != 1 {
|
||||
@@ -36,7 +42,7 @@ func thinSingleToken(utterance string) bool {
|
||||
if completeSingles[w] {
|
||||
return false
|
||||
}
|
||||
return !looksInflected(w)
|
||||
return !morph.IsVerbForm(w)
|
||||
}
|
||||
|
||||
// completeSingles — one-word utterances that need no second half. Greetings,
|
||||
@@ -58,33 +64,3 @@ var completeSingles = map[string]bool{
|
||||
"sure": true, "right": true, "stop": true, "cancel": true, "help": true,
|
||||
"repeat": true, "continue": true,
|
||||
}
|
||||
|
||||
// inflectedSuffixes — endings that mark a finite or past-tense Russian verb.
|
||||
// Ordered longest-first is unnecessary (any match wins), but each entry is
|
||||
// chosen to be long enough that common nouns rarely collide.
|
||||
var inflectedSuffixes = []string{
|
||||
// reflexive — strongly verbal whatever precedes it
|
||||
"ся", "сь",
|
||||
// past tense
|
||||
"ал", "ял", "ил", "ел", "ыл", "ул", "ёл", "ала", "яла", "ила", "ела",
|
||||
"ыла", "ула", "али", "яли", "или", "ели",
|
||||
// 2nd person singular
|
||||
"ешь", "ишь", "ёшь",
|
||||
// 1st/2nd person plural, 3rd person plural
|
||||
"аем", "яем", "уем", "аете", "ите", "ают", "яют", "уют", "ат", "ят",
|
||||
}
|
||||
|
||||
// looksInflected — does the word carry a verb ending? Short words are exempt:
|
||||
// a three-letter token is not enough stem to trust a two-letter suffix on
|
||||
// ("газ" would otherwise never match, but "нос" and "лес" would).
|
||||
func looksInflected(w string) bool {
|
||||
if len([]rune(w)) < 5 {
|
||||
return false
|
||||
}
|
||||
for _, s := range inflectedSuffixes {
|
||||
if strings.HasSuffix(w, s) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
+18
-29
@@ -5,6 +5,8 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/kami/maven/internal/lexicon"
|
||||
)
|
||||
|
||||
// DateTimeParser — resolves relative→absolute AT CAPTURE ("in 4h" → now+4h),
|
||||
@@ -348,26 +350,17 @@ func leadingDigits(s string) (int, string, bool) {
|
||||
return n, s[i:], true
|
||||
}
|
||||
|
||||
// wordNumbers — small set, enough for natural test seeds ("four hours",
|
||||
// "thirty minutes"). Production dateparser handles the full ru/en range.
|
||||
var wordNumbers = map[string]int{
|
||||
"one": 1, "two": 2, "three": 3, "four": 4, "five": 5,
|
||||
"six": 6, "seven": 7, "eight": 8, "nine": 9, "ten": 10,
|
||||
"eleven": 11, "twelve": 12, "fifteen": 15, "twenty": 20,
|
||||
"thirty": 30, "forty": 40, "fifty": 50, "sixty": 60,
|
||||
// Russian word numbers (gender variants cover natural речи)
|
||||
"один": 1, "одна": 1, "одно": 1,
|
||||
"два": 2, "две": 2, "три": 3, "четыре": 4,
|
||||
"пять": 5, "шесть": 6, "семь": 7, "восемь": 8,
|
||||
"девять": 9, "десять": 10,
|
||||
}
|
||||
// leadingWordNumber reads a spoken number off the front of a phrase — "два
|
||||
// часа", "twenty minutes". The number words are a closed class and live in
|
||||
// internal/lexicon, complete: the inline table here stopped at "десять" in
|
||||
// Russian, so "пятнадцать минут" was not a duration (Vikunja #525).
|
||||
|
||||
func leadingWordNumber(s string) (int, string, bool) {
|
||||
toks := strings.Fields(s)
|
||||
if len(toks) == 0 {
|
||||
return 0, "", false
|
||||
}
|
||||
n, ok := wordNumbers[toks[0]]
|
||||
n, ok := lexicon.Cardinal(toks[0])
|
||||
if !ok {
|
||||
return 0, "", false
|
||||
}
|
||||
@@ -450,24 +443,20 @@ func (AnaphoraResolver) Resolve(text string) (ref string, ok bool) {
|
||||
}
|
||||
|
||||
// ParseCalendarDate detects RU/EN calendar day words in text and returns
|
||||
// midnight of that day in now's own time zone. Handles "сегодня", "завтра",
|
||||
// "послезавтра", "вчера" (and the English words). Returns zero time + false
|
||||
// if no match.
|
||||
// midnight of that day in now's own time zone. Returns zero time + false if no
|
||||
// match.
|
||||
//
|
||||
// "послезавтра" is checked before "завтра" because it contains it.
|
||||
// The day words are a closed class and live in internal/lexicon, so this is a
|
||||
// lookup rather than an ordered switch (Vikunja #525). The switch it replaced
|
||||
// had to test "послезавтра" before "завтра" by hand, because one contains the
|
||||
// other — and it matched on substrings, so "завтраком" was tomorrow. The lexicon
|
||||
// matches on word boundaries and gained "позавчера", which was never here.
|
||||
func ParseCalendarDate(text string, now time.Time) (time.Time, bool) {
|
||||
lower := strings.ToLower(text)
|
||||
switch {
|
||||
case strings.Contains(lower, "сегодня") || strings.Contains(lower, "today"):
|
||||
return midnight(now, 0), true
|
||||
case strings.Contains(lower, "послезавтра") || strings.Contains(lower, "day after tomorrow"):
|
||||
return midnight(now, 2), true
|
||||
case strings.Contains(lower, "завтра") || strings.Contains(lower, "tomorrow"):
|
||||
return midnight(now, 1), true
|
||||
case strings.Contains(lower, "вчера") || strings.Contains(lower, "yesterday"):
|
||||
return midnight(now, -1), true
|
||||
days, ok := lexicon.DayOffsetIn(text)
|
||||
if !ok {
|
||||
return time.Time{}, false
|
||||
}
|
||||
return time.Time{}, false
|
||||
return midnight(now, days), true
|
||||
}
|
||||
|
||||
// midnight returns the start of the day that is `days` away from now, in
|
||||
|
||||
@@ -7,10 +7,13 @@ import (
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/kami/maven/internal/lexicon"
|
||||
)
|
||||
|
||||
var months = [...]string{"", "января", "февраля", "марта", "апреля", "мая",
|
||||
"июня", "июля", "августа", "сентября", "октября", "ноября", "декабря"}
|
||||
// The month names are a closed class and live in internal/lexicon, 1-indexed,
|
||||
// which is also where the voice reply path reads them. There used to be a second
|
||||
// copy of the twelve names in cmd/mavend/ruwords.go (Vikunja #525).
|
||||
|
||||
var (
|
||||
reDateY = regexp.MustCompile(`\b(\d{1,2})\.(\d{1,2})\.(\d{4})\b`)
|
||||
@@ -47,7 +50,7 @@ func spokenDate(dd, mm, yyyy string) string {
|
||||
return dd + " " + mm + gap(yyyy)
|
||||
}
|
||||
day := strconv.Itoa(mustInt(dd))
|
||||
out := day + " " + months[mi]
|
||||
out := day + " " + lexicon.MonthGenitive(mi)
|
||||
if yyyy != "" {
|
||||
out += " " + yyyy
|
||||
}
|
||||
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
data
|
||||
vendor
|
||||
.vscode
|
||||
# Testing and benchmarks
|
||||
*.out
|
||||
*.test
|
||||
pprof
|
||||
.DS_Store
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2019 Anton Södergren
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
SHELL:=/usr/bin/env bash
|
||||
default: all
|
||||
LANG=en
|
||||
all:
|
||||
# go get -u github.com/jteeuwen/go-bindata/...
|
||||
mkdir -p data
|
||||
$(MAKE) en sv fr es de it ru uk
|
||||
|
||||
package-all:
|
||||
$(MAKE) LANG=en package
|
||||
$(MAKE) LANG=sv package
|
||||
$(MAKE) LANG=fr package
|
||||
$(MAKE) LANG=es package
|
||||
$(MAKE) LANG=de package
|
||||
$(MAKE) LANG=it package
|
||||
$(MAKE) LANG=ru package
|
||||
$(MAKE) LANG=uk package
|
||||
|
||||
en:
|
||||
$(MAKE) LANG=en download package
|
||||
sv:
|
||||
$(MAKE) LANG=sv download package
|
||||
fr:
|
||||
$(MAKE) LANG=fr download package
|
||||
es:
|
||||
$(MAKE) LANG=es download package
|
||||
de:
|
||||
$(MAKE) LANG=de download package
|
||||
it:
|
||||
$(MAKE) LANG=it download package
|
||||
ru:
|
||||
$(MAKE) LANG=ru download package
|
||||
uk:
|
||||
$(MAKE) LANG=uk download package
|
||||
|
||||
download:
|
||||
curl https://raw.githubusercontent.com/michmech/lemmatization-lists/master/lemmatization-$(LANG).txt > data/$(LANG)
|
||||
|
||||
package:
|
||||
# Packaging $(LANG)
|
||||
go run cmd/simplify/simplify.go data/$(LANG) data/$(LANG).gz
|
||||
go run cmd/genpack/genpack.go -locale $(LANG) -path data/$(LANG).gz > v4/dicts/$(LANG)/pack.go
|
||||
# ----------------
|
||||
|
||||
benchcmp:
|
||||
# ensure no govenor weirdness
|
||||
# sudo cpufreq-set -g performance
|
||||
go test -test.benchmem=true -run=NONE -bench=. ./... > bench_current.test
|
||||
git stash save "stashing for benchcmp"
|
||||
@go test -test.benchmem=true -run=NONE -bench=. ./... > bench_head.test
|
||||
git stash pop
|
||||
benchcmp bench_head.test bench_current.test
|
||||
|
||||
profile:
|
||||
@mkdir -p pprof/
|
||||
go test -run=NONE -cpuprofile pprof/cpu.prof -memprofile pprof/mem.prof -bench .
|
||||
go tool pprof -pdf pprof/cpu.prof > pprof/cpu.pdf
|
||||
xdg-open pprof/cpu.pdf
|
||||
go tool pprof -weblist=.* pprof/cpu.prof
|
||||
+87
@@ -0,0 +1,87 @@
|
||||
# GoLem
|
||||
|
||||
This project is a dictionary based lemmatizer written in go.
|
||||
|
||||
Since v4 all dictionaries need to be gotten individually.
|
||||
|
||||
```
|
||||
go get github.com/aaaton/golem/v4
|
||||
```
|
||||
|
||||
|
||||
### What?
|
||||
|
||||
A [lemmatizer](https://en.wikipedia.org/wiki/Lemmatisation) is a tool that finds the base form of words.
|
||||
|
||||
| Lang | Input | Output |
|
||||
| ------- | ---------- | ------- |
|
||||
| English | aligning | align |
|
||||
| Swedish | sprungit | springa |
|
||||
| French | abattaient | abattre |
|
||||
|
||||
It's based on the dictionaries found on [michmech/lemmatization-lists](https://github.com/michmech/lemmatization-lists), which are available under the [Open Database License](https://opendatacommons.org/licenses/odbl/summary/). This project would not be feasible without them.
|
||||
|
||||
### Languages
|
||||
|
||||
At the moment golem supports English, Swedish, French, Spanish, Italian & German, but adding another language should be no more trouble than getting the dictionary for that language. Some of which are already available on lexiconista. Please let me know if there is something you would like to see in here, or fork the project and create a pull request.
|
||||
|
||||
English
|
||||
```
|
||||
go get github.com/aaaton/golem/v4/dicts/en
|
||||
```
|
||||
|
||||
Swedish
|
||||
```
|
||||
go get github.com/aaaton/golem/v4/dicts/sv
|
||||
```
|
||||
|
||||
French
|
||||
```
|
||||
go get github.com/aaaton/golem/v4/dicts/fr
|
||||
```
|
||||
|
||||
German
|
||||
```
|
||||
go get github.com/aaaton/golem/v4/dicts/de
|
||||
```
|
||||
|
||||
Spanish
|
||||
```
|
||||
go get github.com/aaaton/golem/v4/dicts/es
|
||||
```
|
||||
|
||||
Italian
|
||||
```
|
||||
go get github.com/aaaton/golem/v4/dicts/it
|
||||
```
|
||||
|
||||
### Basic usage
|
||||
|
||||
```golang
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/aaaton/golem/v4"
|
||||
"github.com/aaaton/golem/v4/dicts/en"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// the language packages are available under golem/dicts
|
||||
// "en" is for english
|
||||
lemmatizer, err := golem.New(en.New())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
word := lemmatizer.Lemma("Abducting")
|
||||
if word != "abduct" {
|
||||
panic("The output is not what is expected!")
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Contributors
|
||||
|
||||
- axamon
|
||||
- charlesgiroux
|
||||
- glaslos
|
||||
- ptdewey
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2019 Anton Södergren
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
+43
File diff suppressed because one or more lines are too long
+101
@@ -0,0 +1,101 @@
|
||||
package golem
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// LanguagePack is what each language should implement
|
||||
type LanguagePack interface {
|
||||
GetResource() ([]byte, error)
|
||||
GetLocale() string
|
||||
}
|
||||
|
||||
// Lemmatizer is the key to lemmatizing a word in a language
|
||||
type Lemmatizer struct {
|
||||
m map[string]int
|
||||
v [][]string
|
||||
}
|
||||
|
||||
func newLemmatizerFromBytes(b []byte) (Lemmatizer, error) {
|
||||
lines := strings.Split(string(b), "\n")
|
||||
s := Lemmatizer{
|
||||
m: make(map[string]int),
|
||||
v: [][]string{},
|
||||
}
|
||||
// TODO: Would it be better to do with a reader
|
||||
// instead of loading the full thing into an array?
|
||||
|
||||
// br := bufio.NewReader(bytes.NewReader(b))
|
||||
// line, err := br.ReadString('\n')
|
||||
// for err == nil {
|
||||
// wordIndex := make(map[string])
|
||||
for _, line := range lines {
|
||||
if len(line) == 0 {
|
||||
continue
|
||||
}
|
||||
words := strings.Split(line, "\t")
|
||||
if len(words) < 2 {
|
||||
return s, fmt.Errorf("expected more than 1 form per word")
|
||||
}
|
||||
base := words[0]
|
||||
for _, word := range words {
|
||||
if index, ok := s.m[word]; ok {
|
||||
s.v[index] = append(s.v[index], word)
|
||||
} else {
|
||||
index := len(s.v)
|
||||
s.v = append(s.v, []string{base})
|
||||
s.m[word] = index
|
||||
}
|
||||
}
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// New produces a new Lemmatizer
|
||||
func New(pack LanguagePack) (*Lemmatizer, error) {
|
||||
resource, err := pack.GetResource()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(`Could not open resource file for "%s"`, pack.GetLocale())
|
||||
}
|
||||
l, err := newLemmatizerFromBytes(resource)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(`language %s is not valid: %s`, pack.GetLocale(), err)
|
||||
}
|
||||
return &l, nil
|
||||
}
|
||||
|
||||
// InDict checks if a certain word is in the dictionary
|
||||
func (l *Lemmatizer) InDict(word string) bool {
|
||||
_, ok := l.m[strings.ToLower(word)]
|
||||
return ok
|
||||
}
|
||||
|
||||
// Lemma gets one of the base forms of a word
|
||||
func (l *Lemmatizer) Lemma(word string) string {
|
||||
if out, ok := l.m[strings.ToLower(word)]; ok {
|
||||
return l.v[out][0]
|
||||
}
|
||||
return word
|
||||
}
|
||||
|
||||
// LemmaLower gets one of the base forms of a lower case word
|
||||
// expects `word` to be lowercased
|
||||
func (l *Lemmatizer) LemmaLower(word string) string {
|
||||
if out, ok := l.m[word]; ok {
|
||||
return l.v[out][0]
|
||||
}
|
||||
return word
|
||||
}
|
||||
|
||||
// Lemmas gets all the base forms of a word, if multiple exist
|
||||
func (l *Lemmatizer) Lemmas(word string) (out []string) {
|
||||
if index, ok := l.m[strings.ToLower(word)]; ok {
|
||||
out := l.v[index]
|
||||
// to get rid of the randomness, we sort the output
|
||||
sort.Strings(out)
|
||||
return out
|
||||
}
|
||||
return []string{word}
|
||||
}
|
||||
Vendored
+7
-2
@@ -1,3 +1,9 @@
|
||||
# github.com/aaaton/golem/v4 v4.0.2
|
||||
## explicit; go 1.13
|
||||
github.com/aaaton/golem/v4
|
||||
# github.com/aaaton/golem/v4/dicts/ru v0.0.0-20250408131944-3488790fc110
|
||||
## explicit; go 1.13
|
||||
github.com/aaaton/golem/v4/dicts/ru
|
||||
# github.com/coder/websocket v1.8.12
|
||||
## explicit; go 1.19
|
||||
github.com/coder/websocket
|
||||
@@ -15,8 +21,6 @@ github.com/google/uuid
|
||||
# github.com/kami/hexis v0.0.0 => /home/kami/apps/hexis
|
||||
## explicit; go 1.25.5
|
||||
github.com/kami/hexis/pkg/client
|
||||
# github.com/kami/praxis v0.0.0 => /home/kami/apps/praxis
|
||||
## explicit; go 1.23
|
||||
# github.com/mattn/go-isatty v0.0.20
|
||||
## explicit; go 1.15
|
||||
github.com/mattn/go-isatty
|
||||
@@ -79,4 +83,5 @@ modernc.org/memory
|
||||
modernc.org/sqlite
|
||||
modernc.org/sqlite/lib
|
||||
modernc.org/sqlite/vtab
|
||||
# github.com/kami/praxis v0.0.0 => /home/kami/apps/praxis
|
||||
# github.com/kami/nexus v0.0.0 => /home/kami/apps/nexus
|
||||
|
||||
Reference in New Issue
Block a user