Skip to content

Instantly share code, notes, and snippets.

@paralax
Last active September 20, 2018 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paralax/1d0c786398b590e1ea8a7290fd15edf2 to your computer and use it in GitHub Desktop.
Save paralax/1d0c786398b590e1ea8a7290fd15edf2 to your computer and use it in GitHub Desktop.
Spelling bee puzzle (e.g. https://www.nytimes.com/puzzles/spelling-bee) generator
open System
open System.IO
(*
how it works
- computes the Zipfs law letter distribution
- finds all words with 7 distinct letters
- from those it can then generate puzzles by placing the least likely letter in the center
*)
let words = File.ReadAllLines("/usr/share/dict/words")
let zipfs (words : string []) : Map<char,float> =
let allch =
words
|> Array.map (fun x -> x.Length)
|> Array.sum
words
|> Array.map (fun x -> x.ToLower().ToCharArray())
|> Array.concat
|> Seq.groupBy (fun x -> x)
|> Seq.map (fun (x,y) -> (x, float(Seq.length y)/float(allch)))
|> Map.ofSeq
let letters (word : string) : Set<char> =
word.ToCharArray()
|> Set.ofArray
let lettercount (word : string) : int =
word
|> letters
|> Set.count
let centerletter (word : string) (Z : Map<char,float>) : char =
word
|> letters
|> Seq.map (fun x -> (x, Z.[Char.ToLower(x)]))
|> Seq.minBy snd
|> fst
let Z = zipfs words
words
|> Array.filter (fun x -> lettercount x = 7)
|> Array.map (fun x -> (x, centerletter x Z))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment