使用键出现次数将单词添加到HashMap中 [英] Adding words to a HashMap with a key of how many times they appear

查看:121
本文介绍了使用键出现次数将单词添加到HashMap中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Java程序,它将网站上的所有单词添加到HashMap中,然后为它们分配页面上显示的次数。例如,如果我在页面上只用hello,java,coffee,java来运行它,输出将是

Java:2
咖啡:1
您好:1



这也忽略了某些我不想包含的词。这是我目前所拥有的。

 地图< String,Integer> found = new HashMap<>(); //(单词,频率)
匹配匹配= Pattern.compile(word_pattern).matcher(content);

while(match.find()){

//以小写形式获取网络字词
String word = match.group()。toLowerCase();

//如果不是要忽略的单词集合,添加到找到的Map
if(!ignore.contains(word))
found.put(word,);
}

System.out.println(找到);






第二个参数,int,我假设应该计算在添加单词到HashMap之前。

  found.put(word,int); 

但是我不确定如何在保持O(nlogn)的同时加上一个单词的出现次数) 时间。

解决方案

这看起来像是一个任务,所以我会指出正确的方向,而不是给出明确的代码。



处理单词时,您需要查看HashMap以查看它是否已经存在。如果是,请在当前计数中添加一个并更新地图。如果它不在地图中,请将其添加为1。


I'm writing a Java program which adds all of the words on a website into a HashMap, and then assigns them a key of how many times they appear on the page. For instance, if I ran it on a page with only the words "hello, java, coffee, java", the output would be

Java : 2 Coffee : 1 Hello : 1

This also ignores certain words that I don't want included. Here's what I have so far.

Map<String, Integer> found = new HashMap<>(); // (word,frequency)
Matcher match = Pattern.compile(word_pattern).matcher(content);

while (match.find()) {

  // Get the net word in lowercase
  String word = match.group().toLowerCase();

  //If not the set of words to ignore, add to the found Map
  if(!ignore.contains(word))
      found.put(word,       );  
  }

 System.out.println(found);

}

The second parameter, an int, I assume should be calculated before I add the word to the HashMap.

found.put(word,  int   );  

But I'm unsure of how exactly to add up the occurrences of a word while keeping O(nlogn) time.

解决方案

This really looks like an assignment so I'll point in the right direction rather than give explicit code.

When you process a word, you need to look in the HashMap to see if it is already there. If it is, add one to the current count and update the Map. If it's not present in the Map, add it with a count of 1.

这篇关于使用键出现次数将单词添加到HashMap中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆