<commit_before><?php

class SouhlaskyUprostredSlov extends Formula {
  public static $name = '<NAME>&rcaron;<NAME>';
  public static $subject = '&Ccaron;e&scaron;tina';
  public static $advanced = ''; #TODO: allow to choose individual dictionaries
  protected $element;
  protected $dict_source = array('include/dict/slovnik-bp.dict', 'include/dict/slovnik-dt.dict', 'include/dict/slovnik-sz.dict', 'include/dict/slovnik-vf.dict');
  protected $dict;
  protected $toreplace;

  function __construct($letter = null) {
    switch ($letter) {
      case 'b':
        $this->dict = 'include/dict/slovnik-bp.dict';
        $this->toreplace = array('b', 'p');
        break;
      case 'd':
        $this->dict = 'include/dict/slovnik-dt.dict';
        $this->toreplace = array('d', 't');
        break;
      case 's':
        $this->dict = 'include/dict/slovnik-sz.dict';
        $this->toreplace = array('s', 'z');
        break;
      case 'v':
        $this->dict = 'include/dict/slovnik-vf.dict';
        $this->toreplace = array('v', 'f');
        break;
      default:
        $this->dict = $this->dict_source[mt_rand(0, count($this->dict_source) - 1)];
        $this->toreplace = str_split(substr($this->dict, 16, 2));
    }
    if (in_array('d', $this->toreplace)) {
      $this->toreplace[] = 'ď';
    }
    if (in_array('t', $this->toreplace)) {
      $this->toreplace[] = 'ť';
    }
    if (in_array('s', $this->toreplace)) {
      $this->toreplace[] = 'š';
    }
    if (in_array('z', $this->toreplace)) {
      $this->toreplace[] = 'ž';
    }
    $this->element = new RandomWordElement($this->dict);
  }
  protected function getBlank() {
    return str_replace($this->toreplace, '_', $this->element);
  }
  protected function blankReplace($haystack, $repl) {
    return implode($repl, explode('_', $haystack, 2));
  }

  public function toHTML($result = FALSE, $cls = 'select') {
    $text = '<span class="formula">';
    if ($result) {
      $text .= $this->element->toHTML();
    } else {
      $form = $this->getBlank();
      $rescount = 1;
      $text .= '<label class="'. $cls. '">';
      while (strpos($form, '_') !== false) {
        $input = '<select name="result'. $rescount. '" class="'. $cls. '"><option value="*"> </option>';
        foreach($this->toreplace as $char) {
          $input .= '<option value="'. $char. '">'. htmlentities($char, ENT_HTML5, "UTF-8"). '</option>';
        }
        $input .= '</select>';
        $form = $this->blankReplace($form, $input);
        $rescount++;
        if ($rescount > 256) break;
      }
      $text .= $form. '</label>';
    }
    $text .= '</span>';
    return $text;
  }
  public function toStr($result = FALSE) {
    if ($result) {
      return $this->getBlank(). ' = '. $this->element->toStr();
    } else {
      return $this->getBlank();
    }
  }

  public function getResult() {
    $text = $this->getBlank();
    $result = array();
    while (($i = strpos($text, '_')) !== false) {
      $result[] = $this->element->toStr()[$i];
      $text = $this->blankReplace($text, '*');
    }
    return $result;
  }

  public function getResultHTMLForm() {
    return '';
  }

  public function validateResult($input) {
    if (is_array($input)) {
      $form = $this->getBlank();
      if (count($input) != substr_count($form, '_')) return false;
      while (strpos($form, '_') !== false) {
        $form = $this->blankReplace($form, array_shift($input));
      }
      foreach ($this->dict_source as $dict) {
        if ($handle = fopen($dict, 'r')) {
          while($line = stream_get_line($handle, 256, "\n")) {
            if ($line == $form) return true;
          }
        }
        fclose($handle);
      }
    }
    return false;
  }
} // class SouhlaskyUprostredSlov

class VyjmenovanaSlova extends SouhlaskyUprostredSlov {
  public static $name = 'Vyjmenovan&aacute; slova';
  public static $advanced = '';
  protected $dict_source = array('include/dict/slovnik-i.dict', 'include/dict/slovnik-y.dict');

  function __construct($letter = null) {
    switch ($letter) {
      case 'i':
        $this->dict = 'include/dict/slovnik-i.dict';
        break;
      case 'y':
        $this->dict = 'include/dict/slovnik-y.dict';
        break;
      default:
        $this->dict = $this->dict_source[mt_rand(0, count($this->dict_source) - 1)];
    }
    $this->toreplace = array('i', 'y', 'í', 'ý');<commit_msg>Fixed result validation<commit_after><?php

class SouhlaskyUprostredSlov extends Formula {
  public static $name = 'Souhl&aacute;<NAME>&rcaron;<NAME>';
  public static $subject = '&Ccaron;e&scaron;tina';
  public static $advanced = ''; #TODO: allow to choose individual dictionaries
  protected $element;
  protected $dict_source = array('include/dict/slovnik-bp.dict', 'include/dict/slovnik-dt.dict', 'include/dict/slovnik-sz.dict', 'include/dict/slovnik-vf.dict');
  protected $dict;
  protected $toreplace;

  function __construct($letter = null) {
    switch ($letter) {
      case 'b':
        $this->dict = 'include/dict/slovnik-bp.dict';
        $this->toreplace = array('b', 'p');
        break;
      case 'd':
        $this->dict = 'include/dict/slovnik-dt.dict';
        $this->toreplace = array('d', 't');
        break;
      case 's':
        $this->dict = 'include/dict/slovnik-sz.dict';
        $this->toreplace = array('s', 'z');
        break;
      case 'v':
        $this->dict = 'include/dict/slovnik-vf.dict';
        $this->toreplace = array('v', 'f');
        break;
      default:
        $this->dict = $this->dict_source[mt_rand(0, count($this->dict_source) - 1)];
        $this->toreplace = str_split(substr($this->dict, 16, 2));
    }
    if (in_array('d', $this->toreplace)) {
      $this->toreplace[] = 'ď';
    }
    if (in_array('t', $this->toreplace)) {
      $this->toreplace[] = 'ť';
    }
    if (in_array('s', $this->toreplace)) {
      $this->toreplace[] = 'š';
    }
    if (in_array('z', $this->toreplace)) {
      $this->toreplace[] = 'ž';
    }
    $this->element = new RandomWordElement($this->dict);
  }
  protected function getBlank() {
    return str_replace($this->toreplace, '_', $this->element);
  }
  protected function blankReplace($haystack, $repl) {
    return implode($repl, explode('_', $haystack, 2));
  }

  public function toHTML($result = FALSE, $cls = 'select') {
    $text = '<span class="formula">';
    if ($result) {
      $text .= $this->element->toHTML();
    } else {
      $form = $this->getBlank();
      $rescount = 1;
      $text .= '<label class="'. $cls. '">';
      while (strpos($form, '_') !== false) {
        $input = '<select name="result'. $rescount. '" class="'. $cls. '"><option value="*"> </option>';
        foreach($this->toreplace as $char) {
          $input .= '<option value="'. $char. '">'. htmlentities($char, ENT_HTML5, "UTF-8"). '</option>';
        }
        $input .= '</select>';
        $form = $this->blankReplace($form, $input);
        $rescount++;
        if ($rescount > 256) break;
      }
      $text .= $form. '</label>';
    }
    $text .= '</span>';
    return $text;
  }
  public function toStr($result = FALSE) {
    if ($result) {
      return $this->getBlank(). ' = '. $this->element->toStr();
    } else {
      return $this->getBlank();
    }
  }

  public function getResult() {
    $text = $this->getBlank();
    $result = array();
    while (($i = strpos($text, '_')) !== false) {
      $result[] = $this->element->toStr()[$i];
      $text = $this->blankReplace($text, '*');
    }
    return $result;
  }

  public function getResultHTMLForm() {
    return '';
  }

  public function validateResult($input) {
    $ret = false;
    if (is_array($input)) {
      $form = $this->getBlank();
      if (count($input) != substr_count($form, '_')) return false;
      while (strpos($form, '_') !== false) {
        $form = $this->blankReplace($form, array_shift($input));
      }
      if (MAT_DEBUG) print("Checking $form in ". $this->dict);
      if ($handle = fopen($this->dict, 'r')) {
        while($line = stream_get_line($handle, 256, "\n")) {
          if (trim($line) == trim($form)) {
            $ret = true;
            break;
          }
        }
        fclose($handle);
      }
    }
    return $ret;
  }
} // class SouhlaskyUprostredSlov

class VyjmenovanaSlova extends SouhlaskyUprostredSlov {
  public static $name = '<NAME>';
  public static $advanced = '';
  protected $dict_source = array('include/dict/slovnik-i.dict', 'include/dict/slovnik-y.dict');

  function __construct($letter = null) {
    switch ($letter) {
      case 'i':
        $this->dict = 'include/dict/slovnik-i.dict';
        break;
      case 'y':
        $this->dict = 'include/dict/slovnik-y.dict';
        break;
      default:
        $this->dict = $this->dict_source[mt_rand(0, count($this->dict_source) - 1)];
    }
    $this->toreplace = array('i', 'y', 'í', 'ý');
{'fraction_non_alphanumeric': 0.201286972228494, 'fraction_numerical': 0.0038383382253330323, 'mean_word_length': 2.6307377049180327, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 125, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3288653', 'n_tokens_mistral': 3081, 'n_tokens_neox': 3025, 'n_words': 593}