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

View File

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