|
# |
|
# randomly forming an english "word" |
|
# |
|
|
|
# loading dictionary |
|
# The dictionary lookup object |
|
dict = [] |
|
|
|
# Do a jQuery Ajax request for the text dictionary |
|
$.get "https://s3-us-west-2.amazonaws.com/s.cdpn.io/111863/dictionary.txt", (txt) -> |
|
# dictionary taken from http://zyzzyva.net/lexicons/WWF.txt |
|
# Get an array of all the words |
|
words = txt.split("\n") |
|
# And add them as properties to the dictionary lookup |
|
# This will allow for fast lookups later |
|
i = 0 |
|
while i < words.length |
|
dict.push words[i] |
|
i++ |
|
# dictionary loaded |
|
# console.log 'dictionary loaded' |
|
|
|
# go! |
|
writeWord(randomWord()) |
|
return |
|
|
|
|
|
|
|
# random letter based on english language frequency taken from https://gist.github.com/rorsach/4054073 |
|
randomAtoZ = ((lookup) -> |
|
-> |
|
random = Math.random() * 100000 |
|
char = undefined |
|
prev = 0 |
|
for char of lookup |
|
charfreq = lookup[char] |
|
chance = (charfreq - prev) / 1000 + '%' |
|
return {char, charfreq, chance} if random < charfreq |
|
prev = charfreq |
|
return |
|
)({ |
|
a: 8167, b: 9659, c: 12441, d: 16694, |
|
e: 29396, f: 31624, g: 33639, h: 39733, |
|
i: 46699, j: 46852, k: 47624, l: 51649, |
|
m: 54055, n: 60804, o: 68311, p: 70240, |
|
q: 70335, r: 76322, s: 82649, t: 91705, |
|
u: 94463, v: 95441, w: 97801, x: 97951, |
|
y: 99925, z: 100000 |
|
}) |
|
|
|
|
|
# empty word frequency array |
|
wordfreq = [] |
|
randomWordLength = () -> |
|
# generating word frequency array with given percentages |
|
total = 0 |
|
# word length frequency in the english language from 1-19 characters |
|
# taken from http://www.ravi.io/language-word-lengths |
|
percentages = [0.1, 0.6, 2.6, 5.2, 8.5, 12.2, 14.0, 14.0, 12.6, 10.1, 7.5, 5.2, 3.2, 2.0, 1.0, 0.6, 0.3, 0.2, 0.1] |
|
# building array of percentages |
|
for percent in percentages |
|
amount = total + ((percent / 100) * 100000) |
|
wordfreq.push(amount) |
|
total = amount |
|
# getting random number |
|
random = Math.random() * 100000 |
|
length = undefined |
|
# lookup object |
|
lookup = { |
|
1: wordfreq[0], 2: wordfreq[1], 3: wordfreq[2], 4: wordfreq[3], |
|
5: wordfreq[4], 6: wordfreq[5], 7: wordfreq[6], 8: wordfreq[7], |
|
9: wordfreq[8], 10: wordfreq[9], 11: wordfreq[10], 12: wordfreq[11], |
|
13: wordfreq[12], 14: wordfreq[13], 15: wordfreq[14], 16: wordfreq[15], |
|
17: wordfreq[16], 18: wordfreq[17], 19: wordfreq[18] |
|
} |
|
prev = 0 |
|
for length of lookup |
|
lengthfreq = lookup[length] |
|
chance = Math.round(lengthfreq - prev) / 1000 + '%' |
|
return {length, chance} if random < lengthfreq |
|
prev = lengthfreq |
|
return |
|
|
|
# building the word |
|
randomWord = () -> |
|
# get word length |
|
length = randomWordLength() |
|
length_i = length.length |
|
chance = length.chance |
|
# initiate word |
|
word = '' |
|
# character frequency map |
|
char_map = [] |
|
# build the word |
|
for i in [1..length_i] |
|
# random character |
|
character = randomAtoZ() |
|
word += character.char |
|
char_map.push character |
|
# check if it is a word |
|
if $.inArray(word, dict) > -1 |
|
alert '"' + word + '" is a word in the dictionary. Congrats.' |
|
isword = true |
|
else |
|
isword = false |
|
return {length_i,chance,word,isword,char_map} |
|
|
|
# history |
|
word_history = [] |
|
|
|
# write word data to dom |
|
writeWord = (generated_word) -> |
|
document.getElementsByTagName('body')[0].className = '' |
|
|
|
if generated_word.isword == true |
|
isword = 'isword' |
|
else |
|
isword = 'isnotword' |
|
|
|
word = document.getElementById 'word' |
|
word.innerHTML = generated_word.word |
|
document.getElementsByTagName('body')[0].className = isword |
|
|
|
length = document.getElementById 'length' |
|
length.innerHTML = generated_word.length_i + ' (' + generated_word.chance + ')' |
|
|
|
charlist = document.getElementById 'charlist' |
|
charlist_html = '' |
|
for char in generated_word.char_map |
|
charlist_html += '<li><span class="char">' + char.char + '</span> <span class="charchance">' + char.chance + '</span></li>' |
|
charlist.innerHTML = charlist_html |
|
|
|
word_history.unshift {word: generated_word.word, isword: generated_word.isword} |
|
history_string = '' |
|
for w in word_history |
|
if w.isword == true |
|
isword = 'isword' |
|
else |
|
isword = 'isnotword' |
|
history_string += '<li class="' + isword + '">' + w.word + '</li>' |
|
document.getElementById('history').innerHTML = history_string |
|
|
|
|
|
# button click |
|
document.getElementById('new_word').onclick = () -> |
|
writeWord(randomWord()) |