More work on inverted indexes. I think we're getting there.

This commit is contained in:
Starbeamrainbowlabs 2015-10-28 14:31:27 +00:00
parent 777a27064e
commit 29acc3a5e2
4 changed files with 319 additions and 103 deletions

View File

@ -657,7 +657,71 @@ else
header("x-pageindex-decode-time: " . round(microtime(true) - $pageindex_read_start, 6) . "ms"); header("x-pageindex-decode-time: " . round(microtime(true) - $pageindex_read_start, 6) . "ms");
} }
// Work around an Opera + Syntaxtic bug where there is no margin at the left hand side if there isn't a query string when accessing a .php file //////////////////////////
///// Page id system /////
//////////////////////////
if(!file_exists("idindex.json"))
file_put_contents("idindex.json", "{}");
$idindex = json_decode(file_get_contents("idindex.json"));
class ids
{
/*
* @summary Gets the page id associated with the given pagename.
*/
public static function getid($pagename)
{
global $idindex;
foreach ($idindex as $id => $entry)
{
if($entry == $pagename)
return $id;
}
// This pagename doesn't have an id - assign it one quick!
return self::assign($pagename);
}
/*
* @summary Gets the page name associated with the given page id.
*/
public static function getpagename($id)
{
global $idindex;
if(!isset($idindex->$id))
return false;
else
return $idindex->$id;
}
/*
* @summary Assigns an id to a pagename. Doesn't check to make sure that
* pagename doesn't exist in the pageindex.
*/
protected static function assign($pagename)
{
global $idindex;
$nextid = count(array_keys(get_object_vars($idindex)));
if(isset($idindex->$nextid))
throw new Exception("The pageid is corrupt! Pepperminty Wiki generated the id $nextid, but that id is already in use.");
// Update the id index
$idindex->$nextid = utf8_encode($pagename);
// Save the id index
file_put_contents("idindex.json", json_encode($idindex));
return $nextid;
}
}
//////////////////////////
//////////////////////////
// Work around an Opera + Syntaxtic bug where there is no margin at the left
// hand side if there isn't a query string when accessing a .php file.
if(!isset($_GET["action"]) and !isset($_GET["page"])) if(!isset($_GET["action"]) and !isset($_GET["page"]))
{ {
http_response_code(302); http_response_code(302);
@ -1277,47 +1341,59 @@ register_module([
class search class search
{ {
// Words that we should exclude from the inverted index.
public static $stop_words = [ public static $stop_words = [
"a", "about", "above", "above", "across", "after", "afterwards", "again", "against", "a", "about", "above", "above", "across", "after", "afterwards", "again",
"all", "almost", "alone", "along", "already", "also", "although", "always", "am", "among", "against", "all", "almost", "alone", "along", "already", "also",
"amongst", "amoungst", "amount", "an", "and", "another", "any", "anyhow", "anyone", "anything", "although", "always", "am", "among", "amongst", "amoungst", "amount",
"anyway", "anywhere", "are", "around", "as", "at", "back", "be", "became", "because", "become", "an", "and", "another", "any", "anyhow", "anyone", "anything", "anyway",
"becomes", "becoming", "been", "before", "beforehand", "behind", "being", "below", "beside", "anywhere", "are", "around", "as", "at", "back", "be", "became",
"besides", "between", "beyond", "bill", "both", "bottom", "but", "by", "call", "can", "cannot", "because", "become", "becomes", "becoming", "been", "before",
"cant", "co", "con", "could", "couldnt", "cry", "de", "describe", "detail", "do", "done", "down", "beforehand", "behind", "being", "below", "beside", "besides",
"due", "during", "each", "eg", "eight", "either", "eleven", "else", "elsewhere", "empty", "between", "beyond", "bill", "both", "bottom", "but", "by", "call",
"enough", "etc", "even", "ever", "every", "everyone", "everything", "everywhere", "except", "can", "cannot", "cant", "co", "con", "could", "couldnt", "cry", "de",
"few", "fifteen", "fify", "fill", "find", "fire", "first", "five", "for", "former", "formerly", "describe", "detail", "do", "done", "down", "due", "during", "each",
"forty", "found", "four", "from", "front", "full", "further", "get", "give", "go", "had", "has", "eg", "eight", "either", "eleven", "else", "elsewhere", "empty",
"hasnt", "have", "he", "hence", "her", "here", "hereafter", "hereby", "herein", "hereupon", "enough", "etc", "even", "ever", "every", "everyone", "everything",
"hers", "herself", "him", "himself", "his", "how", "however", "hundred", "ie", "if", "in", "everywhere", "except", "few", "fifteen", "fify", "fill", "find",
"inc", "indeed", "interest", "into", "is", "it", "its", "itself", "keep", "last", "latter", "fire", "first", "five", "for", "former", "formerly", "forty", "found",
"latterly", "least", "less", "ltd", "made", "many", "may", "me", "meanwhile", "might", "mill", "four", "from", "front", "full", "further", "get", "give", "go", "had",
"mine", "more", "moreover", "most", "mostly", "move", "much", "must", "my", "myself", "name", "has", "hasnt", "have", "he", "hence", "her", "here", "hereafter",
"namely", "neither", "never", "nevertheless", "next", "nine", "no", "nobody", "none", "noone", "hereby", "herein", "hereupon", "hers", "herself", "him", "himself",
"nor", "not", "nothing", "now", "nowhere", "of", "off", "often", "on", "once", "one", "only", "his", "how", "however", "hundred", "ie", "if", "in", "inc", "indeed",
"onto", "or", "other", "others", "otherwise", "our", "ours", "ourselves", "out", "over", "own", "interest", "into", "is", "it", "its", "itself", "keep", "last",
"part", "per", "perhaps", "please", "put", "rather", "re", "same", "see", "seem", "seemed", "latter", "latterly", "least", "less", "ltd", "made", "many", "may",
"seeming", "seems", "serious", "several", "she", "should", "show", "side", "since", "sincere", "me", "meanwhile", "might", "mine", "more", "moreover", "most",
"six", "sixty", "so", "some", "somehow", "someone", "something", "sometime", "sometimes", "mostly", "move", "much", "must", "my", "myself", "name", "namely",
"somewhere", "still", "such", "system", "take", "ten", "than", "that", "the", "their", "them", "neither", "never", "nevertheless", "next", "nine", "no", "none",
"themselves", "then", "thence", "there", "thereafter", "thereby", "therefore", "therein", "nor", "not", "nothing", "now", "nowhere", "of", "off", "often", "on",
"thereupon", "these", "they", "thickv", "thin", "third", "this", "those", "though", "three", "once", "one", "only", "onto", "or", "other", "others", "otherwise",
"through", "throughout", "thru", "thus", "to", "together", "too", "top", "toward", "towards", "our", "ours", "ourselves", "out", "over", "own", "part", "per",
"twelve", "twenty", "two", "un", "under", "until", "up", "upon", "us", "very", "via", "was", "perhaps", "please", "put", "rather", "re", "same", "see", "seem",
"we", "well", "were", "what", "whatever", "when", "whence", "whenever", "where", "whereafter", "seemed", "seeming", "seems", "serious", "several", "she", "should",
"whereas", "whereby", "wherein", "whereupon", "wherever", "whether", "which", "while", "whither", "show", "side", "since", "sincere", "six", "sixty", "so", "some",
"who", "whoever", "whole", "whom", "whose", "why", "will", "with", "within", "without", "would", "somehow", "someone", "something", "sometime", "sometimes",
"yet", "you", "your", "yours", "yourself", "yourselves" "somewhere", "still", "such", "system", "take", "ten", "than", "that",
"the", "their", "them", "themselves", "then", "thence", "there",
"thereafter", "thereby", "therefore", "therein", "thereupon", "these",
"they", "thickv", "thin", "third", "this", "those", "though", "three",
"through", "throughout", "thru", "thus", "to", "together", "too", "top",
"toward", "towards", "twelve", "twenty", "two", "un", "under", "until",
"up", "upon", "us", "very", "via", "was", "we", "well", "were", "what",
"whatever", "when", "whence", "whenever", "where", "whereafter",
"whereas", "whereby", "wherein", "whereupon", "wherever", "whether",
"which", "while", "whither", "who", "whoever", "whole", "whom", "whose",
"why", "will", "with", "within", "without", "would", "yet", "you",
"your", "yours", "yourself", "yourselves"
]; ];
public static function index($source) public static function index($source)
{ {
$source = html_entity_decode($source, ENT_QUOTES); $source = html_entity_decode($source, ENT_QUOTES);
$source_length = strlen($source); $source_length = strlen($source);
$index = []; $index = [];
// Regex from // Regex from
$terms = preg_split("/((^\p{P}+)|(\p{P}*\s+\p{P}*)|(\p{P}+$))/", $source, -1, PREG_SPLIT_NO_EMPTY); $terms = preg_split("/((^\p{P}+)|(\p{P}*\s+\p{P}*)|(\p{P}+$))/", $source, -1, PREG_SPLIT_NO_EMPTY);
$i = 0; $i = 0;
@ -1332,47 +1408,79 @@ class search
{ {
$index[$nterm] = [ "freq" => 0, "offsets" => [] ]; $index[$nterm] = [ "freq" => 0, "offsets" => [] ];
} }
$index[$nterm]["freq"]++; $index[$nterm]["freq"]++;
$index[$nterm]["offsets"][] = $i; $index[$nterm]["offsets"][] = $i;
$i++; $i++;
} }
self::sort_index($index);
return $index; return $index;
} }
/* /*
* @sumary Sorts an index alphabetically. This allows us to do a binary search instead of a regular sequential search. * @summary Sorts an index alphabetically. Will also sort an inverted index.
* This allows us to do a binary search instead of a regular
* sequential search.
*/ */
public static function sort_index(&$index) public static function sort_index(&$index)
{ {
ksort($index, SORT_NATURAL); ksort($index, SORT_NATURAL);
} }
}
class ids
{
/* /*
* @summary Gets the page id associated with the given pagename. * @summary Compares two *regular* indexes to find the differences between them.
*
* @param {array} $indexa - The old index.
* @param {array} $indexb - The new index.
* @param {array} $changed - An array to be filled with the nterms of all
* the changed entries.
* @param {array} $removed - An array to be filled with the nterms of all
* the removed entries.
*/ */
public static function get_page_id($pagename) public static function compare_indexes($indexa, $indexb, &$changed, &$removed)
{ {
global $idindex; foreach ($indexa as $nterm => $entrya)
{
if(!isset($indexb[$nterm]))
$removed[] = $nterm;
$entryb = $indexb[$nterm];
if($entrya !== $entryb) $changed[] = $nterm;
}
} }
/* /*
* @summary Gets the page name associated with the given page id. * @summary Reads in and parses an inverted index.
*/ */
public static function get_id_pagename($id) // Todo remove this function and make everything streamable
public static function parse_invindex($invindex_filename) {
$invindex = json_decode(file_get_contents($invindex_filename), true);
return $invindex;
}
/*
* @summary Merge an index into an inverted index.
*/
public static function merge_into_invindex(&$invindex, $pageid, &$index, &$removals = [])
{ {
global $idindex; // Remove all the subentries that were removed since last time
foreach($removals as $nterm)
{
unset($invindex[$nterm][$pageid]);
}
// Merge all the new / changed index entries into the inverted index
foreach($index as $nterm => $newentry)
{
// If the nterm isn't in the inverted index, then create a space for it
if(!isset($invindex[$nterm])) $invindex[$nterm] = [];
$invindex[$nterm][$pageid] = $newentry;
}
}
public static function save_invindex($filename, &$invindex)
{
file_put_contents($filename, json_encode($invindex));
} }
} }

View File

@ -348,7 +348,71 @@ else
header("x-pageindex-decode-time: " . round(microtime(true) - $pageindex_read_start, 6) . "ms"); header("x-pageindex-decode-time: " . round(microtime(true) - $pageindex_read_start, 6) . "ms");
} }
// Work around an Opera + Syntaxtic bug where there is no margin at the left hand side if there isn't a query string when accessing a .php file //////////////////////////
///// Page id system /////
//////////////////////////
if(!file_exists("idindex.json"))
file_put_contents("idindex.json", "{}");
$idindex = json_decode(file_get_contents("idindex.json"));
class ids
{
/*
* @summary Gets the page id associated with the given pagename.
*/
public static function getid($pagename)
{
global $idindex;
foreach ($idindex as $id => $entry)
{
if($entry == $pagename)
return $id;
}
// This pagename doesn't have an id - assign it one quick!
return self::assign($pagename);
}
/*
* @summary Gets the page name associated with the given page id.
*/
public static function getpagename($id)
{
global $idindex;
if(!isset($idindex->$id))
return false;
else
return $idindex->$id;
}
/*
* @summary Assigns an id to a pagename. Doesn't check to make sure that
* pagename doesn't exist in the pageindex.
*/
protected static function assign($pagename)
{
global $idindex;
$nextid = count(array_keys(get_object_vars($idindex)));
if(isset($idindex->$nextid))
throw new Exception("The pageid is corrupt! Pepperminty Wiki generated the id $nextid, but that id is already in use.");
// Update the id index
$idindex->$nextid = utf8_encode($pagename);
// Save the id index
file_put_contents("idindex.json", json_encode($idindex));
return $nextid;
}
}
//////////////////////////
//////////////////////////
// Work around an Opera + Syntaxtic bug where there is no margin at the left
// hand side if there isn't a query string when accessing a .php file.
if(!isset($_GET["action"]) and !isset($_GET["page"])) if(!isset($_GET["action"]) and !isset($_GET["page"]))
{ {
http_response_code(302); http_response_code(302);

View File

@ -50,7 +50,7 @@
"author": "Starbeamrainbowlabs", "author": "Starbeamrainbowlabs",
"description": "Adds proper search functionality to Pepperminty Wiki. Note that this module, at the moment, just contains test code while I figure out how best to write a search engine.", "description": "Adds proper search functionality to Pepperminty Wiki. Note that this module, at the moment, just contains test code while I figure out how best to write a search engine.",
"id": "feature-search", "id": "feature-search",
"lastupdate": 1446032086, "lastupdate": 1446042661,
"optional": false "optional": false
}, },
{ {

View File

@ -27,47 +27,59 @@ register_module([
class search class search
{ {
// Words that we should exclude from the inverted index.
public static $stop_words = [ public static $stop_words = [
"a", "about", "above", "above", "across", "after", "afterwards", "again", "against", "a", "about", "above", "above", "across", "after", "afterwards", "again",
"all", "almost", "alone", "along", "already", "also", "although", "always", "am", "among", "against", "all", "almost", "alone", "along", "already", "also",
"amongst", "amoungst", "amount", "an", "and", "another", "any", "anyhow", "anyone", "anything", "although", "always", "am", "among", "amongst", "amoungst", "amount",
"anyway", "anywhere", "are", "around", "as", "at", "back", "be", "became", "because", "become", "an", "and", "another", "any", "anyhow", "anyone", "anything", "anyway",
"becomes", "becoming", "been", "before", "beforehand", "behind", "being", "below", "beside", "anywhere", "are", "around", "as", "at", "back", "be", "became",
"besides", "between", "beyond", "bill", "both", "bottom", "but", "by", "call", "can", "cannot", "because", "become", "becomes", "becoming", "been", "before",
"cant", "co", "con", "could", "couldnt", "cry", "de", "describe", "detail", "do", "done", "down", "beforehand", "behind", "being", "below", "beside", "besides",
"due", "during", "each", "eg", "eight", "either", "eleven", "else", "elsewhere", "empty", "between", "beyond", "bill", "both", "bottom", "but", "by", "call",
"enough", "etc", "even", "ever", "every", "everyone", "everything", "everywhere", "except", "can", "cannot", "cant", "co", "con", "could", "couldnt", "cry", "de",
"few", "fifteen", "fify", "fill", "find", "fire", "first", "five", "for", "former", "formerly", "describe", "detail", "do", "done", "down", "due", "during", "each",
"forty", "found", "four", "from", "front", "full", "further", "get", "give", "go", "had", "has", "eg", "eight", "either", "eleven", "else", "elsewhere", "empty",
"hasnt", "have", "he", "hence", "her", "here", "hereafter", "hereby", "herein", "hereupon", "enough", "etc", "even", "ever", "every", "everyone", "everything",
"hers", "herself", "him", "himself", "his", "how", "however", "hundred", "ie", "if", "in", "everywhere", "except", "few", "fifteen", "fify", "fill", "find",
"inc", "indeed", "interest", "into", "is", "it", "its", "itself", "keep", "last", "latter", "fire", "first", "five", "for", "former", "formerly", "forty", "found",
"latterly", "least", "less", "ltd", "made", "many", "may", "me", "meanwhile", "might", "mill", "four", "from", "front", "full", "further", "get", "give", "go", "had",
"mine", "more", "moreover", "most", "mostly", "move", "much", "must", "my", "myself", "name", "has", "hasnt", "have", "he", "hence", "her", "here", "hereafter",
"namely", "neither", "never", "nevertheless", "next", "nine", "no", "nobody", "none", "noone", "hereby", "herein", "hereupon", "hers", "herself", "him", "himself",
"nor", "not", "nothing", "now", "nowhere", "of", "off", "often", "on", "once", "one", "only", "his", "how", "however", "hundred", "ie", "if", "in", "inc", "indeed",
"onto", "or", "other", "others", "otherwise", "our", "ours", "ourselves", "out", "over", "own", "interest", "into", "is", "it", "its", "itself", "keep", "last",
"part", "per", "perhaps", "please", "put", "rather", "re", "same", "see", "seem", "seemed", "latter", "latterly", "least", "less", "ltd", "made", "many", "may",
"seeming", "seems", "serious", "several", "she", "should", "show", "side", "since", "sincere", "me", "meanwhile", "might", "mine", "more", "moreover", "most",
"six", "sixty", "so", "some", "somehow", "someone", "something", "sometime", "sometimes", "mostly", "move", "much", "must", "my", "myself", "name", "namely",
"somewhere", "still", "such", "system", "take", "ten", "than", "that", "the", "their", "them", "neither", "never", "nevertheless", "next", "nine", "no", "none",
"themselves", "then", "thence", "there", "thereafter", "thereby", "therefore", "therein", "nor", "not", "nothing", "now", "nowhere", "of", "off", "often", "on",
"thereupon", "these", "they", "thickv", "thin", "third", "this", "those", "though", "three", "once", "one", "only", "onto", "or", "other", "others", "otherwise",
"through", "throughout", "thru", "thus", "to", "together", "too", "top", "toward", "towards", "our", "ours", "ourselves", "out", "over", "own", "part", "per",
"twelve", "twenty", "two", "un", "under", "until", "up", "upon", "us", "very", "via", "was", "perhaps", "please", "put", "rather", "re", "same", "see", "seem",
"we", "well", "were", "what", "whatever", "when", "whence", "whenever", "where", "whereafter", "seemed", "seeming", "seems", "serious", "several", "she", "should",
"whereas", "whereby", "wherein", "whereupon", "wherever", "whether", "which", "while", "whither", "show", "side", "since", "sincere", "six", "sixty", "so", "some",
"who", "whoever", "whole", "whom", "whose", "why", "will", "with", "within", "without", "would", "somehow", "someone", "something", "sometime", "sometimes",
"yet", "you", "your", "yours", "yourself", "yourselves" "somewhere", "still", "such", "system", "take", "ten", "than", "that",
"the", "their", "them", "themselves", "then", "thence", "there",
"thereafter", "thereby", "therefore", "therein", "thereupon", "these",
"they", "thickv", "thin", "third", "this", "those", "though", "three",
"through", "throughout", "thru", "thus", "to", "together", "too", "top",
"toward", "towards", "twelve", "twenty", "two", "un", "under", "until",
"up", "upon", "us", "very", "via", "was", "we", "well", "were", "what",
"whatever", "when", "whence", "whenever", "where", "whereafter",
"whereas", "whereby", "wherein", "whereupon", "wherever", "whether",
"which", "while", "whither", "who", "whoever", "whole", "whom", "whose",
"why", "will", "with", "within", "without", "would", "yet", "you",
"your", "yours", "yourself", "yourselves"
]; ];
public static function index($source) public static function index($source)
{ {
$source = html_entity_decode($source, ENT_QUOTES); $source = html_entity_decode($source, ENT_QUOTES);
$source_length = strlen($source); $source_length = strlen($source);
$index = []; $index = [];
// Regex from // Regex from
$terms = preg_split("/((^\p{P}+)|(\p{P}*\s+\p{P}*)|(\p{P}+$))/", $source, -1, PREG_SPLIT_NO_EMPTY); $terms = preg_split("/((^\p{P}+)|(\p{P}*\s+\p{P}*)|(\p{P}+$))/", $source, -1, PREG_SPLIT_NO_EMPTY);
$i = 0; $i = 0;
@ -89,40 +101,72 @@ class search
$i++; $i++;
} }
self::sort_index($index);
return $index; return $index;
} }
/* /*
* @sumary Sorts an index alphabetically. This allows us to do a binary search instead of a regular sequential search. * @summary Sorts an index alphabetically. Will also sort an inverted index.
* This allows us to do a binary search instead of a regular
* sequential search.
*/ */
public static function sort_index(&$index) public static function sort_index(&$index)
{ {
ksort($index, SORT_NATURAL); ksort($index, SORT_NATURAL);
} }
}
class ids
{
/* /*
* @summary Gets the page id associated with the given pagename. * @summary Compares two *regular* indexes to find the differences between them.
*
* @param {array} $indexa - The old index.
* @param {array} $indexb - The new index.
* @param {array} $changed - An array to be filled with the nterms of all
* the changed entries.
* @param {array} $removed - An array to be filled with the nterms of all
* the removed entries.
*/ */
public static function get_page_id($pagename) public static function compare_indexes($indexa, $indexb, &$changed, &$removed)
{ {
global $idindex; foreach ($indexa as $nterm => $entrya)
{
if(!isset($indexb[$nterm]))
$removed[] = $nterm;
$entryb = $indexb[$nterm];
if($entrya !== $entryb) $changed[] = $nterm;
}
} }
/* /*
* @summary Gets the page name associated with the given page id. * @summary Reads in and parses an inverted index.
*/ */
public static function get_id_pagename($id) // Todo remove this function and make everything streamable
public static function parse_invindex($invindex_filename) {
$invindex = json_decode(file_get_contents($invindex_filename), true);
return $invindex;
}
/*
* @summary Merge an index into an inverted index.
*/
public static function merge_into_invindex(&$invindex, $pageid, &$index, &$removals = [])
{ {
global $idindex; // Remove all the subentries that were removed since last time
foreach($removals as $nterm)
{
unset($invindex[$nterm][$pageid]);
}
// Merge all the new / changed index entries into the inverted index
foreach($index as $nterm => $newentry)
{
// If the nterm isn't in the inverted index, then create a space for it
if(!isset($invindex[$nterm])) $invindex[$nterm] = [];
$invindex[$nterm][$pageid] = $newentry;
}
}
public static function save_invindex($filename, &$invindex)
{
file_put_contents($filename, json_encode($invindex));
} }
} }