2021-02-13 08:52:59 -08:00
|
|
|
package hacker
|
2021-02-13 08:39:05 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"math/rand"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/zorchenhimer/hacker-quotes/database"
|
|
|
|
"github.com/zorchenhimer/hacker-quotes/models"
|
|
|
|
)
|
|
|
|
|
2021-02-17 15:07:23 -08:00
|
|
|
type InitialData struct {
|
|
|
|
Adjectives [][]string
|
|
|
|
Nouns [][]string
|
|
|
|
Verbs [][]string
|
|
|
|
Pronouns [][]string
|
|
|
|
Sentences []string
|
|
|
|
}
|
|
|
|
|
2021-02-17 06:08:41 -08:00
|
|
|
type english struct {
|
2021-02-13 08:39:05 -08:00
|
|
|
db database.DB
|
2021-02-20 18:18:26 -08:00
|
|
|
|
|
|
|
currentPronoun string
|
2021-02-13 08:39:05 -08:00
|
|
|
}
|
|
|
|
|
2021-02-17 06:08:41 -08:00
|
|
|
func NewEnglish(db database.DB) (HackerQuotes, error) {
|
|
|
|
return &english{db: db}, nil
|
2021-02-13 08:39:05 -08:00
|
|
|
}
|
|
|
|
|
2021-02-14 18:19:55 -08:00
|
|
|
/*
|
|
|
|
Sentence format
|
|
|
|
|
2021-02-21 17:13:33 -08:00
|
|
|
bare words
|
2021-02-14 18:19:55 -08:00
|
|
|
{word_type:options}
|
|
|
|
{{word_type:new word:properties}}
|
|
|
|
|
2021-02-21 17:13:33 -08:00
|
|
|
[rng:random,choice,list] // one word is chosen from the comma-separated list
|
|
|
|
|
|
|
|
// Randomly hide/show the enclosed definition
|
|
|
|
[hide:{word_type:options}]
|
|
|
|
[hide:{{word_type:new word:options}}]
|
|
|
|
|
2021-02-14 18:19:55 -08:00
|
|
|
{pronoun} can't {verb:i,present} {noun_phrase}, it {verb:it,future} {noun_phrase}!
|
2021-02-20 18:18:26 -08:00
|
|
|
{verb:you,present} {noun_phrase:indefinite}, then you can {verb:you,present} {noun_phrase:definite}!
|
2021-02-14 18:19:55 -08:00
|
|
|
{noun_phrase} {verb}. With {noun_phrase:indifinite,noadj,compound}!
|
2021-02-20 18:18:26 -08:00
|
|
|
|
|
|
|
{pronoun} {{verb:need:from_pronoun,present}} to {verb:i,present} {noun_phrase:definite}!
|
2021-02-21 17:13:33 -08:00
|
|
|
|
|
|
|
{pronoun} [rng:can,may] {verb:i,present} {noun_phrase} [hide:{noun_phrase:singlular}], it {verb:it,future} {noun_phrase:definite}.
|
2021-02-14 18:19:55 -08:00
|
|
|
*/
|
|
|
|
|
2021-02-17 06:08:41 -08:00
|
|
|
func (g *english) Hack() (string, error) {
|
2021-02-17 15:07:23 -08:00
|
|
|
//var fmtString string = `{verb:you,present} {noun_phrase:definite} then you can {verb:you,present} {noun_phrase:definite}!`
|
2021-02-20 18:18:26 -08:00
|
|
|
//var str string = `{pronoun} {{verb:need:from_pronoun,present}} to {verb:i,present} {noun_phrase:definite}!`
|
2021-02-21 17:13:33 -08:00
|
|
|
//var str string = `{pronoun} [rng:can,may] {verb:i,present} {noun_phrase}[hide: {noun_phrase:singlular}], it {verb:it,future} {noun_phrase:definite}.`
|
2021-02-17 15:07:23 -08:00
|
|
|
str, err := g.randomSentence()
|
2021-02-14 17:35:25 -08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2021-02-17 15:07:23 -08:00
|
|
|
return g.HackThis(str)
|
|
|
|
}
|
2021-02-14 17:35:25 -08:00
|
|
|
|
2021-02-17 15:07:23 -08:00
|
|
|
func (g *english) HackThis(fmtString string) (string, error) {
|
|
|
|
var idx int
|
|
|
|
var err error
|
|
|
|
var nidx int
|
|
|
|
output := &strings.Builder{}
|
2021-02-20 18:18:26 -08:00
|
|
|
g.currentPronoun = ""
|
2021-02-17 15:07:23 -08:00
|
|
|
|
|
|
|
for idx < len(fmtString) {
|
|
|
|
if fmtString[idx] == '{' {
|
2021-02-21 17:13:33 -08:00
|
|
|
nidx, err = g.consumeWord(fmtString, idx, output)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2021-02-17 15:07:23 -08:00
|
|
|
}
|
2021-02-21 17:13:33 -08:00
|
|
|
idx = nidx
|
|
|
|
continue
|
2021-02-17 15:07:23 -08:00
|
|
|
|
2021-02-21 17:13:33 -08:00
|
|
|
} else if fmtString[idx] == '[' {
|
|
|
|
nidx, err = g.consumeRng(fmtString, idx, output)
|
2021-02-17 15:07:23 -08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
idx = nidx
|
|
|
|
continue
|
|
|
|
}
|
2021-02-14 17:35:25 -08:00
|
|
|
|
2021-02-17 15:07:23 -08:00
|
|
|
nidx, err = g.consumeRaw(fmtString, idx, output)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
idx = nidx
|
2021-02-14 17:35:25 -08:00
|
|
|
}
|
|
|
|
|
2021-02-17 15:07:23 -08:00
|
|
|
return toCap(output.String()), nil
|
|
|
|
}
|
2021-02-14 18:18:30 -08:00
|
|
|
|
2021-02-21 17:13:33 -08:00
|
|
|
func (g *english) consumeRng(fmtString string, idx int, output *strings.Builder) (int, error) {
|
|
|
|
idx++
|
|
|
|
def := strings.Index(fmtString[idx:], ":")
|
|
|
|
if def == -1 {
|
|
|
|
return 0, fmt.Errorf("Missing command separator in RNG block")
|
|
|
|
}
|
|
|
|
def += idx
|
|
|
|
|
|
|
|
end := strings.Index(fmtString[idx:], "]")
|
|
|
|
if end == -1 {
|
|
|
|
return 0, fmt.Errorf("Unclosed RNG block starting at offset %d", idx-1)
|
|
|
|
}
|
|
|
|
end += idx
|
|
|
|
|
|
|
|
if def > end {
|
|
|
|
return 0, fmt.Errorf("Missing command separator in RNG block")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch fmtString[idx:def] {
|
|
|
|
case "rng":
|
|
|
|
choices := strings.Split(fmtString[def+1:end], ",")
|
|
|
|
ridx := rand.Intn(len(choices))
|
|
|
|
output.WriteString(choices[ridx])
|
|
|
|
|
|
|
|
case "hide":
|
|
|
|
if rand.Int() % 2 == 0 {
|
|
|
|
return end+1, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var newEnd int = def+1
|
|
|
|
var err error
|
|
|
|
for newEnd < end {
|
|
|
|
if fmtString[newEnd] == '{' {
|
|
|
|
newEnd, err = g.consumeWord(fmtString, newEnd, output)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
newEnd, err = g.consumeRaw(fmtString, newEnd, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return 0, fmt.Errorf("RNG type %q is not implemented", fmtString[idx:def])
|
|
|
|
}
|
|
|
|
|
|
|
|
return end+1, nil
|
|
|
|
}
|
|
|
|
|
2021-02-17 15:07:23 -08:00
|
|
|
func (g *english) consumeRaw(fmtString string, idx int, output *strings.Builder) (int, error) {
|
2021-02-21 17:13:33 -08:00
|
|
|
end := strings.IndexAny(fmtString[idx:], "{[")
|
2021-02-17 15:07:23 -08:00
|
|
|
if end == -1 {
|
|
|
|
output.WriteString(fmtString[idx:len(fmtString)])
|
|
|
|
return len(fmtString), nil
|
2021-02-14 17:35:25 -08:00
|
|
|
}
|
|
|
|
|
2021-02-17 15:07:23 -08:00
|
|
|
output.WriteString(fmtString[idx:end+idx])
|
|
|
|
return idx+end, nil
|
|
|
|
}
|
2021-02-14 17:35:25 -08:00
|
|
|
|
2021-02-20 18:18:26 -08:00
|
|
|
func (g *english) parseVerbOptions(optlist []string) (models.ConjugationType, models.ConjugationTime, bool) {
|
|
|
|
var ct models.ConjugationType = models.ConjugationType(rand.Intn(5) + 1)
|
|
|
|
if sliceContains(optlist, "i") {
|
|
|
|
ct = models.CT_I
|
|
|
|
} else if sliceContains(optlist, "you") {
|
|
|
|
ct = models.CT_You
|
|
|
|
} else if sliceContains(optlist, "it") {
|
|
|
|
ct = models.CT_It
|
|
|
|
} else if sliceContains(optlist, "we") {
|
|
|
|
ct = models.CT_We
|
|
|
|
} else if sliceContains(optlist, "they") {
|
|
|
|
ct = models.CT_They
|
|
|
|
}
|
|
|
|
|
|
|
|
var cm models.ConjugationTime = models.ConjugationTime(rand.Intn(3) + 1)
|
|
|
|
if sliceContains(optlist, "present") {
|
|
|
|
cm = models.CM_Present
|
|
|
|
} else if sliceContains(optlist, "past") {
|
|
|
|
cm = models.CM_Past
|
|
|
|
} else if sliceContains(optlist, "future") {
|
|
|
|
cm = models.CM_Future
|
|
|
|
}
|
|
|
|
|
|
|
|
var invert bool = rand.Int() % 2 == 0
|
|
|
|
if sliceContains(optlist, "invert") {
|
|
|
|
invert = true
|
|
|
|
}
|
|
|
|
|
|
|
|
//var regular bool = true
|
|
|
|
//if sliceContains(optlist, "irregular") {
|
|
|
|
// regular = false
|
|
|
|
//}
|
|
|
|
|
|
|
|
if sliceContains(optlist, "from_pronoun") {
|
|
|
|
switch g.currentPronoun {
|
|
|
|
case "he", "she", "it":
|
|
|
|
ct = models.CT_It
|
|
|
|
case "i":
|
|
|
|
ct = models.CT_I
|
|
|
|
case "you":
|
|
|
|
ct = models.CT_You
|
|
|
|
case "we":
|
|
|
|
ct = models.CT_We
|
|
|
|
case "they":
|
|
|
|
ct = models.CT_They
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ct, cm, invert
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *english) parseNounOptions(optlist []string) (bool, bool) {
|
|
|
|
var plural bool = rand.Int() % 2 == 0
|
|
|
|
if sliceContains(optlist, "plural") {
|
|
|
|
plural = true
|
|
|
|
} else if sliceContains(optlist, "singular") {
|
|
|
|
plural = false
|
|
|
|
}
|
|
|
|
|
|
|
|
var compound bool = rand.Int() % 2 == 0
|
|
|
|
if sliceContains(optlist, "compound") {
|
2021-02-21 17:00:59 -08:00
|
|
|
compound = true
|
2021-02-20 18:18:26 -08:00
|
|
|
} else if sliceContains(optlist, "simple") {
|
2021-02-21 17:00:59 -08:00
|
|
|
compound = false
|
2021-02-20 18:18:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return plural, compound
|
|
|
|
}
|
|
|
|
|
2021-02-17 15:07:23 -08:00
|
|
|
func (g *english) consumeNewWord(fmtString string, idx int, output *strings.Builder) (int, error) {
|
2021-02-20 18:18:26 -08:00
|
|
|
idx += 2
|
|
|
|
var wordtype string
|
|
|
|
var word string
|
|
|
|
var options string
|
|
|
|
|
|
|
|
end := strings.Index(fmtString[idx:], "}")
|
|
|
|
if end == -1 {
|
|
|
|
return 0, fmt.Errorf("[consumeNewWord] Unclosed definition starting at %d", idx)
|
|
|
|
}
|
|
|
|
end += idx
|
|
|
|
|
|
|
|
// Find the start of the new word
|
|
|
|
def := strings.Index(fmtString[idx:], ":")
|
|
|
|
if def == -1 {
|
|
|
|
return 0, fmt.Errorf("[consumeNewWord] Word definition missing word")
|
|
|
|
}
|
|
|
|
def += idx
|
|
|
|
wordtype = fmtString[idx:def]
|
|
|
|
|
|
|
|
// Find the start of the options
|
|
|
|
opts := strings.Index(fmtString[def+1:], ":")
|
|
|
|
|
|
|
|
if opts > -1 {
|
|
|
|
opts += def+2
|
|
|
|
options = fmtString[opts:end]
|
|
|
|
word = fmtString[def+1:opts-1]
|
|
|
|
} else {
|
|
|
|
word = fmtString[def+1:end]
|
|
|
|
}
|
|
|
|
|
|
|
|
//fmt.Printf("idx:%d def:%d opts:%d end:%d type:%q word:%q opts:%q\n",
|
|
|
|
// idx, def, opts, end,
|
|
|
|
// wordtype,
|
|
|
|
// word,
|
|
|
|
// options)
|
|
|
|
|
|
|
|
optlist := strings.Split(options, ",")
|
|
|
|
var formatted string
|
|
|
|
switch wordtype {
|
|
|
|
case "verb":
|
|
|
|
ct, cm, invert := g.parseVerbOptions(optlist)
|
|
|
|
|
|
|
|
v := models.Verb{
|
|
|
|
Regular: true,
|
|
|
|
Word: word,
|
|
|
|
}
|
|
|
|
|
|
|
|
formatted = v.Conjugate(ct, cm, invert)
|
|
|
|
|
|
|
|
case "noun":
|
|
|
|
plural, _ := g.parseNounOptions(optlist)
|
|
|
|
|
|
|
|
if plural {
|
|
|
|
n := models.Noun{
|
|
|
|
Regular: true,
|
|
|
|
Word: word,
|
|
|
|
}
|
|
|
|
formatted = n.Plural()
|
|
|
|
} else {
|
|
|
|
formatted = word
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
// Pronouns and adjectives aren't implemented because they have no modifications to apply.
|
|
|
|
return 0, fmt.Errorf("Word type %q not implemented creating in-sentence", wordtype)
|
|
|
|
}
|
|
|
|
|
|
|
|
output.WriteString(formatted)
|
|
|
|
return end+2, nil
|
2021-02-14 17:35:25 -08:00
|
|
|
}
|
|
|
|
|
2021-02-17 15:07:23 -08:00
|
|
|
func (g *english) consumeWord(fmtString string, idx int, output *strings.Builder) (int, error) {
|
2021-02-21 17:13:33 -08:00
|
|
|
if fmtString[idx+1] == '{' {
|
|
|
|
return g.consumeNewWord(fmtString, idx, output)
|
|
|
|
}
|
|
|
|
|
2021-02-17 15:07:23 -08:00
|
|
|
idx++
|
|
|
|
var wordtype string
|
|
|
|
var options string
|
2021-02-14 17:35:25 -08:00
|
|
|
|
2021-02-17 15:07:23 -08:00
|
|
|
end := strings.Index(fmtString[idx:], "}")
|
|
|
|
if end == -1 {
|
|
|
|
return 0, fmt.Errorf("[consumeWord] Unclosed definition starting at %d", idx)
|
2021-02-14 17:35:25 -08:00
|
|
|
}
|
|
|
|
|
2021-02-17 15:07:23 -08:00
|
|
|
end += idx
|
|
|
|
optsStart := strings.Index(fmtString[idx:end], ":")
|
2021-02-14 17:35:25 -08:00
|
|
|
|
2021-02-17 15:07:23 -08:00
|
|
|
if optsStart != -1 {
|
|
|
|
options = fmtString[optsStart+idx+1:end]
|
|
|
|
wordtype = fmtString[idx:optsStart+idx]
|
|
|
|
} else {
|
|
|
|
wordtype = fmtString[idx:end]
|
|
|
|
}
|
2021-02-14 17:35:25 -08:00
|
|
|
|
2021-02-17 15:07:23 -08:00
|
|
|
if wordtype == "" {
|
|
|
|
return 0, fmt.Errorf("[consumeWord] Missing word type at idx: %d", idx)
|
2021-02-14 17:35:25 -08:00
|
|
|
}
|
|
|
|
|
2021-02-17 15:07:23 -08:00
|
|
|
opts := strings.Split(options, ",")
|
|
|
|
var word string
|
|
|
|
var err error
|
2021-02-14 17:35:25 -08:00
|
|
|
|
2021-02-17 15:07:23 -08:00
|
|
|
switch wordtype {
|
|
|
|
case "pronoun":
|
2021-02-21 17:07:42 -08:00
|
|
|
var plural bool = rand.Int() % 2 == 0
|
2021-02-17 15:07:23 -08:00
|
|
|
if sliceContains(opts, "plural") {
|
|
|
|
plural = true
|
2021-02-21 17:13:33 -08:00
|
|
|
} else if sliceContains(opts, "singular") {
|
2021-02-21 17:07:42 -08:00
|
|
|
plural = false
|
2021-02-17 15:07:23 -08:00
|
|
|
}
|
2021-02-14 17:35:25 -08:00
|
|
|
|
2021-02-17 15:07:23 -08:00
|
|
|
word, err = g.randomPronoun(plural)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2021-02-20 18:18:26 -08:00
|
|
|
g.currentPronoun = word
|
2021-02-14 17:35:25 -08:00
|
|
|
|
2021-02-17 15:07:23 -08:00
|
|
|
case "verb":
|
2021-02-20 18:18:26 -08:00
|
|
|
ct, cm, invert := g.parseVerbOptions(opts)
|
2021-02-14 17:35:25 -08:00
|
|
|
|
2021-02-17 15:07:23 -08:00
|
|
|
word, err = g.randomVerb(ct, cm, invert)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2021-02-13 08:39:05 -08:00
|
|
|
|
2021-02-17 15:07:23 -08:00
|
|
|
case "noun":
|
2021-02-20 18:18:26 -08:00
|
|
|
plural, compound := g.parseNounOptions(opts)
|
2021-02-13 17:22:55 -08:00
|
|
|
|
2021-02-17 15:07:23 -08:00
|
|
|
word, err = g.randomNoun(plural, compound)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2021-02-13 17:22:55 -08:00
|
|
|
|
2021-02-17 15:07:23 -08:00
|
|
|
case "noun_phrase":
|
2021-02-21 17:07:42 -08:00
|
|
|
var definite bool = rand.Int() % 2 == 0
|
|
|
|
var hasAdj bool = rand.Int() % 2 == 0
|
|
|
|
var plural bool = rand.Int() % 2 == 0
|
|
|
|
var compound bool = rand.Int() % 2 == 0
|
2021-02-13 17:22:55 -08:00
|
|
|
|
2021-02-17 15:07:23 -08:00
|
|
|
if sliceContains(opts, "indefinite") {
|
|
|
|
definite = false
|
2021-02-21 17:07:42 -08:00
|
|
|
} else if sliceContains(opts, "definite") {
|
|
|
|
definite = true
|
2021-02-17 15:07:23 -08:00
|
|
|
}
|
2021-02-14 15:33:13 -08:00
|
|
|
|
2021-02-17 15:07:23 -08:00
|
|
|
if sliceContains(opts, "noadj") {
|
|
|
|
hasAdj = false
|
2021-02-21 17:07:42 -08:00
|
|
|
} else if sliceContains(opts, "adj") {
|
|
|
|
hasAdj = true
|
2021-02-17 15:07:23 -08:00
|
|
|
}
|
2021-02-14 15:33:13 -08:00
|
|
|
|
2021-02-17 15:07:23 -08:00
|
|
|
if sliceContains(opts, "plural") {
|
|
|
|
plural = true
|
2021-02-21 17:07:42 -08:00
|
|
|
} else if sliceContains(opts, "singular") {
|
|
|
|
plural = false
|
2021-02-17 15:07:23 -08:00
|
|
|
}
|
2021-02-14 15:33:13 -08:00
|
|
|
|
2021-02-17 15:07:23 -08:00
|
|
|
if sliceContains(opts, "compound") {
|
|
|
|
compound = true
|
2021-02-21 17:07:42 -08:00
|
|
|
} else if sliceContains(opts, "simple") {
|
|
|
|
compound = false
|
2021-02-17 15:07:23 -08:00
|
|
|
}
|
2021-02-14 15:33:13 -08:00
|
|
|
|
2021-02-17 15:07:23 -08:00
|
|
|
word, err = g.nounPhrase(definite, hasAdj, plural, compound)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2021-02-14 15:33:13 -08:00
|
|
|
|
2021-02-17 15:07:23 -08:00
|
|
|
case "adjective":
|
|
|
|
word, err = g.randomAdjective()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2021-02-13 17:22:55 -08:00
|
|
|
|
2021-02-17 15:07:23 -08:00
|
|
|
default:
|
|
|
|
return 0, fmt.Errorf("[consumeWord] Invalid word type %s at %d", wordtype, idx)
|
|
|
|
}
|
2021-02-13 08:39:05 -08:00
|
|
|
|
2021-02-17 15:07:23 -08:00
|
|
|
output.WriteString(word)
|
|
|
|
return end+1, nil
|
2021-02-13 08:39:05 -08:00
|
|
|
}
|
|
|
|
|
2021-02-17 06:08:41 -08:00
|
|
|
func (g *english) nounPhrase(definite, hasAdj, plural, compound bool) (string, error){
|
2021-02-13 08:39:05 -08:00
|
|
|
adj := ""
|
|
|
|
var err error
|
|
|
|
if hasAdj {
|
|
|
|
adj, err = g.randomAdjective()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 15:33:13 -08:00
|
|
|
noun, err := g.randomNoun(plural, compound)
|
2021-02-13 08:39:05 -08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
phrase := adj
|
|
|
|
if phrase != "" {
|
|
|
|
phrase += " " + noun
|
|
|
|
} else {
|
|
|
|
phrase = noun
|
|
|
|
}
|
|
|
|
|
|
|
|
if definite && !plural {
|
|
|
|
//fmt.Println("[nounPhrase] definite && !plural")
|
|
|
|
return "the " + phrase, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !plural {
|
|
|
|
//fmt.Println("[nounPhrase] !plural")
|
2021-02-17 15:07:23 -08:00
|
|
|
return g.ana(phrase), nil
|
2021-02-13 08:39:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return phrase, nil
|
|
|
|
}
|
|
|
|
|
2021-02-17 06:08:41 -08:00
|
|
|
func (g *english) randomAdjective() (string, error) {
|
2021-02-13 08:39:05 -08:00
|
|
|
ids, err := g.db.GetAdjectiveIds()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("[adj] get IDs error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ids) <= 0 {
|
|
|
|
return "", fmt.Errorf("No adjective IDs returned from database")
|
|
|
|
}
|
|
|
|
|
|
|
|
rid := int(rand.Int63n(int64(len(ids))))
|
|
|
|
//fmt.Printf("[adj] len(ids): %d; rid: %d; %d\n", len(ids), rid, ids[rid])
|
|
|
|
|
|
|
|
adj, err := g.db.GetAdjective(ids[rid])
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("[adj] ID: %d; %v", ids[rid], err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return adj.Word, nil
|
|
|
|
}
|
|
|
|
|
2021-02-17 06:08:41 -08:00
|
|
|
func (g *english) randomNoun(plural, compound bool) (string, error) {
|
2021-02-14 15:33:13 -08:00
|
|
|
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)
|
|
|
|
}
|
2021-02-13 08:39:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(ids) <= 0 {
|
|
|
|
return "", fmt.Errorf("No noun IDs returned from database")
|
|
|
|
}
|
|
|
|
|
|
|
|
rid := int(rand.Int63n(int64(len(ids))))
|
|
|
|
//fmt.Printf("[noun] len(ids): %d; rid: %d; ID: %d\n", len(ids), rid, ids[rid])
|
|
|
|
|
|
|
|
noun, err := g.db.GetNoun(ids[rid])
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("[noun] ID: %d; %v", ids[rid], err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if plural {
|
|
|
|
return noun.Plural(), nil
|
|
|
|
}
|
|
|
|
return noun.Word, nil
|
|
|
|
}
|
|
|
|
|
2021-02-17 06:08:41 -08:00
|
|
|
func (g *english) randomVerb(ctype models.ConjugationType, ctime models.ConjugationTime, invert bool) (string, error) {
|
2021-02-13 08:39:05 -08:00
|
|
|
ids, err := g.db.GetVerbIds()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("[verb] get IDs error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ids) <= 0 {
|
|
|
|
return "", fmt.Errorf("No verb IDs returned from database")
|
|
|
|
}
|
|
|
|
|
|
|
|
rid := int(rand.Int63n(int64(len(ids))))
|
|
|
|
verb, err := g.db.GetVerb(ids[rid])
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("[verb] ID: %d; %v", ids[rid], err)
|
|
|
|
}
|
|
|
|
|
2021-02-13 17:22:55 -08:00
|
|
|
return verb.Conjugate(ctype, ctime, invert), nil
|
2021-02-13 08:39:05 -08:00
|
|
|
}
|
|
|
|
|
2021-02-17 06:08:41 -08:00
|
|
|
func (g *english) randomPronoun(plural bool) (string, error) {
|
2021-02-14 17:35:25 -08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-02-21 17:04:05 -08:00
|
|
|
if pronoun.Word == "i" {
|
|
|
|
return strings.ToUpper(pronoun.Word), nil
|
|
|
|
}
|
2021-02-14 17:35:25 -08:00
|
|
|
return pronoun.Word, nil
|
|
|
|
}
|
|
|
|
|
2021-02-17 15:07:23 -08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-02-17 06:08:41 -08:00
|
|
|
func (g *english) InitData(filename string) error {
|
2021-02-13 08:39:05 -08:00
|
|
|
fmt.Printf("Initializing database with data in %q\n", filename)
|
|
|
|
if g.db == nil {
|
|
|
|
return fmt.Errorf("databse is nil!")
|
|
|
|
}
|
|
|
|
|
|
|
|
raw, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-17 15:07:23 -08:00
|
|
|
//data := map[string][]interface{}{}
|
|
|
|
data := InitialData{}
|
2021-02-13 08:39:05 -08:00
|
|
|
if err = json.Unmarshal(raw, &data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-17 15:07:23 -08:00
|
|
|
if data.Adjectives == nil || len(data.Adjectives) == 0 {
|
|
|
|
return fmt.Errorf("Missing Adjectives in input data")
|
2021-02-13 08:39:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
adjectives := []models.Adjective{}
|
2021-02-17 15:07:23 -08:00
|
|
|
for _, adj := range data.Adjectives {
|
2021-02-13 08:39:05 -08:00
|
|
|
t, word := adj[0], adj[1]
|
|
|
|
a := models.Adjective{Word: word}
|
|
|
|
if strings.Contains(t, "a") {
|
|
|
|
a.Absolute = true
|
|
|
|
}
|
|
|
|
if strings.Contains(t, "e") {
|
|
|
|
a.AppendEst = true
|
|
|
|
}
|
|
|
|
if strings.Contains(t, "m") {
|
|
|
|
a.AppendMore = true
|
|
|
|
}
|
|
|
|
|
|
|
|
adjectives = append(adjectives, a)
|
|
|
|
}
|
|
|
|
|
2021-02-17 15:07:23 -08:00
|
|
|
if data.Nouns == nil || len(data.Nouns) == 0 {
|
2021-02-13 08:39:05 -08:00
|
|
|
return fmt.Errorf("Missing nouns key in data")
|
|
|
|
}
|
|
|
|
|
|
|
|
nouns := []models.Noun{}
|
2021-02-17 15:07:23 -08:00
|
|
|
for _, noun := range data.Nouns {
|
2021-02-13 08:39:05 -08:00
|
|
|
t, word := noun[0], noun[1]
|
|
|
|
n := models.Noun{Word: word}
|
|
|
|
|
|
|
|
if strings.Contains(t, "m") {
|
|
|
|
n.Multiple = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(t, "b") {
|
|
|
|
n.Begin = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(t, "e") {
|
|
|
|
n.End = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(t, "a") {
|
|
|
|
n.Alone = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(t, "r") {
|
|
|
|
n.Regular = true
|
|
|
|
}
|
|
|
|
|
|
|
|
nouns = append(nouns, n)
|
|
|
|
}
|
|
|
|
|
2021-02-17 15:07:23 -08:00
|
|
|
if data.Verbs == nil || len(data.Verbs) == 0 {
|
2021-02-13 08:39:05 -08:00
|
|
|
return fmt.Errorf("Missing verbs key in data")
|
|
|
|
}
|
|
|
|
|
|
|
|
verbs := []models.Verb{}
|
2021-02-17 15:07:23 -08:00
|
|
|
for _, verb := range data.Verbs {
|
|
|
|
v := models.Verb{Word: verb[1]}
|
|
|
|
if strings.Contains(verb[0], "r") {
|
2021-02-13 08:39:05 -08:00
|
|
|
v.Regular = true
|
|
|
|
}
|
|
|
|
|
|
|
|
verbs = append(verbs, v)
|
|
|
|
}
|
|
|
|
|
2021-02-17 15:07:23 -08:00
|
|
|
if data.Pronouns == nil || len(data.Pronouns) == 0 {
|
2021-02-14 17:35:25 -08:00
|
|
|
return fmt.Errorf("Missing pronouns key in data")
|
|
|
|
}
|
|
|
|
|
|
|
|
pronouns := []models.Pronoun{}
|
2021-02-17 15:07:23 -08:00
|
|
|
for _, pro := range data.Pronouns {
|
|
|
|
p := models.Pronoun{Word: pro[1]}
|
|
|
|
if strings.Contains(pro[0], "p") {
|
2021-02-14 17:35:25 -08:00
|
|
|
p.Plural = true
|
|
|
|
}
|
|
|
|
pronouns = append(pronouns, p)
|
|
|
|
}
|
|
|
|
|
2021-02-17 15:07:23 -08:00
|
|
|
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)
|
2021-02-13 08:39:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Prepend "a", "an" or nothing to a phrase
|
2021-02-17 15:07:23 -08:00
|
|
|
func (g *english) ana(phrase string) string {
|
2021-02-13 08:39:05 -08:00
|
|
|
//fmt.Printf("[ana] phrase[0]: %s; %q\n", string(phrase[0]), phrase)
|
|
|
|
if strings.ContainsAny(string(phrase[0]), "aeiou") {
|
|
|
|
return "an " + phrase
|
|
|
|
}
|
|
|
|
|
|
|
|
return "a " + phrase
|
|
|
|
}
|
2021-02-14 15:31:56 -08:00
|
|
|
|
2021-02-17 15:07:23 -08:00
|
|
|
// toCap capitalizes the first word of each sentence in the input string.
|
2021-02-14 15:31:56 -08:00
|
|
|
func toCap(words string) string {
|
2021-02-17 15:07:23 -08:00
|
|
|
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
|
2021-02-14 15:31:56 -08:00
|
|
|
}
|