Skip to content

Instantly share code, notes, and snippets.

@mkb218
Last active August 29, 2015 14:08
Show Gist options
  • Save mkb218/72fbe2a96de24801d159 to your computer and use it in GitHub Desktop.
Save mkb218/72fbe2a96de24801d159 to your computer and use it in GitHub Desktop.
download cmudict.0.7a from http://svn.code.sf.net/p/cmusphinx/code/trunk/cmudict/ and provide path as first arg
package main
import (
"bufio"
"encoding/gob"
"fmt"
"math/rand"
"os"
"strings"
"time"
)
import "unicode"
import "strconv"
type phoneme struct {
sound string
emphasis int
}
type word struct {
w string
phonemes []phoneme
}
func parse_line(s string) (word, error) {
fields := strings.Split(s, " ")
var w word
w.w = fields[0]
for _, s := range strings.Split(fields[1], " ") {
var phon = phoneme{"", -1}
for i, c := range []rune(s) {
if unicode.IsLetter(c) {
phon.sound = string(append([]rune(phon.sound), c))
} else {
num := string([]rune(s)[i:])
e, err := strconv.ParseInt(num, 0, 32)
if err != nil {
return word{}, err
}
phon.emphasis = int(e)
break
}
}
w.phonemes = append(w.phonemes, phon)
}
return w, nil
}
func main() {
ones := make([]string, 0)
twos := make([]string, 0)
if _, err := os.Stat("gunkygoo.gob"); err != nil {
f, err := os.Open(os.Args[1])
if err != nil {
panic(err)
}
scanner := bufio.NewScanner(f)
for scanner.Scan() {
if scanner.Text()[0:3] == ";;;" {
continue
}
word, err := parse_line(scanner.Text())
if err != nil {
panic(err)
}
if len(word.phonemes) == 0 {
fmt.Println("failed parse?", scanner.Text())
}
if word.phonemes[0].sound != "G" {
// fmt.Println("rejecting", word.w, "for non G")
continue
}
sylcount := 0
for _, p := range word.phonemes {
if p.emphasis >= 0 {
sylcount++
}
}
if sylcount > 3 {
// fmt.Println("rejecting", word.w, "for too many syllables")
continue
}
if sylcount > 2 {
// fmt.Println("rejecting", word.w, "for too many syllables, but should check to see if third syllable starts with hard g")
continue
}
if sylcount == 2 {
twos = append(twos, word.w)
} else {
ones = append(ones, word.w)
}
}
if scanner.Err() != nil {
panic(scanner.Err())
}
gobfile, err := os.Create("gunkygoo.gob")
if err != nil {
panic(err)
}
enc := gob.NewEncoder(gobfile)
enc.Encode(twos)
enc.Encode(ones)
err = gobfile.Close()
if err != nil {
panic(err)
}
} else {
f, err := os.Open("gunkygoo.gob")
if err != nil {
fmt.Println("problem opening gunkygoo.gob file, maybe delete")
panic(err)
}
dec := gob.NewDecoder(f)
dec.Decode(&twos)
dec.Decode(&ones)
}
rand.Seed(time.Now().UnixNano())
two := twos[rand.Intn(len(twos))]
one := ones[rand.Intn(len(ones))]
fmt.Print(two, one, "\n")
}
GUINEASGROUNDS(1)
GAUVREAUGREENS
GIESBRECHTGROWLS
GOSLINEGIL
GARRICKGRAHN
GORDIEGRAPE
GOBAINGOLF(1)
GRANNYGULPED
GINSUGEIB
GEWIRTZGARBED
GATORGRIST
GRUISGLAWE
GIDNEYGROVE'S
GEARHART(1)GIRE
GOODWYNGERK
GOERLITZGRAS(1)
GRANDMA'S(1)GLOBS
GAULTIER(1)GANS
GAUGES(1)GREG
GUPPYGRUPP
GALLUSGOWNS
GLENDALEGAYE
GETTYGREASE
GOLANDGRAEFF
GARSTENGAL
GLOBOGUYNN
GRADCHEV(1)GIFF
GRANTORSGUAY
GARNISHGANG
GRACINGGERTSCH
GANGWERGALL
GABELGRIER'S(1)
GIFFERDGWINN
GRETHERGERD
GRATTANGREENE
GUMPERTGAPS
GIVERGAVE
GOODLANDGROAT
GILTNERGROFF
GLACIER'SGHOSTS
GIFFINGOOK
GOSBANKGROOVE
GERSTEIN(1)GUMP
GLADMANGLUECK
GREYLAGGLUNTZ
GORNICKGRADE
GOADINGGLOOR
GRIZZARDGREWE
GOLLYGAUL
GLAXOGLOB
GRINSTEADGAUT
GELLMANGWEN
GANGWERGRIEME
GREENISHGRUN
GORIGURR
GAULTIERGIFF
GORGES(1)GOTTS
GILLMANGRETZ
GARNSEYGEHL
GRUMMAN'SGRELL
GISBERT(1)GROPE
GRANDMA'S(1)GUILT
GLAVINGROBE
GARTLEYGRAUE
GABBROGIGS
GRINNINGGULCH
GASSETTGRIESE
GRIMMESTGAN
GRINERGEHL
GIRDLER(1)GILLE
GILLERGRAN
GLASPERGLOW
GARLANGAINS
GOLSONGOOF
GHADAGLEE
GRIFFYGOINES
GREENSLADEGUNN
GRAMBLINGGAYS
GRATISGUILL
GUMIGASPS
GELVINGARL
GLENDONGRANGE
GILLANDGLOOM
GLITTERGUEST
GUERRYGRIEVES
GLAXOGOUIN
GLADISGATZ
GILLSONGREENS
GARNELLGAP'S
GRINDLAY(1)GAUGE
GROUPEMENTGAUGED
GREENWAYGRUM
GUISESGERE
GUILMETTEGILB
GRAMZAGOENS
GILLICKNGAI
GARGERGRING
GRANDKIDS(1)GUTS
GELBARDGAS
GUANDONG'SGANT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment