java 统计各个单词出现的次数,忽略大小写
统计各个单词出现的次数,忽略大小写
/**
* 根据文章内容统计各个单词出现的次数,忽略大小写
*
* @param article 待统计的文章内容
* @return 返回一个Map,其中键为单词,值为统计数目
*/
public static Map<String, Integer> statistic(String article) {
Map<String, Integer> map = new HashMap<>();
String lowerCaseArticle = article.toLowerCase();
Matcher matcher = Pattern.compile("([a-zA-Z]+)").matcher(lowerCaseArticle);
String key;
while (matcher.find()) {
key = matcher.group();
if (map.containsKey(key)) {
map.put(key, map.get(key) + 1);
} else {
map.put(key, 1);
}
}
return map;
}