function generate($minLength) { // choose starting word at random $word = choose($this->initials); // put into array $title = array($word); // loop over random words while ($this->stats[$word]) { $nextWords = $this->stats[$word]; $word = choose($nextWords); array_push($title, $word); // if this word is known to end a sentence / phrase and the minimum string length has been met if (count($title) > $minLength && $this->terminals[$word]) { // exit break; } } // join by spaces $str = implode(' ', $title); // ensure that the randomly generated string doesn't exist in the source if (count($title) < $minLength || in_array($str, $this->source)) { return $this->generate($minLength); } return $str; }
function choose($search_path, $segments = []) { $candidates = array_shift($search_path); foreach ($candidates as $segment) { if (count($search_path) > 0) { $chosen = choose($search_path, array_merge($segments, [$segment])); if ($chosen !== NULL) { return $chosen; } } else { $chosen = BASEPATH . implode(DIRECTORY_SEPARATOR, array_merge($segments, [$segment])); if (file_exists($chosen)) { return $chosen; } } } return NULL; }
/** * Does the same as {@see choose()}, but always asks yes/no and returns a boolean * * @param string $question The question to ask the user. * @param bool|null $default The default choice, in a boolean format. * @return bool */ function confirm($question, $default = false) { if (is_bool($default)) { $default = $default ? 'y' : 'n'; } $result = choose($question, 'yn', $default); return $result == 'y'; }
function chwrap($x) { return choose($x); }
protected function choose($dirname, $segments, $filename, $extension = NULL) { $filename .= $extension !== NULL ? '.' . $extension : ''; $search_path = []; $search_path[] = $this->controller->packages; $search_path[] = [$dirname]; if ($segments === NULL) { $segments = isset($this->controller->segments) ? $this->controller->segments : []; } foreach ($segments as $segment_key => $segment_value) { if ($segment_key === 'args') { continue; } $search_path[] = [$segment_value, '']; } $search_path[] = [$filename]; $chosen = choose($search_path); if ($chosen === NULL) { die("File '{$filename}' not found in '{$dirname}'."); } return $chosen; }
<?php header("Content-type: text/html; charset=gb2312"); include "Course.class.php"; switch ($_GET['action']) { case 'login': login(); break; case 'choose': choose(); break; case 'grab': grab(); break; } function login() { //登录 $grab = new Course($_POST['username']); $username = $_POST['username']; $password = $_POST['password']; $url = "http://jw.gdufs.edu.cn/pkmslogin.form"; $field = array('username' => $username, 'password' => $password, 'login-form-type' => 'pwd'); if ($grab->login($url, $field)) { $res = my_iconv($grab->loginResult()); echo json_encode($res); } else { echo 0; } } function choose()