Text Processing with Nu
Nu’s not a bad little language for processing text. The key is to take advantage of the many features of the Foundation classes.
Here’s a small example that might help you the next time that you’re playing Scrabble.
The problem
Let’s say that you need to make a word but all you have to work with are vowels. Can you take your vowels and build on someone else’s consonant? The key is finding a word with only one consonant. How many of those can you name?
For some of us, brute force is finesse
Unfortunately (or fortunately) for me, I’m better at writing programs than I am at remembering things. But I did remember that there are lots of word lists online that we can download, so I pulled one down from the Ubuntu distribution, using% sudo apt-get install wamerican-largeThat put a list of over 57,000 words in /etc/dictionaries-common/words.
The Nu program
Here’s a Nu script that loads a copy of that file and picks out all the words with exactly four letters and three vowels:
(set VOWELS (NSSet setWithList:'( 'a' 'e' 'i' 'o' 'u' )))
(class NSString
(- vowels is
(set sum 0)
((self length) times:
(do (index)
(set c (self characterAtIndex:index))
(if (VOWELS containsObject:c)
(set sum (+ sum 1)))))
sum))
(set wordLength 4)
(set vowelCount (- wordLength 1))
(set allWords ((NSString stringWithContentsOfFile:"words")
componentsSeparatedByString:"\n"))
(set allWordsWithLength
(allWords select:
(do (w) (eq (w length) wordLength))))
(set allWordsWithLengthAndVowelCount
(allWordsWithLength select:
(do (w) (eq (w vowels) vowelCount))))
(puts (allWordsWithLengthAndVowelCount description))
Results
When I ran that script with nush, I got this result:
(
aide,
area,
aria,
aura,
auto,
ease,
eave,
idea,
iota,
lieu,
oboe,
ooze
)
Running again to look for similar five-letter words:
(
audio,
aurae,
eerie,
queue
)
That’s the best I could do with the dictionary that I had – there were no words with six or more letters that had exactly one consonant.
So happy Scrabbling, or if you happen to be playing online, this can just be our little secret :-) .
Comments (1) post a reply
Point of interest, OS X includes a list of words in /usr/share/dict/. The file I use is web2 but I haven’t really looked at anything else in the folder.