2011-03-11 126 views
1
//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其中公式是對數答案不準確

日誌(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 
+0

您可以發佈程序執行打印嗎? – MByD

+0

嗨,我已經更新了我的問題。感謝您的關注。 –

回答

4

Math.log()是在日誌中的基礎e,也許你正在尋找Math.log10()。但是既然你沒有提供任何有關預期結果和錯誤的信息,我不能肯定地說。

也有可能你有一個舍入誤差,因爲你的兩個變量是int。嘗試:

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

嗨,我已經更新了我的問題。感謝您的關注。這是不準確的。我想知道我的錯誤在哪裏。 :( –

+0

謝謝,我已經修好了,非常感謝你:) –

+1

爲什麼要用float來代替double?我認爲使用float而不是double的唯一真正原因是當內存是一個問題時 –

3

由於numDoc和numOfDoc是整數,您使用整數除法。嘗試鑄造他們兩個翻番,像這樣:

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

當你這樣做numDoc/numOfDock,因爲變量類型int,它會做一個整數除法,然後把結果給doubleMath.log呼叫,那麼這將產生約結果爲您案件。

0

你可能不想使用整數除法,所以你可能想要取((double)numDoc)/numofDoc的日誌。此外,它看起來像你可能想要base 10 logarithm ,但在Java中的Math.log是基地e或natural logarithm:。