fooDigi Posted February 20, 2008 Share Posted February 20, 2008 i have been racking me brain trying to find an effective way of doing this... given a word list (20,000+) and a list of characters (about 5-10), how can i find all the words in the word list that use those characters only once... so, example: character_list = mlfrau it would find: stay,flu,arm,far,ram,fur,farm,maul,mural,armful as long as they exist in the word list. thanks for any help! Quote Link to comment https://forums.phpfreaks.com/topic/92044-word-list-algorithm/ Share on other sites More sharing options...
rofl90 Posted February 20, 2008 Share Posted February 20, 2008 *cough* bruter *cough* :PP probably not anyway Quote Link to comment https://forums.phpfreaks.com/topic/92044-word-list-algorithm/#findComment-471361 Share on other sites More sharing options...
Isityou Posted February 20, 2008 Share Posted February 20, 2008 Have you looked into regular expressions? Quote Link to comment https://forums.phpfreaks.com/topic/92044-word-list-algorithm/#findComment-471364 Share on other sites More sharing options...
fooDigi Posted February 20, 2008 Author Share Posted February 20, 2008 yes, of course, but i do not know the depths of which you speak. any insight to my problem would be great. thx Quote Link to comment https://forums.phpfreaks.com/topic/92044-word-list-algorithm/#findComment-471366 Share on other sites More sharing options...
sasa Posted February 20, 2008 Share Posted February 20, 2008 try <?php $char = 'mlfrau'; $word_list = 'frr,print,mam,the'; $a = explode(',',$word_list); foreach ($a as $word){ preg_match_all("/[$char]/", $word, $b); if (count($b[0]) == count(array_unique($b[0])) and count($b[0]) > 0) echo $word,"\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/92044-word-list-algorithm/#findComment-471489 Share on other sites More sharing options...
fooDigi Posted February 23, 2008 Author Share Posted February 23, 2008 thx sasa, this will set me in the right direction. this is great, but this returns just "print". i change the "==" to "!=" and it returns "frr" and "mam", which is cool, cause only those letters exist in $char. but, i wish not to find words that use the letters twice. so "frr" would not be found, but "fr" would, so would "ma" but not "mam". guess, i have a regular expressions book next to me, maybe i should read that. thx Quote Link to comment https://forums.phpfreaks.com/topic/92044-word-list-algorithm/#findComment-474279 Share on other sites More sharing options...
sasa Posted February 23, 2008 Share Posted February 23, 2008 condition if (count($b[0]) == count(array_unique($b[0])) and count($b[0]) > 0) means 1st part just unique characters and 2nd part one or more character mach just word 'print' mach both if you want to mach word 'the' remove 2nd conditon (and count($b[0]) > 0) Quote Link to comment https://forums.phpfreaks.com/topic/92044-word-list-algorithm/#findComment-474283 Share on other sites More sharing options...
fooDigi Posted February 23, 2008 Author Share Posted February 23, 2008 what i mean is that the word 'frr' should not exist in 'mlfrau', cause the 'r' is used once already. i don't want to match the world 'the' cause none of those letters exist in 'mlfrau'. i want to match only words that have unique letters in 'mlfrau', if the following were real words in the master word list, i would like this result... mlfrau rau ulm far mal *edit crap, i noticed in my first post, the word stay should not have been found. $%^&! hope that wasn't too confusing. Quote Link to comment https://forums.phpfreaks.com/topic/92044-word-list-algorithm/#findComment-474286 Share on other sites More sharing options...
sasa Posted February 23, 2008 Share Posted February 23, 2008 if $char is 'mlfrau' and in your word list is word 'mlfrX', is it good result or not? Quote Link to comment https://forums.phpfreaks.com/topic/92044-word-list-algorithm/#findComment-474290 Share on other sites More sharing options...
fooDigi Posted February 23, 2008 Author Share Posted February 23, 2008 that would not match. Quote Link to comment https://forums.phpfreaks.com/topic/92044-word-list-algorithm/#findComment-474291 Share on other sites More sharing options...
fooDigi Posted February 23, 2008 Author Share Posted February 23, 2008 this works like i want it, from what i have tested. <?php $char = 'mlfrau'; $word_list = 'frr,print,mam,the,ra,fratzl,rai,far,maul,maal,mail,ram,ramm,farm,farrm'; $a = explode(',',$word_list); foreach ($a as $word){ preg_match_all("/[$word]/", $char, $b); $check_word = ''; foreach($b[0] as $x) $check_word .= $x; if(strlen($check_word) == strlen($word)) echo $word."\n"; } ?> this will output: ra far maul ram farm Quote Link to comment https://forums.phpfreaks.com/topic/92044-word-list-algorithm/#findComment-474300 Share on other sites More sharing options...
Daniel0 Posted February 23, 2008 Share Posted February 23, 2008 Can't you run through all the words, inside the loop run through all the chars and check if they only occur once using substr_count()? Quote Link to comment https://forums.phpfreaks.com/topic/92044-word-list-algorithm/#findComment-474303 Share on other sites More sharing options...
fooDigi Posted February 23, 2008 Author Share Posted February 23, 2008 good point, but there in lies another problem. what if the char list is 'mlfrrau', instead of 'mlfrau'. there are two r's, so the word 'frr' should pass for a match now. as long as the correct amount of that characters exist in the char list Quote Link to comment https://forums.phpfreaks.com/topic/92044-word-list-algorithm/#findComment-474310 Share on other sites More sharing options...
sasa Posted February 23, 2008 Share Posted February 23, 2008 try <?php $char = 'mlfraum'; $word_list = 'frr,print,mam,the,ra,fratzl,rai,far,maul,maal,mail,ram,ramm,farm,farrm'; $a = explode(',',$word_list); $char = str_split($char); sort($char); $char ='/^'.implode('?',$char).'?$/'; foreach ($a as $word){ $word1 = str_split($word); sort($word1); $word1 =implode('',$word1); if (preg_match($char, $word1)) echo $word; } ?> speed 10 000 words per sec Quote Link to comment https://forums.phpfreaks.com/topic/92044-word-list-algorithm/#findComment-474355 Share on other sites More sharing options...
fooDigi Posted February 23, 2008 Author Share Posted February 23, 2008 excellent! this looks like it works very well. sasa, thx alot! Quote Link to comment https://forums.phpfreaks.com/topic/92044-word-list-algorithm/#findComment-474366 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.