Exemplo n.º 1
0
/**
 * Returns the value at an index of an array. This function simplifies the
 * common pattern of checking for an index in an array and selecting a default
 * value if it does not exist. You should NOT use `idx` as a general replacement
 * for accessing array indices.
 *
 * `idx` is used to look for an index in an array, and return either the value
 * at that index (if it exists) or some default (if it does not). Without
 * `idx`, you need to do this:
 *
 * ```
 * array_key_exists('index', $arr) ? $arr['index'] : $default
 * ```
 *
 * This is verbose, and duplicates the variable name and index name, which can
 * lead to errors. With `idx`, you can simplify the expression:
 *
 * ```
 * idx($arr, 'index', $default);
 * ```
 *
 * The value `$default` is optional, and defaults to null if unspecified.
 *
 * The array `$arr` is permitted to be null; if it is null, `idx` guarantees
 * it will return `$default`.
 *
 * You should NOT use `idx` as a general replacement for accessing array
 * indices. If you expect 'index' to always exist, DON'T use idx()!
 *
 * ```
 * // COUNTEREXAMPLE
 * idx($arr, 'index'); // If you expect 'index' to exist, this is WRONG!
 * ```
 *
 * Instead, just access it normally like a sensible human being:
 *
 * ```
 * $arr['index']
 * ```
 *
 * This will give you a helpful warning if the index doesn't exist, allowing
 * you to identify a bug in your program and fix it. In contrast, idx() will
 * fail silently if the index doesn't exist, which won't help you out at all.
 *
 * `idx` is for default selection, not a blanket replacement for array access.
 *
 * Finally, you should NOT fix errors about array indexes in parts of the code
 * you don't understand by just replacing an array access with a call to `idx`.
 * This is sweeping the problem under the rug. Instead, you need to actually
 * understand the problem and determine the most appropriate solution. It is
 * possible that this really is `idx`, but you can only make that determination
 * after understanding the context of the error.
 *
 * @param array $arr - Array to look for an index in.
 * @param scalar $idx - Index to check for. No longer accepts negative integers
 *                      for accessing elements from the end of the array.
 * @param mixed $default - Default value to return if index is not found. By
 *                         default, this is null.
 * @return mixed Value at array index if it exists, or the default value if not.
 */
function idx($arr, $idx, $default = null)
{
    if (\is_array($arr) || \is_vec($arr) || \is_dict($arr) || \is_keyset($arr)) {
        return \hphp_array_idx($arr, $idx, $default);
    }
    if ($idx !== null) {
        if (\is_object($arr)) {
            if ($arr instanceof \ConstIndexAccess) {
                if ($arr->containsKey($idx)) {
                    return $arr[$idx];
                }
            } else {
                if ($arr instanceof \ConstSet) {
                    if ($arr->contains($idx)) {
                        return $idx;
                    }
                } else {
                    if ($arr instanceof \ArrayAccess) {
                        if ($arr->offsetExists($idx)) {
                            return $arr->offsetGet($idx);
                        }
                    }
                }
            }
        } else {
            if (\is_string($arr)) {
                if (isset($arr[$idx])) {
                    return $arr[(int) $idx];
                }
            }
        }
    }
    return $default;
}
Exemplo n.º 2
0
function read_depaths($path, $lang = NULL)
{
    $string = file_get_contents($path);
    $json = json_decode($string, true);
    $parts = $_parts = $json["parts"];
    if (is_vec($parts)) {
        $parts = [];
        foreach ($_parts as $part) {
            $parts = array_merge($parts, $part);
        }
    } elseif ($parts === NULL) {
        $parts = [];
    }
    $result = [];
    # DEPATHs generated
    foreach (array_keys($parts) as $key) {
        init_values($key, $result, $parts, $lang);
    }
    $depaths = $json["depaths"];
    # depath JSON representation
    unset($json);
    if (isset($depaths)) {
        return make_depaths($result, $depaths, $parts, $lang);
    }
}
Exemplo n.º 3
0
function _get_first_last($arr, &$first, &$last)
{
    if (!$arr) {
        return;
    }
    if (is_vec($arr)) {
        $first = $arr[0];
        $last = $arr[count($arr) - 1];
    } else {
        $first = array_keys($arr)[0];
        $last = array_keys($arr)[count($arr) - 1];
    }
}
Exemplo n.º 4
0
 function __construct($init = NULL, $aliases = NULL, $name = NULL)
 {
     $this->key2values = [];
     $this->value2key = [];
     $this->all_sub_keys = [];
     $this->level = [];
     $this->simple_keys = [];
     $this->recursive_keys = [];
     $this->name = $name;
     if ($aliases === NULL) {
         $aliases = [];
     }
     $this->aliases = $aliases;
     #echo "\$init:\n";
     #var_dump($init);
     if ($init !== NULL) {
         foreach ($init as $k => $v) {
             if (!is_array($v)) {
                 _die("bad type, not array");
             }
             if (is_vec($v)) {
                 $this->simple_keys[] = $k;
                 register2($this, $k, $v);
             } else {
                 $this->recursive_keys[] = $k;
                 foreach ($v as $_ => &$v_) {
                     register($this, $k, $_);
                     #echo "\$v_:\n";
                     #var_dump($v_);
                     if (!ISDEPATH($v_)) {
                         $v_ = DEPATH($v_, $aliases);
                     }
                     $this->all_sub_keys = array_merge($this->all_sub_keys, $v_->all_sub_keys);
                     register3($this, $v_->key2values);
                 }
             }
             $this->level[$k] = $v;
         }
     }
     # Sort and remove duplicates
     $this->all_sub_keys = array_values(array_unique($this->all_sub_keys));
     foreach (array_keys($this->key2values) as $k) {
         $this->key2values[$k] = array_unique($this->key2values[$k]);
     }
 }
Exemplo n.º 5
0
function find_word_path($word)
{
    $path = array_slice(func_get_args(), 1);
    if (!ISWORD($word)) {
        $word = WORD($word);
    }
    $p = PATH($word);
    foreach ($path as $v) {
        if (!is_vec($v)) {
            foreach (array_keys($v) as $k) {
                $path->add($k, $v[$k]);
            }
        } else {
            $path->add($v);
        }
    }
    return $p->get();
}
Exemplo n.º 6
0
function word_table_values($w, $ignore = NULL)
{
    if (!$w->read_paths()) {
        return [NULL, NULL, NULL, NULL, NULL];
    }
    $lang = $w->lang();
    $spart = $w->speechpart();
    $values4 = $values3 = $values2 = $values1 = NULL;
    $values0 = false;
    // values0 : table name
    // values1 : major vertical
    // values2 : minor vertical
    // values3 : major horizontal
    // values4 : minor horizontal
    if ($lang === "la" or $lang === "grc") {
        if ($spart === "noun" or $spart === "adjective" or $spart === "pronoun") {
            if ($spart === "adjective") {
                $values1 = $w->path()->iterate("degree");
            } else {
                $values1 = [];
            }
            $values2 = $w->path()->iterate("case");
            $values3 = $w->path()->iterate("number");
            $values4 = $w->path()->iterate("gender");
        } elseif ($spart === "verb") {
            $values0 = $moods = $w->path()->iterate("mood");
            $values1 = [];
            $values2 = [];
            $values3 = [];
            $values4 = [];
            $hspan4 = [];
            $persons = PATH($w, "indicative")->iterate("person");
            $persons = _filter_ignore($persons, $ignore, PATH($w, "indicative"));
            foreach ($persons as $_) {
                $hspan4[] = FALSE;
            }
            $hacked = NULL;
            foreach ($moods as $_0) {
                if ($ignore !== NULL and in_array($_0, $ignore)) {
                    continue;
                }
                $vals4 = $vals3 = $vals2 = $vals1 = NULL;
                $path = PATH($w, $_0);
                if ($_0 === "indicative" or $_0 === "subjunctive" or $_0 === "imperative") {
                    $vals1 = $path->iterate("voice");
                    $vals2 = $path->iterate("tense");
                    $vals3 = $path->iterate("number");
                    $vals4 = PATH($w, "indicative")->iterate("person");
                    if (!$values1) {
                        $values1 = [FALSE];
                    }
                } else {
                    if ($_0 === "participle") {
                        $vals1 = [""];
                        $vals2 = $path->iterate("tense");
                        $vals3 = $path->iterate("voice");
                        $vals4 = $hspan4;
                    } else {
                        if ($_0 === "infinitive") {
                            $vals1 = [""];
                            $vals2 = $path->iterate("tense");
                            $vals3 = $path->iterate("voice");
                            $vals4 = $hspan4;
                        } else {
                            if ($_0 === "supine" or $_0 === "gerund") {
                                if ($hacked !== NULL) {
                                    foreach ($values0 as $i => $v) {
                                        if ($v !== $_0) {
                                            continue;
                                        }
                                        unset($values0[$i]);
                                        break;
                                    }
                                    $values3[$hacked][] = $_0;
                                    continue;
                                }
                                $hacked = "";
                                foreach ($values0 as $i => $v) {
                                    if ($v !== $_0) {
                                        continue;
                                    }
                                    $values0[$i] = $hacked;
                                    break;
                                }
                                $vals1 = [""];
                                $vals2 = PATH($w, "gerund")->iterate("case");
                                if (!$vals2) {
                                    $vals2 = PATH($w, "supine")->iterate("case");
                                }
                                $vals3 = [$_0];
                                $vals4 = $hspan4;
                                $_0 = $hacked;
                            }
                        }
                    }
                }
                $values1[$_0] = $vals1;
                $values2[$_0] = $vals2;
                $values3[$_0] = $vals3;
                $values4[$_0] = $vals4;
            }
        } elseif ($spart === "adverb") {
            $values0 = [];
            $values1 = $w->path()->iterate("degree");
        }
    } else {
        if ($lang === "fr") {
            if ($spart === "noun" or $spart === "adjective" or $spart === "pronoun") {
                if ($spart === "adjective") {
                    $values1 = $w->path()->iterate("degree");
                } else {
                    $values1 = [];
                }
                $values3 = $w->path()->iterate("number");
                $values4 = $w->path()->iterate("gender");
            } elseif ($spart === "verb") {
                $values0 = $moods = $w->path()->iterate("mood");
                $values1 = [];
                $values2 = [];
                $values3 = [];
                $values4 = [];
                foreach ($moods as $_0) {
                    if ($ignore !== NULL and in_array($_0, $ignore)) {
                        continue;
                    }
                    $vals4 = $vals3 = $vals2 = $vals1 = NULL;
                    $path = PATH($w, $_0);
                    if ($_0 === "indicative" or $_0 === "subjunctive") {
                        if (!$vals1) {
                            $vals1 = [FALSE];
                        }
                        $vals2 = $path->iterate("tense");
                        $vals3 = $path->iterate("number");
                        $vals4 = $path->iterate("person");
                    } else {
                        if ($_0 === "infinitive" or $_0 === "gerund") {
                            $vals1 = [""];
                            $vals2 = [""];
                            $vals3 = $path->iterate("type");
                            $vals4 = [FALSE, FALSE, FALSE];
                        } else {
                            if ($_0 === "imperative") {
                                $vals1 = [""];
                                $vals2 = [""];
                                $vals3 = $path->iterate("number");
                                $vals4 = $path->iterate("person");
                            } else {
                                if ($_0 === "participle") {
                                    $vals1 = [""];
                                    $vals2 = $path->iterate("gender");
                                    $vals3 = $path->iterate("tense");
                                    $vals4 = $path->iterate("number");
                                }
                            }
                        }
                    }
                    /**/
                    $values1[$_0] = $vals1;
                    $values2[$_0] = $vals2;
                    $values3[$_0] = $vals3;
                    $values4[$_0] = $vals4;
                }
            } elseif ($spart === "adverb") {
                $values1 = $w->path()->iterate("degree");
            }
        } else {
            if ($lang === "es") {
                if ($spart === "noun" or $spart === "adjective" or $spart === "pronoun") {
                    if ($spart === "adjective") {
                        $values1 = $w->path()->iterate("degree");
                    } else {
                        $values1 = [];
                    }
                    $values3 = $w->path()->iterate("number");
                    $values4 = $w->path()->iterate("gender");
                } elseif ($spart === "verb") {
                    $moods = $w->path()->iterate("mood");
                    $values0 = [];
                    foreach ($moods as $_0) {
                        if ($ignore !== NULL and in_array($_0, $ignore)) {
                            continue;
                        }
                        $path = PATH($w, $_0);
                        if ($_0 === "indicative" or $_0 === "subjunctive") {
                            if (!$values1) {
                                $values1 = [FALSE];
                            }
                            $values2 = $path->iterate("tense");
                            $values3 = $path->iterate("number");
                            $values4 = $path->iterate("person");
                        } else {
                            if ($_0 === "infinitive" or $_0 === "gerund") {
                                $values1 = [""];
                                $values2 = [""];
                                $values3 = [""];
                                $values4 = [FALSE, FALSE, FALSE];
                            } else {
                                if ($_0 === "imperative") {
                                    $values1 = [""];
                                    $values2 = $path->iterate("imperative-mood");
                                    $values3 = $path->iterate("number");
                                    $values4 = $path->iterate("person");
                                } else {
                                    if ($_0 === "past-participle") {
                                        $values1 = [""];
                                        $values2 = $path->iterate("gender");
                                        $values3 = $path->iterate("number");
                                        $values4 = [false, false, false];
                                    }
                                }
                            }
                        }
                        $values0[$_0] = [$values1, $values2, $values3, $values4];
                    }
                } elseif ($spart === "adverb") {
                    $values1 = $w->path()->iterate("degree");
                }
            } else {
                if ($lang === "eo") {
                    if ($spart === "noun" or $spart === "adjective" or $spart === "pronoun") {
                        $values1 = [];
                        $values2 = $w->path()->iterate("case");
                        $values3 = $w->path()->iterate("number");
                        $values4 = [];
                    } elseif ($spart === "verb") {
                        $moods = $w->path()->iterate("mood");
                        $values0 = [];
                        $hacked = NULL;
                        foreach ($moods as $_0) {
                            if ($ignore !== NULL and in_array($_0, $ignore)) {
                                continue;
                            }
                            $path = PATH($w, $_0);
                            $name = NULL;
                            if ($_0 === "indicative") {
                                $values1 = [""];
                                $values2 = [""];
                                $values3 = $path->iterate("tense");
                                $values4 = [FALSE, FALSE];
                            } else {
                                if ($_0 === "infinitive" or $_0 === "conditional" or $_0 === "imperative") {
                                    if ($hacked) {
                                        $values0[$hacked][2][] = $_0;
                                        continue;
                                    }
                                    $hacked = $_0;
                                    $name = "";
                                    $values1 = [""];
                                    $values2 = [""];
                                    $values3 = [$_0];
                                    $values4 = [FALSE, FALSE];
                                } else {
                                    if ($_0 === "adjectival-participle") {
                                        $values1 = $path->iterate("voice");
                                        $values2 = $path->iterate("case");
                                        $values3 = $path->iterate("tense");
                                        $values4 = $path->iterate("number");
                                    } else {
                                        if ($_0 === "nominal-participle") {
                                            $values1 = $path->iterate("voice");
                                            $values2 = $path->iterate("case");
                                            $values3 = $path->iterate("tense");
                                            $values4 = $path->iterate("number");
                                        } else {
                                            if ($_0 === "adverbial-participle") {
                                                $values1 = $path->iterate("voice");
                                                $values2 = [""];
                                                $values3 = $path->iterate("tense");
                                                $values4 = [FALSE, FALSE];
                                            }
                                        }
                                    }
                                }
                            }
                            $values0[$_0] = [$values1, $values2, $values3, $values4];
                            if ($name !== NULL) {
                                $values0[$_0][] = $name;
                            }
                        }
                    }
                } else {
                    if ($lang === "ith" && $spart === "root") {
                        $values0 = $w->path()->iterate("complement");
                        $values3 = $w->path()->iterate("formality");
                        $values2 = $w->path()->iterate("stem");
                    }
                }
            }
        }
    }
    // values0 : table name
    // values1 : major vertical
    // values2 : minor vertical
    // values3 : major horizontal
    // values4 : minor horizontal
    // #1,2,3,4 may depend on #0 (already done)
    // #2       may depend on #1
    // #4       may depend on #3
    $values0 = _do_ignore($values0, $ignore);
    if ($values0 === false) {
        $values0 = [false];
    }
    if (is_vec($values1)) {
        $values1 = _fill($values1, $values0);
    }
    if (is_vec($values2)) {
        $values2 = _fill($values2, $values0);
    }
    if (is_vec($values3)) {
        $values3 = _fill($values3, $values0);
    }
    if (is_vec($values4)) {
        $values4 = _fill($values4, $values0);
    }
    _filter_ignore2($values1, $ignore, PATH($w), $values0);
    _filter_ignore2($values2, $ignore, PATH($w), $values0, $values1);
    _filter_ignore2($values3, $ignore, PATH($w), $values0);
    _filter_ignore2($values4, $ignore, PATH($w), $values0, $values3);
    /*var_dump($values0);
    	var_dump($values1);
    	var_dump($values2);
    	var_dump($values3);
    	var_dump($values4);*/
    return [$values0, $values1, $values2, $values3, $values4];
}
Exemplo n.º 7
0
function make_vec($obj)
{
    return is_vec($obj) ? $obj : array_values($obj);
}