Created
April 8, 2013 17:30
-
-
Save booyaa/5338667 to your computer and use it in GitHub Desktop.
porter-stemmer test
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
| 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