Skip to content

Instantly share code, notes, and snippets.

@Haspaker
Last active August 29, 2015 14:01
Show Gist options
  • Save Haspaker/2fc6f325a4954cb7b0d4 to your computer and use it in GitHub Desktop.
Save Haspaker/2fc6f325a4954cb7b0d4 to your computer and use it in GitHub Desktop.
fs = require \fs
{count-by, group-by, last, filter, reject, obj-to-pairs, pairs-to-obj, unique, sort, map, foldl1} = require \prelude-ls
const primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113]
const alphabet = [\a to \z] ++ <[ å ä ö é ]>
const alpha_indices = pairs-to-obj alphabet.map (c, i) -> [c, i]
const filepath = (langcode, wordlist, length) -> "./words/#langcode/#wordlist/#length.txt"
class AnagramFinder
get_prime_product: (word) ->
prime_product = 1
for char in word.toLowerCase()
if char in alphabet
prime_product *= primes[ alpha_indices[char] ]
return prime_product
find_anagrams_among_words: (target_word, words) ->
target_prime_product = @get_prime_product target_word
anagrams = []
for word in words
prime_product = @get_prime_product word
if prime_product is target_prime_product
anagrams ++= word
return anagrams |> filter (.toLowerCase() isnt target_word.toLowerCase())
get_anagrams: (word, wordlists, langcode, callback) ->
console.log langcode
wordlists .= split ','
wordlength = word |> (.split '') |> reject (not in alphabet) |> (.length)
if wordlength < 2 then return callback []
found_anagrams = []
read_wordlist_files = 0
for let wordlist in wordlists
error, words <~ fs.readFile (filepath langcode, wordlist, wordlength), encoding:\utf8
if error then console.log error
else
words .= split \\n
anagrams = @find_anagrams_among_words word, words
found_anagrams ++= anagrams |> map (anagram) -> language:langcode, wordlist:wordlist, word:anagram
read_wordlist_files += 1
if read_wordlist_files == wordlists.length then callback found_anagrams
module.exports = new AnagramFinder()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment