JS從一段英文單詞中,找出出現次數最多的前10個單詞


一、JS查找一篇英文文章中出現頻率最高的單詞

下面這個函數是js查找一篇英文文章中出現頻率最高的單詞(由26個英文字母大小寫構成),輸出該單詞及出現次數,不區分大小寫,主要是正則的運用:

function counts(article){
    article = article.trim().toUpperCase();
    var array = article.match(/[A-z]+/g);
    article = " "+array.join("  ")+" ";
    var max = 0,word,num = 0,maxword="";
    for(var i = 0; i < array.length; i++) {        
        word = new RegExp(" "+array[i]+" ",'g');
    num = article.match(word).length;
    if(num>max){
        max=num;
        maxword = array[i];
    }
   }
   console.log(maxword+" "+max);
}
counts("Age has reached the end of the beginning of a word. May be guilty in his seems to passing a lot of different life became the appearance of the same day;");


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM