Skip to content
Permalink
Browse files

Adding mutex

  • Loading branch information...
Depado committed Feb 28, 2016
1 parent f0dde58 commit ddd8d83f6f3ca359bbea32c655729e06462fcb26
Showing with 4 additions and 0 deletions.
  1. +4 −0 plugins/markov/chains.go
@@ -4,6 +4,7 @@ import (
"encoding/json"
"math/rand"
"strings"
"sync"

"github.com/depado/go-b0tsec/database"
)
@@ -15,6 +16,7 @@ const PrefixLen = 2

// MainChain is the chain that will be available outside the package.
var MainChain *Chain
var protect sync.RWMutex

// Prefix is a Markov chain prefix of one or more words.
type Prefix []string
@@ -41,11 +43,13 @@ type Chain struct {
// Build builds the chain using the given string parameter
func (c *Chain) Build(s string) {
p := make(Prefix, PrefixLen)
protect.Lock()
for _, v := range strings.Split(s, " ") {
key := p.String()
c.Chain[key] = append(c.Chain[key], v)
p.Shift(v)
}
protect.Unlock()
}

// Generate returns a string of at most n words generated from Chain.

0 comments on commit ddd8d83

Please sign in to comment.
You can’t perform that action at this time.