如何使用java计算哈希表中单词出现的频率
我有一个小作业,其中有一个哈希表格式。现在我想查找某个单词在其中出现的次数。
请在这方面指导我。 谢谢 编辑#1
Hashtable<String, Integer> h = new Hashtable<String, Integer>();
编辑#2
if(spam.containsKey(s)){
int value = spam.get(s);
value += 1;
spam.put(s,value);
}else{
spam.put(s,1);
}
好的,我现在将代码更改为这样。我将把该单词的计数作为一个值。
I have a small assignment, where I have a in Hashtable format. Now I want to find a number of times a word is present in that.
Kindly guide me in this.
thanks
Edit#1
Hashtable<String, Integer> h = new Hashtable<String, Integer>();
Edit #2
if(spam.containsKey(s)){
int value = spam.get(s);
value += 1;
spam.put(s,value);
}else{
spam.put(s,1);
}
Ok, I changed my code to this now. I will have the count of that word as a value.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于哈希表不允许键重复,因此特定单词始终会出现 0 或 1 次。
如果你先执行 h.add("hi",1) 然后执行 h.add("hi",2) 然后执行 n = h.get("hi") 你会得到 2。
h 将仅包含一个“hi”字符串作为键。
You will always have 0 or 1 occurrences of a specific word since a Hashtable does not allow key duplicates.
If you do h.add("hi",1) and then h.add("hi",2) and then you do n = h.get("hi") you will get 2.
And h will contain just one "hi" string as key.
一种常见的作业是使用哈希表,就像您为稍微不同的问题所展示的那样:查找文本部分中的词频(通常以字符串形式给出)。您可能对作业的措辞感到困惑吗?
如果我的假设是正确的,那么这里有一个小提示:您必须以哈希将单词(哈希表中的键)映射到它们在文本中出现的频率的方式填充哈希表。
A common assignment is to use a hash-table like the one you show for a slightly different problem: to find the word-frequencies in a section of text (typically given as a String). Are you perhaps confused by the wording of the assignment?
If my hypothesis is correct, then here is a small hint: you have to fill the hash table in such a way that the hash maps words (the keys in the hash table) to the frequence with which they occur in the text.