Last active
June 28, 2016 21:47
-
-
Save Meshiest/ad7b0a47eaa7161c5488f35c1c8a2b62 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"strings" | |
"net/http" | |
"io/ioutil" | |
"math/rand" | |
"time" | |
) | |
var dict map[int][]string | |
func RandInt(min int, max int) int { | |
if max == min { | |
return min | |
} | |
return rand.Int() % (max-min) + min | |
} | |
func SyllableLine(syllables int) string { | |
words := []string{} | |
for syllables > 0 { | |
max := syllables | |
if max > 7 { | |
max = 7 | |
} | |
syl := RandInt(1, max) | |
word := dict[syl][RandInt(0, len(dict[syl]))] | |
words = append(words, word) | |
syllables -= syl | |
} | |
return strings.Join(words, " ") | |
} | |
func Haiku() string { | |
return SyllableLine(5)+"\n"+SyllableLine(7)+"\n"+SyllableLine(5) | |
} | |
func main() { | |
resp, err := http.Get("http://meshiest.com/syldict.txt") | |
if err != nil { | |
resp.Body.Close() | |
fmt.Println("Could not fetch dictionary. Exiting.") | |
return | |
} | |
rand.Seed(time.Now().Unix()) | |
body, err := ioutil.ReadAll(resp.Body) | |
resp.Body.Close() | |
words := strings.Split(string(body), "\n") | |
dict = make(map[int][]string) | |
// parse word list into an integer(syllable) map of arrays of strings | |
for i, length := 0, len(words); i < length; i++ { | |
arr := strings.Split(words[i], "*") | |
count := len(arr) | |
word := strings.Join(arr, "") | |
if dict[count] == nil { | |
dict[count] = []string{} | |
} | |
dict[count] = append(dict[count], word) | |
} | |
fmt.Println(Haiku()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
good starting point