Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


Release 1.0
[palacios.git] / misc / decoder_test / XED2 / doc / html / search.php
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
3 <title>XED2: XED2 User Guide - Thu May 15 03:15:09 2008 </title>
4 <link href="doxygen.css" rel="stylesheet" type="text/css">
5 <link href="tabs.css" rel="stylesheet" type="text/css">
6 </head><body>
7 <!-- Generated by Doxygen 1.4.6 -->
8 <div class="tabs">
9   <ul>
10     <li><a href="main.html"><span>Main&nbsp;Page</span></a></li>
11     <li><a href="modules.html"><span>Modules</span></a></li>
12     <li><a href="classes.html"><span>Data&nbsp;Structures</span></a></li>
13     <li><a href="files.html"><span>Files</span></a></li>
14     <li>
15       <form action="search.php" method="get">
16         <table cellspacing="0" cellpadding="0" border="0">
17           <tr>
18             <td><label>&nbsp;<u>S</u>earch&nbsp;for&nbsp;</label></td>
19
20 <?php
21
22 function search_results()
23 {
24   return "Search Results";
25 }
26
27 function matches_text($num)
28 {
29   if ($num==0)
30   {
31     return "Sorry, no documents matching your query.";
32   }
33   else if ($num==1)
34   {
35     return "Found <b>1</b> document matching your query.";
36   }
37   else // $num>1
38   {
39     return "Found <b>$num</b> documents matching your query. Showing best matches first.";
40   }
41 }
42
43 function report_matches()
44 {
45   return "Matches: ";
46 }
47 function end_form($value)
48 {
49   echo "            <td><input type=\"text\" name=\"query\" value=\"$value\" size=\"20\" accesskey=\"s\"/></td>\n          </tr>\n        </table>\n      </form>\n    </li>\n  </ul>\n</div>\n";
50 }
51
52 function readInt($file)
53 {
54   $b1 = ord(fgetc($file)); $b2 = ord(fgetc($file));
55   $b3 = ord(fgetc($file)); $b4 = ord(fgetc($file));
56   return ($b1<<24)|($b2<<16)|($b3<<8)|$b4;
57 }
58
59 function readString($file)
60 {
61   $result="";
62   while (ord($c=fgetc($file))) $result.=$c;
63   return $result;
64 }
65
66 function readHeader($file)
67 {
68   $header =fgetc($file); $header.=fgetc($file);
69   $header.=fgetc($file); $header.=fgetc($file);
70   return $header;
71 }
72
73 function computeIndex($word)
74 {
75   // Fast string hashing
76   //$lword = strtolower($word);
77   //$l = strlen($lword);
78   //for ($i=0;$i<$l;$i++)
79   //{
80   //  $c = ord($lword{$i});
81   //  $v = (($v & 0xfc00) ^ ($v << 6) ^ $c) & 0xffff;
82   //}
83   //return $v;
84
85   // Simple hashing that allows for substring search
86   if (strlen($word)<2) return -1;
87   // high char of the index
88   $hi = ord($word{0});
89   if ($hi==0) return -1;
90   // low char of the index
91   $lo = ord($word{1});
92   if ($lo==0) return -1;
93   // return index
94   return $hi*256+$lo;
95 }
96
97 function search($file,$word,&$statsList)
98 {
99   $index = computeIndex($word);
100   if ($index!=-1) // found a valid index
101   {
102     fseek($file,$index*4+4); // 4 bytes per entry, skip header
103     $index = readInt($file);
104     if ($index) // found words matching the hash key
105     {
106       $start=sizeof($statsList);
107       $count=$start;
108       fseek($file,$index);
109       $w = readString($file);
110       while ($w)
111       {
112         $statIdx = readInt($file);
113         if ($word==substr($w,0,strlen($word)))
114         { // found word that matches (as substring)
115           $statsList[$count++]=array(
116               "word"=>$word,
117               "match"=>$w,
118               "index"=>$statIdx,
119               "full"=>strlen($w)==strlen($word),
120               "docs"=>array()
121               );
122         }
123         $w = readString($file);
124       }
125       $totalHi=0;
126       $totalFreqHi=0;
127       $totalFreqLo=0;
128       for ($count=$start;$count<sizeof($statsList);$count++)
129       {
130         $statInfo = &$statsList[$count];
131         $multiplier = 1;
132         // whole word matches have a double weight
133         if ($statInfo["full"]) $multiplier=2;
134         fseek($file,$statInfo["index"]); 
135         $numDocs = readInt($file);
136         $docInfo = array();
137         // read docs info + occurrence frequency of the word
138         for ($i=0;$i<$numDocs;$i++)
139         {
140           $idx=readInt($file); 
141           $freq=readInt($file); 
142           $docInfo[$i]=array("idx"  => $idx,
143                              "freq" => $freq>>1,
144                              "rank" => 0.0,
145                              "hi"   => $freq&1
146                             );
147           if ($freq&1) // word occurs in high priority doc
148           {
149             $totalHi++;
150             $totalFreqHi+=$freq*$multiplier;
151           }
152           else // word occurs in low priority doc
153           {
154             $totalFreqLo+=$freq*$multiplier;
155           }
156         }
157         // read name and url info for the doc
158         for ($i=0;$i<$numDocs;$i++)
159         {
160           fseek($file,$docInfo[$i]["idx"]);
161           $docInfo[$i]["name"]=readString($file);
162           $docInfo[$i]["url"]=readString($file);
163         }
164         $statInfo["docs"]=$docInfo;
165       }
166       $totalFreq=($totalHi+1)*$totalFreqLo + $totalFreqHi;
167       for ($count=$start;$count<sizeof($statsList);$count++)
168       {
169         $statInfo = &$statsList[$count];
170         $multiplier = 1;
171         // whole word matches have a double weight
172         if ($statInfo["full"]) $multiplier=2;
173         for ($i=0;$i<sizeof($statInfo["docs"]);$i++)
174         {
175           $docInfo = &$statInfo["docs"];
176           // compute frequency rank of the word in each doc
177           $freq=$docInfo[$i]["freq"];
178           if ($docInfo[$i]["hi"])
179           {
180             $statInfo["docs"][$i]["rank"]=
181               (float)($freq*$multiplier+$totalFreqLo)/$totalFreq;
182           }
183           else
184           {
185             $statInfo["docs"][$i]["rank"]=
186               (float)($freq*$multiplier)/$totalFreq;
187           }
188         }
189       }
190     }
191   }
192   return $statsList;
193 }
194
195 function combine_results($results,&$docs)
196 {
197   foreach ($results as $wordInfo)
198   {
199     $docsList = &$wordInfo["docs"];
200     foreach ($docsList as $di)
201     {
202       $key=$di["url"];
203       $rank=$di["rank"];
204       if (in_array($key, array_keys($docs)))
205       {
206         $docs[$key]["rank"]+=$rank;
207       }
208       else
209       {
210         $docs[$key] = array("url"=>$key,
211             "name"=>$di["name"],
212             "rank"=>$rank
213             );
214       }
215       $docs[$key]["words"][] = array(
216                "word"=>$wordInfo["word"],
217                "match"=>$wordInfo["match"],
218                "freq"=>$di["freq"]
219                );
220     }
221   }
222   return $docs;
223 }
224
225 function filter_results($docs,&$requiredWords,&$forbiddenWords)
226 {
227   $filteredDocs=array();
228   while (list ($key, $val) = each ($docs)) 
229   {
230     $words = &$docs[$key]["words"];
231     $copy=1; // copy entry by default
232     if (sizeof($requiredWords)>0)
233     {
234       foreach ($requiredWords as $reqWord)
235       {
236         $found=0;
237         foreach ($words as $wordInfo)
238         { 
239           $found = $wordInfo["word"]==$reqWord;
240           if ($found) break;
241         }
242         if (!$found) 
243         {
244           $copy=0; // document contains none of the required words
245           break;
246         }
247       }
248     }
249     if (sizeof($forbiddenWords)>0)
250     {
251       foreach ($words as $wordInfo)
252       {
253         if (in_array($wordInfo["word"],$forbiddenWords))
254         {
255           $copy=0; // document contains a forbidden word
256           break;
257         }
258       }
259     }
260     if ($copy) $filteredDocs[$key]=$docs[$key];
261   }
262   return $filteredDocs;
263 }
264
265 function compare_rank($a,$b)
266 {
267   if ($a["rank"] == $b["rank"]) 
268   {
269     return 0;
270   }
271   return ($a["rank"]>$b["rank"]) ? -1 : 1; 
272 }
273
274 function sort_results($docs,&$sorted)
275 {
276   $sorted = $docs;
277   usort($sorted,"compare_rank");
278   return $sorted;
279 }
280
281 function report_results(&$docs)
282 {
283   echo "<table cellspacing=\"2\">\n";
284   echo "  <tr>\n";
285   echo "    <td colspan=\"2\"><h2>".search_results()."</h2></td>\n";
286   echo "  </tr>\n";
287   $numDocs = sizeof($docs);
288   if ($numDocs==0)
289   {
290     echo "  <tr>\n";
291     echo "    <td colspan=\"2\">".matches_text(0)."</td>\n";
292     echo "  </tr>\n";
293   }
294   else
295   {
296     echo "  <tr>\n";
297     echo "    <td colspan=\"2\">".matches_text($numDocs);
298     echo "\n";
299     echo "    </td>\n";
300     echo "  </tr>\n";
301     $num=1;
302     foreach ($docs as $doc)
303     {
304       echo "  <tr>\n";
305       echo "    <td align=\"right\">$num.</td>";
306       echo     "<td><a class=\"el\" href=\"".$doc["url"]."\">".$doc["name"]."</a></td>\n";
307       echo "  <tr>\n";
308       echo "    <td></td><td class=\"tiny\">".report_matches()." ";
309       foreach ($doc["words"] as $wordInfo)
310       {
311         $word = $wordInfo["word"];
312         $matchRight = substr($wordInfo["match"],strlen($word));
313         echo "<b>$word</b>$matchRight(".$wordInfo["freq"].") ";
314       }
315       echo "    </td>\n";
316       echo "  </tr>\n";
317       $num++;
318     }
319   }
320   echo "</table>\n";
321 }
322
323 function main()
324 {
325   if(strcmp('4.1.0', phpversion()) > 0) 
326   {
327     die("Error: PHP version 4.1.0 or above required!");
328   }
329   if (!($file=fopen("search.idx","rb"))) 
330   {
331     die("Error: Search index file could NOT be opened!");
332   }
333   if (readHeader($file)!="DOXS")
334   {
335     die("Error: Header of index file is invalid!");
336   }
337   $query="";
338   if (array_key_exists("query", $_GET))
339   {
340     $query=$_GET["query"];
341   }
342   end_form($query);
343   echo "&nbsp;\n<div class=\"searchresults\">\n";
344   $results = array();
345   $requiredWords = array();
346   $forbiddenWords = array();
347   $foundWords = array();
348   $word=strtok($query," ");
349   while ($word) // for each word in the search query
350   {
351     if (($word{0}=='+')) { $word=substr($word,1); $requiredWords[]=$word; }
352     if (($word{0}=='-')) { $word=substr($word,1); $forbiddenWords[]=$word; }
353     if (!in_array($word,$foundWords))
354     {
355       $foundWords[]=$word;
356       search($file,strtolower($word),$results);
357     }
358     $word=strtok(" ");
359   }
360   $docs = array();
361   combine_results($results,$docs);
362   // filter out documents with forbidden word or that do not contain
363   // required words
364   $filteredDocs = filter_results($docs,$requiredWords,$forbiddenWords);
365   // sort the results based on rank
366   $sorted = array();
367   sort_results($filteredDocs,$sorted);
368   // report results to the user
369   report_results($sorted);
370   echo "</div>\n";
371   fclose($file);
372 }
373
374 main();
375
376
377 ?>
378 <hr size="1"><address style="align: right;"><small>Generated on Thu May 15 03:15:09 2008 for XED2 by&nbsp;
379 <a href="http://www.doxygen.org/index.html">
380 <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.6 </small></address>
381 </body>
382 </html>