Esempio n. 1
0
 /**
  * Reads a dictionary and then translates the words in an input text
  * word-by-word, printing the result on the output stream.
  * The dictionary consists of pairs of words.
  * The first element of each pair is a word in source language.
  * The second element of each pair is the word in the target language.
  * Uses a search tree.
  *
  * @param resource $dictionary An input stream
  * from which the pairs of words are read.
  * @param resource $in The input stream to be translated.
  * @param resource $out The output stream on which to print the translation.
  */
 public static function translate($dictionary, $in, $out)
 {
     $searchTree = new AVLTree();
     while (($line = fgets($dictionary)) != false) {
         $line = preg_replace('/\\n$/', '', $line);
         list($key, $value) = preg_split('/[ \\t]+/', $line, 2);
         $searchTree->insert(new Association(box($key), box($value)));
     }
     while (($line = fgets($in)) != false) {
         $words = preg_split('/[ \\t\\n]+/', $line, -1, PREG_SPLIT_NO_EMPTY);
         foreach ($words as $word) {
             $obj = $searchTree->find(new Association(box($word)));
             if ($obj === NULL) {
                 fprintf($out, "%s ", $word);
             } else {
                 fprintf($out, "%s ", str($obj->getValue()));
             }
         }
     }
 }