Last active
September 20, 2018 18:16
-
-
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
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
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