2014-04-07 3 views
0

이 코드는 디렉토리의 각 파일에있는 모든 단어에 대해 tfidf를 출력합니다. 이 행을 각 행이 디렉터리의 각 파일에 해당하고 파일의 모든 단어에 해당하는 행렬로 전송하려고하는데이 작업을 수행하는 데 약간의 어려움이 있으며 도움이 필요합니다. 매트릭스를 출력하려고하면 java.lang.NullPointerException이 발생합니다. 값이 나타나기 시작하지만 어떤 이유로 중지되고 널 (null) 오류가 생성됩니다. java.lang.NullPointerException 출력 용어 빈도 - 문서 빈도 (tfidf) 행렬 java

코드를

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 file name being processed 
    //String file; 


    tf.buildAllDocuments(); 


    int numDocuments = tf.documents.size(); 
    Double matrix[][] = new Double[numDocuments][]; 

    int documentIndex = 0; 
    for (String file : tf.documents.keySet()) 
    { 
     // System.out.println("File \t" + file); 

     Map<String, Double[]> myMap = 
      tf.documents.get(file).getF_TF_TFIDF(); 

     int numWords = myMap.size(); 
     matrix[documentIndex] = new Double[numWords]; 

     int wordIndex = 0; 
     for (String key : myMap.keySet()) 
     { 
      Double[] values = myMap.get(key); 
      matrix[documentIndex][wordIndex] = values[2]; 
      wordIndex++; 
      //System.out.print("file="+ file+ "term=" +key + values[2]+" "); 
     } 
     documentIndex++; 


     for(int i=0; i<numDocuments;i++){ 
      for(int j=0; j<numWords;j++){ 

      System.out.print("file="+ file+ matrix[i][j]+ " "); //error here 
      } 
     } 
    } 

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

어떤 아이디어입니다. 감사합니다

+0

'tf.documents'에는 무엇이 들어 있습니까? 특히'Double []'배열은 무엇입니까? (주파수를 포함하는이 1 요소 배열입니까?) – Marco13

+0

@ Marco13 문서는 TFIdf 클래스에서 선언 된 문서를 포함하고있는 TreeMap (TreeMap documents)이고 tf는 해당 클래스의 개체입니다 (5 행 참조) 배열에 대해서는 더 이상 사용되지 않는 dfIdf 배열을 무시하고 값 배열에 대해서는 myMay의 값을 보유합니다. – Souad

+0

이 매트릭스를 만드는 것이 쉽다는 것을 확신합니다. 그러나 어떤 데이터를 가지고 있는지 명확하지 않은 경우 (어떤 구조로되어 있는지),이 데이터 중 무엇을 매트릭스에 포함시켜야하는지는 알 수 없습니다. 그래서'tf.documents'는 파일 이름을'Document'에 매핑합니까? 그리고'getF_TF_TFIDF'는 ... 단어를 매핑하는지도를 반환합니까? 무엇을? – Marco13

답변

1

비록 질문이 현저하게 불분명하지만, 여기에 질문과 의견을 토대로 추측하려고 한 내용이 있습니다.

import java.util.Map; 

public class TestTF_IDF 
{ 
    public static void main(String[] args) throws Exception 
    { 
     TfIdf tf = new TfIdf("E:/Thesis/ThesisWork/data1"); 
     tf.buildAllDocuments(); 

     int numDocuments = tf.documents.size(); 
     Double[] matrix[][] = new Double[numDocuments][][]; 

     int documentIndex = 0; 
     for (String file : tf.documents.keySet()) 
     { 
      System.out.println("File \t" + file); 

      Map<String, Double[]> myMap = 
       tf.documents.get(file).getF_TF_TFIDF(); 

      int numWords = myMap.size(); 
      matrix[documentIndex] = new Double[numWords][]; 

      int wordIndex = 0; 
      for (String key : myMap.keySet()) 
      { 
       Double[] values = myMap.get(key); 
       matrix[documentIndex][wordIndex] = values; 
       wordIndex++; 
      } 
      documentIndex++; 
     } 
    } 
} 


class Document 
{ 
    public Map<String, Double[]> getF_TF_TFIDF() 
    { 
     return null; 
    } 
} 

class TfIdf 
{ 
    public Map<String, Document> documents; 
    TfIdf(String s) 
    { 
    } 
    public void buildAllDocuments() 
    { 
    } 
} 
+0

Marco 13 대단히 감사합니다. 이제 해결해야 할 부분이 있습니다. 귀하의 코드를 추가하고 행렬을 인쇄하면 첫 번째 파일의 일부 단어 값을 출력하기 시작하고 java.lang.NullPointerException이 나타납니다. – Souad

+0

아마도 원래 질문을 편집하고 예외에 대한 자세한 정보를 제공 할 수 있습니다. – Marco13

+0

질문이 수정되었습니다. – Souad

관련 문제