1 ! Copyright (C) 2010 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
4 USING: ascii assocs fry io.encodings.ascii io.files kernel math
5 memoize sequences sequences.extras sorting sets ;
8 : make-anagram-hash ( strings -- assoc )
9 [ natural-sort ] collect-by
11 [ nip length 1 > ] assoc-filter ;
13 MEMO: dict-words ( -- seq )
14 "/usr/share/dict/words" ascii file-lines [ >lower ] map ;
16 MEMO: dict-anagrams ( -- assoc )
17 dict-words make-anagram-hash ;
19 : anagrams ( str -- seq/f )
20 >lower natural-sort dict-anagrams at ;
22 : most-anagrams ( -- seq )
23 dict-anagrams values all-longest ;
25 : longest-anagrams ( -- seq )
26 dict-anagrams [ keys all-longest ] keep '[ _ at ] map ;