Add some more sentence types; Add pronouns
Added pronouns to the data. These act similar to the other types of words regarding getting random ones from the database. More sentence types have been added, however they are all in their own functions for now. Eventually, these functions will be replaced by format strings in the database and will not be hard coded.
This commit is contained in:
parent
72298d6e8e
commit
965a84725f
|
@ -21,20 +21,24 @@ type DB interface {
|
|||
AddAdjective(word models.Adjective) error
|
||||
AddNoun(word models.Noun) error
|
||||
AddVerb(word models.Verb) error
|
||||
//AddPronoun(word models.Verb) error
|
||||
|
||||
RemoveAdjective(id int) error
|
||||
RemoveNoun(id int) error
|
||||
RemoveVerb(id int) error
|
||||
//RemovePronoun(id int) error
|
||||
|
||||
GetAdjectiveIds() ([]int, error)
|
||||
GetNounIds(begin, end, alone bool) ([]int, error)
|
||||
GetVerbIds() ([]int, error)
|
||||
GetPronounIds(plural bool) ([]int, error)
|
||||
|
||||
GetAdjective(id int) (*models.Adjective, error)
|
||||
GetNoun(id int) (*models.Noun, error)
|
||||
GetVerb(id int) (*models.Verb, error)
|
||||
GetPronoun(id int) (*models.Pronoun, error)
|
||||
|
||||
InitData([]models.Adjective, []models.Noun, []models.Verb, []string) error
|
||||
InitData([]models.Adjective, []models.Noun, []models.Verb, []models.Pronoun, []string) error
|
||||
IsNew() bool
|
||||
Close()
|
||||
}
|
||||
|
|
|
@ -37,6 +37,7 @@ func sqliteInit(connectionString string) (DB, error) {
|
|||
create table Adjectives (id integer not null primary key, absolute bool, appendMore bool, appendEst bool, word text);
|
||||
create table Nouns (id integer not null primary key, multiple bool, begin bool, end bool, alone bool, regular bool, word text);
|
||||
create table Verbs (id integer not null primary key, regular bool, word text);
|
||||
create table Pronouns (id integer not null primary key, plural bool, word text);
|
||||
`
|
||||
//create table Sentences (id integer not null primary key, sentence text)
|
||||
|
||||
|
@ -194,6 +195,10 @@ func (s *sqliteDb) GetVerbIds() ([]int, error) {
|
|||
return s.readIds("select id from verbs")
|
||||
}
|
||||
|
||||
func (s *sqliteDb) GetPronounIds(plural bool) ([]int, error) {
|
||||
return s.readIds("select id from pronouns where plural = ?", plural)
|
||||
}
|
||||
|
||||
func (s *sqliteDb) GetAdjective(id int) (*models.Adjective, error) {
|
||||
stmt, err := s.db.Prepare("select Id, Absolute, AppendMore, AppendEst, Word from Adjectives where id = ?")
|
||||
if err != nil {
|
||||
|
@ -239,7 +244,22 @@ func (s *sqliteDb) GetVerb(id int) (*models.Verb, error) {
|
|||
return verb, nil
|
||||
}
|
||||
|
||||
func (s *sqliteDb) InitData(adjectives []models.Adjective, nouns []models.Noun, verbs []models.Verb, sentences []string) error {
|
||||
func (s *sqliteDb) GetPronoun(id int) (*models.Pronoun, error) {
|
||||
stmt, err := s.db.Prepare("select Id, Plural, Word from Pronouns where id = ?")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
pn := &models.Pronoun{}
|
||||
if err = stmt.QueryRow(id).Scan(&pn.Id, &pn.Plural, &pn.Word); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return pn, nil
|
||||
}
|
||||
|
||||
func (s *sqliteDb) InitData(adjectives []models.Adjective, nouns []models.Noun, verbs []models.Verb, pronouns []models.Pronoun, sentences []string) error {
|
||||
tx, err := s.db.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -298,6 +318,28 @@ func (s *sqliteDb) InitData(adjectives []models.Adjective, nouns []models.Noun,
|
|||
}
|
||||
vstmt.Close()
|
||||
|
||||
if pronouns == nil || len(pronouns) == 0 {
|
||||
tx.Rollback()
|
||||
return fmt.Errorf("[init] no pronouns to insert")
|
||||
}
|
||||
pstmt_txt := "insert into pronouns (Plural, Word) values (?, ?)"
|
||||
fmt.Println(pstmt_txt)
|
||||
|
||||
pstmt, err := tx.Prepare(pstmt_txt)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
|
||||
for _, pronoun := range pronouns {
|
||||
_, err := pstmt.Exec(pronoun.Plural, pronoun.Word)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
}
|
||||
pstmt.Close()
|
||||
|
||||
//sstmt, err := tx.Prepare("insert into sentences (Sentence) values (?)")
|
||||
//if err != nil {
|
||||
// tx.Rollback()
|
||||
|
|
135
generic.go
135
generic.go
|
@ -20,6 +20,106 @@ func NewGeneric(db database.DB) (HackerQuotes, error) {
|
|||
}
|
||||
|
||||
func (g *generic) Hack() (string, error) {
|
||||
sb := strings.Builder{}
|
||||
|
||||
invert := rand.Int() % 2 == 0
|
||||
plural := rand.Int() % 2 == 0
|
||||
|
||||
pn, err := g.randomPronoun(plural)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
sb.WriteString(pn)
|
||||
sb.WriteString(" can't ")
|
||||
|
||||
v, err := g.randomVerb(models.CT_I, models.CM_Present, invert)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
sb.WriteString(v)
|
||||
sb.WriteString(" ")
|
||||
|
||||
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, compound)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
sb.WriteString(np)
|
||||
sb.WriteString(", it ")
|
||||
|
||||
v2, err := g.randomVerb(models.CT_It, models.CM_Future, invert)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
sb.WriteString(v2)
|
||||
sb.WriteString(" ")
|
||||
|
||||
np2, err := g.nounPhrase(definite, hasAdj, plural, compound)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
sb.WriteString(np2)
|
||||
sb.WriteString("!")
|
||||
|
||||
return toCap(sb.String()), nil
|
||||
}
|
||||
|
||||
func (g *generic) Hack_t1() (string, error) {
|
||||
sb := strings.Builder{}
|
||||
invert := false
|
||||
|
||||
v, err := g.randomVerb(models.CT_You, models.CM_Present, invert)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
sb.WriteString(toCap(v))
|
||||
sb.WriteString(" ")
|
||||
|
||||
hasAdj := rand.Int() % 2 == 0
|
||||
plural := rand.Int() % 2 == 0
|
||||
compound := rand.Int() % 2 == 0
|
||||
|
||||
np, err := g.nounPhrase(true, hasAdj, plural, compound)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
sb.WriteString(np)
|
||||
sb.WriteString(", then you can ")
|
||||
|
||||
v2, err := g.randomVerb(models.CT_You, models.CM_Present, invert)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
sb.WriteString(v2)
|
||||
sb.WriteString(" ")
|
||||
|
||||
hasAdj = rand.Int() % 2 == 0
|
||||
plural = rand.Int() % 2 == 0
|
||||
compound = rand.Int() % 2 == 0
|
||||
|
||||
np2, err := g.nounPhrase(true, hasAdj, plural, compound)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
sb.WriteString(np2)
|
||||
sb.WriteString("!")
|
||||
|
||||
return sb.String(), err
|
||||
}
|
||||
|
||||
func (g *generic) Hack_t0() (string, error) {
|
||||
definite := rand.Int() % 2 == 0
|
||||
hasAdj := rand.Int() % 2 == 0
|
||||
plural := rand.Int() % 2 == 0
|
||||
|
@ -188,6 +288,25 @@ func (g *generic) randomVerb(ctype models.ConjugationType, ctime models.Conjugat
|
|||
return verb.Conjugate(ctype, ctime, invert), nil
|
||||
}
|
||||
|
||||
func (g *generic) randomPronoun(plural bool) (string, error) {
|
||||
ids, err := g.db.GetPronounIds(plural)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("[pronoun] get IDs error: %v", err)
|
||||
}
|
||||
|
||||
if len(ids) <= 0 {
|
||||
return "", fmt.Errorf("No pronoun IDs returned from database")
|
||||
}
|
||||
|
||||
rid := int(rand.Int63n(int64(len(ids))))
|
||||
pronoun, err := g.db.GetPronoun(ids[rid])
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("[pronoun] ID: %d; %v", ids[rid], err)
|
||||
}
|
||||
|
||||
return pronoun.Word, nil
|
||||
}
|
||||
|
||||
func (g *generic) InitData(filename string) error {
|
||||
fmt.Printf("Initializing database with data in %q\n", filename)
|
||||
if g.db == nil {
|
||||
|
@ -275,7 +394,21 @@ func (g *generic) InitData(filename string) error {
|
|||
verbs = append(verbs, v)
|
||||
}
|
||||
|
||||
return g.db.InitData(adjectives, nouns, verbs, nil)
|
||||
rawpronouns, ok := data["pronouns"]
|
||||
if !ok {
|
||||
return fmt.Errorf("Missing pronouns key in data")
|
||||
}
|
||||
|
||||
pronouns := []models.Pronoun{}
|
||||
for _, word := range rawpronouns {
|
||||
p := models.Pronoun{Word: word[1]}
|
||||
if strings.Contains(word[0], "p") {
|
||||
p.Plural = true
|
||||
}
|
||||
pronouns = append(pronouns, p)
|
||||
}
|
||||
|
||||
return g.db.InitData(adjectives, nouns, verbs, pronouns, nil)
|
||||
}
|
||||
|
||||
// Prepend "a", "an" or nothing to a phrase
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
package models
|
||||
|
||||
type Pronoun struct {
|
||||
Id int
|
||||
Plural bool
|
||||
Word string
|
||||
}
|
|
@ -401,5 +401,17 @@
|
|||
["r", "alter"],
|
||||
["r", "mute"],
|
||||
["r", "hash"]
|
||||
],
|
||||
|
||||
"pronouns": [
|
||||
["s", "he"],
|
||||
["s", "she"],
|
||||
["s", "it"],
|
||||
["s", "you"],
|
||||
["p", "you"],
|
||||
["s", "they"],
|
||||
["p", "they"],
|
||||
["s", "i"],
|
||||
["p", "we"]
|
||||
]
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue