2011-03-26 39 views
0
import java.awt.BorderLayout; 

import java.io.*; 

import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.Collections; 
import java.util.Scanner; 
import java.util.StringTokenizer; 
import java.util.TreeMap; 
import java.util.regex.Pattern; 
import java.io.FilenameFilter; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTextArea; 

import org.apache.commons.io.FileUtils; 

@SuppressWarnings("unused") 
public class readFile extends JFrame { 

JTextArea _resultArea = new JTextArea(100, 100); 



JScrollPane scrollingArea = new JScrollPane(_resultArea); 

final String newline = "\n"; 


public readFile(){ 

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

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

    System.out.println("The number of files is this folder is : " + numDoc); 

    // Calculating term frequency 
    int filename = 11; 
    String[] fileName = new String[filename]; 
    int a = 0; 
    int totalCount = 0; 
    int wordCount = 0; 

    // Count the number of documents containing the query 

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

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

     numofDoc[b] = 0; 

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

      try { 

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

       int matchedWord = 0; 

       Scanner s2 = new Scanner(bf); 

       { 

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

       } 
       if (matchedWord > 0) 
        numofDoc[b]++; 

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

     } 
     _resultArea.append(array2[b] 
       + " --> This number of files that contain this term " 
       + numofDoc[b]+ newline); 
    } 

    // calculate TF-IDF (TermFrequency/InverseTermFrequency) 

    int queryVector = 1; 
    double similarity = 0.0; 
    int wordPower; 



    double[] similarityScore = new double [11]; 




    double[][] arrays = new double[11][1]; 

    for (a = 0; a < filename; a++) { 
     int totalwordPower = 0; 
     int totalWords = 0; 
     try { 
      _resultArea.append(" _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ "+ newline); 
      _resultArea.append("\n"+ newline); 
      _resultArea.append("The word inputted : " + word2+ newline); 
      File file = new File(
        "C:\\Users\\user\\fypworkspace\\FYP\\abc" 
        + a + ".txt"); 
      _resultArea.append(" _________________"+ newline); 

      _resultArea.append("| File = abc" + a + ".txt | \t\t \n"+ newline); 

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

       totalCount = 0; 
       wordCount = 0; 

       Scanner s = new Scanner(file); 
       { 
        while (s.hasNext()) { 
         totalCount++; 
         if (s.next().equals(array2[i])) 
          wordCount++; 

        } 

        _resultArea.append(array2[i] + " --> Word count = " 
          + "\t " + "|" + wordCount + "|"+ newline); 
        _resultArea.append(" Total count = " + "\t " + "|" 
          + totalCount + "|"+ newline); 
        System.out.printf(" Term Frequency = | %8.4f |", 
          (double) wordCount/totalCount); 

        _resultArea.append("\t "+ newline); 

        double inverseTF = Math.log10((float) numDoc 
          /(numofDoc[i])); 
        _resultArea.append(" --> IDF = " + inverseTF+ newline); 

        double TFIDF = (((double) wordCount/totalCount) * inverseTF); 
        _resultArea.append(" --> TF/IDF = " + TFIDF + "\n"+ newline); 

        totalWords += wordCount; 

        wordPower = (int) Math.pow(wordCount, 2); 

        totalwordPower += wordPower; 

        _resultArea.append("Document Vector : " + wordPower+ newline); 

        similarity = (totalWords * queryVector) 
          /((Math.sqrt((totalwordPower)) * (Math 
            .sqrt(((queryVector * 3)))))); 


        similarityScore[a] = similarity; 

       } 
      } 
     } catch (FileNotFoundException e) { 
      System.out.println("File is not found"); 
     } 
     _resultArea.append("The total query frequency for this file is " 
       + totalWords+ newline); 
     _resultArea.append("The total document vector : " + totalwordPower+ newline); 

     _resultArea.append("The similarity is " + similarity+ newline); 
     _resultArea.append("\n"+ newline); 

    } 
    _resultArea.append(" _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ " 
        + "\n"+ newline); 

    // Display the similarity score between the query and the document in a 
    // sorted form 
    _resultArea.append("The resulting similarity score of the query " + word2+ newline); 
    for (a = 0; a < filename; a++) { 

     _resultArea.append("abc" + a + ".txt = " + similarityScore[a]+ newline); 

    } 
    _resultArea.append("\n"+ newline); 


    //Array of sorted similarity score 



    for(int i=0; i<arrays.length; i++){ 


    Arrays.sort(similarityScore); 
    arrays[i][0] = similarityScore[i] ;   
    } 

    for(int row = 0; row<11; row++){ 
     for(int col=0; col<1; col ++){ 

      _resultArea.append(arrays[row][col]+"\t"+ newline); 
     } 
     _resultArea.append("\n"+ newline); 
    } 
    JPanel content = new JPanel(); 
    content.setLayout(new BorderLayout()); 
    content.add(scrollingArea, BorderLayout.CENTER); 

    this.setContentPane(content); 
    this.setTitle("TextAreaDemo B"); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.pack(); 
} 


public static void main(String[] args) throws FileNotFoundException { 
    JFrame win = new readFile(); 
    win.setVisible(true); 
} 
} 

嗨,我不能添加這一行不能追加行之一的JTextArea

System.out.printf(" Term Frequency = | %8.4f |", 

(double) wordCount/totalCount); 

到JTextArea中。我錯過了什麼?

我嘗試改變

_resultArea.append(" Term Frequency = | %8.4f |", 

(double) wordCount/totalCount); 

,但無濟於事。 :(

回答

2

printf和println不一樣,前者的格式與String.format(...)所做的相同,這就是你如何解決它,調用字符串的格式:

_resultArea.append(String.format(" Term Frequency = | %8.4f |", (double) wordCount/totalCount)); 

更多關於這一點,檢查出String APIFormatter API

順便說一句,如果你有在未來類似的問題,不糊,因爲大部分你已經碼了這麼多的代碼上面的帖子與手頭的問題完全無關,最好的帖子是SSCCE,這個鏈接會告訴你所有關於這個的:SSCCE

+0

謝謝先生,我知道我發佈的代碼是叢林代碼。但是,我擔心如果我錯過了一些小的細節,它無法幫助人們來幫助我。對不起,代碼顯示不好。我會記住你的建議。謝謝你的幫助。它編譯。非常感謝。 – jasper 2011-03-26 15:36:37

+0

這就是爲什麼最好將問題與SSCCE分開,正如我在上面提供的鏈接中所討論的那樣。別客氣。 – 2011-03-26 15:40:15