function getMap($key) { $alphabet = getPermutations($key, strlen($key) - 1); $index = 0; for ($i = 65; $i < 92; $i++) { $map[chr($i)] = $alphabet[$index]; $index++; } return $map; }
function getPermutations(array $items, &$result, array $perms = array()) { if (empty($items)) { $result[] = $perms; } else { for ($i = 0; $i < count($items); $i++) { $newItems = $items; $newPerms = $perms; list($item) = array_splice($newItems, $i, 1); $newPerms[] = $item; getPermutations($newItems, $result, $newPerms); } } }
/** * Recupera todas permutações de uma matriz * * Copyright © <2011-2012> <Andrey Knupp Vital> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * @author Andrey Knupp Vital * @param Array $Array * @param Array $Permutations * @param Integer $limitOffset * @throws LengthException se o número de índices / valores no array for maior que $limitOffset */ function getPermutations(array $Array, array $Permutations = array(), $limitOffset = 5) { static $permutedItems = array(); $FlattenArray = array(); foreach (new RecursiveIteratorIterator(new RecursiveArrayIterator($Array)) as $Data) { $FlattenArray[] = $Data; } if (count($FlattenArray) > intval($limitOffset)) { throw new LengthException(sprintf('Não podemos gerar permutações com mais de %d valores', $limitOffset)); } $Array = array_filter(array_unique($FlattenArray)); if (count($Array)) { for ($i = 0; $i < count($Array); ++$i) { $newArray = $Array; $newPermutations = $Permutations; array_push($newPermutations, array_shift(array_splice($newArray, $i, 1))); getPermutations($newArray, $newPermutations, $limitOffset); } return $permutedItems; } else { $permutedItems[] = $Permutations; } }
function evalQueryNBLNK($context, $queryString, $minRank, $maxNofRanks) { $storage = $context->createStorageClient(""); $analyzer = $context->createQueryAnalyzer(); $queryeval = $context->createQueryEval(); $analyzer->definePhraseType("text", "stem", "word", array("lc", array("dictmap", "irregular_verbs_en.txt"), array("stem", "en"), array("convdia", "en"), "lc")); $queryeval->addWeightingFunction(1.0, "BM25", array("k1" => 0.75, "b" => 2.1, "avgdoclen" => 500, ".match" => "docfeat")); $queryeval->addWeightingFunction(2.0, "metadata", array("name" => "pageweight")); $queryeval->addSummarizer("LINK", "accuvariable", array(".match" => "sumfeat", "var" => "LINK", "type" => "linkid")); $queryeval->addSelectionFeature("selfeat"); $query = $queryeval->createQuery($storage); $terms = $analyzer->analyzePhrase("text", $queryString); if (count($terms) > 0) { if (count($terms) > 1) { $pairs = getPermutations(count($terms)); foreach ($pairs as &$pair) { $term1 = $terms[$pair[0]]; $term2 = $terms[$pair[1]]; if ($pair[0] + 1 == $pair[1]) { $expr = array(array("sequence_struct", 3, array("sent"), array($term1->type, $term1->value), array($term2->type, $term2->value)), array("sequence_struct", 3, array("sent"), array($term2->type, $term2->value), array($term1->type, $term1->value)), array("within_struct", 5, array("sent"), array($term1->type, $term1->value), array($term2->type, $term2->value)), array("within_struct", 20, array("sent"), array($term1->type, $term1->value), array($term2->type, $term2->value))); $weight = array(3.0, 2.0, 2.0, 1.5); $ii = 0; while ($ii < 4) { $sumexpr = array("chain_struct", 50, array("sent"), array("=LINK", "linkvar"), $expr[$ii]); $query->defineFeature("sumfeat", $sumexpr, $weight[$ii]); $sumexpr = array("sequence_struct", -50, array("sent"), $expr[$ii], array("=LINK", "linkvar")); $query->defineFeature("sumfeat", $sumexpr, $weight[$ii]); ++$ii; } } elseif ($pair[0] + 2 < $pair[1]) { $expr = array("within_struct", 20, array("sent"), array($term1->type, $term1->value), array($term2->type, $term2->value)); $weight = 1.1; $sumexpr = array("inrange_struct", 50, array("sent"), array("=LINK", "linkvar"), $expr); $query->defineFeature("sumfeat", $sumexpr, $weight); } elseif ($pair[0] < $pair[1]) { $expr = array(array("within_struct", 5, array("sent"), array($term1->type, $term1->value), array($term2->type, $term2->value)), array("within_struct", 20, array("sent"), array($term1->type, $term1->value), array($term2->type, $term2->value))); $weight = array(1.6, 1.2); $ii = 0; while ($ii < 2) { # The summarization expression attaches a variable # LINK ("=LINK") to links (terms of type 'linkvar'): $sumexpr = array("chain_struct", 50, array("sent"), array("=LINK", "linkvar"), $expr[$ii]); $query->defineFeature("sumfeat", $sumexpr, $weight[$ii]); $sumexpr = array("sequence_struct", -50, array("sent"), $expr[$ii], array("=LINK", "linkvar")); $query->defineFeature("sumfeat", $sumexpr, $weight[$ii]); ++$ii; } } } } else { $expr = array($terms[0]->type, $terms[0]->value); # The summarization expression attaches a variable # LINK ("=LINK") to links (terms of type 'linkvar'): $sumexpr = array("chain_struct", 50, array("sent"), array("=LINK", "linkvar"), $expr); $query->defineFeature("sumfeat", $sumexpr, 1.0); $sumexpr = array("sequence_struct", -50, array("sent"), $expr, array("=LINK", "linkvar")); $query->defineFeature("sumfeat", $sumexpr, 1.0); } $selexpr = array("contains"); foreach ($terms as &$term) { $selexpr[] = array($term->type, $term->value); $query->defineFeature("docfeat", array($term->type, $term->value), 1.0); } $query->defineFeature("selfeat", $selexpr, 1.0); } $query->setMaxNofRanks(300); $query->setMinRank(0); $candidates = $query->evaluate(); $linktab = array(); foreach ($candidates as &$candidate) { foreach ($candidate->attributes as &$attrib) { if (strcmp($attrib->name, 'LINK') == 0) { $lnkid = trim($attrib->value); if (array_key_exists($lnkid, $linktab)) { $linktab[$lnkid] = 0.0 + $linktab[$lnkid] + $attrib->weight * $candidate->weight; } else { $linktab[$lnkid] = 0.0 + $attrib->weight * $candidate->weight; } } } } # Extract the top weighted documents in the linktable as result: $bestn = new LinkSet($minRank, $maxNofRanks); if (empty($linktab)) { return array(); } foreach ($linktab as $link => $weight) { $bestn->insert($link, $weight); } $rt = $bestn->getBestLinks(); return $rt; }