Start implementing format sentences
Base word types are implemented. The format for defining a word is as follows: {word_type:optionA,optionB} Where `word_type` is one of the following: - adjective - noun - noun_phrase - pronoun - verb Followed by a colon and a comma separated list of options for that word. Options are as follows (adjectives have no options): Nouns: - compound - plural Noun Phrase: - indefinite - noadj - plural - compound Pronouns: - plural Verbs: - conjugation type: i (default), you, it, we, or they - conjugation time: past, present (default), or future - invert (not implemented yet) Options for a word type are optional and may be omitted.
This commit is contained in:
parent
c376180b27
commit
94a46a7072
|
@ -15,9 +15,6 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
type DB interface {
|
type DB interface {
|
||||||
// Sentence returns a format string for a sentence with the given ID.
|
|
||||||
//Sentence(id int) (string, error)
|
|
||||||
|
|
||||||
AddAdjective(word models.Adjective) error
|
AddAdjective(word models.Adjective) error
|
||||||
AddNoun(word models.Noun) error
|
AddNoun(word models.Noun) error
|
||||||
AddVerb(word models.Verb) error
|
AddVerb(word models.Verb) error
|
||||||
|
@ -32,11 +29,13 @@ type DB interface {
|
||||||
GetNounIds(begin, end, alone bool) ([]int, error)
|
GetNounIds(begin, end, alone bool) ([]int, error)
|
||||||
GetVerbIds() ([]int, error)
|
GetVerbIds() ([]int, error)
|
||||||
GetPronounIds(plural bool) ([]int, error)
|
GetPronounIds(plural bool) ([]int, error)
|
||||||
|
GetSentenceIds() ([]int, error)
|
||||||
|
|
||||||
GetAdjective(id int) (*models.Adjective, error)
|
GetAdjective(id int) (*models.Adjective, error)
|
||||||
GetNoun(id int) (*models.Noun, error)
|
GetNoun(id int) (*models.Noun, error)
|
||||||
GetVerb(id int) (*models.Verb, error)
|
GetVerb(id int) (*models.Verb, error)
|
||||||
GetPronoun(id int) (*models.Pronoun, error)
|
GetPronoun(id int) (*models.Pronoun, error)
|
||||||
|
GetSentence(id int) (string, error)
|
||||||
|
|
||||||
InitData([]models.Adjective, []models.Noun, []models.Verb, []models.Pronoun, []string) error
|
InitData([]models.Adjective, []models.Noun, []models.Verb, []models.Pronoun, []string) error
|
||||||
IsNew() bool
|
IsNew() bool
|
||||||
|
|
|
@ -38,8 +38,8 @@ func sqliteInit(connectionString string) (DB, error) {
|
||||||
create table Nouns (id integer not null primary key, multiple bool, begin bool, end bool, alone bool, regular 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 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 Pronouns (id integer not null primary key, plural bool, word text);
|
||||||
|
create table Sentences (id integer not null primary key, sentence text);
|
||||||
`
|
`
|
||||||
//create table Sentences (id integer not null primary key, sentence text)
|
|
||||||
|
|
||||||
if _, err := db.Exec(stmt); err != nil {
|
if _, err := db.Exec(stmt); err != nil {
|
||||||
fmt.Println("[sqlite], DB table creation error:", err)
|
fmt.Println("[sqlite], DB table creation error:", err)
|
||||||
|
@ -66,21 +66,6 @@ func (s *sqliteDb) prep(query string) (*sql.Tx, *sql.Stmt, error) {
|
||||||
return tx, stmt, nil
|
return tx, stmt, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *sqliteDb) Sentence(id int) (string, error) {
|
|
||||||
stmt, err := s.db.Prepare("select from sentences where id = ?")
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
defer stmt.Close()
|
|
||||||
|
|
||||||
var sentence string
|
|
||||||
if err = stmt.QueryRow(id).Scan(&sentence); err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
return sentence, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *sqliteDb) AddAdjective(word models.Adjective) error {
|
func (s *sqliteDb) AddAdjective(word models.Adjective) error {
|
||||||
tx, stmt, err := s.prep("insert into Adjectives (Absolute, AppendMore, AppendEst, Word) values (?, ?, ?, ?)")
|
tx, stmt, err := s.prep("insert into Adjectives (Absolute, AppendMore, AppendEst, Word) values (?, ?, ?, ?)")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -199,6 +184,10 @@ func (s *sqliteDb) GetPronounIds(plural bool) ([]int, error) {
|
||||||
return s.readIds("select id from pronouns where plural = ?", plural)
|
return s.readIds("select id from pronouns where plural = ?", plural)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *sqliteDb) GetSentenceIds() ([]int, error) {
|
||||||
|
return s.readIds("select id from sentences")
|
||||||
|
}
|
||||||
|
|
||||||
func (s *sqliteDb) GetAdjective(id int) (*models.Adjective, error) {
|
func (s *sqliteDb) GetAdjective(id int) (*models.Adjective, error) {
|
||||||
stmt, err := s.db.Prepare("select Id, Absolute, AppendMore, AppendEst, Word from Adjectives where id = ?")
|
stmt, err := s.db.Prepare("select Id, Absolute, AppendMore, AppendEst, Word from Adjectives where id = ?")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -259,6 +248,21 @@ func (s *sqliteDb) GetPronoun(id int) (*models.Pronoun, error) {
|
||||||
return pn, nil
|
return pn, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *sqliteDb) GetSentence(id int) (string, error) {
|
||||||
|
stmt, err := s.db.Prepare("select sentence from sentences where id = ?")
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
defer stmt.Close()
|
||||||
|
|
||||||
|
var sentence string
|
||||||
|
if err = stmt.QueryRow(id).Scan(&sentence); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return sentence, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *sqliteDb) InitData(adjectives []models.Adjective, nouns []models.Noun, verbs []models.Verb, pronouns []models.Pronoun, sentences []string) error {
|
func (s *sqliteDb) InitData(adjectives []models.Adjective, nouns []models.Noun, verbs []models.Verb, pronouns []models.Pronoun, sentences []string) error {
|
||||||
tx, err := s.db.Begin()
|
tx, err := s.db.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -340,20 +344,23 @@ func (s *sqliteDb) InitData(adjectives []models.Adjective, nouns []models.Noun,
|
||||||
}
|
}
|
||||||
pstmt.Close()
|
pstmt.Close()
|
||||||
|
|
||||||
//sstmt, err := tx.Prepare("insert into sentences (Sentence) values (?)")
|
sstmt_text := "insert into sentences (Sentence) values (?)"
|
||||||
//if err != nil {
|
fmt.Println(sstmt_text)
|
||||||
// tx.Rollback()
|
|
||||||
// return err
|
|
||||||
//}
|
|
||||||
|
|
||||||
//for _, sentence := range sentences {
|
sstmt, err := tx.Prepare(sstmt_text)
|
||||||
// _, err = sstmt.Exec(sentence)
|
if err != nil {
|
||||||
// if err != nil {
|
tx.Rollback()
|
||||||
// tx.Rollback()
|
return err
|
||||||
// return err
|
}
|
||||||
// }
|
|
||||||
//}
|
for _, sentence := range sentences {
|
||||||
//sstmt.Close()
|
_, err = sstmt.Exec(sentence)
|
||||||
|
if err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sstmt.Close()
|
||||||
|
|
||||||
err = tx.Commit()
|
err = tx.Commit()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
371
english.go
371
english.go
|
@ -11,6 +11,14 @@ import (
|
||||||
"github.com/zorchenhimer/hacker-quotes/models"
|
"github.com/zorchenhimer/hacker-quotes/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type InitialData struct {
|
||||||
|
Adjectives [][]string
|
||||||
|
Nouns [][]string
|
||||||
|
Verbs [][]string
|
||||||
|
Pronouns [][]string
|
||||||
|
Sentences []string
|
||||||
|
}
|
||||||
|
|
||||||
type english struct {
|
type english struct {
|
||||||
db database.DB
|
db database.DB
|
||||||
}
|
}
|
||||||
|
@ -27,172 +35,199 @@ func NewEnglish(db database.DB) (HackerQuotes, error) {
|
||||||
{{word_type:new word:properties}}
|
{{word_type:new word:properties}}
|
||||||
|
|
||||||
{pronoun} can't {verb:i,present} {noun_phrase}, it {verb:it,future} {noun_phrase}!
|
{pronoun} can't {verb:i,present} {noun_phrase}, it {verb:it,future} {noun_phrase}!
|
||||||
{verb:you,present} {noun_phrase:definite}, than you can {verb:you,present} {noun_phrase:definite}!
|
{verb:you,present} {noun_phrase:definite}, then you can {verb:you,present} {noun_phrase:definite}!
|
||||||
{noun_phrase} {verb}. With {noun_phrase:indifinite,noadj,compound}!
|
{noun_phrase} {verb}. With {noun_phrase:indifinite,noadj,compound}!
|
||||||
*/
|
*/
|
||||||
|
|
||||||
func (g *english) Hack() (string, error) {
|
func (g *english) Hack() (string, error) {
|
||||||
sb := strings.Builder{}
|
//var fmtString string = `{verb:you,present} {noun_phrase:definite} then you can {verb:you,present} {noun_phrase:definite}!`
|
||||||
|
str, err := g.randomSentence()
|
||||||
invert := rand.Int() % 2 == 0
|
|
||||||
plural := rand.Int() % 2 == 0
|
|
||||||
|
|
||||||
pn, err := g.randomPronoun(plural)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
sb.WriteString(pn)
|
return g.HackThis(str)
|
||||||
sb.WriteString(" can't ")
|
}
|
||||||
|
|
||||||
v, err := g.randomVerb(models.CT_I, models.CM_Present, invert)
|
func (g *english) HackThis(fmtString string) (string, error) {
|
||||||
|
var idx int
|
||||||
|
var err error
|
||||||
|
var nidx int
|
||||||
|
output := &strings.Builder{}
|
||||||
|
|
||||||
|
for idx < len(fmtString) {
|
||||||
|
if fmtString[idx] == '{' {
|
||||||
|
if fmtString[idx+1] == '{' {
|
||||||
|
nidx, err = g.consumeNewWord(fmtString, idx, output)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
idx = nidx
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
sb.WriteString(v)
|
nidx, err = g.consumeWord(fmtString, idx, output)
|
||||||
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 {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
idx = nidx
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
sb.WriteString(np)
|
nidx, err = g.consumeRaw(fmtString, idx, output)
|
||||||
sb.WriteString(", it ")
|
|
||||||
|
|
||||||
v2, err := g.randomVerb(models.CT_It, models.CM_Future, invert)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
idx = nidx
|
||||||
|
}
|
||||||
|
|
||||||
sb.WriteString(v2)
|
return toCap(output.String()), nil
|
||||||
sb.WriteString(" ")
|
}
|
||||||
|
|
||||||
definite = rand.Int() % 2 == 0
|
func (g *english) consumeRaw(fmtString string, idx int, output *strings.Builder) (int, error) {
|
||||||
hasAdj = rand.Int() % 2 == 0
|
end := strings.Index(fmtString[idx:], "{")
|
||||||
plural = rand.Int() % 2 == 0
|
if end == -1 {
|
||||||
compound = rand.Int() % 2 == 0
|
output.WriteString(fmtString[idx:len(fmtString)])
|
||||||
|
return len(fmtString), nil
|
||||||
|
}
|
||||||
|
|
||||||
np2, err := g.nounPhrase(definite, hasAdj, plural, compound)
|
output.WriteString(fmtString[idx:end+idx])
|
||||||
|
return idx+end, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *english) consumeNewWord(fmtString string, idx int, output *strings.Builder) (int, error) {
|
||||||
|
return 0, fmt.Errorf("not implemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *english) consumeWord(fmtString string, idx int, output *strings.Builder) (int, error) {
|
||||||
|
idx++
|
||||||
|
var wordtype string
|
||||||
|
var options string
|
||||||
|
|
||||||
|
end := strings.Index(fmtString[idx:], "}")
|
||||||
|
if end == -1 {
|
||||||
|
return 0, fmt.Errorf("[consumeWord] Unclosed definition starting at %d", idx)
|
||||||
|
}
|
||||||
|
|
||||||
|
end += idx
|
||||||
|
optsStart := strings.Index(fmtString[idx:end], ":")
|
||||||
|
|
||||||
|
if optsStart != -1 {
|
||||||
|
options = fmtString[optsStart+idx+1:end]
|
||||||
|
wordtype = fmtString[idx:optsStart+idx]
|
||||||
|
} else {
|
||||||
|
wordtype = fmtString[idx:end]
|
||||||
|
}
|
||||||
|
|
||||||
|
if wordtype == "" {
|
||||||
|
return 0, fmt.Errorf("[consumeWord] Missing word type at idx: %d", idx)
|
||||||
|
}
|
||||||
|
|
||||||
|
opts := strings.Split(options, ",")
|
||||||
|
var word string
|
||||||
|
var err error
|
||||||
|
|
||||||
|
switch wordtype {
|
||||||
|
case "pronoun":
|
||||||
|
var plural bool
|
||||||
|
if sliceContains(opts, "plural") {
|
||||||
|
plural = true
|
||||||
|
}
|
||||||
|
|
||||||
|
word, err = g.randomPronoun(plural)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
sb.WriteString(np2)
|
case "verb":
|
||||||
sb.WriteString("!")
|
var ct models.ConjugationType = models.CT_I
|
||||||
|
if sliceContains(opts, "i") {
|
||||||
return toCap(sb.String()), nil
|
ct = models.CT_I
|
||||||
|
} else if sliceContains(opts, "you") {
|
||||||
|
ct = models.CT_You
|
||||||
|
} else if sliceContains(opts, "it") {
|
||||||
|
ct = models.CT_It
|
||||||
|
} else if sliceContains(opts, "we") {
|
||||||
|
ct = models.CT_We
|
||||||
|
} else if sliceContains(opts, "they") {
|
||||||
|
ct = models.CT_They
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *english) Hack_t1() (string, error) {
|
var cm models.ConjugationTime = models.CM_Present
|
||||||
sb := strings.Builder{}
|
if sliceContains(opts, "present") {
|
||||||
invert := false
|
cm = models.CM_Present
|
||||||
|
} else if sliceContains(opts, "past") {
|
||||||
|
cm = models.CM_Past
|
||||||
|
} else if sliceContains(opts, "future") {
|
||||||
|
cm = models.CM_Future
|
||||||
|
}
|
||||||
|
|
||||||
v, err := g.randomVerb(models.CT_You, models.CM_Present, invert)
|
var invert bool = false
|
||||||
|
if sliceContains(opts, "invert") {
|
||||||
|
invert = true
|
||||||
|
}
|
||||||
|
|
||||||
|
word, err = g.randomVerb(ct, cm, invert)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
sb.WriteString(toCap(v))
|
case "noun":
|
||||||
sb.WriteString(" ")
|
var plural bool
|
||||||
|
var compound bool
|
||||||
|
|
||||||
hasAdj := rand.Int() % 2 == 0
|
if sliceContains(opts, "plural") {
|
||||||
plural := rand.Int() % 2 == 0
|
plural = true
|
||||||
compound := rand.Int() % 2 == 0
|
}
|
||||||
|
|
||||||
np, err := g.nounPhrase(true, hasAdj, plural, compound)
|
if sliceContains(opts, "compound") {
|
||||||
|
compound = true
|
||||||
|
}
|
||||||
|
|
||||||
|
word, err = g.randomNoun(plural, compound)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
sb.WriteString(np)
|
case "noun_phrase":
|
||||||
sb.WriteString(", then you can ")
|
var definite bool = true
|
||||||
|
var hasAdj bool = true
|
||||||
|
var plural bool = false
|
||||||
|
var compound bool = false
|
||||||
|
|
||||||
v2, err := g.randomVerb(models.CT_You, models.CM_Present, invert)
|
if sliceContains(opts, "indefinite") {
|
||||||
|
definite = false
|
||||||
|
}
|
||||||
|
|
||||||
|
if sliceContains(opts, "noadj") {
|
||||||
|
hasAdj = false
|
||||||
|
}
|
||||||
|
|
||||||
|
if sliceContains(opts, "plural") {
|
||||||
|
plural = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if sliceContains(opts, "compound") {
|
||||||
|
compound = true
|
||||||
|
}
|
||||||
|
|
||||||
|
word, err = g.nounPhrase(definite, hasAdj, plural, compound)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
sb.WriteString(v2)
|
case "adjective":
|
||||||
sb.WriteString(" ")
|
word, err = g.randomAdjective()
|
||||||
|
|
||||||
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 {
|
if err != nil {
|
||||||
return "", err
|
return 0, err
|
||||||
}
|
|
||||||
sb.WriteString(np2)
|
|
||||||
sb.WriteString("!")
|
|
||||||
|
|
||||||
return sb.String(), err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *english) Hack_t0() (string, error) {
|
default:
|
||||||
definite := rand.Int() % 2 == 0
|
return 0, fmt.Errorf("[consumeWord] Invalid word type %s at %d", wordtype, idx)
|
||||||
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 := strings.Builder{}
|
output.WriteString(word)
|
||||||
sb.WriteString(toCap(np))
|
return end+1, nil
|
||||||
|
|
||||||
ctime := models.CM_Present
|
|
||||||
ctype := models.CT_It
|
|
||||||
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)
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *english) Format(format string) (string, error) {
|
|
||||||
return "", fmt.Errorf("Not implemented")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *english) nounPhrase(definite, hasAdj, plural, compound bool) (string, error){
|
func (g *english) nounPhrase(definite, hasAdj, plural, compound bool) (string, error){
|
||||||
|
@ -224,7 +259,7 @@ func (g *english) nounPhrase(definite, hasAdj, plural, compound bool) (string, e
|
||||||
|
|
||||||
if !plural {
|
if !plural {
|
||||||
//fmt.Println("[nounPhrase] !plural")
|
//fmt.Println("[nounPhrase] !plural")
|
||||||
return ana(phrase), nil
|
return g.ana(phrase), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return phrase, nil
|
return phrase, nil
|
||||||
|
@ -322,6 +357,25 @@ func (g *english) randomPronoun(plural bool) (string, error) {
|
||||||
return pronoun.Word, nil
|
return pronoun.Word, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (g *english) randomSentence() (string, error) {
|
||||||
|
ids, err := g.db.GetSentenceIds()
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("[sentence] get IDs error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(ids) <= 0 {
|
||||||
|
return "", fmt.Errorf("[sentence] No sentence IDs returned from database")
|
||||||
|
}
|
||||||
|
|
||||||
|
rid := int(rand.Int63n(int64(len(ids))))
|
||||||
|
sentence, err := g.db.GetSentence(ids[rid])
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("[sentence] ID: %d, %v", ids[rid], err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return sentence, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (g *english) InitData(filename string) error {
|
func (g *english) InitData(filename string) error {
|
||||||
fmt.Printf("Initializing database with data in %q\n", filename)
|
fmt.Printf("Initializing database with data in %q\n", filename)
|
||||||
if g.db == nil {
|
if g.db == nil {
|
||||||
|
@ -333,19 +387,18 @@ func (g *english) InitData(filename string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
data := map[string][][]string{}
|
//data := map[string][]interface{}{}
|
||||||
|
data := InitialData{}
|
||||||
if err = json.Unmarshal(raw, &data); err != nil {
|
if err = json.Unmarshal(raw, &data); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
rawadj, ok := data["adjectives"]
|
if data.Adjectives == nil || len(data.Adjectives) == 0 {
|
||||||
if !ok {
|
return fmt.Errorf("Missing Adjectives in input data")
|
||||||
return fmt.Errorf("Missing adjectives key in data")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
adjectives := []models.Adjective{}
|
adjectives := []models.Adjective{}
|
||||||
for _, adj := range rawadj {
|
for _, adj := range data.Adjectives {
|
||||||
t, word := adj[0], adj[1]
|
t, word := adj[0], adj[1]
|
||||||
a := models.Adjective{Word: word}
|
a := models.Adjective{Word: word}
|
||||||
if strings.Contains(t, "a") {
|
if strings.Contains(t, "a") {
|
||||||
|
@ -361,13 +414,12 @@ func (g *english) InitData(filename string) error {
|
||||||
adjectives = append(adjectives, a)
|
adjectives = append(adjectives, a)
|
||||||
}
|
}
|
||||||
|
|
||||||
rawnoun, ok := data["nouns"]
|
if data.Nouns == nil || len(data.Nouns) == 0 {
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("Missing nouns key in data")
|
return fmt.Errorf("Missing nouns key in data")
|
||||||
}
|
}
|
||||||
|
|
||||||
nouns := []models.Noun{}
|
nouns := []models.Noun{}
|
||||||
for _, noun := range rawnoun {
|
for _, noun := range data.Nouns {
|
||||||
t, word := noun[0], noun[1]
|
t, word := noun[0], noun[1]
|
||||||
n := models.Noun{Word: word}
|
n := models.Noun{Word: word}
|
||||||
|
|
||||||
|
@ -394,40 +446,42 @@ func (g *english) InitData(filename string) error {
|
||||||
nouns = append(nouns, n)
|
nouns = append(nouns, n)
|
||||||
}
|
}
|
||||||
|
|
||||||
rawverbs, ok := data["verbs"]
|
if data.Verbs == nil || len(data.Verbs) == 0 {
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("Missing verbs key in data")
|
return fmt.Errorf("Missing verbs key in data")
|
||||||
}
|
}
|
||||||
|
|
||||||
verbs := []models.Verb{}
|
verbs := []models.Verb{}
|
||||||
for _, word := range rawverbs {
|
for _, verb := range data.Verbs {
|
||||||
v := models.Verb{Word: word[1]}
|
v := models.Verb{Word: verb[1]}
|
||||||
if strings.Contains(word[0], "r") {
|
if strings.Contains(verb[0], "r") {
|
||||||
v.Regular = true
|
v.Regular = true
|
||||||
}
|
}
|
||||||
|
|
||||||
verbs = append(verbs, v)
|
verbs = append(verbs, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
rawpronouns, ok := data["pronouns"]
|
if data.Pronouns == nil || len(data.Pronouns) == 0 {
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("Missing pronouns key in data")
|
return fmt.Errorf("Missing pronouns key in data")
|
||||||
}
|
}
|
||||||
|
|
||||||
pronouns := []models.Pronoun{}
|
pronouns := []models.Pronoun{}
|
||||||
for _, word := range rawpronouns {
|
for _, pro := range data.Pronouns {
|
||||||
p := models.Pronoun{Word: word[1]}
|
p := models.Pronoun{Word: pro[1]}
|
||||||
if strings.Contains(word[0], "p") {
|
if strings.Contains(pro[0], "p") {
|
||||||
p.Plural = true
|
p.Plural = true
|
||||||
}
|
}
|
||||||
pronouns = append(pronouns, p)
|
pronouns = append(pronouns, p)
|
||||||
}
|
}
|
||||||
|
|
||||||
return g.db.InitData(adjectives, nouns, verbs, pronouns, nil)
|
if data.Sentences == nil || len(data.Sentences) == 0 {
|
||||||
|
return fmt.Errorf("Missing sentences key in data")
|
||||||
|
}
|
||||||
|
|
||||||
|
return g.db.InitData(adjectives, nouns, verbs, pronouns, data.Sentences)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepend "a", "an" or nothing to a phrase
|
// Prepend "a", "an" or nothing to a phrase
|
||||||
func ana(phrase string) string {
|
func (g *english) ana(phrase string) string {
|
||||||
//fmt.Printf("[ana] phrase[0]: %s; %q\n", string(phrase[0]), phrase)
|
//fmt.Printf("[ana] phrase[0]: %s; %q\n", string(phrase[0]), phrase)
|
||||||
if strings.ContainsAny(string(phrase[0]), "aeiou") {
|
if strings.ContainsAny(string(phrase[0]), "aeiou") {
|
||||||
return "an " + phrase
|
return "an " + phrase
|
||||||
|
@ -436,6 +490,37 @@ func ana(phrase string) string {
|
||||||
return "a " + phrase
|
return "a " + phrase
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// toCap capitalizes the first word of each sentence in the input string.
|
||||||
func toCap(words string) string {
|
func toCap(words string) string {
|
||||||
return strings.ToUpper(string(words[0])) + words[1:]
|
ret := strings.ToUpper(string(words[0])) + words[1:]
|
||||||
|
|
||||||
|
next := strings.Index(words, ". ")
|
||||||
|
if next == -1 {
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
for next+3 < len(words) {
|
||||||
|
newnext := strings.Index(words[next+1:], ". ")
|
||||||
|
ret = ret[0:next+2] + strings.ToUpper(string(ret[next+2])) + ret[next+3:]
|
||||||
|
|
||||||
|
if newnext == -1 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
next = newnext + next+1
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func sliceContains(haystack []string, needle string) bool {
|
||||||
|
if len(haystack) == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, item := range haystack {
|
||||||
|
if item == needle {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,7 @@ type HackerQuotes interface {
|
||||||
Hack() (string, error)
|
Hack() (string, error)
|
||||||
|
|
||||||
// Format returns a quote in the given format.
|
// Format returns a quote in the given format.
|
||||||
Format(format string) (string, error)
|
HackThis(format string) (string, error)
|
||||||
|
|
||||||
// InitData populates the underlying database with data from the given json file.
|
// InitData populates the underlying database with data from the given json file.
|
||||||
InitData(filename string) error
|
InitData(filename string) error
|
||||||
|
|
|
@ -25,7 +25,8 @@ type Verb struct {
|
||||||
|
|
||||||
type ConjugationType int
|
type ConjugationType int
|
||||||
const (
|
const (
|
||||||
CT_I ConjugationType = iota
|
CT_Unknown ConjugationType = iota
|
||||||
|
CT_I
|
||||||
CT_You
|
CT_You
|
||||||
CT_It
|
CT_It
|
||||||
CT_We
|
CT_We
|
||||||
|
@ -51,7 +52,8 @@ func (ct ConjugationType) String() string {
|
||||||
|
|
||||||
type ConjugationTime int
|
type ConjugationTime int
|
||||||
const (
|
const (
|
||||||
CM_Present ConjugationTime = iota
|
CM_Unknown ConjugationTime = iota
|
||||||
|
CM_Present
|
||||||
CM_Past
|
CM_Past
|
||||||
CM_Future
|
CM_Future
|
||||||
)
|
)
|
||||||
|
|
|
@ -413,5 +413,11 @@
|
||||||
["p", "they"],
|
["p", "they"],
|
||||||
["s", "i"],
|
["s", "i"],
|
||||||
["p", "we"]
|
["p", "we"]
|
||||||
|
],
|
||||||
|
|
||||||
|
"sentences": [
|
||||||
|
"{pronoun} can't {verb:i,present} {noun_phrase}, it {verb:it,future} {noun_phrase}!",
|
||||||
|
"{verb:you,present} {noun_phrase:definite}, then you can {verb:you,present} {noun_phrase:definite}!",
|
||||||
|
"{noun_phrase:it,past} {verb}. With {noun_phrase:indifinite,noadj,compound}!"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue