Add verb conjugation

Added conjugation for verbs.  A single verb now gets appended onto a
noun phrase.
This commit is contained in:
Zorchenhimer 2021-02-13 20:22:55 -05:00
parent 32638d3b97
commit 64b7ce434f
2 changed files with 117 additions and 10 deletions

View File

@ -34,6 +34,23 @@ func (g *generic) Random() (string, error) {
sb := strings.Builder{} sb := strings.Builder{}
sb.WriteString(np) 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 return sb.String(), nil
} }
@ -121,7 +138,7 @@ func (g *generic) randomNoun(plural bool) (string, error) {
return noun.Word, nil 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() ids, err := g.db.GetVerbIds()
if err != nil { if err != nil {
return "", fmt.Errorf("[verb] get IDs error: %v", err) 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 "", 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 { func (g *generic) InitData(filename string) error {

View File

@ -1,5 +1,10 @@
package models package models
import (
"fmt"
"strings"
)
type Verb struct { type Verb struct {
Id int Id int
@ -8,21 +13,106 @@ type Verb struct {
// Indefinite form // Indefinite form
Word string Word string
ConjugationsPast []Conjugate //ConjugationsPast []Conjugate
ConjugationsPresent []Conjugate //ConjugationsPresent []Conjugate
ConjugationsFuture []Conjugate //ConjugationsFuture []Conjugate
} }
type ConjugateType int //type Conjugate struct {
// Type ConjugateType
// Form string
//}
type ConjugationType int
const ( const (
CT_I ConjugateType = iota CT_I ConjugationType = iota
CT_You CT_You
CT_It CT_It
CT_We CT_We
CT_They CT_They
) )
type Conjugate struct { func (ct ConjugationType) String() string {
Type ConjugateType switch ct {
Form string 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
} }