双师行为错误 [英] Double division behaving wrongly

查看:154
本文介绍了双师行为错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 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;

这篇关于双师行为错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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