Skip to content

Instantly share code, notes, and snippets.

@arran4
Created April 26, 2020 04:00
Show Gist options
  • Save arran4/6f5ec6da8c09466b6c077e1eed2c74c5 to your computer and use it in GitHub Desktop.
Save arran4/6f5ec6da8c09466b6c077e1eed2c74c5 to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"encoding/csv"
"log"
"math/rand"
"os"
"time"
)
type C struct {
NextR rune
Stop bool
}
var (
m = []map[string][]C{}
knownTitles = map[string]struct{}{}
)
func addWeight(pos int, preceding string, cur rune, stop bool) {
for pos >= len(m) {
m = append(m, map[string][]C{})
}
v, ok := m[pos][preceding]
if !ok {
v = []C{}
}
m[pos][preceding] = append(v, C{NextR: cur, Stop: stop})
}
func getOneWeight(pos int, preceding string) (rune, bool) {
if pos >= len(m) {
return '.', false
}
v, ok := m[pos][preceding]
if !ok {
return '.', false
}
if len(v) == 0 {
return '.', false
}
c := v[rand.Int31n(int32(len(v)))]
return c.NextR, c.Stop
}
func addToChain(title string) {
t := bytes.Runes([]byte(title))
for i := 0; i < len(t); i++ {
s := i - 3
if s < 0 {
s = 0
}
p := string(t[s:i])
addWeight(i, p, t[i], i == len(t) - 1)
}
}
func main() {
rand.Seed(time.Now().UnixNano())
log.SetFlags(log.Lshortfile|log.Flags())
f, err := os.Open("list.csv")
if err != nil {
log.Panic(err)
}
defer f.Close()
cr := csv.NewReader(f)
all, err := cr.ReadAll()
if err != nil {
log.Panic(err)
}
for _, row := range all {
knownTitles[row[0]] = struct{}{}
addToChain(row[0])
}
for i := 0; i < 26; i++ {
log.Print(makeNewTitle())
}
}
func makeNewTitle() string {
for i := 0; i < 100; i++ {
title := makeTitle()
if _, ok := knownTitles[title]; !ok {
return title
}
}
log.Panic("Couldn't find new title!")
return ""
}
func makeTitle() string {
result := []rune{}
stop := false
for p:=0;!stop;p++ {
s := p - 3
if s < 0 {
s = 0
}
k := string(result[s:p])
r, b := getOneWeight(p, k)
result = append(result, r)
stop = b
}
return string(result)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment