开发者社区> 问答> 正文

如何查找一篇英文文章中出现频率最高的单词?

如何查找一篇英文文章中出现频率最高的单词?

展开
收起
请回答1024 2020-04-08 12:40:32 1250 0
2 条回答
写回答
取消 提交回答
  • 有点尴尬唉 你要寻找的东西已经被吃掉啦!

    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;");

    2020-04-08 21:35:51
    赞同 展开评论 打赏
  • function findMostWord(article) { // 合法性判断 if (!article) return;

    // 参数处理 article = article.trim().toLowerCase();

    let wordList = article.match(/[a-z]+/g), visited = [], maxNum = 0, maxWord = "";

    article = " " + wordList.join(" ") + " ";

    // 遍历判断单词出现次数 wordList.forEach(function(item) { if (visited.indexOf(item) < 0) { let word = new RegExp(" " + item + " ", "g"), num = article.match(word).length;

      if (num > maxNum) {
        maxNum = num;
        maxWord = item;
      }
    }
    

    });

    2020-04-08 12:40:38
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载