0

found some cool code to generate words from given letters in English language it works flawlessly, but to use some special characters it has some glitches it thinks that "ū" is equal to "ī" you can try adding "drūtsjē" and it somehow finds "tīrs","rīts", "x"... why is it and how it is possible to fix it ? dont forget to check Unikāli burti: here is some code index.php

<?php
    //Require the word finder class
    require_once("com/app/word_finder/word_finder.php");
?>
<html>
    <body>
        <div id="message_box">
            <hr />
            <form method="get">
                <ul id="submitform">
                    <li><label for="word"><strong>Burti:</strong></label><input type="text" name="word" id="word" value="<?php echo isset($_GET['word']) ? $_GET['word'] : ""; ?>" /></li>
                    <li><label for="match_letter_count"><strong>Unikāli burti:</strong></label><input type="checkbox" name="match_letter_count" id="match_letter_count" value="1" <?php echo isset($_GET['match_letter_count']) && $_GET['match_letter_count'] == "1"  ? "checked='checked'" : "0"; ?>/></li>
                    <li><label for="strict"><strong>Noteikti burti:</strong></label><input type="checkbox" name="strict" id="strict" value="1" <?php echo isset($_GET['strict']) && $_GET['strict'] == "1"  ? "checked='checked'" : "0"; ?> /></li>
                    <!-- <li><label for="lrt_count"><strong>min 3 burti:</strong></label><input type="checkbox" name="lrt_count" id="lrt_count" value="1" < ?php echo isset($_GET['lrt_count']) && $_GET['lrt_count'] == "1"  ? "checked='checked'" : "0"; ?> /></li> -->
                    <li><input type="submit" name="submit" id="submit" value="Spēlēt" /></li>
                </ul>
            </form>
            
            <?php
            
            
            
                //Example Usage
                if(isset($_GET['word']) && $_GET['word'] != NULL) {
                    
                    //The required usage to get the return array
                    $Word_Finders = new Com\App\Word_Finder($_GET['word'], isset($_GET['strict']) ? TRUE : FALSE, isset($_GET['match_letter_count']) ? TRUE : FALSE);
                    $arrWord_Finders = $Word_Finders->getWords();
                    //The required usage to get the return array
                    
                    //Echo out the results
                    echo '<h1>Atrasti</h1>';
                    echo "<ul id='wordlist'>";
                        foreach($arrWord_Finders as $anagram) {
                            echo "<li>" . $anagram . " (" . (grapheme_strlen($anagram)-1). ")</li>";
                        }
                    echo "</ul>";
                }
            ?>
            
        </div>
        
    </body>
</html>

and word_finder.php

<?php

namespace Com\App {

    /*
    * This class can be used to search for words within other words, or find 
    * out what words can be made using reusable letters for example. It can
    * also be used to solve countdown conundrums and other related issues.
    *
    * It uses an experimental method by reading in the entire dictionary, then
    * eliminating words one by one until it's left with the desired array.
    *
    * @version 1.0
    * @copyright 2012 Mark Gannaway <mark@ganners.co.uk>
    * @link http://ganners.co.uk
    * @license LGPL
    */
    class Word_Finder {
        
        private $word,
            $dictionary = array(),
            $alphabet = array();
        
        /**
        * Public construct - used to define all required variables
        * @param (string) $word - The inputted word/letters to be searched
        * @param (bool) $strict - If the search is strict - which means it must use all letters at least once
        * @param (bool) $match_letter_count - This will mean it should match the maximum letter count, so the words can't reuse letters
        * @return void
        */
        public function __construct($word, $strict = FALSE, $match_letter_count = FALSE, $lrt_count = FALSE) {
            
            //Sets the word to the lowercase array of letters
            $this->word = (array) str_split(strtolower($word));
            
            //Reads in the dictionary
            $this->dictionary = $this->getDictionary(dirname(__FILE__) . "/words.latvian");
            
            //Grabs the alphabet array
            $this->alphabet = $this->getFilteredAlphabetArray();
            
            //Performs the initial array magic and will loosely filter out words which don't contain any of the letters
            $this->filterLoose();
            
            //Will match the maximum letter counts if true
            if($match_letter_count) {
                $this->matchLetterCounts();
            }
            
            //Will strict search if true
            if($strict) {
                $this->filterStrict();
            }
            
            
            // vaardi saakot ar burtu daudzumu
            if($lrt_count){
                
            }
            
        }
        
        /**
        * Will retrieve the words and perform a sort by string length
        * @param void
        * @return void
        */
        public function getWords() {
        
            $sortArray = function($a, $b) {
                return grapheme_strlen($b)-grapheme_strlen($a);
            };
        
            usort($this->dictionary, $sortArray);
            return $this->dictionary;
        
        }
        
        /**
        * Will read in the dictionary file and return the array
        * @param (string) $file_name - The location of the dictionary file
        * @return (array) The dictionary array
        */
        private function getDictionary($file_name) {
            $dictionary_file = file_get_contents($file_name);
            return (array) explode("\n", $dictionary_file);
        }
        
        /**
        * This will create an alphabet based on the letters that aren't in the inputted word.
        * @param void
        * @return (array) $alphabet - The alphabet containing words not in the object's word
        */
        private function getFilteredAlphabetArray() {
        
            $alphabet = str_split("aābcčdeēfgģhiījkķlļmnņoprsštuūvzž");
            
            foreach($this->word as $wordLetter) {
                foreach($alphabet as $key => $alphabetLetter) {
                    if($wordLetter == $alphabetLetter) {
                        unset($alphabet[$key]);
                    }
                }
            }
            
            return (array) $alphabet;
        }
        
        /**
        * This will filter out words which don't contain any letters from the formatted alphabet
        * @param void
        * @return void
        */
        private function filterLoose() {
            foreach($this->alphabet as $letter) {
                foreach($this->dictionary as $key => $word) {
                    $wordArr = str_split(strtolower($word));
                    if(in_array($letter, $wordArr)) {
                        unset($this->dictionary[$key]);
                    }
                }
            }
            
        }
        
        /**
        * This will filter out words which don't contain all letters from the formatted alphabet
        * @param void
        * @return void
        */
        private function filterStrict() {
            foreach($this->word as $wordLetter) {
                foreach($this->dictionary as $key => $word) {
                    $wordArr = str_split($word);
                    if(!in_array($wordLetter, $wordArr)) {
                        unset($this->dictionary[$key]);
                    }
                }
            }
        }
        
        /**
        * This will filter out words which don't contain more individual letters than that in the original word
        * @param void
        * @return void
        */
        private function matchLetterCounts() {
        
            foreach($this->dictionary as $key => $dictionary_word) {
                $dictionary_word_count_values = array_count_values(str_split(strtolower($dictionary_word)));
                $word_count_values = array_count_values($this->word);
                
                foreach($word_count_values as $letter_count_value => $letter_count_amount) {
                    if(isset($dictionary_word_count_values[$letter_count_value]) && ($letter_count_amount < $dictionary_word_count_values[$letter_count_value])) {
                        unset($this->dictionary[$key]);
                    }
                }
                
            }
        
        }
        
    }
}
?>

words.latvian

dūrējs
rūsēt
trūds
tīrs
rīts
rūts
trīs
tūrs
ērts
ūdrs
jērs
dēts
sūt
īrs
ēst
tīs
sēt
jūs
dēt
dēs
rīs
jūt
Jūs
rīt
ST
ēd
ts
sū
x

thanks for helping

macropod
  • 12,757
  • 2
  • 9
  • 21

0 Answers0