问题描述
我有一个 HashMap
调用列表< String,Intger> wordFreqMap
其大小
是 234
wordFreqMap = {radiology = 1,shift = 2,mummy = 1,empirical = 1,awful = 1,geoff = 1,.......}
我想计算每个单词的术语频率
。
术语频率=术语频率/总词数
public static Map< String,Double> ; getTFMap(Map< String,Integer> wordFreqMap)
{
Map< String,Double> tfMap = new HashMap< String,Double>();
int noOfTerms = wordFreqMap.size();
双tf;
for(Entry< String,Integer> word:wordFreqMap.entrySet())
{
tf =(double)(word.getValue()/ noOfTerms);
tfMap.put(word.getKey(),tf);
}
return tfMap;
}
我的问题是, tfMap
正在返回 {radiology = 0.0,shift = 0.0,mummy = 0.0,empirical = 0.0,awful = 0.0,geoff = 0.0,.....}
/ p>
我不明白为什么它为每个术语返回 0.0
。我应该如何解决?
我应该得到一些类似 {radiology = 0.00427,shift = 0.00854,...}
您正在执行一个整数除法,然后键入cast:
tf =(double)(word.getValue()/ noOfTerms);
^ -----整数除法---- ^
分区中的元素转换成浮点除法:
tf =((double)word.getValue())/ noOfTerms;
I have a HashMap
called List<String, Intger> wordFreqMap
whose size
is 234
wordFreqMap = {radiology=1, shift=2, mummy=1, empirical=1, awful=1, geoff=1, .......}
I want to calculate the term frequency
of each word.
term frequency = frequency of term / total number of terms
public static Map<String, Double> getTFMap (Map<String, Integer> wordFreqMap)
{
Map<String, Double> tfMap = new HashMap<String, Double>();
int noOfTerms = wordFreqMap.size();
Double tf;
for (Entry<String, Integer> word : wordFreqMap.entrySet() )
{
tf = (double) ( word.getValue() / noOfTerms );
tfMap.put(word.getKey(), tf );
}
return tfMap;
}
My problem is that, tfMap
is returning {radiology=0.0, shift=0.0, mummy=0.0, empirical=0.0, awful=0.0, geoff=0.0, .....}
I don't understand why it returns 0.0
for every term. How do I fix it?
I should get something like {radiology=0.00427, shift=0.00854, ...}
You're performing an integer division and then type casting:
tf = (double) ( word.getValue() / noOfTerms );
^-----integer division----^
Type cast one of the elements in the division to convert into a floating point division:
tf = ((double)word.getValue()) / noOfTerms;
这篇关于双师行为错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!