diff --git a/database/database.go b/database/database.go index b1a4168..1abc4d8 100644 --- a/database/database.go +++ b/database/database.go @@ -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) diff --git a/database/sqlite.go b/database/sqlite.go index 67bb4f6..d7677fe 100644 --- a/database/sqlite.go +++ b/database/sqlite.go @@ -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) { diff --git a/generic.go b/generic.go index 273e9cb..4d81ae3 100644 --- a/generic.go +++ b/generic.go @@ -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 {