对数回答不准确

时间:2011-03-11 13:19:52

标签: java

//Count the number of documents available

 File folder = new File("C:\\Users\\user\\fypworkspace\\TextRenderer");
    File[] listOfFiles = folder.listFiles();
    int numDoc = 0;

    for (int i = 0; i < listOfFiles.length; i++) {
        if ((listOfFiles[i].getName().endsWith(".txt"))) {
            numDoc++;
        }
    }

//Count the number of documents containing a string

System.out.println("Please enter the required word  :");
    Scanner scan2 = new Scanner(System.in);
    String word2 = scan2.nextLine();
    String[] array2 = word2.split(" ");

    for (int b = 0; b < array2.length; b++) {
        int numofDoc = 0;



        for (int i = 0; i < filename; i++) {

            try {

                BufferedReader in = new BufferedReader(new FileReader(
                        "C:\\Users\\user\\fypworkspace\\TextRenderer\\abc"
                                + i + ".txt"));

                int matchedWord = 0;

                Scanner s2 = new Scanner(in);

                {

                    while (s2.hasNext()) {
                        if (s2.next().equals(array2[b]))
                            matchedWord++;
                    }

                }
                if (matchedWord > 0)
                    numofDoc++;

            } catch (IOException e) {
                System.out.println("File not found.");
            }

        }
        System.out.println(array2[b] + " --> This number of files that contain the term  " +  numofDoc);
        double inverseTF = Math.log ( numDoc/ numofDoc );
        System.out.println(array2[b] + " --> This inverse term frequency that contain the term  " +  inverseTF);
    }

}
}

当我试图计算其中公式为

的inverseTF时

log(numDoc / numofDoc),

我没有得到正确的答案。

有人有想法吗?

程序的输出是

The number of files available is 11.
is --> This number of files that contain the term is  7
is --> This inverse term frequency that contain the term  0.45198510206864073

4 个答案:

答案 0 :(得分:4)

Math.log()是登录基础e,也许您正在寻找Math.log10()。但既然你没有提供任何关于预期结果和错误的信息,我不能肯定地说。

由于您的两个变量为int,因此也可能出现舍入错误。试试:

Math.log ((float) numDoc / numofDoc);

答案 1 :(得分:3)

由于numDoc和numOfDoc是整数,因此您使用的是整数除法。尝试将它们两者都加倍,如下所示:

double inverseTF = Math.log ( (double)numDoc / (double)numofDoc );

答案 2 :(得分:2)

执行numDoc/numOfDock时,由于变量的类型为int,因此会进行整数除法,然后将结果转换为double Math.log调用,这将为您的案件产生一个近似的结果。

答案 3 :(得分:0)

您可能不想使用整数除法,因此您可能希望记录((double)numDoc)/numofDoc的日志。此外,看起来你可能想要base 10 logarithm ,但Java中的Math.log是基础e或natural logarithm :.