Jump to content

word list algorithm


fooDigi

Recommended Posts

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!

Link to comment
Share on other sites

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";
}
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.