souad 0 Newbie Poster

I have this code that outputs the tfidf for all words in each file in the directory. I'm trying to transfer this to a matrix where each row correspond to each file in the directory and each column to all words in the files and I have some difficulty in doing it and i need some help

Here is my try

public class TestTF_IDF {

public static void main(String[] args) throws UnsupportedEncodingException, FileNotFoundException{
    //Test code for TfIdf
    TfIdf tf = new TfIdf("E:/Thesis/ThesisWork/data1");
    //Contains words in the documents
    String word;
    //Contains file name being processed
    String file;
    //Variable to hold document frequency and IDF of each word
    Double[] dfIDF;


    //Prints each term in a document, its frequency, term frequency and tf-idf
    Map<String, Double[]> myMap = new HashMap<String, Double[]>();
    Double[] values;
    for (Iterator<String> it = tf.documents.keySet().iterator(); it.hasNext(); ) {
        file =;
        System.out.println("File \t" + file);

        myMap = tf.documents.get(file).getF_TF_TFIDF();

        for (String key : myMap.keySet()) {

            values = myMap.get(key);
            //System.out.println("Term = " + key + " Frequency = " + values[0] + " Term Frequency " + values[1] + " TF-IDF " + values[2]);

            int d=myMap.size();
            int a= key.length();

            String[][] matrix = new String[d][a];

            for (int i = 0; i < d; i++) {
                for (int j = 0; j < a; j++) {

                  matrix[i][j]= Double.toString(values[2]) ;

                 System.out.print(matrix[i][j]+ " ");

        }//for (String key : myMap.keySet())
    }//for (Iterator<String> it = tf.documents.keySet().iterator(); it.hasNext(); )

}//public static void main(String[] args)
    }//public class TestTF_IDF 

Any ideas. Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.