Java 无限结果
我试图计算一组文件的tf和idf。我的问题是我得到无穷大,我不想要无穷大,但我想要双重结果 这是我的密码:Java 无限结果,java,tf-idf,Java,Tf Idf,我试图计算一组文件的tf和idf。我的问题是我得到无穷大,我不想要无穷大,但我想要双重结果 这是我的密码: String key = entry.getKey().toString(); Integer value = entry.getValue().intValue(); System.out.println("key " + key + " value " + value); BigDecimal tf=new BigDecimal(entry.getValue()/collec
String key = entry.getKey().toString();
Integer value = entry.getValue().intValue();
System.out.println("key " + key + " value " + value);
BigDecimal tf=new BigDecimal(entry.getValue()/collection.size());
BigDecimal idf=new BigDecimal(Math.log(counter/entry.getValue()));
BigDecimal tfidf =new BigDecimal((tf.doubleValue())*(idf.doubleValue()));
System.out.println("TF*IDF: " + key + ": " + tfidf.doubleValue());
输入:
映射其中字符串是关键字,整数是word在文档中出现的次数
输出:
关键字+TF*IDF:编号
你知道怎么解决这个问题吗 以下行可能有问题:
BigDecimal tf = new BigDecimal(entry.getValue()/collection.size());
您正在执行整数除法。这可能会被截断为零。尝试将此更改为
BigDecimal tf=new BigDecimal((double) entry.getValue()/collection.size());
同样的问题:
new BigDecimal(Math.log(counter/entry.getValue()));
换成
new BigDecimal(Math.log((double) counter/entry.getValue()));
您可能在以下行中遇到问题:
BigDecimal tf = new BigDecimal(entry.getValue()/collection.size());
您正在执行整数除法。这可能会被截断为零。尝试将此更改为
BigDecimal tf=new BigDecimal((double) entry.getValue()/collection.size());
同样的问题:
new BigDecimal(Math.log(counter/entry.getValue()));
换成
new BigDecimal(Math.log((double) counter/entry.getValue()));
请给出输入、实际输出和所需输出的示例。我认为idf永远不会为0,因为映射中总是有一些最有可能的项。getValue()、collection.size()或counter的值为0。这很可能是你得到无穷大的原因。您需要调试以确保其中一个值为零是不可接受的,或者将1添加到这些值中以确保不会得到无穷大。请给出输入、实际输出和所需输出的示例。我认为idf永远不会为0,因为映射始终具有最有可能的项。getValue()、collection.size(),或计数器的值为0。这很可能是获得无穷大的原因。您需要进行调试,以确保其中一个值为零是不可接受的,或者将1添加到这些值以确保不会获得无穷大。