如何计算每个单词的出现次数?

编程入门 行业动态 更新时间:2024-10-28 01:20:25
本文介绍了如何计算每个单词的出现次数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如果我有一篇英文文章或一本英文小说,我想计算每个单词出现的次数,用Java编写的最快的算法是什么?

If I have an article in English, or a novel in English, and I want to count how many times each words appears, what is the fastest algorithm written in Java?

有人说你可以使用地图< String,Integer>()来完成这个,但我想知道我怎么知道是什么关键词?

Some people said you can use Map < String, Integer>() to complete this, but I was wondering how do I know what is the key words? Every article has different words and how do you know the "key" words then add one on its count?

推荐答案

Map<String, Integer> countByWords = new HashMap<String, Integer>(); Scanner s = new Scanner(new File("your_file_path")); while (s.hasNext()) { String next = s.next(); Integer count = countByWords.get(next); if (count != null) { countByWords.put(next, count + 1); } else { countByWords.put(next, 1); } } s.close();

这个计数I'm只有一个字

this count "I'm" as only one word

更多推荐

如何计算每个单词的出现次数?

本文发布于:2023-11-12 02:53:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580299.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单词   次数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!