Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $lf = str_replace('/', '-', (isset($_REQUEST['lx'])?$_REQUEST['lx']:'en-f')) . '.json';
- $lex = json_decode(file_get_contents('./lexicons/' . $lf), true);
- $q = isset($_REQUEST['q'])?$_REQUEST['q']:'';
- function nextWord($w) {
- global $lex;
- if(array_key_exists($w, $lex)) {
- return $lex[$w][rand(0, count($lex[$w])-1)];
- } else return null;
- }
- function endsWith($haystack, $needle) {
- return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== false);
- }
- function endsWithPunctuations($t) {
- $p = str_split('(),.?!');
- $r = false;
- foreach($p as $n) {
- if(endsWith($t, $n)) $r = true;
- }
- return $r;
- }
- if(strlen($q) < 1) {
- die;
- }
- $t = explode(' ', $q);
- if(count($t) > 1) {
- $u = array_unique($t);
- foreach($u as $i => $c) {
- if(array_key_exists($c, $lex))
- $w = $lex[$c];
- else
- $w = [];
- for($n = 0; $n < count($t); $n++) {
- if($c == $t[$n] && !endsWithPunctuations($t[$n])) {
- array_push($w, $t[$n+1]);
- }
- }
- $lex[$c] = array_values(array_unique($w));
- }
- file_put_contents('./lexicons/' . $lf, strtolower(json_encode($lex)));
- }
- $s = $t[rand(0, count($t)-1)];
- dbg('RndWrd: ' . $s);
- if(array_key_exists($s, $lex)) {
- dbg('Constructing setnence');
- $r = $s;
- $words = 0;
- while(true) {
- $nw = nextWord($s);
- if($nw == null) break;
- dbg('NxtWrd: ' . $nw);
- $r .= ' ' . $nw;
- $s = $nw;
- if(endsWithPunctuations($nw, -1)) break;
- $words++;
- if($words > 50) break;
- }
- echo str_replace(' ', ' ', $r);
- } else echo 'What?';
- ?>
Advertisement
Add Comment
Please, Sign In to add comment