As I went to sleep last night I was wondering how I could create some random word-like words, for something I’ve been working on. When I woke up, I had the solution! So, I thought I’d share how I did it.
The thing that was bothering me was where to find a list of dictionary words to look at to generate a nonsensical, but ‘word-like’ part of a word. I started thinking about dictionary.com and if they had some kind of API, amaong others.
Then it occurred to me that most Linux distros have a dictionary built in to the system. It lives at /usr/share/dict/words
So, with a little snippet of PHP, I was able to look at a part of this file and grab some characters.
here it is:
function makeNonsense($length){
// used to create word fragments $length long
$dictionaryFile = "/usr/share/dict/words";
// eventually need to make this go higher than the rand max 32768
$content=file_get_contents($dictionaryFile,FALSE,NULL,rand(0,32768),$length);
// get rid of numbers. spaces and other characters
$content = ereg_replace("[^a-zA-Z]“, “”, $content);
$content = strtolower(str_replace(” “,”",$content));
$nonsense = str_replace(”\n”,”",$content);
return $nonsense;
}
And that’s it! 
recent comments