Skip to content

Commit

Permalink
enable local storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Roger Braun committed Feb 20, 2012
1 parent b7bbb83 commit f01203d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
26 changes: 25 additions & 1 deletion filter.js
Expand Up @@ -16,16 +16,36 @@ Array.prototype.uniq = function(){
,[]);
}

BayesFilter = function() {
BayesFilter = function(useLocalStorage) {

// Variables
this.klasses = {};
this.data = {};
this.assumedProbability = 0.5;
this.assumedProbabilityWeight = 1;
this.documentCount = 0;
this.useLocalStorage = !!useLocalStorage;

// Local Storage

this.loadFromLocalStorage = function(){
var data = window.localStorage.getItem("BayesFilterData");
if(data){
this.data = JSON.parse(data);
}
}

this.saveToLocalStorage = function(){
var dataJSON = JSON.stringify(this.data);
window.localStorage.setItem("BayesFilterData", dataJSON);
}

if(this.useLocalStorage){
this.loadFromLocalStorage();
}

// Helpers
// May not really be thought out too well...
this.helpers = {};

this.helpers.getWordSet = function(text) {
Expand Down Expand Up @@ -126,6 +146,10 @@ BayesFilter = function() {
this.data = this.helpers.addWordSet(this.data, words, klass);
this.klasses = this.helpers.addKlass(this.klasses, klass);
this.documentCount += 1;

if(this.useLocalStorage){
this.saveToLocalStorage();
}
return this;
};

Expand Down
2 changes: 1 addition & 1 deletion script.js
@@ -1,6 +1,6 @@
$.domReady(function() {

window.filter = new BayesFilter();
window.filter = new BayesFilter(true); // Use local storage


var currentData = $("#currentData")[0];
Expand Down

0 comments on commit f01203d

Please sign in to comment.