Skip to content

Instantly share code, notes, and snippets.

@duggan
Created May 14, 2012 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duggan/2694292 to your computer and use it in GitHub Desktop.
Save duggan/2694292 to your computer and use it in GitHub Desktop.
Mash dictionary words into a selected word to create neologism candidates
<?php
/**
* Neologism
* Mash a selected word into dictionary words,
* sort into a list for perusal. Not terribly memory friendly.
*
* @arg string $word The word to be mashed.
* @arg string $end_part The end part of the word to start the mashing from.
*/
// Retrieve system dictionary (OSX) and split to array
$dict = file_get_contents('/usr/share/dict/words');
$dict = explode("\n", $dict);
// Parse command line arguments into variables
list($filename, $word, $end_part) = $argv;
// Compile a list of words which contain the end part.
$wordlist = array();
foreach ($dict as $entry) {
if (strpos($entry, $end_part)) {
$wordlist[]=$entry;
}
}
// The magic - mash the selected word into discovered dictionary words
$mashed = array();
foreach ($wordlist as $entry) {
$result = trim(preg_replace('/(.+)'.$end_part.'(.+)?/', $word.'$2', $entry));
if ($result != $word) {
$mashed[]= $result;
}
}
// De-dupe
$mashed = array_unique($mashed);
// Sort, shortest to longest
uasort($mashed, function($a, $b){
if (strlen($a) == strlen($b)) {
return 0;
}
return (strlen($a) < strlen($b)) ? -1 : 1;
});
foreach ($mashed as $entry) {
print $entry . PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment