From 64b7ce434ff836a2c453a08abe4387929fa4ac9a Mon Sep 17 00:00:00 2001 From: Zorchenhimer Date: Sat, 13 Feb 2021 20:22:55 -0500 Subject: [PATCH] Add verb conjugation Added conjugation for verbs. A single verb now gets appended onto a noun phrase. --- generic.go | 21 +++++++++- models/verb.go | 106 +++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 117 insertions(+), 10 deletions(-) diff --git a/generic.go b/generic.go index decb930..4e3cfd1 100644 --- a/generic.go +++ b/generic.go @@ -34,6 +34,23 @@ func (g *generic) Random() (string, error) { sb := strings.Builder{} sb.WriteString(np) + ctime := models.CM_Present + ctype := models.CT_I + invert := false // TODO: implement this + + if plural { + ctype = models.CT_They + } + + v, err := g.randomVerb(ctype, ctime, invert) + if err != nil { + return "", err + } + + sb.WriteString(" ") + sb.WriteString(v) + sb.WriteString(" ") + return sb.String(), nil } @@ -121,7 +138,7 @@ func (g *generic) randomNoun(plural bool) (string, error) { return noun.Word, nil } -func (g *generic) randomVerb() (string, error) { +func (g *generic) randomVerb(ctype models.ConjugationType, ctime models.ConjugationTime, invert bool) (string, error) { ids, err := g.db.GetVerbIds() if err != nil { return "", fmt.Errorf("[verb] get IDs error: %v", err) @@ -137,7 +154,7 @@ func (g *generic) randomVerb() (string, error) { return "", fmt.Errorf("[verb] ID: %d; %v", ids[rid], err) } - return verb.Word, nil + return verb.Conjugate(ctype, ctime, invert), nil } func (g *generic) InitData(filename string) error { diff --git a/models/verb.go b/models/verb.go index 15634d6..472cb1a 100644 --- a/models/verb.go +++ b/models/verb.go @@ -1,5 +1,10 @@ package models +import ( + "fmt" + "strings" +) + type Verb struct { Id int @@ -8,21 +13,106 @@ type Verb struct { // Indefinite form Word string - ConjugationsPast []Conjugate - ConjugationsPresent []Conjugate - ConjugationsFuture []Conjugate + //ConjugationsPast []Conjugate + //ConjugationsPresent []Conjugate + //ConjugationsFuture []Conjugate } -type ConjugateType int +//type Conjugate struct { +// Type ConjugateType +// Form string +//} + +type ConjugationType int const ( - CT_I ConjugateType = iota + CT_I ConjugationType = iota CT_You CT_It CT_We CT_They ) -type Conjugate struct { - Type ConjugateType - Form string +func (ct ConjugationType) String() string { + switch ct { + case CT_I: + return "I" + case CT_It: + return "It" + case CT_You: + return "You" + case CT_We: + return "We" + case CT_They: + return "They" + default: + return "Unknown" + } +} + +type ConjugationTime int +const ( + CM_Present ConjugationTime = iota + CM_Past + CM_Future +) + +func (cm ConjugationTime) String() string { + switch cm { + case CM_Present: + return "Present" + case CM_Past: + return "Past" + case CM_Future: + return "Future" + default: + return "Unknown" + } +} + +// TODO: implement inverted +func (v Verb) Conjugate(conjugation ConjugationType, time ConjugationTime, inverted bool ) string { + if !v.Regular { + fmt.Printf("[verb] Irregular verbs not implemented! %q\n", v.Word) + return v.Word + } + + //fmt.Printf("[verb] word: %s; type: %s; time: %s\n", v.Word, conjugation, time) + + switch time { + case CM_Present: + if conjugation == CT_It { + sfx := []string{"s", "x", "sh", "ch", "ss"} + for _, s := range sfx { + if strings.HasSuffix(v.Word, s) { + return v.Word + "es" + } + } + + if strings.HasSuffix(v.Word, "y") && !strings.ContainsAny(string(v.Word[len(v.Word)-2]), "aeiou") { + return v.Word[:len(v.Word)-1] + "ies" + } + + return v.Word + "s" + + } else { + return v.Word + } + + case CM_Past: + if strings.HasSuffix(v.Word, "e") { + return v.Word + "d" + } + + if strings.HasSuffix(v.Word, "y") && !strings.ContainsAny(string(v.Word[len(v.Word)-2]), "aeiou") { + return v.Word[:len(v.Word)-1] + "ied" + } + + return v.Word + "ed" + + case CM_Future: + return "will " + v.Word + } + + fmt.Printf("[verb] Unknown ConjugationTime: %d\n", conjugation) + return v.Word }