Skip to main content
Added dictionary download link to the end question
Source Link

You can download my dictionary from here and testing your code using dictionary words.

You can download my dictionary from here and testing your code using dictionary words.

added 283 characters in body; edited title
Source Link
Dan Oberlam
  • 7.9k
  • 2
  • 32
  • 74

How to write functions which can find Retrieve words with specified lettersfrom dictionary when they meet letter requirements

I have sample function which can searcha set of functions that retrieve words from some arbitrary dictionary with input specifiedbased on what letters they have. For example, this function gets words:

that use Function code:only the specified letters:

Return: api, pia This would return the words api and pia.

Return: sample This one would return the word sample.


  
**Second function code:**

I also have a function that doesn't require that they exclusively use the selected letters, but rather that they use all of the specified letters (and any other letters).

This function can search word with specified letters and other letters.

Example usage:

Return: sample, apple This returns the words sample and apple.

I have more 90000 words in my dictionary (utfUTF-8). Functions workThis results in a very slow. For example program; if I'm trying to find something from 90000 words found words on 50000 or 60000 loopthe full dictionary it may take tens of thousands of loops. Can be performed thisHow can I improve the performance of these functions?

How to write functions which can find words with specified letters

I have sample function which can search words from dictionary with input specified words:

Function code:

Return: api, pia

Return: sample


  **Second function code:**

This function can search word with specified letters and other letters.

Example usage:

Return: sample, apple

I have more 90000 words in my dictionary (utf-8). Functions work very slow. For example from 90000 words found words on 50000 or 60000 loop. Can be performed this functions?

Retrieve words from dictionary when they meet letter requirements

I have a set of functions that retrieve words from some arbitrary dictionary based on what letters they have. For example, this function gets words that use only the specified letters:

This would return the words api and pia.

This one would return the word sample.

 

I also have a function that doesn't require that they exclusively use the selected letters, but rather that they use all of the specified letters (and any other letters).

Example usage:

This returns the words sample and apple.

I have more 90000 words in my dictionary (UTF-8). This results in a very slow program; if I'm trying to find something from the full dictionary it may take tens of thousands of loops. How can I improve the performance of these functions?

Corrected function names from specifed to specified.
Source Link
function getWordsWithOnlySpecifedLettersgetWordsWithOnlySpecifiedLetters(array $dictionary, string $letters)
{
    foreach ($dictionary as $key => $value) {
        if(mb_strlen($value) > mb_strlen($letters)) unset($dictionary[$key]);
    }

    $step = 0;
    $strSplit = preg_split('//u', $letters, null, PREG_SPLIT_NO_EMPTY);
    $result = [];
    foreach ($dictionary as $word) {
        $step++;
        $wordSplit = preg_split('//u', $word, null, PREG_SPLIT_NO_EMPTY);
        sort($wordSplit);
        sort($strSplit);
        if (array_map('mb_strtolower', $wordSplit) === array_map('mb_strtolower', $strSplit)) {
            //echo "All specified letters from $letters are in $word | Step $step<hr>" . PHP_EOL;
            $result[] = $word;
        }
    }

    return $result;
}
$dictionary = ['apple', 'sample', 'api', 'pia', 'тоҷик'];
getWordsWithOnlySpecifedLettersgetWordsWithOnlySpecifiedLetters($dictionary, "aip");
getWordsWithOnlySpecifedLettersgetWordsWithOnlySpecifiedLetters($dictionary, "leamps");
function getWordsWithSpecifedLettersgetWordsWithSpecifiedLetters(array $dictionary, string $letters)
{
    $step = 0;
    mb_internal_encoding("UTF-8");
    $result = [];
    foreach ($dictionary as $word) {
        $step++;
        $wordSplit = preg_split('//u', $word, null, PREG_SPLIT_NO_EMPTY);
        $strSplit = preg_split('//u', $letters, null, PREG_SPLIT_NO_EMPTY);
        $wordSplit = array_filter($wordSplit, function($x) use (&$strSplit) {
            if (in_array(strtolower($x), array_map('strtolower', $strSplit), true)) {
                $pos = array_search(strtolower($x), array_map('strtolower', $strSplit), true);
                unset($strSplit[$pos]);
                return false;
            }
            return true;
        });
        if (count(array_diff($strSplit,$wordSplit)) === 0) {
            //echo "$word contains all letters of $letters | Step $step<hr>" . PHP_EOL;
            $result[] = $word;
        }
    }

    return $result;
}
$dictionary = ['apple', 'sample', 'api', 'pia', 'тоҷик'];
getWordsWithSpecifedLettersgetWordsWithSpecifiedLetters($dictionary, "ple");
function getWordsWithOnlySpecifedLetters(array $dictionary, string $letters)
{
    foreach ($dictionary as $key => $value) {
        if(mb_strlen($value) > mb_strlen($letters)) unset($dictionary[$key]);
    }

    $step = 0;
    $strSplit = preg_split('//u', $letters, null, PREG_SPLIT_NO_EMPTY);
    $result = [];
    foreach ($dictionary as $word) {
        $step++;
        $wordSplit = preg_split('//u', $word, null, PREG_SPLIT_NO_EMPTY);
        sort($wordSplit);
        sort($strSplit);
        if (array_map('mb_strtolower', $wordSplit) === array_map('mb_strtolower', $strSplit)) {
            //echo "All specified letters from $letters are in $word | Step $step<hr>" . PHP_EOL;
            $result[] = $word;
        }
    }

    return $result;
}
$dictionary = ['apple', 'sample', 'api', 'pia', 'тоҷик'];
getWordsWithOnlySpecifedLetters($dictionary, "aip");
getWordsWithOnlySpecifedLetters($dictionary, "leamps");
function getWordsWithSpecifedLetters(array $dictionary, string $letters)
{
    $step = 0;
    mb_internal_encoding("UTF-8");
    $result = [];
    foreach ($dictionary as $word) {
        $step++;
        $wordSplit = preg_split('//u', $word, null, PREG_SPLIT_NO_EMPTY);
        $strSplit = preg_split('//u', $letters, null, PREG_SPLIT_NO_EMPTY);
        $wordSplit = array_filter($wordSplit, function($x) use (&$strSplit) {
            if (in_array(strtolower($x), array_map('strtolower', $strSplit), true)) {
                $pos = array_search(strtolower($x), array_map('strtolower', $strSplit), true);
                unset($strSplit[$pos]);
                return false;
            }
            return true;
        });
        if (count(array_diff($strSplit,$wordSplit)) === 0) {
            //echo "$word contains all letters of $letters | Step $step<hr>" . PHP_EOL;
            $result[] = $word;
        }
    }

    return $result;
}
$dictionary = ['apple', 'sample', 'api', 'pia', 'тоҷик'];
getWordsWithSpecifedLetters($dictionary, "ple");
function getWordsWithOnlySpecifiedLetters(array $dictionary, string $letters)
{
    foreach ($dictionary as $key => $value) {
        if(mb_strlen($value) > mb_strlen($letters)) unset($dictionary[$key]);
    }

    $step = 0;
    $strSplit = preg_split('//u', $letters, null, PREG_SPLIT_NO_EMPTY);
    $result = [];
    foreach ($dictionary as $word) {
        $step++;
        $wordSplit = preg_split('//u', $word, null, PREG_SPLIT_NO_EMPTY);
        sort($wordSplit);
        sort($strSplit);
        if (array_map('mb_strtolower', $wordSplit) === array_map('mb_strtolower', $strSplit)) {
            //echo "All specified letters from $letters are in $word | Step $step<hr>" . PHP_EOL;
            $result[] = $word;
        }
    }

    return $result;
}
$dictionary = ['apple', 'sample', 'api', 'pia', 'тоҷик'];
getWordsWithOnlySpecifiedLetters($dictionary, "aip");
getWordsWithOnlySpecifiedLetters($dictionary, "leamps");
function getWordsWithSpecifiedLetters(array $dictionary, string $letters)
{
    $step = 0;
    mb_internal_encoding("UTF-8");
    $result = [];
    foreach ($dictionary as $word) {
        $step++;
        $wordSplit = preg_split('//u', $word, null, PREG_SPLIT_NO_EMPTY);
        $strSplit = preg_split('//u', $letters, null, PREG_SPLIT_NO_EMPTY);
        $wordSplit = array_filter($wordSplit, function($x) use (&$strSplit) {
            if (in_array(strtolower($x), array_map('strtolower', $strSplit), true)) {
                $pos = array_search(strtolower($x), array_map('strtolower', $strSplit), true);
                unset($strSplit[$pos]);
                return false;
            }
            return true;
        });
        if (count(array_diff($strSplit,$wordSplit)) === 0) {
            //echo "$word contains all letters of $letters | Step $step<hr>" . PHP_EOL;
            $result[] = $word;
        }
    }

    return $result;
}
$dictionary = ['apple', 'sample', 'api', 'pia', 'тоҷик'];
getWordsWithSpecifiedLetters($dictionary, "ple");
Rollback to Revision 2
Source Link
Loading
Rollback to Revision 1 - Edit approval overridden by post owner or moderator
Source Link
Loading
Source Link
Loading