Last active
January 11, 2016 09:15
-
-
Save lungati/498b3e14206f03828861 to your computer and use it in GitHub Desktop.
javascript function to count words and order by occurence
This file contains 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
//instructions: run in the console of your browser with index page open | |
var i,x, | |
dict = [], | |
uniqueDict = [], | |
txt = document.body.textContent.toLowerCase().replace(/[&\/\\#,+()$~%.'":*?<>{}(\d+)\[\]]|[\n$]/gmi,'').trim(), | |
txt2 = txt.split(' '); | |
txt2.forEach(function (element, index) { | |
if(element.length>0){ | |
reg = new RegExp('\\b'+element+'\\b','gi'); | |
occur = (txt.split(reg).length - 1); | |
// normal sort | |
dict.push([element,occur]); | |
dict.sort(function(a,b) {return b[1]-a[1]}); | |
} | |
}); | |
//Objects in JavaScript are hashes | |
function eliminateDuplicates(arr) { | |
var i,len=arr.length,out=[],obj={}; | |
for (i=0;i<len;i++) { | |
obj[arr[i]]=0; | |
} | |
for (i in obj) { | |
out.push(i); | |
} | |
return out; | |
} | |
uniqueDict = eliminateDuplicates(dict); | |
//console.log(uniqueDict); | |
console.log(document.body.innerHTML); | |
var output ='<ol>'; | |
for (var a in uniqueDict){ | |
output += '<li>'; | |
output +=/*(parseInt(a)+1)+'. '+*/uniqueDict[a]; | |
output +='</li>'; | |
} | |
output +='</ol>'; | |
document.getElementById('result').innerHTML=output; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment