$text = file_get_contents($filePath);
    if (false === $text) {
        throw new \RuntimeException(sprintf('An error occured while trying to read the contents of \'%s\'', $filePath));
    }
    if (empty($text)) {
        throw new \DomainException(sprintf('The text file \'%s\' contains no text!', $filePath));
    }
    $winningWord = null;
    $charCount = 0;
    foreach (str_word_count(strtolower($text), 1) as $word) {
        $counts = count_chars($word, 1);
        if (!empty($counts)) {
            rsort($counts);
            $count = current($counts);
            if ($charCount == 0 || $count > $charCount) {
                $winningWord = $word;
                $charCount = $count;
            }
        }
    }
    return $winningWord;
}
chdir('.');
echo findWord('romeo.txt');
echo "<br/>";
echo findWord('somepeople.txt');
echo "<br/>";
echo findWord('test1.txt');
echo "<br/>";
echo findWord('test2.txt');
예제 #2
0
 function findPath($StringX, $StringY)
 {
     $Start = findWord($StringY);
     /*Check if the word given by the user even exist in the dictionary*/
     if ($Start == NULL) {
         print $Start;
         print " does not exist in the dictionary!";
     }
     $End = findWord($StringY);
     /*Check to see if second word given even exists in the dictionary*/
     if ($End == NULL) {
         print $End;
         print " does not exist in the dictionary!";
     }
     /* Run breadth first search on the start vertex*/
     breadthFirstSearch($Start);
     /*We will traverse backwards on the list from breadfirstSearch to find the path*/
     while ($End->pred != NULL) {
         /*If the predecessor for end is NULL means there is no neighbor for this word*/
         if ($End->pret == NULL) {
             print "No Path Found!";
         }
         /*Otherwise print out as it traverses*/
         print $end->word . " -> ";
         $End = $End->pred;
     }
     print $End->word;
 }