Last active
January 2, 2016 08:39
-
-
Save coderek/8278242 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Text Search</title> | |
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script> | |
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.2/underscore-min.js"></script> | |
<link rel="stylesheet" type="text/css" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css"> | |
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script> | |
</head> | |
<body> | |
<div class="container"> | |
<table class="db table"></table> | |
<h5>Search: </h5> | |
<div class="form-inline" role="form"> | |
<div class="form-group"> | |
<input class="form-control"> | |
</div> | |
<div class="form-group"> | |
<div class="btn btn-primary form-control">Search</div> | |
</div> | |
</div> | |
<h5>Outputs: </h5> | |
<ol class="output"></ol> | |
</div> | |
<script> | |
var db = [ | |
{content: "I love cats. Cats are awesome"}, | |
{content: "Dogs are loyal. My dogs are protecting my home"}, | |
{content: "I love animals, especially cats and dogs"}, | |
{content: "I love bats."} | |
]; | |
var index = []; | |
var binaryIndexOf = function(searchElement) { | |
var minIndex = 0; | |
var maxIndex = index.length - 1; | |
var currentIndex; | |
var currentElement; | |
while (minIndex <= maxIndex) { | |
currentIndex = (minIndex + maxIndex) / 2 | 0; | |
currentElement = index[currentIndex][0]; | |
if (currentElement < searchElement) { | |
minIndex = currentIndex + 1; | |
} | |
else if (currentElement > searchElement) { | |
maxIndex = currentIndex - 1; | |
} | |
else { | |
return currentIndex; | |
} | |
} | |
return -1; | |
} | |
var indexing = function () { | |
_.each(db, function (entry, i) { | |
var words = _.uniq( | |
_.map( | |
_.compact(entry.content.split(/[\W]/)), function (e) { | |
return [e.toLowerCase(), i]; | |
}) | |
); | |
_.each(words, function (w) { | |
index.push(w); | |
}); | |
// index and sort by individual word | |
index = _.sortBy(index, function (item) { | |
return item[0]; | |
}); | |
}); | |
}; | |
// now the db is indexed | |
var search = function (term) { | |
term = term.toLowerCase(); | |
var found = binaryIndexOf(term); | |
if (found == -1) { | |
return ["not found"]; | |
} | |
var allFound = [found]; | |
var regx = new RegExp(term); | |
// look before it | |
for (var i = found-1; i >= 0; i--) { | |
if (regx.test(index[i][0])) { | |
allFound.push(i); | |
} | |
} | |
// look after it | |
for (var i = found+1; i < index.length; i++) { | |
if (regx.test(index[i][0])) { | |
allFound.push(i); | |
} | |
} | |
// convert to indexed item | |
allFound = _.map(allFound, function (a) {return index[a];}); | |
// group it, get key:count pairs and sort by occurence | |
allFound = _.groupBy(allFound, function (a) {return a[1];}); | |
allFound = _.map(allFound, function (val, key) { | |
return [key, val.length]; | |
}); | |
allFound = _.sortBy(allFound, function (a) { return -a[1];}); | |
return _.map(allFound, function (a) {return db[a[0]].content}); | |
}; | |
// UI | |
$(function () { | |
// build index or db | |
indexing(); | |
// display db data in a table | |
_.each(db, function (d, i) { | |
$(".db").append("<tr><td>"+i+"</td><td>"+d.content+"</td></tr>") | |
}); | |
$("input").keydown(function (ev) { | |
if (ev.keyCode == 13) $(".btn").click(); | |
}); | |
$(".btn").click(function () { | |
var term = $("input").val(); | |
var entries = search(term); | |
$(".output").empty(); | |
_.each(entries, function (e) { | |
$(".output").append("<li>"+e+"</li>"); | |
return false; | |
}); | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment