Skip to content

Instantly share code, notes, and snippets.

@booyaa
Created April 8, 2013 17:30
Show Gist options
  • Select an option

  • Save booyaa/5338667 to your computer and use it in GitHub Desktop.

Select an option

Save booyaa/5338667 to your computer and use it in GitHub Desktop.
porter-stemmer test
var stemmer = require('porter-stemmer').stemmer;
var sentence = "bar. barred. bar it! barred! go bar yourself! foo-head";
var blockList = ['foo', 'bar'];
function fillString(size, char) {
str = "";
for(var i=0; i<size; i++) {
str += char;
}
return str;
}
function replacer(str, p1, offset, s) {
var index = blockList.indexOf(stemmer(p1));
if (index === -1) {
return str;
} else {
return p1.substring(0,1) + fillString(p1.length-1,'*');
}
}
console.log("blockList: %s\noriginal: %s\nreplaced: %s\n", blockList, sentence, sentence.replace(/(\w+)/gi, replacer));
===================
blockList: foo,bar
original: bar. barred. bar it! barred! go bar yourself! foo-head
replaced: b**. b*****. b** it! b*****! go b** yourself! f**-head
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment