Implement generating compound nouns

Implemented generating compound nouns as well as filtering for the type
of noun when getting a list of noun IDs.
This commit is contained in:
Zorchenhimer 2021-02-14 18:33:13 -05:00
parent 8fd9d71625
commit 72298d6e8e
Signed by: Zorchenhimer
GPG Key ID: 70A1AB767AAB9C20
3 changed files with 50 additions and 13 deletions

View File

@ -27,7 +27,7 @@ type DB interface {
RemoveVerb(id int) error RemoveVerb(id int) error
GetAdjectiveIds() ([]int, error) GetAdjectiveIds() ([]int, error)
GetNounIds() ([]int, error) GetNounIds(begin, end, alone bool) ([]int, error)
GetVerbIds() ([]int, error) GetVerbIds() ([]int, error)
GetAdjective(id int) (*models.Adjective, error) GetAdjective(id int) (*models.Adjective, error)

View File

@ -157,8 +157,14 @@ func (s *sqliteDb) RemoveVerb(id int) error {
return s.removeWord("delete from verbs where id = ?", id) return s.removeWord("delete from verbs where id = ?", id)
} }
func (s *sqliteDb) readIds(query string) ([]int, error) { func (s *sqliteDb) readIds(query string, args ...interface{}) ([]int, error) {
rows, err := s.db.Query("select id from adjectives") stmt, err := s.db.Prepare(query)
if err != nil {
return nil, err
}
defer stmt.Close()
rows, err := stmt.Query(args...)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -180,8 +186,8 @@ func (s *sqliteDb) GetAdjectiveIds() ([]int, error) {
return s.readIds("select id from adjectives") return s.readIds("select id from adjectives")
} }
func (s *sqliteDb) GetNounIds() ([]int, error) { func (s *sqliteDb) GetNounIds(begin, end, alone bool) ([]int, error) {
return s.readIds("select id from nouns") return s.readIds("select id from nouns where begin = ? or end = ? or alone = ?", begin, end, alone)
} }
func (s *sqliteDb) GetVerbIds() ([]int, error) { func (s *sqliteDb) GetVerbIds() ([]int, error) {

View File

@ -23,8 +23,9 @@ func (g *generic) Hack() (string, error) {
definite := rand.Int() % 2 == 0 definite := rand.Int() % 2 == 0
hasAdj := rand.Int() % 2 == 0 hasAdj := rand.Int() % 2 == 0
plural := rand.Int() % 2 == 0 plural := rand.Int() % 2 == 0
compound := rand.Int() % 2 == 0
np, err := g.nounPhrase(definite, hasAdj, plural) np, err := g.nounPhrase(definite, hasAdj, plural, compound)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -35,7 +36,7 @@ func (g *generic) Hack() (string, error) {
sb.WriteString(toCap(np)) sb.WriteString(toCap(np))
ctime := models.CM_Present ctime := models.CM_Present
ctype := models.CT_I ctype := models.CT_It
invert := false // TODO: implement this invert := false // TODO: implement this
if plural { if plural {
@ -49,7 +50,28 @@ func (g *generic) Hack() (string, error) {
sb.WriteString(" ") sb.WriteString(" ")
sb.WriteString(v) sb.WriteString(v)
definite = rand.Int() % 2 == 0
hasAdj = rand.Int() % 2 == 0
plural = rand.Int() % 2 == 0
np2, err := g.nounPhrase(definite, hasAdj, plural, false)
if err != nil {
return "", err
}
sb.WriteString(" ") sb.WriteString(" ")
sb.WriteString(np2)
sb.WriteString(". With ")
plural = rand.Int() % 2 == 0
np3, err := g.nounPhrase(false, false, plural, true)
if err != nil {
return "", err
}
sb.WriteString(np3)
sb.WriteString("!")
return sb.String(), nil return sb.String(), nil
} }
@ -58,7 +80,7 @@ func (g *generic) Format(format string) (string, error) {
return "", fmt.Errorf("Not implemented") return "", fmt.Errorf("Not implemented")
} }
func (g *generic) nounPhrase(definite, hasAdj, plural bool) (string, error){ func (g *generic) nounPhrase(definite, hasAdj, plural, compound bool) (string, error){
adj := "" adj := ""
var err error var err error
if hasAdj { if hasAdj {
@ -68,7 +90,7 @@ func (g *generic) nounPhrase(definite, hasAdj, plural bool) (string, error){
} }
} }
noun, err := g.randomNoun(plural) noun, err := g.randomNoun(plural, compound)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -114,10 +136,19 @@ func (g *generic) randomAdjective() (string, error) {
return adj.Word, nil return adj.Word, nil
} }
func (g *generic) randomNoun(plural bool) (string, error) { func (g *generic) randomNoun(plural, compound bool) (string, error) {
ids, err := g.db.GetNounIds() var ids []int
if err != nil { var err error
return "", fmt.Errorf("[noun] get IDs error: %v", err) if compound {
ids, err = g.db.GetNounIds(true, true, true)
if err != nil {
return "", fmt.Errorf("[noun] get IDs error: %v", err)
}
} else {
ids, err = g.db.GetNounIds(true, false, false)
if err != nil {
return "", fmt.Errorf("[noun] get IDs error: %v", err)
}
} }
if len(ids) <= 0 { if len(ids) <= 0 {