I'm using to php to make a simple search engine that is base on the boolean retrieval I have predefined documents for example: - doc0: my name is caesar - doc1: caesar is character... -doc2.....
I've constructed the term-document matrix as follows :
so for example "my" exists in the first documents , but not in the second and "caesar" is in the both documents when I do a search for a single term i get the boolean values of the term thus if I type "name" in the search engine i'll get 1 0 .
my problem is that I want to search for mor than one term e.g: "caesar character" I want to make a boolean and between their boolean values thus the result of the search will be 0 1 as 1 1 & 0 1 = 01 So I need php function that make me the search for more than one word using the mentioned method
This is the code $query = $_REQUEST['keyword']; $stoplists = array("i",".","a"," "); $words=array(); $wordsdoc=array(); $matrix=array(); $docs = array ("my name is caesar","caesar is a character"); $k=0;
//looping the docs array
for ($i=0;$i<sizeof($docs);$i++)
{
//splitting doc[i] on " " (space)
$words_temp=explode(" ",$docs[$i]);
//looping the splitted words
for ($j=0;$j<sizeof($words_temp);$j++)
{
//checking if the word is not in stop dictionnary and does not already added in words array
if (!in_array($words_temp[$j],$stoplists) && !in_array($words_temp[$j],$words))
{
//adding word to words array
$words[$k]=$words_temp[$j];
//incrementing counter
$k++;
}
}
//print_r($words[1]);
}
echo "<b>Words:</b> ";
for ($j=0;$j<sizeof($words);$j++)
{
echo $words[$j]." ";
}
echo "<br><br>";
//looping the docs array
for($i=0;$i<sizeof($docs);$i++)
{
//splitting doc[i] on " " (space)
$words_temp=explode(" ",$docs[$i]);
//initialize counter
$l=0;
//looping the splitted words
for ($j=0;$j<sizeof($words_temp);$j++)
{
//checking if the word is not in stop dictionnary
if (!in_array($words_temp[$j],$stoplists) )
{
//adding word to 2d array
$wordsdoc[$i][$l]=$words_temp[$j];
//incrementing counter
$l++;
}
}
}
echo "<b><u>Docs:</u></b><br>";
for($i=0;$i<sizeof($wordsdoc);$i++)
{
echo "doc".$i.": ";
for($j=0;$j<sizeof($wordsdoc[$i]);$j++)
{
echo $wordsdoc[$i][$j]." ";
}
echo "<br>";
}
echo "<br>";
echo "<b>Res Matrix First Col:</b><br>";
for($i=0;$i<sizeof($words);$i++)
{
$matrix[$i][0]=$words[$i];
echo $matrix[$i][0]."<br>";
}
$i1=0;
$i2=0;
foreach($wordsdoc as $items)
{
for($i=0;$i<sizeof($words);$i++)
{
if(in_array($matrix[$i][0],$items))
$matrix[$i][$i1+1] = 1;
else
$matrix[$i][$i1+1] =0;
}
$i1++;
}
echo "<table border=1><br>";
echo "<tr><td></td>";
for($i=0;$i<sizeof($docs);$i++)
{
echo "<td>doc".($i+1)."</td>";
}
echo "</tr><br>";
foreach($matrix as $items)
{
echo "<tr>";
foreach($items as $item)
{
echo "<td>".$item."</td>";
}
echo "</tr><br>";
}
echo "</table><br>";
*I'm sorry for posting such a long question, but I really need help :S * thank You guys in advance :)
function search($word) {
... code to query the matrix ...
return $result_array;
}
use with multiple words
$search_terms = array('my', 'caesar');
$overall_result = array(true, true);
foreach($search_terms as $st) {
$this_result = search($st);
$overal_index = 0;
foreach($this_result as $b) {
$overall_result[$overal_index] = $b && $overall_result[$overal_index];
$overal_index++;
}
}