babbler: create tables no matter what

This commit is contained in:
Chris Sexton 2017-10-25 18:45:45 -04:00
parent df29c89d84
commit 6af9114a37
1 changed files with 33 additions and 35 deletions

View File

@ -56,39 +56,37 @@ type BabblerArc struct {
func New(bot bot.Bot) *BabblerPlugin { func New(bot bot.Bot) *BabblerPlugin {
log.SetFlags(log.LstdFlags | log.Lshortfile) log.SetFlags(log.LstdFlags | log.Lshortfile)
if bot.DBVersion() == 1 { if _, err := bot.DB().Exec(`create table if not exists babblers (
if _, err := bot.DB().Exec(`create table if not exists babblers (
id integer primary key, id integer primary key,
babbler string babbler string
);`); err != nil { );`); err != nil {
log.Fatal(err) log.Fatal(err)
} }
if _, err := bot.DB().Exec(`create table if not exists babblerWords ( if _, err := bot.DB().Exec(`create table if not exists babblerWords (
id integer primary key, id integer primary key,
word string word string
);`); err != nil { );`); err != nil {
log.Fatal(err) log.Fatal(err)
} }
if _, err := bot.DB().Exec(`create table if not exists babblerNodes ( if _, err := bot.DB().Exec(`create table if not exists babblerNodes (
id integer primary key, id integer primary key,
babblerId integer, babblerId integer,
wordId integer, wordId integer,
root integer, root integer,
rootFrequency integer rootFrequency integer
);`); err != nil { );`); err != nil {
log.Fatal(err) log.Fatal(err)
} }
if _, err := bot.DB().Exec(`create table if not exists babblerArcs ( if _, err := bot.DB().Exec(`create table if not exists babblerArcs (
id integer primary key, id integer primary key,
fromNodeId integer, fromNodeId integer,
toNodeId interger, toNodeId interger,
frequency integer frequency integer
);`); err != nil { );`); err != nil {
log.Fatal(err) log.Fatal(err)
}
} }
plugin := &BabblerPlugin{ plugin := &BabblerPlugin{
@ -120,18 +118,18 @@ func (p *BabblerPlugin) Message(message msg.Message) bool {
} else if numTokens > 2 && tokens[1] == "says-tail" { } else if numTokens > 2 && tokens[1] == "says-tail" {
saidWhat, saidSomething = p.getBabbleWithSuffix(tokens) saidWhat, saidSomething = p.getBabbleWithSuffix(tokens)
} else if numTokens >= 2 && tokens[1] == "says-middle-out" { } else if numTokens >= 2 && tokens[1] == "says-middle-out" {
saidWhatStart, saidSomethingStart := p.getBabbleWithSuffix(tokens) saidWhatStart, saidSomethingStart := p.getBabbleWithSuffix(tokens)
neverSaidLooksLike := fmt.Sprintf("%s never said '%s'", tokens[0], strings.Join(tokens[2:], " ")) neverSaidLooksLike := fmt.Sprintf("%s never said '%s'", tokens[0], strings.Join(tokens[2:], " "))
if !saidSomethingStart || saidWhatStart == neverSaidLooksLike { if !saidSomethingStart || saidWhatStart == neverSaidLooksLike {
saidSomething = saidSomethingStart saidSomething = saidSomethingStart
saidWhat = saidWhatStart saidWhat = saidWhatStart
} else { } else {
saidWhatEnd, saidSomethingEnd := p.getBabble(tokens) saidWhatEnd, saidSomethingEnd := p.getBabble(tokens)
saidSomething = saidSomethingStart && saidSomethingEnd saidSomething = saidSomethingStart && saidSomethingEnd
if saidSomething { if saidSomething {
saidWhat = saidWhatStart + " " + strings.Join(strings.Fields(saidWhatEnd)[len(tokens)-2:], " ") saidWhat = saidWhatStart + " " + strings.Join(strings.Fields(saidWhatEnd)[len(tokens)-2:], " ")
}
} }
}
} else if len(tokens) == 4 && strings.Index(lowercase, "initialize babbler for ") == 0 { } else if len(tokens) == 4 && strings.Index(lowercase, "initialize babbler for ") == 0 {
saidWhat, saidSomething = p.initializeBabbler(tokens) saidWhat, saidSomething = p.initializeBabbler(tokens)
} else if strings.Index(lowercase, "batch learn for ") == 0 { } else if strings.Index(lowercase, "batch learn for ") == 0 {
@ -792,8 +790,8 @@ func (p *BabblerPlugin) babbleSeedSuffix(babblerName string, seed []string) (str
} }
} }
for i := 0; i < len(words) / 2; i++ { for i := 0; i < len(words)/2; i++ {
index := len(words)-(i+1) index := len(words) - (i + 1)
words[i], words[index] = words[index], words[i] words[i], words[index] = words[index], words[i]
} }
@ -834,10 +832,10 @@ func (p *BabblerPlugin) getBabblerNodeById(nodeId int64) (*BabblerNode, error) {
} }
func shuffle(a []*BabblerArc) { func shuffle(a []*BabblerArc) {
for i := range a { for i := range a {
j := rand.Intn(i + 1) j := rand.Intn(i + 1)
a[i], a[j] = a[j], a[i] a[i], a[j] = a[j], a[i]
} }
} }
func (p *BabblerPlugin) babbleSeedBookends(babblerName string, start, end []string) (string, error) { func (p *BabblerPlugin) babbleSeedBookends(babblerName string, start, end []string) (string, error) {
@ -861,11 +859,11 @@ func (p *BabblerPlugin) babbleSeedBookends(babblerName string, start, end []stri
type searchNode struct { type searchNode struct {
babblerNodeId int64 babblerNodeId int64
previous *searchNode previous *searchNode
} }
open := []*searchNode{ &searchNode{startWordNode.NodeId, nil} } open := []*searchNode{&searchNode{startWordNode.NodeId, nil}}
closed := map[int64]*searchNode{ startWordNode.NodeId : open[0] } closed := map[int64]*searchNode{startWordNode.NodeId: open[0]}
goalNodeId := int64(-1) goalNodeId := int64(-1)
for i := 0; i < len(open) && i < 1000; i++ { for i := 0; i < len(open) && i < 1000; i++ {
@ -927,8 +925,8 @@ func (p *BabblerPlugin) babbleSeedBookends(babblerName string, start, end []stri
} }
} }
for i := 0; i < len(words) / 2; i++ { for i := 0; i < len(words)/2; i++ {
index := len(words)-(i+1) index := len(words) - (i + 1)
words[i], words[index] = words[index], words[i] words[i], words[index] = words[index], words[i]
} }