我一直在研究搜索功能,它可以让我对特定的单词进行分组,然后在一个数组中递归地搜索它们,我已经把自己编码成一个我认为的洞。这是我第一次尝试递归,我确信我已经搞砸了。我很接近,但我似乎无法正确完成它,并希望有人能指出我正确的方向。
我正在使用二元组。我从从当前事件中提取的二元组列表开始。
“最少,6”,“6,德州”,“强力球,大奖”,“杀死,德州”,“德州,龙卷风”,“嫌疑人,游行”等。有 50 个独特的短语。
我正在尝试做的是找到相关的单词并将它们分组。使用上述数据,可以组合的单词是:“least, 6, texas,killed, tornado”,因为它们都是相关的。
我的方法是。取第一个二元组,将其拆分,搜索所有二元组,然后在找到这两个词的任何地方抓住这些二元组,拆分它们并再次重复。那应该让我得到所有的比赛。(或足以做我需要做的事情)。
我很确定我得到了正确的数据,但是我在从数组中删除这些单词时遇到了问题,因此它们不会被一遍又一遍地搜索。搜索的每次迭代都应该变得更小,因为字数较少。
好的代码。(注意递归函数不是我的,那部分确实有效。)也是的,它很乱。就像我说的,只是学习,我打算以后清理它。
底部附近的 array_diff 应该删除已经搜索过的单词,然后重复该过程。提前致谢。
<?php
$mainbigramarray = array();
$explodedarray = array();
$resarr = array();
$i=1;
function recursive_array_search($needle,$haystack,$subloop = false) {
if($subloop === false) $resarr = array();
foreach($haystack as $key=>$value) {
$current_key=$key;
if(is_string($needle)) $needle = trim(strtolower($needle));
if(is_string($value)) $value = trim(strtolower($value));
if($needle===$value OR (is_array($value) && recursive_array_search($needle,$value,true) === true)) {
$resarr[] = $current_key;
if($subloop === true) return true;
}
}
return $resarr;
}
WHILE($rows = mysql_fetch_array($query))
{
unset($bigrams);
$bigram = $rows['bigram'];
$count = $rows['m'];
$rid = $rows['RID'];
$bgexplode = explode(" ", $bigram);
foreach($bgexplode as $bg) {
$bigrams[] = $bg;
}
$workingbigram [] = $bigrams;
array_push($bigrams, $count);
$mainbigramarray[] = $bigrams;
}
echo "<table>";
echo "<tr>";
$firstrun = array();
$secondrun = array();
$resultrun = array();
$matchset1 = array();
$bigramset = array();
echo "<td>";
foreach($workingbigram as $bgg) { //steps through the main array that holds the exploded bigram
unset($firstrun);
foreach($bgg as $word) { //steps through both words of the bigram
$search1 = recursive_array_search($word, $mainbigramarray);
$firstrun[$word] = $search1;
}
$bigramset[] = $firstrun;
}
//echo "<pre>";
//print_r($bigramset);
//echo "</pre>";
echo "</td>";
echo "<td>";
$counter = 0;
foreach($bigramset as $key1=>$value1) { //get the array that holds the exploded biram
foreach($value1 as $key2=>$value2) { //get the array that holds the ids of whre the word is found
//echo "$key2<br>";
foreach($value2 as $searchid) { //gets the id to pull the matching exploded bigrams from.
unset($bigresult);
foreach($workingbigram[$searchid] as $wordresult) { //gets word to seasrch from by iding mainbigram array
$bigresult = recursive_array_search($wordresult, $mainbigramarray);
}
$resultrun[] = $bigresult;
}
}
foreach($resultrun as $key3=>$value3) {
foreach($value3 as $finalsearchid) {
foreach($workingbigram[$finalsearchid] as $lastsearchterm) {
$finalwordset[] = $lastsearchterm;
}
}
}
$finalwordset = array_unique($finalwordset);
foreach($finalwordset as $word) {
$total = recursive_array_search($word, $mainbigramarray);
$totalsum = 0;
foreach($total as $lastlookup) {
unset($bucket);
foreach($mainbigramarray[$lastlookup] as $total6) {
echo "$total6<br>";
$bucket[] = $total6;
}
//echo "Score:" . $bucket[2] . "<br>";
$totalsum = $bucket[2] + $totalsum;
//echo "TOTAL SUM: $totalsum<br>";
}
echo "TOTAL SUM: $totalsum<br>";
}
$bigramset = array_diff($bigramset[$i], $finalwordset);
//if ($counter ==1) break;
$i++;
}
//echo"<pre>";
//print_r($newarray);
//echo "</pre>";
echo "</td>";
echo "</tr>";
echo "</table>";