function get_id($value, $sentence) { foreach ($sentence as $_ => $__) { if (ISOP($__)) { continue; } if ($__["value"] === $value) { return $_; } } }
function simplify_punctuation($ops) { global $DEBUG_STRING_PHP; global $OP_COMMA, $OP_PERIOD, $OP_COLON, $OP_APOS, $OP_QUEST, $OP_EXCL, $OP_MDASH, $OP_NDASH, $OP_LPAREN, $OP_RPAREN, $OP_LQUOTE, $OP_RQUOTE; if (!$ops) { return $ops; } // Remove punctuation that doesn't belong at start of a sentence while (in_array($ops[0], [$OP_COMMA, $OP_PERIOD, $OP_COLON, $OP_APOS, $OP_QUEST, $OP_EXCL])) { array_shift($ops); } $precedence = [[$OP_QUEST, $OP_EXCL, $OP_MDASH, $OP_NDASH], [$OP_PERIOD], [$OP_COMMA]]; $get_precedence = function ($o) use($precedence) { $prec = NULL; foreach ($precedence as $p => $ops) { foreach ($ops as $op) { if ($o === $op) { $prec = count($precedence) - $p; break; } } if ($prec !== NULL) { break; } } return $prec; }; // Question marks, etc., can override periods, which can override commas. // Also remove straight duplicates, keeping the first one. $i = 0; while ($i < count($ops) - 1) { if (!ISOP($op = $ops[$i]) or ($prec = $get_precedence($op)) === NULL) { $i += 1; continue; } $j = $i + 1; while ($j < count($ops)) { if (!ISOP($ops[$j])) { break; } $p = $get_precedence($o = $ops[$j]); if ($p === NULL) { $j += 1; } else { if ($p < $prec) { if ($DEBUG_STRING_PHP) { echo "Remove {$o} (j={$j}) vs {$op} (i={$i})<br>"; } array_splice($ops, $j, 1); } else { if ($p > $prec || $op === $o) { if ($DEBUG_STRING_PHP) { echo "Remove {$op} (i={$i}) vs {$o} (j={$j})<br>"; } array_splice($ops, $i, 1); $i = $j - 1; } else { $j += 1; } } } } $i = $j; } // Follow American quotation rules for commas and periods $i = 0; while ($i < count($ops) - 1) { $op = $ops[$i]; if (!in_array($op, [$OP_LQUOTE, $OP_RQUOTE])) { $i += 1; continue; } $j = $i + 1; $o = $ops[$j]; if (in_array($o, [$OP_PERIOD, $OP_COMMA])) { if ($DEBUG_STRING_PHP) { echo "Switch {$op} (i={$i}) and {$o} (j={$j})<br>"; } $ops[$i] = $o; $ops[$j] = $op; $i = $j + 1; } else { $i += 1; } } // Obliterate close quotes followed by open quotes $i = 0; while ($i < count($ops) - 1) { $op = $ops[$i]; if ($op !== $OP_RQUOTE) { $i += 1; continue; } $j = $i + 1; while ($j < count($ops)) { $o = $ops[$j]; if (!ISOP($o)) { break; } if ($o !== $OP_LQUOTE) { $j += 1; continue; } array_splice($ops, $i, $j - $i + 1); break; } $i = $j; } return $ops; }
function do_pick($t, $db, &$pick_db, &$reason) { if ($db === NULL) { $db = defaultDB(); } if ($t === NULL) { $reason = "pick was null"; return $t; } elseif (ISOP($t) or ISHTML($t)) { return $t; } elseif (ISPICK($t)) { return $t->rand($db); } elseif (is_string($t)) { return $t; } elseif (is_callable($t)) { $t = _process_value($t, $pick_db, $db); if ($t === NULL) { $reason = "custom function returned NULL"; return $t; } else { return do_pick($t, $db, $pick_db, $reason); } } elseif (array_key_exists("condition", $t) and !$t["condition"]($pick_db, $db, null)) { return FALSE; } elseif (array_key_exists("value", $t)) { return _process_value($t["value"], $pick_db, $db); } if (!($word = safe_get("word", $t))) { $searcher = $db->searcher(); #var_dump(array_keys($searcher->master)); if (array_key_exists("name", $t)) { $searcher = $searcher->name(_process_value($t["name"], $pick_db, $db)); } if (array_key_exists("language", $t)) { $searcher = $searcher->lang(_process_value($t["language"], $pick_db, $db)); } elseif (array_key_exists("lang", $t)) { $searcher = $searcher->lang(_process_value($t["lang"], $pick_db, $db)); } if (array_key_exists("speechpart", $t)) { $searcher = $searcher->partofspeech(_process_value($t["speechpart"], $pick_db, $db)); } elseif (array_key_exists("spart", $t)) { $searcher = $searcher->partofspeech(_process_value($t["spart"], $pick_db, $db)); } if (array_key_exists("attr", $t)) { foreach ($t["attr"] as $k => $v) { $v = _process_value($v, $pick_db, $db); if ($reverse = substr($k, 0, 1) === "!") { $k = substr($k, 1); $m = "only_without_attr"; } else { $m = "only_with_attr"; } $searcher = $searcher->{$m}($v !== NULL ? ATTR($k, $v) : ATTR($k)); } } $word = $searcher->rand(); } if (!ISWORD($word)) { $reason = "could not find a word with name " . var_export($t["name"], 1) . " and attrs " . var_export(safe_get("attr", $t), 1); return; } if (array_key_exists("store_word", $t)) { $pick_db[$t["store_word"]] = $word; } $word->read_paths(); $path = PATH($word); if (array_key_exists("path", $t)) { $p = $t["path"]; if (!is_array($p)) { $p = _process_value($p, $pick_db, $db, $path); } foreach ($p as $k => $_) { $path->add2([$k => _process_value($_, $pick_db, $db, $path)]); } } if (array_key_exists("verb-gender", $t)) { $g = $t["verb-gender"]; $g = _process_value($g, $pick_db, $db, $path); if ($g !== NULL and $path->exists()) { $path->add($g); if (!$path->hasvalue()) { $path->take("gender"); } } } if ($path->hasvalue() || !(string) $path) { $ret = $path->hasvalue() ? $path->get() : $word->name(); if (array_key_exists("store", $t)) { $pick_db[$t["store"]] = $ret; } if (array_key_exists("store_path", $t)) { $pick_db[$t["store_path"]] = $path; } return format_word($ret, $word->lang()); } else { $reason = "path '{$path}' didn't exist in word with id <a target='_blank' href='dictionary.php?id=" . $word->id() . "'>" . $word->id() . "</a> or was NULL"; return; } }